77
88package org .jd .gui .service .sourcesaver ;
99
10- import jd .core .CoreConstants ;
11- import jd .core .Decompiler ;
12- import jd .core .process .DecompilerImpl ;
10+ import org .jd .core .v1 .ClassFileToJavaSourceDecompiler ;
1311import org .jd .gui .api .API ;
1412import org .jd .gui .api .model .Container ;
13+ import org .jd .gui .util .decompiler .LineNumberStringBuilderPrinter ;
1514import org .jd .gui .util .decompiler .ContainerLoader ;
16- import org .jd .gui .util .decompiler .GuiPreferences ;
17- import org .jd .gui .util .decompiler .PlainTextPrinter ;
1815import org .jd .gui .util .exception .ExceptionUtil ;
1916import org .jd .gui .util .io .NewlineOutputStream ;
2017
2118import java .io .*;
19+ import java .net .URL ;
2220import java .nio .charset .Charset ;
2321import java .nio .file .Files ;
2422import java .nio .file .Path ;
23+ import java .util .Enumeration ;
24+ import java .util .HashMap ;
2525import java .util .Map ;
26+ import java .util .jar .Manifest ;
2627
2728public class ClassFileSourceSaverProvider extends AbstractSourceSaverProvider {
2829 protected static final String ESCAPE_UNICODE_CHARACTERS = "ClassFileSaverPreferences.escapeUnicodeCharacters" ;
29- protected static final String OMIT_THIS_PREFIX = "ClassFileSaverPreferences.omitThisPrefix" ;
30- protected static final String WRITE_DEFAULT_CONSTRUCTOR = "ClassFileSaverPreferences.writeDefaultConstructor" ;
3130 protected static final String REALIGN_LINE_NUMBERS = "ClassFileSaverPreferences.realignLineNumbers" ;
3231 protected static final String WRITE_LINE_NUMBERS = "ClassFileSaverPreferences.writeLineNumbers" ;
3332 protected static final String WRITE_METADATA = "ClassFileSaverPreferences.writeMetadata" ;
3433
35- protected static final Decompiler DECOMPILER = new DecompilerImpl ();
34+ protected static final ClassFileToJavaSourceDecompiler DECOMPILER = new ClassFileToJavaSourceDecompiler ();
3635
37- protected GuiPreferences preferences = new GuiPreferences ();
3836 protected ContainerLoader loader = new ContainerLoader ();
39- protected PlainTextPrinter printer = new PlainTextPrinter ();
40- protected ByteArrayOutputStream baos = new ByteArrayOutputStream ();
37+ protected LineNumberStringBuilderPrinter printer = new LineNumberStringBuilderPrinter ();
38+ protected String jdCoreVersion = "SNAPSHOT" ;
39+
40+ public ClassFileSourceSaverProvider () {
41+ try {
42+ Enumeration <URL > enumeration = ClassFileSourceSaverProvider .class .getClassLoader ().getResources ("META-INF/MANIFEST.MF" );
43+
44+ while (enumeration .hasMoreElements ()) {
45+ try (InputStream is = enumeration .nextElement ().openStream ()) {
46+ String attribute = new Manifest (is ).getMainAttributes ().getValue ("JD-Core-Version" );
47+ if (attribute != null ) {
48+ jdCoreVersion = attribute ;
49+ }
50+ }
51+ }
52+ } catch (IOException e ) {
53+ assert ExceptionUtil .printStackTrace (e );
54+ }
55+ }
4156
4257 @ Override public String [] getSelectors () { return appendSelectors ("*:file:*.class" ); }
4358
@@ -74,60 +89,67 @@ public void saveContent(API api, Controller controller, Listener listener, Path
7489 listener .pathSaved (path );
7590 }
7691 // Init preferences
77- Map <String , String > p = api .getPreferences ();
78- preferences .setUnicodeEscape (getPreferenceValue (p , ESCAPE_UNICODE_CHARACTERS , false ));
79- preferences .setShowPrefixThis (! getPreferenceValue (p , OMIT_THIS_PREFIX , false ));
80- preferences .setShowDefaultConstructor (getPreferenceValue (p , WRITE_DEFAULT_CONSTRUCTOR , false ));
81- preferences .setRealignmentLineNumber (getPreferenceValue (p , REALIGN_LINE_NUMBERS , true ));
82- preferences .setShowLineNumbers (getPreferenceValue (p , WRITE_LINE_NUMBERS , true ));
92+ Map <String , String > preferences = api .getPreferences ();
93+ boolean realignmentLineNumbers = getPreferenceValue (preferences , REALIGN_LINE_NUMBERS , true );
94+ boolean unicodeEscape = getPreferenceValue (preferences , ESCAPE_UNICODE_CHARACTERS , false );
95+ boolean showLineNumbers = getPreferenceValue (preferences , WRITE_LINE_NUMBERS , true );
96+
97+ Map <String , Object > configuration = new HashMap <>();
98+ configuration .put ("realignLineNumbers" , realignmentLineNumbers );
8399
84100 // Init loader
85101 loader .setEntry (entry );
86102
87103 // Init printer
88- baos .reset ();
89- PrintStream ps = new PrintStream (baos , true , "UTF-8" );
90- printer .setPrintStream (ps );
91- printer .setPreferences (preferences );
104+ printer .setRealignmentLineNumber (realignmentLineNumbers );
105+ printer .setUnicodeEscape (unicodeEscape );
106+ printer .setShowLineNumbers (showLineNumbers );
107+
108+ // Format internal name
109+ String entryPath = entry .getPath ();
110+ assert entryPath .endsWith (".class" );
111+ String entryInternalName = entryPath .substring (0 , entryPath .length () - 6 ); // 6 = ".class".length()
92112
93113 // Decompile class file
94- DECOMPILER .decompile (preferences , loader , printer , entry .getPath ());
114+ DECOMPILER .decompile (configuration , loader , printer , entryInternalName );
115+
116+ StringBuilder stringBuffer = printer .getStringBuffer ();
95117
96118 // Metadata
97- if (getPreferenceValue (p , WRITE_METADATA , true )) {
119+ if (getPreferenceValue (preferences , WRITE_METADATA , true )) {
98120 // Add location
99121 String location =
100122 new File (entry .getUri ()).getPath ()
101123 // Escape "\ u" sequence to prevent "Invalid unicode" errors
102124 .replaceAll ("(^|[^\\ \\ ])\\ \\ u" , "\\ \\ \\ \\ u" );
103- ps . print ("\n \n /* Location: " );
104- ps . print (location );
125+ stringBuffer . append ("\n \n /* Location: " );
126+ stringBuffer . append (location );
105127 // Add Java compiler version
106128 int majorVersion = printer .getMajorVersion ();
107129
108130 if (majorVersion >= 45 ) {
109- ps . print ("\n * Java compiler version: " );
131+ stringBuffer . append ("\n * Java compiler version: " );
110132
111133 if (majorVersion >= 49 ) {
112- ps . print (majorVersion - (49 - 5 ));
134+ stringBuffer . append (majorVersion - (49 - 5 ));
113135 } else {
114- ps . print (majorVersion - (45 - 1 ));
136+ stringBuffer . append (majorVersion - (45 - 1 ));
115137 }
116138
117- ps . print (" (" );
118- ps . print (majorVersion );
119- ps . print ('.' );
120- ps . print (printer .getMinorVersion ());
121- ps . print (')' );
139+ stringBuffer . append (" (" );
140+ stringBuffer . append (majorVersion );
141+ stringBuffer . append ('.' );
142+ stringBuffer . append (printer .getMinorVersion ());
143+ stringBuffer . append (')' );
122144 }
123145 // Add JD-Core version
124- ps . print ("\n * JD-Core Version: " );
125- ps . print ( CoreConstants . JD_CORE_VERSION );
126- ps . print ("\n */" );
146+ stringBuffer . append ("\n * JD-Core Version: " );
147+ stringBuffer . append ( jdCoreVersion );
148+ stringBuffer . append ("\n */" );
127149 }
128150
129- try (OutputStream os = new NewlineOutputStream (Files .newOutputStream (path ))) {
130- baos . writeTo ( os );
151+ try (PrintStream ps = new PrintStream ( new NewlineOutputStream (Files .newOutputStream (path )), true , "UTF-8" )) {
152+ ps . print ( stringBuffer . toString () );
131153 } catch (IOException e ) {
132154 assert ExceptionUtil .printStackTrace (e );
133155 }
@@ -144,11 +166,6 @@ public void saveContent(API api, Controller controller, Listener listener, Path
144166
145167 protected static boolean getPreferenceValue (Map <String , String > preferences , String key , boolean defaultValue ) {
146168 String v = preferences .get (key );
147-
148- if (v == null ) {
149- return defaultValue ;
150- } else {
151- return Boolean .valueOf (v );
152- }
169+ return (v == null ) ? defaultValue : Boolean .valueOf (v );
153170 }
154171}
0 commit comments