Skip to content

Commit

Permalink
Update JD-Core
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Apr 14, 2019
1 parent 3218e9f commit 4f60170
Show file tree
Hide file tree
Showing 18 changed files with 471 additions and 810 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/org/jd/gui/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ public void show(List<File> files) {
// Background initializations
executor.schedule(() -> {
// Background service initialization
ContextualActionsFactoryService.getInstance();
ContainerFactoryService.getInstance();
UriLoaderService.getInstance();
FileLoaderService.getInstance();
ContainerFactoryService.getInstance();
IndexerService.getInstance();
PasteHandlerService.getInstance();
PreferencesPanelService.getInstance();
TreeNodeFactoryService.getInstance();
TypeFactoryService.getInstance();
UriLoaderService.getInstance();
PasteHandlerService.getInstance();
PreferencesPanelService.getInstance();
ContextualActionsFactoryService.getInstance();

SwingUtil.invokeLater(() -> {
// Populate recent files menu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public void addPage(String title, Icon icon, String tip, T page) {
tabCloseButton.addMouseListener(new MouseListener() {
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) { ((JLabel)e.getSource()).setIcon(TabbedPanel.CLOSE_ACTIVE_ICON); }
@Override public void mouseExited(MouseEvent e) { ((JLabel)e.getSource()).setIcon(TabbedPanel.CLOSE_ICON); }
@Override public void mouseEntered(MouseEvent e) { ((JLabel)e.getSource()).setIcon(CLOSE_ACTIVE_ICON); }
@Override public void mouseExited(MouseEvent e) { ((JLabel)e.getSource()).setIcon(CLOSE_ICON); }
@Override public void mouseClicked(MouseEvent e) { removeComponent(page); }
});

Expand Down Expand Up @@ -129,12 +129,12 @@ protected void setSelectedIndex(int index) {

@SuppressWarnings("unchecked")
protected T showPage(URI uri) {
String u1 = uri.toString();
String u1 = uri.getPath();
int i = tabbedPane.getTabCount();

while (i-- > 0) {
T page = (T)tabbedPane.getComponentAt(i);
String u2 = page.getUri().toString();
String u2 = page.getUri().getPath();
if (u1.startsWith(u2)) {
tabbedPane.setSelectedIndex(i);
return page;
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ apply plugin: 'distribution'
apply plugin: 'edu.sc.seis.launch4j'
apply plugin: 'nebula.ospackage'


// Common configuration //
version='1.4.2'
rootProject.version='1.4.2'
rootProject.ext.set('jdCoreVersion', '1.0.0')
targetCompatibility = '1.8'

allprojects {
Expand Down Expand Up @@ -77,7 +77,7 @@ jar {
attributes 'Main-Class': 'org.jd.gui.App',
'SplashScreen-Image': 'org/jd/gui/images/jd_icon_128.png',
'JD-GUI-Version': project.version,
'JD-Core-Version': '0.7.1'
'JD-Core-Version': project.jdCoreVersion
}
exclude 'META-INF/licenses/**', 'META-INF/maven/**', 'META-INF/INDEX.LIST'
duplicatesStrategy DuplicatesStrategy.EXCLUDE
Expand Down
2 changes: 1 addition & 1 deletion services/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'java'
dependencies {
compile 'com.fifesoft:rsyntaxtextarea:2.5.6'
compile 'org.ow2.asm:asm:7.1'
compile 'jd:jd-core:0.7.1'
compile 'org.jd:jd-core:' + parent.jdCoreVersion
compile project(':api')
testCompile 'junit:junit:4.12'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public void index(API api, Container.Entry entry, Indexes indexes) {

try {
depth = Integer.valueOf(api.getPreferences().get("DirectoryIndexerPreferences.maximumDepth"));
} catch (NumberFormatException e) {
assert ExceptionUtil.printStackTrace(e);
} catch (NumberFormatException ignore) {
}

index(api, entry, indexes, depth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,25 @@

public class ClassFileSaverPreferencesProvider extends JPanel implements PreferencesPanel {
protected static final String ESCAPE_UNICODE_CHARACTERS = "ClassFileSaverPreferences.escapeUnicodeCharacters";
protected static final String OMIT_THIS_PREFIX = "ClassFileSaverPreferences.omitThisPrefix";
protected static final String REALIGN_LINE_NUMBERS = "ClassFileSaverPreferences.realignLineNumbers";
protected static final String WRITE_DEFAULT_CONSTRUCTOR = "ClassFileSaverPreferences.writeDefaultConstructor";
protected static final String WRITE_LINE_NUMBERS = "ClassFileSaverPreferences.writeLineNumbers";
protected static final String WRITE_METADATA = "ClassFileSaverPreferences.writeMetadata";

protected JCheckBox escapeUnicodeCharactersCheckBox;
protected JCheckBox omitThisPrefixCheckBox;
protected JCheckBox realignLineNumbersCheckBox;
protected JCheckBox writeDefaultConstructorCheckBox;
protected JCheckBox writeLineNumbersCheckBox;
protected JCheckBox writeMetadataCheckBox;

public ClassFileSaverPreferencesProvider() {
super(new GridLayout(0,1));

escapeUnicodeCharactersCheckBox = new JCheckBox("Escape unicode characters");
omitThisPrefixCheckBox = new JCheckBox("Omit the prefix 'this' if possible");
realignLineNumbersCheckBox = new JCheckBox("Realign line numbers");
writeDefaultConstructorCheckBox = new JCheckBox("Write default constructor");
writeLineNumbersCheckBox = new JCheckBox("Write original line numbers");
writeMetadataCheckBox = new JCheckBox("Write metadata");

add(escapeUnicodeCharactersCheckBox);
add(omitThisPrefixCheckBox);
add(realignLineNumbersCheckBox);
add(writeDefaultConstructorCheckBox);
add(writeLineNumbersCheckBox);
add(writeMetadataCheckBox);
}
Expand All @@ -58,19 +50,15 @@ public ClassFileSaverPreferencesProvider() {
@Override
public void loadPreferences(Map<String, String> preferences) {
escapeUnicodeCharactersCheckBox.setSelected("true".equals(preferences.get(ESCAPE_UNICODE_CHARACTERS)));
omitThisPrefixCheckBox.setSelected("true".equals(preferences.get(OMIT_THIS_PREFIX)));
realignLineNumbersCheckBox.setSelected(!"false".equals(preferences.get(REALIGN_LINE_NUMBERS)));
writeDefaultConstructorCheckBox.setSelected("true".equals(preferences.get(WRITE_DEFAULT_CONSTRUCTOR)));
writeLineNumbersCheckBox.setSelected(!"false".equals(preferences.get(WRITE_LINE_NUMBERS)));
writeMetadataCheckBox.setSelected(!"false".equals(preferences.get(WRITE_METADATA)));
}

@Override
public void savePreferences(Map<String, String> preferences) {
preferences.put(ESCAPE_UNICODE_CHARACTERS, Boolean.toString(escapeUnicodeCharactersCheckBox.isSelected()));
preferences.put(OMIT_THIS_PREFIX, Boolean.toString(omitThisPrefixCheckBox.isSelected()));
preferences.put(REALIGN_LINE_NUMBERS, Boolean.toString(realignLineNumbersCheckBox.isSelected()));
preferences.put(WRITE_DEFAULT_CONSTRUCTOR, Boolean.toString(writeDefaultConstructorCheckBox.isSelected()));
preferences.put(WRITE_LINE_NUMBERS, Boolean.toString(writeLineNumbersCheckBox.isSelected()));
preferences.put(WRITE_METADATA, Boolean.toString(writeMetadataCheckBox.isSelected()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,20 @@

public class ClassFileViewerPreferencesProvider extends JPanel implements PreferencesPanel {
protected static final String ESCAPE_UNICODE_CHARACTERS = "ClassFileViewerPreferences.escapeUnicodeCharacters";
protected static final String OMIT_THIS_PREFIX = "ClassFileViewerPreferences.omitThisPrefix";
protected static final String REALIGN_LINE_NUMBERS = "ClassFileViewerPreferences.realignLineNumbers";
protected static final String DISPLAY_DEFAULT_CONSTRUCTOR = "ClassFileViewerPreferences.displayDefaultConstructor";

protected PreferencesPanel.PreferencesPanelChangeListener listener = null;
protected JCheckBox escapeUnicodeCharactersCheckBox;
protected JCheckBox omitThisPrefixCheckBox;
protected JCheckBox realignLineNumbersCheckBox;
protected JCheckBox displayDefaultConstructorCheckBox;

public ClassFileViewerPreferencesProvider() {
super(new GridLayout(0,1));

escapeUnicodeCharactersCheckBox = new JCheckBox("Escape unicode characters");
omitThisPrefixCheckBox = new JCheckBox("Omit the prefix 'this' if possible");
realignLineNumbersCheckBox = new JCheckBox("Realign line numbers");
displayDefaultConstructorCheckBox = new JCheckBox("Display default constructor");

add(escapeUnicodeCharactersCheckBox);
add(omitThisPrefixCheckBox);
add(realignLineNumbersCheckBox);
add(displayDefaultConstructorCheckBox);
}

// --- PreferencesPanel --- //
Expand All @@ -51,17 +43,13 @@ public ClassFileViewerPreferencesProvider() {
@Override
public void loadPreferences(Map<String, String> preferences) {
escapeUnicodeCharactersCheckBox.setSelected(!"false".equals(preferences.get(ESCAPE_UNICODE_CHARACTERS)));
omitThisPrefixCheckBox.setSelected("true".equals(preferences.get(OMIT_THIS_PREFIX)));
realignLineNumbersCheckBox.setSelected("true".equals(preferences.get(REALIGN_LINE_NUMBERS)));
displayDefaultConstructorCheckBox.setSelected("true".equals(preferences.get(DISPLAY_DEFAULT_CONSTRUCTOR)));
}

@Override
public void savePreferences(Map<String, String> preferences) {
preferences.put(ESCAPE_UNICODE_CHARACTERS, Boolean.toString(escapeUnicodeCharactersCheckBox.isSelected()));
preferences.put(OMIT_THIS_PREFIX, Boolean.toString(omitThisPrefixCheckBox.isSelected()));
preferences.put(REALIGN_LINE_NUMBERS, Boolean.toString(realignLineNumbersCheckBox.isSelected()));
preferences.put(DISPLAY_DEFAULT_CONSTRUCTOR, Boolean.toString(displayDefaultConstructorCheckBox.isSelected()));
}

@Override public boolean arePreferencesValid() { return true; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,52 @@

package org.jd.gui.service.sourcesaver;

import jd.core.CoreConstants;
import jd.core.Decompiler;
import jd.core.process.DecompilerImpl;
import org.jd.core.v1.ClassFileToJavaSourceDecompiler;
import org.jd.gui.api.API;
import org.jd.gui.api.model.Container;
import org.jd.gui.util.decompiler.LineNumberStringBuilderPrinter;
import org.jd.gui.util.decompiler.ContainerLoader;
import org.jd.gui.util.decompiler.GuiPreferences;
import org.jd.gui.util.decompiler.PlainTextPrinter;
import org.jd.gui.util.exception.ExceptionUtil;
import org.jd.gui.util.io.NewlineOutputStream;

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.Manifest;

public class ClassFileSourceSaverProvider extends AbstractSourceSaverProvider {
protected static final String ESCAPE_UNICODE_CHARACTERS = "ClassFileSaverPreferences.escapeUnicodeCharacters";
protected static final String OMIT_THIS_PREFIX = "ClassFileSaverPreferences.omitThisPrefix";
protected static final String WRITE_DEFAULT_CONSTRUCTOR = "ClassFileSaverPreferences.writeDefaultConstructor";
protected static final String REALIGN_LINE_NUMBERS = "ClassFileSaverPreferences.realignLineNumbers";
protected static final String WRITE_LINE_NUMBERS = "ClassFileSaverPreferences.writeLineNumbers";
protected static final String WRITE_METADATA = "ClassFileSaverPreferences.writeMetadata";

protected static final Decompiler DECOMPILER = new DecompilerImpl();
protected static final ClassFileToJavaSourceDecompiler DECOMPILER = new ClassFileToJavaSourceDecompiler();

protected GuiPreferences preferences = new GuiPreferences();
protected ContainerLoader loader = new ContainerLoader();
protected PlainTextPrinter printer = new PlainTextPrinter();
protected ByteArrayOutputStream baos = new ByteArrayOutputStream();
protected LineNumberStringBuilderPrinter printer = new LineNumberStringBuilderPrinter();
protected String jdCoreVersion = "SNAPSHOT";

public ClassFileSourceSaverProvider() {
try {
Enumeration<URL> enumeration = ClassFileSourceSaverProvider.class.getClassLoader().getResources("META-INF/MANIFEST.MF");

while (enumeration.hasMoreElements()) {
try (InputStream is = enumeration.nextElement().openStream()) {
String attribute = new Manifest(is).getMainAttributes().getValue("JD-Core-Version");
if (attribute != null) {
jdCoreVersion = attribute;
}
}
}
} catch (IOException e) {
assert ExceptionUtil.printStackTrace(e);
}
}

@Override public String[] getSelectors() { return appendSelectors("*:file:*.class"); }

Expand Down Expand Up @@ -74,60 +89,67 @@ public void saveContent(API api, Controller controller, Listener listener, Path
listener.pathSaved(path);
}
// Init preferences
Map<String, String> p = api.getPreferences();
preferences.setUnicodeEscape(getPreferenceValue(p, ESCAPE_UNICODE_CHARACTERS, false));
preferences.setShowPrefixThis(! getPreferenceValue(p, OMIT_THIS_PREFIX, false));
preferences.setShowDefaultConstructor(getPreferenceValue(p, WRITE_DEFAULT_CONSTRUCTOR, false));
preferences.setRealignmentLineNumber(getPreferenceValue(p, REALIGN_LINE_NUMBERS, true));
preferences.setShowLineNumbers(getPreferenceValue(p, WRITE_LINE_NUMBERS, true));
Map<String, String> preferences = api.getPreferences();
boolean realignmentLineNumbers = getPreferenceValue(preferences, REALIGN_LINE_NUMBERS, true);
boolean unicodeEscape = getPreferenceValue(preferences, ESCAPE_UNICODE_CHARACTERS, false);
boolean showLineNumbers = getPreferenceValue(preferences, WRITE_LINE_NUMBERS, true);

Map<String, Object> configuration = new HashMap<>();
configuration.put("realignLineNumbers", realignmentLineNumbers);

// Init loader
loader.setEntry(entry);

// Init printer
baos.reset();
PrintStream ps = new PrintStream(baos, true, "UTF-8");
printer.setPrintStream(ps);
printer.setPreferences(preferences);
printer.setRealignmentLineNumber(realignmentLineNumbers);
printer.setUnicodeEscape(unicodeEscape);
printer.setShowLineNumbers(showLineNumbers);

// Format internal name
String entryPath = entry.getPath();
assert entryPath.endsWith(".class");
String entryInternalName = entryPath.substring(0, entryPath.length() - 6); // 6 = ".class".length()

// Decompile class file
DECOMPILER.decompile(preferences, loader, printer, entry.getPath());
DECOMPILER.decompile(configuration, loader, printer, entryInternalName);

StringBuilder stringBuffer = printer.getStringBuffer();

// Metadata
if (getPreferenceValue(p, WRITE_METADATA, true)) {
if (getPreferenceValue(preferences, WRITE_METADATA, true)) {
// Add location
String location =
new File(entry.getUri()).getPath()
// Escape "\ u" sequence to prevent "Invalid unicode" errors
.replaceAll("(^|[^\\\\])\\\\u", "\\\\\\\\u");
ps.print("\n\n/* Location: ");
ps.print(location);
stringBuffer.append("\n\n/* Location: ");
stringBuffer.append(location);
// Add Java compiler version
int majorVersion = printer.getMajorVersion();

if (majorVersion >= 45) {
ps.print("\n * Java compiler version: ");
stringBuffer.append("\n * Java compiler version: ");

if (majorVersion >= 49) {
ps.print(majorVersion - (49 - 5));
stringBuffer.append(majorVersion - (49 - 5));
} else {
ps.print(majorVersion - (45 - 1));
stringBuffer.append(majorVersion - (45 - 1));
}

ps.print(" (");
ps.print(majorVersion);
ps.print('.');
ps.print(printer.getMinorVersion());
ps.print(')');
stringBuffer.append(" (");
stringBuffer.append(majorVersion);
stringBuffer.append('.');
stringBuffer.append(printer.getMinorVersion());
stringBuffer.append(')');
}
// Add JD-Core version
ps.print("\n * JD-Core Version: ");
ps.print(CoreConstants.JD_CORE_VERSION);
ps.print("\n */");
stringBuffer.append("\n * JD-Core Version: ");
stringBuffer.append(jdCoreVersion);
stringBuffer.append("\n */");
}

try (OutputStream os = new NewlineOutputStream(Files.newOutputStream(path))) {
baos.writeTo(os);
try (PrintStream ps = new PrintStream(new NewlineOutputStream(Files.newOutputStream(path)), true, "UTF-8")) {
ps.print(stringBuffer.toString());
} catch (IOException e) {
assert ExceptionUtil.printStackTrace(e);
}
Expand All @@ -144,11 +166,6 @@ public void saveContent(API api, Controller controller, Listener listener, Path

protected static boolean getPreferenceValue(Map<String, String> preferences, String key, boolean defaultValue) {
String v = preferences.get(key);

if (v == null) {
return defaultValue;
} else {
return Boolean.valueOf(v);
}
return (v == null) ? defaultValue : Boolean.valueOf(v);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.jd.gui.api.feature.ContainerEntryGettable;
import org.jd.gui.api.feature.UriGettable;
import org.jd.gui.api.model.Container;
import org.jd.gui.util.decompiler.GuiPreferences;
import org.jd.gui.util.exception.ExceptionUtil;
import org.jd.gui.view.component.ClassFilePage;
import org.jd.gui.view.data.TreeNodeBean;
Expand Down
Loading

0 comments on commit 4f60170

Please sign in to comment.