Skip to content

Commit

Permalink
fix problem where "Could not use {} for decompilation" error messages
Browse files Browse the repository at this point in the history
increase with every usage
  • Loading branch information
jpstotz committed Jun 13, 2024
1 parent 5e665ed commit d3294fc
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public List<Exception> getExceptions() {
return exceptions;
}

@Override
public void clearExceptions() {
exceptions.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public char[] findSource(IType type, IBinaryType info) {

classLocation = ""; //$NON-NLS-1$

usedDecompiler = decompile(null, type, exceptions, root, className);
usedDecompiler = decompile(type, exceptions, root, className);

if (noDecompiledSourceCodeAvailable(usedDecompiler)) {
if (usedDecompiler == null) {
Expand Down Expand Up @@ -260,9 +260,12 @@ private boolean fromInput(IType type) {
return false;
}

private IDecompiler decompile(IDecompiler decompiler, IType type, Collection<Exception> exceptions,
IPackageFragmentRoot root, String className) {
IDecompiler result = decompiler;
private IDecompiler decompile(IType type, Collection<Exception> exceptions, IPackageFragmentRoot root,
String className) {

originalDecompiler.clearExceptions();

IDecompiler result = null;

String pkg = type.getPackageFragment().getElementName().replace('.', '/');

Expand All @@ -277,13 +280,11 @@ private IDecompiler decompile(IDecompiler decompiler, IType type, Collection<Exc
String archivePath = getArchivePath(root);
classLocation += archivePath;

if (result == null) {
try (InputStream in = new ByteArrayInputStream(type.getClassFile().getBytes())) {
result = ClassUtil.checkAvailableDecompiler(originalDecompiler, in);
} catch (JavaModelException e) {
Logger.error(e.toString());
result = originalDecompiler;
}
try (InputStream in = new ByteArrayInputStream(type.getClassFile().getBytes())) {
result = ClassUtil.checkAvailableDecompiler(originalDecompiler, in);
} catch (JavaModelException e) {
Logger.error(e.toString());
result = originalDecompiler;
}
result.decompileFromArchive(archivePath, pkg, className);
} else {
Expand All @@ -306,9 +307,7 @@ private IDecompiler decompile(IDecompiler decompiler, IType type, Collection<Exc
+ className;
}

if (result == null) {
result = ClassUtil.checkAvailableDecompiler(originalDecompiler, new File(classLocation));
}
result = ClassUtil.checkAvailableDecompiler(originalDecompiler, new File(classLocation));
result.decompile(rootLocation, pkg, className);
} catch (JavaModelException e) {
exceptions.add(e);
Expand All @@ -325,7 +324,7 @@ private IDecompiler decompile(IDecompiler decompiler, IType type, Collection<Exc
}

@Override
public String decompile(String decompilerType, File file) {
public String decompileFile(String decompilerType, File file) {
IPreferenceStore prefs = JavaDecompilerPlugin.getDefault().getPreferenceStore();

Boolean displayNumber = null;
Expand Down Expand Up @@ -379,7 +378,6 @@ public String decompile(String decompilerType, File file) {
} else {
source.append(code);
}

return source.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,5 @@ protected IJavaElement findElement(IJavaElement elt, int position) {
return elt;
}

public abstract String decompile(String decompilerType, File file);
public abstract String decompileFile(String decompilerType, File file);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public interface IDecompiler {
*/
List<Exception> getExceptions();

void clearExceptions();

/**
* @return decompilation log specific to physical decompiler
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ protected void doSetInput(IEditorInput input) throws CoreException {
FileStoreEditorInput storeInput = (FileStoreEditorInput) input;
IPreferenceStore prefs = JavaDecompilerPlugin.getDefault().getPreferenceStore();
String decompilerType = prefs.getString(JavaDecompilerConstants.DECOMPILER_TYPE);
String source = DecompileUtil.decompiler(storeInput, decompilerType);
String source = DecompileUtil.decompileInput(storeInput, decompilerType);

if (source != null) {
String packageName = DecompileUtil.getPackageName(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static IDecompiler checkAvailableDecompiler(IDecompiler decompiler, Input

boolean debug = isDebug();
IDecompiler defaultDecompiler = getDebugDecompiler(classLevel, debug);
defaultDecompiler.clearExceptions();

if (decompiler.supportLevel(classLevel)) {
if (debug) {
Expand Down Expand Up @@ -128,7 +129,9 @@ public static IDecompiler getDebugDecompiler(int level, boolean debug) {
if (!defaultDecompiler.isPresent()) {
return null;
}
return defaultDecompiler.get().getDecompiler();
IDecompiler decompiler = defaultDecompiler.get().getDecompiler();
decompiler.clearExceptions();
return decompiler;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ public static String decompile(IClassFile cf, String type, boolean always, boole
return origSrc;
}

public static String decompiler(FileStoreEditorInput input, String decompilerType) {
public static String decompileInput(FileStoreEditorInput input, String decompilerType) {
DecompilerSourceMapper sourceMapper = SourceMapperFactory.getSourceMapper(decompilerType);
File file = new File(input.getURI());
return sourceMapper.decompile(decompilerType, file);

return sourceMapper.decompileFile(decompilerType, file);
}

public static String getPackageName(String source) {
Expand Down

0 comments on commit d3294fc

Please sign in to comment.