From 4f60170081b4d51159c65f38eaef2abd45127d39 Mon Sep 17 00:00:00 2001 From: emmanue1 Date: Sun, 14 Apr 2019 22:34:28 +0200 Subject: [PATCH] Update JD-Core --- .../org/jd/gui/controller/MainController.java | 10 +- .../gui/view/component/panel/TabbedPanel.java | 8 +- build.gradle | 6 +- services/build.gradle | 2 +- .../indexer/DirectoryIndexerProvider.java | 3 +- .../ClassFileSaverPreferencesProvider.java | 12 - .../ClassFileViewerPreferencesProvider.java | 12 - .../ClassFileSourceSaverProvider.java | 105 +++--- .../ClassFileTreeNodeFactoryProvider.java | 1 - .../decompiler/ClassFileSourcePrinter.java | 224 ------------- .../gui/util/decompiler/ClassPathLoader.java | 47 +++ .../gui/util/decompiler/ContainerLoader.java | 47 ++- .../gui/util/decompiler/GuiPreferences.java | 43 --- .../LineNumberStringBuilderPrinter.java | 106 ++++++ .../jd/gui/util/decompiler/NopPrinter.java | 33 ++ .../gui/util/decompiler/PlainTextPrinter.java | 309 ------------------ .../util/decompiler/StringBuilderPrinter.java | 98 ++++++ .../jd/gui/view/component/ClassFilePage.java | 215 +++++------- 18 files changed, 471 insertions(+), 810 deletions(-) delete mode 100644 services/src/main/java/org/jd/gui/util/decompiler/ClassFileSourcePrinter.java create mode 100644 services/src/main/java/org/jd/gui/util/decompiler/ClassPathLoader.java delete mode 100644 services/src/main/java/org/jd/gui/util/decompiler/GuiPreferences.java create mode 100644 services/src/main/java/org/jd/gui/util/decompiler/LineNumberStringBuilderPrinter.java create mode 100644 services/src/main/java/org/jd/gui/util/decompiler/NopPrinter.java delete mode 100644 services/src/main/java/org/jd/gui/util/decompiler/PlainTextPrinter.java create mode 100644 services/src/main/java/org/jd/gui/util/decompiler/StringBuilderPrinter.java diff --git a/app/src/main/java/org/jd/gui/controller/MainController.java b/app/src/main/java/org/jd/gui/controller/MainController.java index 939de1b0..67f6da5c 100644 --- a/app/src/main/java/org/jd/gui/controller/MainController.java +++ b/app/src/main/java/org/jd/gui/controller/MainController.java @@ -120,15 +120,15 @@ public void show(List 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 diff --git a/app/src/main/java/org/jd/gui/view/component/panel/TabbedPanel.java b/app/src/main/java/org/jd/gui/view/component/panel/TabbedPanel.java index 6907bb0a..3dd04748 100644 --- a/app/src/main/java/org/jd/gui/view/component/panel/TabbedPanel.java +++ b/app/src/main/java/org/jd/gui/view/component/panel/TabbedPanel.java @@ -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); } }); @@ -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; diff --git a/build.gradle b/build.gradle index 2ae2a48a..d7c72d29 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { @@ -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 diff --git a/services/build.gradle b/services/build.gradle index 8550852a..917fc7ad 100644 --- a/services/build.gradle +++ b/services/build.gradle @@ -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' } diff --git a/services/src/main/java/org/jd/gui/service/indexer/DirectoryIndexerProvider.java b/services/src/main/java/org/jd/gui/service/indexer/DirectoryIndexerProvider.java index 02ae8b5a..a86c69da 100644 --- a/services/src/main/java/org/jd/gui/service/indexer/DirectoryIndexerProvider.java +++ b/services/src/main/java/org/jd/gui/service/indexer/DirectoryIndexerProvider.java @@ -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); diff --git a/services/src/main/java/org/jd/gui/service/preferencespanel/ClassFileSaverPreferencesProvider.java b/services/src/main/java/org/jd/gui/service/preferencespanel/ClassFileSaverPreferencesProvider.java index 4162a181..6f9cb420 100644 --- a/services/src/main/java/org/jd/gui/service/preferencespanel/ClassFileSaverPreferencesProvider.java +++ b/services/src/main/java/org/jd/gui/service/preferencespanel/ClassFileSaverPreferencesProvider.java @@ -15,16 +15,12 @@ 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; @@ -32,16 +28,12 @@ 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); } @@ -58,9 +50,7 @@ public ClassFileSaverPreferencesProvider() { @Override public void loadPreferences(Map 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))); } @@ -68,9 +58,7 @@ public void loadPreferences(Map preferences) { @Override public void savePreferences(Map 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())); } diff --git a/services/src/main/java/org/jd/gui/service/preferencespanel/ClassFileViewerPreferencesProvider.java b/services/src/main/java/org/jd/gui/service/preferencespanel/ClassFileViewerPreferencesProvider.java index ab7570d5..7c5ad178 100644 --- a/services/src/main/java/org/jd/gui/service/preferencespanel/ClassFileViewerPreferencesProvider.java +++ b/services/src/main/java/org/jd/gui/service/preferencespanel/ClassFileViewerPreferencesProvider.java @@ -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 --- // @@ -51,17 +43,13 @@ public ClassFileViewerPreferencesProvider() { @Override public void loadPreferences(Map 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 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; } diff --git a/services/src/main/java/org/jd/gui/service/sourcesaver/ClassFileSourceSaverProvider.java b/services/src/main/java/org/jd/gui/service/sourcesaver/ClassFileSourceSaverProvider.java index 6538230b..4fd248f4 100644 --- a/services/src/main/java/org/jd/gui/service/sourcesaver/ClassFileSourceSaverProvider.java +++ b/services/src/main/java/org/jd/gui/service/sourcesaver/ClassFileSourceSaverProvider.java @@ -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 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"); } @@ -74,60 +89,67 @@ public void saveContent(API api, Controller controller, Listener listener, Path listener.pathSaved(path); } // Init preferences - Map 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 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 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); } @@ -144,11 +166,6 @@ public void saveContent(API api, Controller controller, Listener listener, Path protected static boolean getPreferenceValue(Map 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); } } diff --git a/services/src/main/java/org/jd/gui/service/treenode/ClassFileTreeNodeFactoryProvider.java b/services/src/main/java/org/jd/gui/service/treenode/ClassFileTreeNodeFactoryProvider.java index a4fe40ad..b545800f 100644 --- a/services/src/main/java/org/jd/gui/service/treenode/ClassFileTreeNodeFactoryProvider.java +++ b/services/src/main/java/org/jd/gui/service/treenode/ClassFileTreeNodeFactoryProvider.java @@ -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; diff --git a/services/src/main/java/org/jd/gui/util/decompiler/ClassFileSourcePrinter.java b/services/src/main/java/org/jd/gui/util/decompiler/ClassFileSourcePrinter.java deleted file mode 100644 index a5559faf..00000000 --- a/services/src/main/java/org/jd/gui/util/decompiler/ClassFileSourcePrinter.java +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright (c) 2008-2019 Emmanuel Dupuy. - * This project is distributed under the GPLv3 license. - * This is a Copyleft license that gives the user the right to use, - * copy and modify the code freely for non-commercial purposes. - */ - -package org.jd.gui.util.decompiler; - -import jd.core.printer.Printer; - -public abstract class ClassFileSourcePrinter implements Printer { - protected static final String TAB = " "; - protected static final String NEWLINE = "\n"; - - protected int maxLineNumber = 0; - protected int indentationCount; - protected boolean display; - - protected abstract boolean getRealignmentLineNumber(); - protected abstract boolean isShowPrefixThis(); - protected abstract boolean isUnicodeEscape(); - - protected abstract void append(char c); - protected abstract void append(String s); - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - public void print(byte b) { append(String.valueOf(b)); } - public void print(int i) { append(String.valueOf(i)); } - - public void print(char c) { - if (this.display) - append(c); - } - - public void print(String s) { - if (this.display) - printEscape(s); - } - - public void printNumeric(String s) { append(s); } - - public void printString(String s, String scopeInternalName) { append(s); } - - public void printKeyword(String s) { - if (this.display) - append(s); - } - - public void printJavaWord(String s) { append(s); } - - public void printType(String internalName, String name, String scopeInternalName) { - if (this.display) - printEscape(name); - } - - public void printTypeDeclaration(String internalName, String name) { - printEscape(name); - } - - public void printTypeImport(String internalName, String name) { - printEscape(name); - } - - public void printField(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printFieldDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void printStaticField(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printStaticFieldDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void printConstructor(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printConstructorDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void printStaticConstructorDeclaration(String internalName, String name) { - append(name); - } - - public void printMethod(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printMethodDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void printStaticMethod(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printStaticMethodDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void start(int maxLineNumber, int majorVersion, int minorVersion) { - this.indentationCount = 0; - this.display = true; - this.maxLineNumber = maxLineNumber; - } - - public void end() {} - - public void indent() { - this.indentationCount++; - } - public void desindent() { - if (this.indentationCount > 0) - this.indentationCount--; - } - - public void startOfLine(int lineNumber) { - for (int i=0; i 0) { - append(NEWLINE); - } - } - } - - public void startOfComment() {} - public void endOfComment() {} - - public void startOfJavadoc() {} - public void endOfJavadoc() {} - - public void startOfXdoclet() {} - public void endOfXdoclet() {} - - public void startOfError() {} - public void endOfError() {} - - public void startOfImportStatements() {} - public void endOfImportStatements() {} - - public void startOfTypeDeclaration(String internalPath) {} - public void endOfTypeDeclaration() {} - - public void startOfAnnotationName() {} - public void endOfAnnotationName() {} - - public void startOfOptionalPrefix() { - if (!isShowPrefixThis()) - this.display = false; - } - - public void endOfOptionalPrefix() { - this.display = true; - } - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - public void debugStartOfLayoutBlock() {} - public void debugEndOfLayoutBlock() {} - - public void debugStartOfSeparatorLayoutBlock() {} - public void debugEndOfSeparatorLayoutBlock(int min, int value, int max) {} - - public void debugStartOfStatementsBlockLayoutBlock() {} - public void debugEndOfStatementsBlockLayoutBlock(int min, int value, int max) {} - - public void debugStartOfInstructionBlockLayoutBlock() {} - public void debugEndOfInstructionBlockLayoutBlock() {} - - public void debugStartOfCommentDeprecatedLayoutBlock() {} - public void debugEndOfCommentDeprecatedLayoutBlock() {} - - public void debugMarker(String marker) {} - - public void debugStartOfCaseBlockLayoutBlock() {} - public void debugEndOfCaseBlockLayoutBlock() {} - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - protected void printEscape(String s) { - if (isUnicodeEscape()) { - int length = s.length(); - - for (int i=0; i> 3))); - append((char) ('0' + (c & 0x7))); - } else if (c > 127) { - // Write octal format - append("\\u"); - - int z = (c >> 12); - append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); - z = ((c >> 8) & 0xF); - append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); - z = ((c >> 4) & 0xF); - append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); - z = (c & 0xF); - append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); - } else { - append(c); - } - } - } else { - append(s); - } - } -} diff --git a/services/src/main/java/org/jd/gui/util/decompiler/ClassPathLoader.java b/services/src/main/java/org/jd/gui/util/decompiler/ClassPathLoader.java new file mode 100644 index 00000000..30f850e3 --- /dev/null +++ b/services/src/main/java/org/jd/gui/util/decompiler/ClassPathLoader.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2008-2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.gui.util.decompiler; + +import org.jd.core.v1.api.loader.Loader; +import org.jd.core.v1.api.loader.LoaderException; +import org.jd.gui.api.model.Container; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +public class ClassPathLoader implements Loader { + protected byte[] buffer = new byte[1024 * 4]; + + @Override + public boolean canLoad(String internalName) { + return this.getClass().getResource("/" + internalName + ".class") != null; + } + + @Override + public byte[] load(String internalName) throws LoaderException { + InputStream is = this.getClass().getResourceAsStream("/" + internalName + ".class"); + + if (is == null) { + return null; + } else { + try (InputStream input=is; ByteArrayOutputStream output=new ByteArrayOutputStream()) { + int len = input.read(buffer); + + while (len > 0) { + output.write(buffer, 0, len); + len = input.read(buffer); + } + + return output.toByteArray(); + } catch (IOException e) { + throw new LoaderException(e); + } + } + } +} diff --git a/services/src/main/java/org/jd/gui/util/decompiler/ContainerLoader.java b/services/src/main/java/org/jd/gui/util/decompiler/ContainerLoader.java index 9e6bcc9a..9d491bfc 100644 --- a/services/src/main/java/org/jd/gui/util/decompiler/ContainerLoader.java +++ b/services/src/main/java/org/jd/gui/util/decompiler/ContainerLoader.java @@ -7,13 +7,16 @@ package org.jd.gui.util.decompiler; -import jd.core.loader.Loader; -import jd.core.loader.LoaderException; +import org.jd.core.v1.api.loader.Loader; +import org.jd.core.v1.api.loader.LoaderException; import org.jd.gui.api.model.Container; -import java.io.DataInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; public class ContainerLoader implements Loader { + protected byte[] buffer = new byte[1024 * 4]; protected Container.Entry entry; public ContainerLoader() { this.entry = null; } @@ -21,23 +24,47 @@ public ContainerLoader(Container.Entry entry) { this.entry = entry; } + public void setEntry(Container.Entry e) { this.entry = e; } + protected Container.Entry getEntry(String internalPath) { + String path = internalPath + ".class"; + + if (entry.getPath().equals(path)) { + return entry; + } for (Container.Entry e : entry.getParent().getChildren()) { - if (e.getPath().equals(internalPath)) { + if (e.getPath().equals(path)) { return e; } } return null; } - public void setEntry(Container.Entry e) { this.entry = e; } - // --- Loader --- // - public DataInputStream load(String internalPath) throws LoaderException { - return new DataInputStream(getEntry(internalPath).getInputStream()); + @Override + public boolean canLoad(String internalName) { + return getEntry(internalName) != null; } - public boolean canLoad(String internalPath) { - return getEntry(internalPath) != null; + @Override + public byte[] load(String internalName) throws LoaderException { + Container.Entry entry = getEntry(internalName); + + if (entry == null) { + return null; + } else { + try (InputStream input=entry.getInputStream(); ByteArrayOutputStream output=new ByteArrayOutputStream()) { + int len = input.read(buffer); + + while (len > 0) { + output.write(buffer, 0, len); + len = input.read(buffer); + } + + return output.toByteArray(); + } catch (IOException e) { + throw new LoaderException(e); + } + } } } diff --git a/services/src/main/java/org/jd/gui/util/decompiler/GuiPreferences.java b/services/src/main/java/org/jd/gui/util/decompiler/GuiPreferences.java deleted file mode 100644 index c4dd9bfc..00000000 --- a/services/src/main/java/org/jd/gui/util/decompiler/GuiPreferences.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2008-2019 Emmanuel Dupuy. - * This project is distributed under the GPLv3 license. - * This is a Copyleft license that gives the user the right to use, - * copy and modify the code freely for non-commercial purposes. - */ - -package org.jd.gui.util.decompiler; - -import jd.core.preferences.Preferences; - -public class GuiPreferences extends Preferences { - protected boolean showPrefixThis; - protected boolean unicodeEscape; - protected boolean showLineNumbers; - - public GuiPreferences() - { - this.showPrefixThis = true; - this.unicodeEscape = false; - this.showLineNumbers = true; - } - - public GuiPreferences( - boolean showDefaultConstructor, boolean realignmentLineNumber, - boolean showPrefixThis, boolean unicodeEscape, boolean showLineNumbers) - { - super(showDefaultConstructor, realignmentLineNumber); - this.showPrefixThis = showPrefixThis; - this.unicodeEscape = unicodeEscape; - this.showLineNumbers = showLineNumbers; - } - - public void setShowDefaultConstructor(boolean b) { showDefaultConstructor = b; } - public void setRealignmentLineNumber(boolean b) { realignmentLineNumber=b; } - public void setShowPrefixThis(boolean b) { showPrefixThis = b; } - public void setUnicodeEscape(boolean b) { unicodeEscape=b; } - public void setShowLineNumbers(boolean b) { showLineNumbers=b; } - - public boolean isShowPrefixThis() { return showPrefixThis; } - public boolean isUnicodeEscape() { return unicodeEscape; } - public boolean isShowLineNumbers() { return showLineNumbers; } -} diff --git a/services/src/main/java/org/jd/gui/util/decompiler/LineNumberStringBuilderPrinter.java b/services/src/main/java/org/jd/gui/util/decompiler/LineNumberStringBuilderPrinter.java new file mode 100644 index 00000000..0b195e12 --- /dev/null +++ b/services/src/main/java/org/jd/gui/util/decompiler/LineNumberStringBuilderPrinter.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2008-2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.gui.util.decompiler; + +public class LineNumberStringBuilderPrinter extends StringBuilderPrinter { + protected boolean showLineNumbers = false; + + protected int maxLineNumber = 0; + protected int digitCount = 0; + + protected String lineNumberBeginPrefix; + protected String lineNumberEndPrefix; + protected String unknownLineNumberPrefix; + + public void setShowLineNumbers(boolean showLineNumbers) { this.showLineNumbers = showLineNumbers; } + + protected int printDigit(int dcv, int lineNumber, int divisor, int left) { + if (digitCount >= dcv) { + if (lineNumber < divisor) { + append(' '); + } else { + int e = (lineNumber-left) / divisor; + append((char)('0' + e)); + left += e*divisor; + } + } + + return left; + } + + // --- Printer --- // + @Override + public void start(int maxLineNumber, int majorVersion, int minorVersion) { + super.start(maxLineNumber, majorVersion, minorVersion); + + if (showLineNumbers) { + this.maxLineNumber = maxLineNumber; + + if (maxLineNumber > 0) { + digitCount = 1; + unknownLineNumberPrefix = " "; + int maximum = 9; + + while (maximum < maxLineNumber) { + digitCount++; + unknownLineNumberPrefix += ' '; + maximum = maximum*10 + 9; + } + + lineNumberBeginPrefix = "/* "; + lineNumberEndPrefix = " */ "; + } else { + unknownLineNumberPrefix = ""; + lineNumberBeginPrefix = ""; + lineNumberEndPrefix = ""; + } + } else { + this.maxLineNumber = 0; + unknownLineNumberPrefix = ""; + lineNumberBeginPrefix = ""; + lineNumberEndPrefix = ""; + } + } + + @Override public void startLine(int lineNumber) { + if (maxLineNumber > 0) { + append(lineNumberBeginPrefix); + + if (lineNumber == UNKNOWN_LINE_NUMBER) { + append(unknownLineNumberPrefix); + } else { + int left = 0; + + left = printDigit(5, lineNumber, 10000, left); + left = printDigit(4, lineNumber, 1000, left); + left = printDigit(3, lineNumber, 100, left); + left = printDigit(2, lineNumber, 10, left); + append((char)('0' + (lineNumber-left))); + } + + append(lineNumberEndPrefix); + } + + for (int i=0; i 0) { + if (maxLineNumber > 0) { + append(lineNumberBeginPrefix); + append(unknownLineNumberPrefix); + append(lineNumberEndPrefix); + } + + append(NEWLINE); + } + } + } +} diff --git a/services/src/main/java/org/jd/gui/util/decompiler/NopPrinter.java b/services/src/main/java/org/jd/gui/util/decompiler/NopPrinter.java new file mode 100644 index 00000000..363b8d13 --- /dev/null +++ b/services/src/main/java/org/jd/gui/util/decompiler/NopPrinter.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2008-2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.gui.util.decompiler; + +import org.jd.core.v1.api.printer.Printer; + +public class NopPrinter implements Printer { + @Override public void start(int maxLineNumber, int majorVersion, int minorVersion) {} + @Override public void end() {} + + @Override public void printText(String text) {} + @Override public void printNumericConstant(String constant) {} + @Override public void printStringConstant(String constant, String ownerInternalName) {} + @Override public void printKeyword(String keyword) {} + + @Override public void printDeclaration(int flags, String internalTypeName, String name, String descriptor) {} + @Override public void printReference(int flags, String internalTypeName, String name, String descriptor, String ownerInternalName) {} + + @Override public void indent() {} + @Override public void unindent() {} + + @Override public void startLine(int lineNumber) {} + @Override public void endLine() {} + @Override public void extraLine(int count) {} + + @Override public void startMarker(int type) {} + @Override public void endMarker(int type) {} +} diff --git a/services/src/main/java/org/jd/gui/util/decompiler/PlainTextPrinter.java b/services/src/main/java/org/jd/gui/util/decompiler/PlainTextPrinter.java deleted file mode 100644 index 4727506b..00000000 --- a/services/src/main/java/org/jd/gui/util/decompiler/PlainTextPrinter.java +++ /dev/null @@ -1,309 +0,0 @@ -/* - * Copyright (c) 2008-2019 Emmanuel Dupuy. - * This project is distributed under the GPLv3 license. - * This is a Copyleft license that gives the user the right to use, - * copy and modify the code freely for non-commercial purposes. - */ - -package org.jd.gui.util.decompiler; - -import java.io.PrintStream; - -import jd.core.model.instruction.bytecode.instruction.Instruction; -import jd.core.printer.Printer; - -public class PlainTextPrinter implements Printer { - protected static final String TAB = " "; - protected static final String NEWLINE = "\n"; - - protected GuiPreferences preferences = null; - protected PrintStream printStream = null; - protected int maxLineNumber = 0; - protected int majorVersion = 0; - protected int minorVersion = 0; - protected int digitCount = 0; - - protected String lineNumberBeginPrefix; - protected String lineNumberEndPrefix; - protected String unknownLineNumberPrefix; - protected int indentationCount; - protected boolean display; - - public void setPreferences(GuiPreferences preferences) { this.preferences = preferences; } - public void setPrintStream(PrintStream printStream) { this.printStream = printStream; } - - public int getMajorVersion() { return majorVersion; } - public int getMinorVersion() { return minorVersion; } - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - public void print(byte b) { this.printStream.append(String.valueOf(b)); } - public void print(int i) { this.printStream.append(String.valueOf(i)); } - - public void print(char c) { - if (this.display) - this.printStream.append(String.valueOf(c)); - } - - public void print(String s) { - if (this.display) - printEscape(s); - } - - public void printNumeric(String s) { this.printStream.append(s); } - - public void printString(String s, String scopeInternalName) { this.printStream.append(s); } - - public void printKeyword(String s) { - if (this.display) - this.printStream.append(s); - } - - public void printJavaWord(String s) { this.printStream.append(s); } - - public void printType(String internalName, String name, String scopeInternalName) { - if (this.display) - printEscape(name); - } - - public void printTypeDeclaration(String internalName, String name) - { - printEscape(name); - } - - public void printTypeImport(String internalName, String name) - { - printEscape(name); - } - - public void printField(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printFieldDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void printStaticField(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printStaticFieldDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void printConstructor(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printConstructorDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void printStaticConstructorDeclaration(String internalName, String name) { - this.printStream.append(name); - } - - public void printMethod(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printMethodDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void printStaticMethod(String internalName, String name, String descriptor, String scopeInternalName) { - printEscape(name); - } - public void printStaticMethodDeclaration(String internalName, String name, String descriptor) { - printEscape(name); - } - - public void start(int maxLineNumber, int majorVersion, int minorVersion) { - this.majorVersion = majorVersion; - this.minorVersion = minorVersion; - this.indentationCount = 0; - this.display = true; - - if (this.preferences.isShowLineNumbers()) { - this.maxLineNumber = maxLineNumber; - - if (maxLineNumber > 0) { - this.digitCount = 1; - this.unknownLineNumberPrefix = " "; - int maximum = 9; - - while (maximum < maxLineNumber) { - this.digitCount++; - this.unknownLineNumberPrefix += ' '; - maximum = maximum*10 + 9; - } - - this.lineNumberBeginPrefix = "/* "; - this.lineNumberEndPrefix = " */ "; - } else { - this.unknownLineNumberPrefix = ""; - this.lineNumberBeginPrefix = ""; - this.lineNumberEndPrefix = ""; - } - } else { - this.maxLineNumber = 0; - this.unknownLineNumberPrefix = ""; - this.lineNumberBeginPrefix = ""; - this.lineNumberEndPrefix = ""; - } - } - - public void end() {} - - public void indent() { - this.indentationCount++; - } - public void desindent() { - if (this.indentationCount > 0) - this.indentationCount--; - } - - public void startOfLine(int lineNumber) { - if (this.maxLineNumber > 0) - { - this.printStream.append(this.lineNumberBeginPrefix); - - if (lineNumber == Instruction.UNKNOWN_LINE_NUMBER) { - this.printStream.append(this.unknownLineNumberPrefix); - } else { - int left = 0; - - left = printDigit(5, lineNumber, 10000, left); - left = printDigit(4, lineNumber, 1000, left); - left = printDigit(3, lineNumber, 100, left); - left = printDigit(2, lineNumber, 10, left); - this.printStream.append((char)('0' + (lineNumber-left))); - } - - this.printStream.append(this.lineNumberEndPrefix); - } - - for (int i=0; i 0) { - if (this.maxLineNumber > 0) { - this.printStream.append(this.lineNumberBeginPrefix); - this.printStream.append(this.unknownLineNumberPrefix); - this.printStream.append(this.lineNumberEndPrefix); - } - - this.printStream.append(NEWLINE); - } - } - } - - public void startOfComment() {} - public void endOfComment() {} - - public void startOfJavadoc() {} - public void endOfJavadoc() {} - - public void startOfXdoclet() {} - public void endOfXdoclet() {} - - public void startOfError() {} - public void endOfError() {} - - public void startOfImportStatements() {} - public void endOfImportStatements() {} - - public void startOfTypeDeclaration(String internalPath) {} - public void endOfTypeDeclaration() {} - - public void startOfAnnotationName() {} - public void endOfAnnotationName() {} - - public void startOfOptionalPrefix() { - if (!this.preferences.isShowPrefixThis()) - this.display = false; - } - - public void endOfOptionalPrefix() - { - this.display = true; - } - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - public void debugStartOfLayoutBlock() {} - public void debugEndOfLayoutBlock() {} - - public void debugStartOfSeparatorLayoutBlock() {} - public void debugEndOfSeparatorLayoutBlock(int min, int value, int max) {} - - public void debugStartOfStatementsBlockLayoutBlock() {} - public void debugEndOfStatementsBlockLayoutBlock(int min, int value, int max) {} - - public void debugStartOfInstructionBlockLayoutBlock() {} - public void debugEndOfInstructionBlockLayoutBlock() {} - - public void debugStartOfCommentDeprecatedLayoutBlock() {} - public void debugEndOfCommentDeprecatedLayoutBlock() {} - - public void debugMarker(String marker) {} - - public void debugStartOfCaseBlockLayoutBlock() {} - public void debugEndOfCaseBlockLayoutBlock() {} - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - protected void printEscape(String s) { - if (this.preferences.isUnicodeEscape()) { - int length = s.length(); - - for (int i=0; i> 3))); - this.printStream.append((char)('0' + (c & 0x7))); - } else if (c > 127) { - // Write octal format - this.printStream.append("\\u"); - - int z = (c >> 12); - this.printStream.append((char)((z <= 9) ? ('0' + z) : (('A' - 10) + z))); - z = ((c >> 8) & 0xF); - this.printStream.append((char)((z <= 9) ? ('0' + z) : (('A' - 10) + z))); - z = ((c >> 4) & 0xF); - this.printStream.append((char)((z <= 9) ? ('0' + z) : (('A' - 10) + z))); - z = (c & 0xF); - this.printStream.append((char)((z <= 9) ? ('0' + z) : (('A' - 10) + z))); - } else { - this.printStream.append(c); - } - } - } else { - this.printStream.append(s); - } - } - - protected int printDigit(int dcv, int lineNumber, int divisor, int left) { - if (this.digitCount >= dcv) { - if (lineNumber < divisor) { - this.printStream.append(' '); - } else { - int e = (lineNumber-left) / divisor; - this.printStream.append((char)('0' + e)); - left += e*divisor; - } - } - - return left; - } -} diff --git a/services/src/main/java/org/jd/gui/util/decompiler/StringBuilderPrinter.java b/services/src/main/java/org/jd/gui/util/decompiler/StringBuilderPrinter.java new file mode 100644 index 00000000..5618b817 --- /dev/null +++ b/services/src/main/java/org/jd/gui/util/decompiler/StringBuilderPrinter.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2008-2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.gui.util.decompiler; + +import org.jd.core.v1.api.printer.Printer; + +public class StringBuilderPrinter implements Printer { + protected static final String TAB = " "; + protected static final String NEWLINE = "\n"; + + protected StringBuilder stringBuffer = new StringBuilder(10*1024); + + protected boolean unicodeEscape = true; + protected boolean realignmentLineNumber = false; + + protected int majorVersion = 0; + protected int minorVersion = 0; + protected int indentationCount; + + public void setUnicodeEscape(boolean unicodeEscape) { this.unicodeEscape = unicodeEscape; } + public void setRealignmentLineNumber(boolean realignmentLineNumber) { this.realignmentLineNumber = realignmentLineNumber; } + + public int getMajorVersion() { return majorVersion; } + public int getMinorVersion() { return minorVersion; } + public StringBuilder getStringBuffer() { return stringBuffer; } + + public void append(char c) { stringBuffer.append(c); } + public void append(String s) { stringBuffer.append(s); } + + protected void printEscape(String s) { + if (unicodeEscape) { + int length = s.length(); + + for (int i=0; i> 3))); + append((char) ('0' + (c & 0x7))); + } else if (c > 127) { + // Write octal format + append("\\u"); + + int z = (c >> 12); + append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); + z = ((c >> 8) & 0xF); + append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); + z = ((c >> 4) & 0xF); + append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); + z = (c & 0xF); + append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); + } else { + append(c); + } + } + } else { + append(s); + } + } + + // --- Printer --- // + @Override + public void start(int maxLineNumber, int majorVersion, int minorVersion) { + this.stringBuffer.setLength(0); + this.majorVersion = majorVersion; + this.minorVersion = minorVersion; + this.indentationCount = 0; + } + + @Override public void end() {} + + @Override public void printText(String text) { printEscape(text); } + @Override public void printNumericConstant(String constant) { append(constant); } + @Override public void printStringConstant(String constant, String ownerInternalName) { append(constant); } + @Override public void printKeyword(String keyword) { append(keyword); } + + @Override public void printDeclaration(int flags, String internalTypeName, String name, String descriptor) { append(name); } + @Override public void printReference(int flags, String internalTypeName, String name, String descriptor, String ownerInternalName) { append(name); } + + @Override public void indent() { indentationCount++; } + @Override public void unindent() { if (indentationCount > 0) indentationCount--; } + + @Override public void startLine(int lineNumber) { for (int i=0; i 0) append(NEWLINE); } + + @Override public void startMarker(int type) {} + @Override public void endMarker(int type) {} +} diff --git a/services/src/main/java/org/jd/gui/view/component/ClassFilePage.java b/services/src/main/java/org/jd/gui/view/component/ClassFilePage.java index bc51ecb9..09bde429 100644 --- a/services/src/main/java/org/jd/gui/view/component/ClassFilePage.java +++ b/services/src/main/java/org/jd/gui/view/component/ClassFilePage.java @@ -7,55 +7,37 @@ package org.jd.gui.view.component; -import jd.core.Decompiler; -import jd.core.loader.Loader; -import jd.core.loader.LoaderException; -import jd.core.process.DecompilerImpl; import org.fife.ui.rsyntaxtextarea.DocumentRange; import org.fife.ui.rsyntaxtextarea.SyntaxConstants; +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.ClassFileSourcePrinter; +import org.jd.gui.util.decompiler.ClassPathLoader; +import org.jd.gui.util.decompiler.NopPrinter; +import org.jd.gui.util.decompiler.StringBuilderPrinter; import org.jd.gui.util.decompiler.ContainerLoader; -import org.jd.gui.util.decompiler.GuiPreferences; import org.jd.gui.util.exception.ExceptionUtil; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultCaret; import java.awt.*; -import java.io.DataInputStream; +import java.util.Collections; import java.util.HashMap; import java.util.Map; public class ClassFilePage extends TypePage { 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 static final Decompiler DECOMPILER = new DecompilerImpl(); + protected static final ClassFileToJavaSourceDecompiler DECOMPILER = new ClassFileToJavaSourceDecompiler(); protected int maximumLineNumber = -1; static { // Early class loading - String internalTypeName = ClassFilePage.class.getName().replace('.', '/'); - GuiPreferences preferences = new GuiPreferences(); - Loader loader = new Loader() { - public DataInputStream load(String internalTypePath) throws LoaderException { - return new DataInputStream(ClassFilePage.class.getClassLoader().getResourceAsStream(internalTypeName + ".class")); - } - public boolean canLoad(String internalTypePath) { return false; } - }; - ClassFileSourcePrinter printer = new ClassFileSourcePrinter() { - public boolean getRealignmentLineNumber() { return false; } - public boolean isShowPrefixThis() { return false; } - public boolean isUnicodeEscape() { return false; } - public void append(char c) {} - public void append(String s) {} - }; try { - DECOMPILER.decompile(preferences, loader, printer, internalTypeName); + String internalTypeName = ClassFilePage.class.getName().replace('.', '/'); + DECOMPILER.decompile(Collections.emptyMap(), new ClassPathLoader(), new NopPrinter(), internalTypeName); } catch (Throwable t) { assert ExceptionUtil.printStackTrace(t); } @@ -78,22 +60,33 @@ public void decompile(Map preferences) { declarations.clear(); typeDeclarations.clear(); strings.clear(); + // Init preferences - GuiPreferences p = new GuiPreferences(); - p.setUnicodeEscape(getPreferenceValue(preferences, ESCAPE_UNICODE_CHARACTERS, false)); - p.setShowPrefixThis(! getPreferenceValue(preferences, OMIT_THIS_PREFIX, false)); - p.setShowDefaultConstructor(getPreferenceValue(preferences, DISPLAY_DEFAULT_CONSTRUCTOR, false)); - p.setRealignmentLineNumber(getPreferenceValue(preferences, REALIGN_LINE_NUMBERS, false)); + boolean realignmentLineNumbers = getPreferenceValue(preferences, REALIGN_LINE_NUMBERS, true); + boolean unicodeEscape = getPreferenceValue(preferences, ESCAPE_UNICODE_CHARACTERS, false); + + Map configuration = new HashMap<>(); + configuration.put("realignLineNumbers", realignmentLineNumbers); + + setShowMisalignment(realignmentLineNumbers); - setShowMisalignment(p.getRealignmentLineNumber()); // Init loader ContainerLoader loader = new ContainerLoader(entry); + // Init printer - Printer printer = new Printer(p); + ClassFilePrinter printer = new ClassFilePrinter(); + printer.setRealignmentLineNumber(realignmentLineNumbers); + printer.setUnicodeEscape(unicodeEscape); + + // 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(p, loader, printer, entry.getPath()); + DECOMPILER.decompile(configuration, loader, printer, entryInternalName); + setText(printer.getStringBuffer().toString()); - setText(printer.toString()); // Show hyperlinks indexesChanged(api.getCollectionOfIndexes()); } catch (Throwable t) { @@ -106,12 +99,7 @@ public void decompile(Map preferences) { protected static boolean getPreferenceValue(Map 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); } public String getSyntaxStyle() { return SyntaxConstants.SYNTAX_STYLE_JAVA; } @@ -153,29 +141,13 @@ public void preferencesChanged(Map preferences) { super.preferencesChanged(preferences); } - public class Printer extends ClassFileSourcePrinter { - protected StringBuilder stringBuffer = new StringBuilder(10*1024); - protected boolean realignmentLineNumber; - protected boolean showPrefixThis; - protected boolean unicodeEscape; + public class ClassFilePrinter extends StringBuilderPrinter { protected HashMap referencesCache = new HashMap<>(); - public Printer(GuiPreferences preferences) { - this.realignmentLineNumber = preferences.getRealignmentLineNumber(); - this.showPrefixThis = preferences.isShowPrefixThis(); - this.unicodeEscape = preferences.isUnicodeEscape(); - } - - public boolean getRealignmentLineNumber() { return realignmentLineNumber; } - public boolean isShowPrefixThis() { return showPrefixThis; } - public boolean isUnicodeEscape() { return unicodeEscape; } - - public void append(char c) { stringBuffer.append(c); } - public void append(String s) { stringBuffer.append(s); } - // Manage line number and misalignment int textAreaLineNumber = 1; + @Override public void start(int maxLineNumber, int majorVersion, int minorVersion) { super.start(maxLineNumber, majorVersion, minorVersion); @@ -185,12 +157,51 @@ public void start(int maxLineNumber, int majorVersion, int minorVersion) { setMaxLineNumber(maxLineNumber); } } - public void startOfLine(int sourceLineNumber) { - super.startOfLine(sourceLineNumber); - setLineNumber(textAreaLineNumber, sourceLineNumber); + + // --- Add strings --- // + @Override public void printStringConstant(String constant, String ownerInternalName) { + strings.add(new TypePage.StringData(stringBuffer.length(), constant.length(), constant, ownerInternalName)); + super.printStringConstant(constant, ownerInternalName); + } + + @Override public void printDeclaration(int flags, String internalTypeName, String name, String descriptor) { + switch (flags) { + case TYPE_FLAG: + TypePage.DeclarationData data = new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalTypeName, null, null); + declarations.put(internalTypeName, data); + typeDeclarations.put(stringBuffer.length(), data); + break; + case CONSTRUCTOR_FLAG: + declarations.put(internalTypeName + "--" + descriptor, new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalTypeName, "", descriptor)); + break; + default: + declarations.put(internalTypeName + '-' + name + '-' + descriptor, new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalTypeName, name, descriptor)); + break; + } + super.printDeclaration(flags, internalTypeName, name, descriptor); + } + + @Override public void printReference(int flags, String internalTypeName, String name, String descriptor, String ownerInternalName) { + switch (flags) { + case TYPE_FLAG: + addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalTypeName, null, null, ownerInternalName))); + break; + case CONSTRUCTOR_FLAG: + addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalTypeName, "", descriptor, ownerInternalName))); + break; + default: + addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalTypeName, name, descriptor, ownerInternalName))); + break; + } + super.printReference(flags, internalTypeName, name, descriptor, ownerInternalName); } - public void endOfLine() { - super.endOfLine(); + + @Override public void startLine(int lineNumber) { + super.startLine(lineNumber); + setLineNumber(textAreaLineNumber, lineNumber); + } + @Override public void endLine() { + super.endLine(); textAreaLineNumber++; } public void extraLine(int count) { @@ -200,46 +211,7 @@ public void extraLine(int count) { } } - // --- Add strings --- // - public void printString(String s, String scopeInternalName) { - strings.add(new TypePage.StringData(stringBuffer.length(), s.length(), s, scopeInternalName)); - super.printString(s, scopeInternalName); - } - // --- Add references --- // - public void printTypeImport(String internalName, String name) { - addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalName, null, null, null))); - super.printTypeImport(internalName, name); - } - - public void printType(String internalName, String name, String scopeInternalName) { - addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalName, null, null, scopeInternalName))); - super.printType(internalName, name, scopeInternalName); - } - - public void printField(String internalName, String name, String descriptor, String scopeInternalName) { - addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalName, name, descriptor, scopeInternalName))); - super.printField(internalName, name, descriptor, scopeInternalName); - } - public void printStaticField(String internalName, String name, String descriptor, String scopeInternalName) { - addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalName, name, descriptor, scopeInternalName))); - super.printStaticField(internalName, name, descriptor, scopeInternalName); - } - - public void printConstructor(String internalName, String name, String descriptor, String scopeInternalName) { - addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalName, "", descriptor, scopeInternalName))); - super.printConstructor(internalName, name, descriptor, scopeInternalName); - } - - public void printMethod(String internalName, String name, String descriptor, String scopeInternalName) { - addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalName, name, descriptor, scopeInternalName))); - super.printMethod(internalName, name, descriptor, scopeInternalName); - } - public void printStaticMethod(String internalName, String name, String descriptor, String scopeInternalName) { - addHyperlink(new TypePage.HyperlinkReferenceData(stringBuffer.length(), name.length(), newReferenceData(internalName, name, descriptor, scopeInternalName))); - super.printStaticMethod(internalName, name, descriptor, scopeInternalName); - } - public TypePage.ReferenceData newReferenceData(String internalName, String name, String descriptor, String scopeInternalName) { String key = internalName + '-' + name + '-'+ descriptor + '-' + scopeInternalName; ReferenceData reference = referencesCache.get(key); @@ -252,42 +224,5 @@ public TypePage.ReferenceData newReferenceData(String internalName, String name, return reference; } - - // --- Add declarations --- // - public void printTypeDeclaration(String internalName, String name) { - TypePage.DeclarationData data = new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalName, null, null); - declarations.put(internalName, data); - typeDeclarations.put(stringBuffer.length(), data); - super.printTypeDeclaration(internalName, name); - } - - public void printFieldDeclaration(String internalName, String name, String descriptor) { - declarations.put(internalName + '-' + name + '-' + descriptor, new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalName, name, descriptor)); - super.printFieldDeclaration(internalName, name, descriptor); - } - public void printStaticFieldDeclaration(String internalName, String name, String descriptor) { - declarations.put(internalName + '-' + name + '-' + descriptor, new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalName, name, descriptor)); - super.printStaticFieldDeclaration(internalName, name, descriptor); - } - - public void printConstructorDeclaration(String internalName, String name, String descriptor) { - declarations.put(internalName + "--" + descriptor, new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalName, "", descriptor)); - super.printConstructorDeclaration(internalName, name, descriptor); - } - public void printStaticConstructorDeclaration(String internalName, String name) { - declarations.put(internalName + "--()V", new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalName, "", "()V")); - super.printStaticConstructorDeclaration(internalName, name); - } - - public void printMethodDeclaration(String internalName, String name, String descriptor) { - declarations.put(internalName + '-' + name + '-' + descriptor, new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalName, name, descriptor)); - super.printMethodDeclaration(internalName, name, descriptor); - } - public void printStaticMethodDeclaration(String internalName, String name, String descriptor) { - declarations.put(internalName + '-' + name + '-' + descriptor, new TypePage.DeclarationData(stringBuffer.length(), name.length(), internalName, name, descriptor)); - super.printStaticMethodDeclaration(internalName, name, descriptor); - } - - public String toString() { return stringBuffer.toString(); } } }