Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public interface CompilerDescription {
public String getName();
public boolean isValid();
public Result decompile(DecompilerDescription decompiler, Input input);
public String objectClassURI();

public static class Factory {
private static Collection<? extends CompilerDescription> descriptions;
Expand All @@ -72,7 +73,10 @@ public static synchronized Collection<? extends CompilerDescription> description
FileObject jrtfs = installDir.getFileObject("lib/jrt-fs.jar");

if (toolsJar != null || jrtfs != null) {
result.add(new ExecCompilerDescription(platform, moduleJar, toolsJar != null ? FileUtil.toFile(toolsJar) : null));
FileObject objectClass = platform.getBootstrapLibraries().findResource("java/lang/Object.class");
String objectClassURI = objectClass != null ? objectClass.toURI().toString() : null;

result.add(new ExecCompilerDescription(platform, moduleJar, toolsJar != null ? FileUtil.toFile(toolsJar) : null, objectClassURI));
}
}
}
Expand All @@ -94,10 +98,12 @@ public static synchronized Collection<? extends CompilerDescription> description
private static class LoaderBased implements CompilerDescription {
public final String displayName;
public final URL[] jars;
private final String objectClassURI;

private LoaderBased(String displayName, URL[] jars) {
private LoaderBased(String displayName, URL[] jars, String objectClassURI) {
this.displayName = displayName;
this.jars = jars;
this.objectClassURI = objectClassURI;
}

@Override
Expand Down Expand Up @@ -158,6 +164,11 @@ public Result decompile(DecompilerDescription decompiler, Input input) {
return decompiler.createDecompiler(loader).decompile(input);
}

@Override
public String objectClassURI() {
return objectClassURI;
}

}

private static final class ExecCompilerDescription implements CompilerDescription {
Expand All @@ -167,11 +178,13 @@ private static final class ExecCompilerDescription implements CompilerDescriptio
private final JavaPlatform platform;
private final File moduleJar;
private final File toolsJar;
private final String objectClassURI;

public ExecCompilerDescription(JavaPlatform platform, File moduleJar, File toolsJar) {
public ExecCompilerDescription(JavaPlatform platform, File moduleJar, File toolsJar, String objectClassURI) {
this.platform = platform;
this.moduleJar = moduleJar;
this.toolsJar = toolsJar;
this.objectClassURI = objectClassURI;
}

@Override
Expand Down Expand Up @@ -222,6 +235,11 @@ public Result decompile(DecompilerDescription decompiler, Input input) {

}

@Override
public String objectClassURI() {
return objectClassURI;
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@
import java.util.List;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import java.util.stream.Collectors;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.MutableComboBoxModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.queries.CompilerOptionsQuery;
import org.netbeans.api.java.queries.CompilerOptionsQuery.Result;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.openide.util.NbPreferences;
Expand All @@ -54,9 +60,17 @@ public DecompileToolbar(final FileObject decompiled, final FileObject originalSo

DefaultComboBoxModel<CompilerDescription> compilerModel = new DefaultComboBoxModel<>();
Collection<? extends CompilerDescription> compilerDescriptions = CompilerDescription.Factory.descriptions();

ClassPath boot = ClassPath.getClassPath(originalSource, ClassPath.BOOT);
FileObject objectClass = boot != null ? boot.findResource("java/lang/Object.class") : null;
String objectClassURI = objectClass != null ? objectClass.toURI().toString() : null;
CompilerDescription defaultCompilerDescription = null;

for (CompilerDescription cd : compilerDescriptions) {
compilerModel.addElement(cd);
String currentCompilerObjectClass = cd.objectClassURI();
if (currentCompilerObjectClass != null && currentCompilerObjectClass.equals(objectClassURI)) {
defaultCompilerDescription = cd;
}
}

compiler.setModel(compilerModel);
Expand Down Expand Up @@ -84,7 +98,11 @@ public DecompileToolbar(final FileObject decompiled, final FileObject originalSo
List<DecompilerDescription> decompilers = new ArrayList<>();

if (compilerDescriptions.size() > 0) {
compiler.setSelectedIndex(0);
if (defaultCompilerDescription != null) {
compiler.setSelectedItem(defaultCompilerDescription);
} else {
compiler.setSelectedIndex(0);
}

for (DecompilerDescription decompiler : DecompilerDescription.getDecompilers()) {
decompilerModel.addElement(decompiler);
Expand All @@ -109,36 +127,45 @@ public DecompileToolbar(final FileObject decompiled, final FileObject originalSo
}
});
decompiler.setSelectedIndex(0);
extraOptions.setModel(createModel());
extraOptions.setModel(createModel(originalSource));
Object extraParams = originalSource.getAttribute(DecompiledTab.PROP_EXTRA_PARAMS);
Object selectedExtraParams;
if (extraParams instanceof String) {
try {
extraOptions.setSelectedItem(extraParams);
decompiled.setAttribute(DecompiledTab.PROP_EXTRA_PARAMS, extraParams);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
selectedExtraParams = extraParams;
} else {
selectedExtraParams = extraOptions.getItemAt(0);
}
extraOptions.setSelectedItem(selectedExtraParams);
try {
decompiled.setAttribute(DecompiledTab.PROP_EXTRA_PARAMS, selectedExtraParams);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
extraOptions.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
boolean implicitItem = extraOptions.getSelectedIndex() == 0;
String selected = (String) extraOptions.getSelectedItem();

masterModel.removeElement(selected);
masterModel.insertElementAt(selected, 0);
if (!implicitItem) {
masterModel.removeElement(selected);
masterModel.insertElementAt(selected, 0);

while (masterModel.getSize() > HISTORY_LIMIT) {
masterModel.removeElementAt(masterModel.getSize() - 1);
}
while (masterModel.getSize() > HISTORY_LIMIT) {
masterModel.removeElementAt(masterModel.getSize() - 1);
}

storeMasterData();
storeMasterData();

originalSource.setAttribute(DecompiledTab.PROP_EXTRA_PARAMS, selected);
} else {
originalSource.setAttribute(DecompiledTab.PROP_EXTRA_PARAMS, null);
}

extraOptions.getModel().setSelectedItem(selected); //the above changes the selection

decompiled.setAttribute(DecompiledTab.PROP_EXTRA_PARAMS, selected);
originalSource.setAttribute(DecompiledTab.PROP_EXTRA_PARAMS, selected);

} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
Expand All @@ -156,7 +183,7 @@ private static Preferences getHistoryNode() {
return prefs.node(KEY_EXTRA_PARAMS_HISTORY);
}

private ComboBoxModel<String> createModel() {
private ComboBoxModel<String> createModel(FileObject file) {
if (masterModel == null) {
masterModel = new DefaultComboBoxModel<>();

Expand All @@ -174,7 +201,7 @@ private ComboBoxModel<String> createModel() {
}
}

return new DelegatingModel(masterModel);
return new DelegatingModel(file, masterModel);
}

private static void storeMasterData() {
Expand Down Expand Up @@ -262,13 +289,27 @@ private static class DelegatingModel implements ComboBoxModel<String>, ListDataL
private final ComboBoxModel<String> master;
private final List<ListDataListener> listeners = new ArrayList<>();

private final Result defaultExtraOptions;
private String defaultArgs;
private Object selected;

public DelegatingModel(ComboBoxModel<String> master) {
public DelegatingModel(FileObject file, ComboBoxModel<String> master) {
this.defaultExtraOptions = CompilerOptionsQuery.getOptions(file);
this.defaultExtraOptions.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
updateDefaultArgs();
}
});
updateDefaultArgs();
this.master = master;
this.master.addListDataListener(WeakListeners.create(ListDataListener.class, this, master));
}

private synchronized void updateDefaultArgs() {
this.defaultArgs = defaultExtraOptions.getArguments().stream().collect(Collectors.joining(" "));
}

@Override
public void setSelectedItem(Object anItem) {
this.selected = anItem;
Expand All @@ -286,12 +327,12 @@ public Object getSelectedItem() {

@Override
public int getSize() {
return master.getSize();
return master.getSize() + 1;
}

@Override
public String getElementAt(int index) {
return master.getElementAt(index);
return index == 0 ? defaultArgs : master.getElementAt(index - 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ private static void doDecompileIntoDocument(FileObject source) {
NbDocument.runAtomic((StyledDocument) doc, new Runnable() {
@Override public void run() {
try {
String existing = doc.getText(0, doc.getLength());
if (existing.equals(decompiledCode)) {
return ;
}
doc.remove(0, doc.getLength());
if (doc instanceof GuardedDocument) {
((GuardedDocument) doc).getGuardedBlockChain().removeEmptyBlocks();
Expand Down Expand Up @@ -369,25 +373,6 @@ public void changedUpdate(DocumentEvent e) {
private static final class Updater implements CancellableTask<CompilationInfo> {
@Override public void run(CompilationInfo parameter) throws Exception {
doDecompileIntoDocument(parameter.getFileObject());
// FileObject sourceFile = parameter.getFileObject();
//// if (sourceFile.getAttribute(ATTR_DECOMPILED) == Boolean.TRUE) return;
// final FileObject decompiled = findDecompiled(sourceFile, false);
//
// if (decompiled == null) return ;
//
// final ElementHandle<?> handle = ElementHandle.create(parameter.getTopLevelElements().get(0));
//
// JavaSource.create(parameter.getClasspathInfo(), decompiled).runModificationTask(new Task<WorkingCopy>() {
// @Override public void run(WorkingCopy copy) throws Exception {
// copy.toPhase(Phase.RESOLVED);
//
// copy.rewrite(copy.getCompilationUnit(), CodeGenerator.generateCode(copy, (TypeElement) handle.resolve(copy)));
// }
// }).commit();
//
// SaveCookie sc = DataObject.find(decompiled).getLookup().lookup(SaveCookie.class);
//
// if (sc != null) sc.save();
}

@Override public void cancel() {
Expand Down