├── .gitignore ├── settings.gradle ├── lib ├── classes.jar ├── jd-core-0.7.1.jar └── jd-common-0.7.1.jar ├── src ├── test │ └── java │ │ └── com │ │ └── bryansharp │ │ └── jar2java │ │ └── Test.java └── main │ └── java │ └── com │ └── bryansharp │ └── jar2java │ ├── analyze │ ├── AnalyzeFieldVisitor.java │ ├── AnalyzeClassVisitor.java │ ├── AnalyzeAnnotationVisitor.java │ ├── AnalyzeMethodVisitor.java │ └── JarAnalyzer.java │ ├── convert │ ├── Jar2JavaPreferences.java │ ├── Decompiler.java │ ├── decompiler │ │ ├── GuiPreferences.java │ │ ├── ClassFileSourcePrinter.java │ │ └── PlainTextPrinter.java │ ├── Jar2JavaDecompiler.java │ ├── JavapJarParser.java │ └── JavaSourceTextPrinter.java │ ├── TextFileWritter.java │ ├── AidlProcessor.java │ ├── Main.java │ └── Utils.java ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .gradle 3 | build 4 | *.iml 5 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Jar2Java' 2 | 3 | -------------------------------------------------------------------------------- /lib/classes.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hydraxman/Jar2Java/HEAD/lib/classes.jar -------------------------------------------------------------------------------- /lib/jd-core-0.7.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hydraxman/Jar2Java/HEAD/lib/jd-core-0.7.1.jar -------------------------------------------------------------------------------- /lib/jd-common-0.7.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hydraxman/Jar2Java/HEAD/lib/jd-common-0.7.1.jar -------------------------------------------------------------------------------- /src/test/java/com/bryansharp/jar2java/Test.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java; 2 | 3 | /** 4 | * Created by bushaopeng on 17/7/10. 5 | */ 6 | public class Test { 7 | @org.junit.Test 8 | public void testMain() throws Exception { 9 | Main.main(new String[]{""}); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jar2Java 2 | 3 | ## Introduction 4 | 5 | The output of this project, which is a runnable jar file, is able to transfer jar to organized java files. Futher plan is to analyze them. 6 | 7 | ## Build 8 | 9 | gradle clean build fatJar 10 | 11 | ## How to Use 12 | After build, find the output jar in build/libs/, and run it like below: 13 | 14 | java -jar Jar2Java-1.0-SNAPSHOT.jar /Users/bryansharp/jd-common-ide-0.7.1.jar -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/analyze/AnalyzeFieldVisitor.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java.analyze; 2 | 3 | import com.bryansharp.jar2java.Utils; 4 | 5 | import org.objectweb.asm.AnnotationVisitor; 6 | import org.objectweb.asm.Attribute; 7 | import org.objectweb.asm.FieldVisitor; 8 | 9 | /** 10 | * Created by bushaopeng on 17/9/14. 11 | */ 12 | public class AnalyzeFieldVisitor implements FieldVisitor { 13 | 14 | @Override 15 | public AnnotationVisitor visitAnnotation(String s, boolean b) { 16 | Utils.logEach("visitAttribute", s, b); 17 | return null; 18 | } 19 | 20 | @Override 21 | public void visitAttribute(Attribute attribute) { 22 | Utils.logEach("visitAttribute", attribute.type); 23 | } 24 | 25 | @Override 26 | public void visitEnd() { 27 | Utils.logEach("visitEnd"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/convert/Jar2JavaPreferences.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java.convert; 2 | 3 | import jd.common.preferences.CommonPreferences; 4 | 5 | public class Jar2JavaPreferences extends CommonPreferences { 6 | protected boolean showMetadata; 7 | 8 | public Jar2JavaPreferences() { 9 | this.showMetadata = true; 10 | } 11 | 12 | public Jar2JavaPreferences(boolean showDefaultConstructor, boolean realignmentLineNumber, boolean showPrefixThis, boolean mergeEmptyLines, boolean unicodeEscape, boolean showLineNumbers, boolean showMetadata) { 13 | super(showDefaultConstructor, realignmentLineNumber, showPrefixThis, mergeEmptyLines, unicodeEscape, showLineNumbers); 14 | this.showMetadata = showMetadata; 15 | } 16 | 17 | public boolean isShowMetadata() { 18 | return this.showMetadata; 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/convert/Decompiler.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java.convert; 2 | 3 | 4 | /** 5 | * Created by bushaopeng on 17/1/6. 6 | */ 7 | public class Decompiler { 8 | /** 9 | * Actual call to the native lib. 10 | * 11 | * @param basePath Path to the root of the classpath, either a path to a directory or a path to a jar file. 12 | * @param internalTypeName internal name of the type. 13 | * @return Decompiled class text. 14 | */ 15 | public static String decompile(String basePath, String internalTypeName) { 16 | // Load preferences 17 | boolean showDefaultConstructor = false; 18 | boolean realignmentLineNumber = true; 19 | boolean showPrefixThis = false; 20 | boolean mergeEmptyLines = true; 21 | boolean unicodeEscape = false; 22 | boolean showLineNumbers = false; 23 | boolean showMetadata = true; 24 | 25 | // Create preferences 26 | Jar2JavaPreferences preferences = new Jar2JavaPreferences( 27 | showDefaultConstructor, realignmentLineNumber, showPrefixThis, 28 | mergeEmptyLines, unicodeEscape, showLineNumbers, showMetadata); 29 | 30 | // Decompile 31 | return Jar2JavaDecompiler.decompile(preferences, basePath, internalTypeName); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/convert/decompiler/GuiPreferences.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2015 Emmanuel Dupuy 3 | * This program is made available under the terms of the GPLv3 License. 4 | */ 5 | 6 | package com.bryansharp.jar2java.convert.decompiler; 7 | 8 | import jd.core.preferences.Preferences; 9 | 10 | public class GuiPreferences extends Preferences { 11 | protected boolean showPrefixThis; 12 | protected boolean unicodeEscape; 13 | protected boolean showLineNumbers; 14 | 15 | public GuiPreferences() { 16 | this.showPrefixThis = false; 17 | this.unicodeEscape = false; 18 | this.showLineNumbers = false; 19 | } 20 | 21 | public GuiPreferences( 22 | boolean showDefaultConstructor, boolean realignmentLineNumber, 23 | boolean showPrefixThis, boolean unicodeEscape, boolean showLineNumbers) { 24 | super(showDefaultConstructor, realignmentLineNumber); 25 | this.showPrefixThis = showPrefixThis; 26 | this.unicodeEscape = unicodeEscape; 27 | this.showLineNumbers = showLineNumbers; 28 | this.realignmentLineNumber = false; 29 | } 30 | 31 | public void setShowDefaultConstructor(boolean b) { 32 | showDefaultConstructor = b; 33 | } 34 | 35 | public void setRealignmentLineNumber(boolean b) { 36 | realignmentLineNumber = b; 37 | } 38 | 39 | public void setShowPrefixThis(boolean b) { 40 | showPrefixThis = b; 41 | } 42 | 43 | public void setUnicodeEscape(boolean b) { 44 | unicodeEscape = b; 45 | } 46 | 47 | public void setShowLineNumbers(boolean b) { 48 | showLineNumbers = b; 49 | } 50 | 51 | public boolean isShowPrefixThis() { 52 | return showPrefixThis; 53 | } 54 | 55 | public boolean isUnicodeEscape() { 56 | return unicodeEscape; 57 | } 58 | 59 | public boolean isShowLineNumbers() { 60 | return showLineNumbers; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/analyze/AnalyzeClassVisitor.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java.analyze; 2 | 3 | import com.bryansharp.jar2java.Utils; 4 | 5 | import org.objectweb.asm.AnnotationVisitor; 6 | import org.objectweb.asm.Attribute; 7 | import org.objectweb.asm.ClassVisitor; 8 | import org.objectweb.asm.FieldVisitor; 9 | import org.objectweb.asm.MethodVisitor; 10 | 11 | /** 12 | * Created by bushaopeng on 17/9/14. 13 | */ 14 | public class AnalyzeClassVisitor implements ClassVisitor { 15 | String className; 16 | 17 | public AnalyzeClassVisitor(String className) { 18 | this.className = className; 19 | } 20 | 21 | @Override 22 | public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { 23 | Utils.logEach("visit", Utils.accCode2String(access), name, signature, superName, interfaces); 24 | } 25 | 26 | @Override 27 | public void visitSource(String source, String debug) { 28 | Utils.logEach("visitSource", source, debug); 29 | } 30 | 31 | @Override 32 | public void visitOuterClass(String owner, String name, String desc) { 33 | Utils.logEach("visitOuterClass", owner, name, desc); 34 | } 35 | 36 | @Override 37 | public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 38 | return null; 39 | } 40 | 41 | @Override 42 | public void visitAttribute(Attribute attr) { 43 | 44 | } 45 | 46 | @Override 47 | public void visitInnerClass(String name, String outerName, String innerName, int access) { 48 | 49 | } 50 | 51 | @Override 52 | public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { 53 | Utils.logEach("visitField", Utils.accCode2String(access), name, desc, signature, value); 54 | return null; 55 | } 56 | 57 | @Override 58 | public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { 59 | Utils.logEach("visitMethod", Utils.accCode2String(access), name, desc, signature, exceptions); 60 | return null; 61 | } 62 | 63 | @Override 64 | public void visitEnd() { 65 | // Utils.logEach("visitEnd"); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/TextFileWritter.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.File; 5 | import java.io.FileOutputStream; 6 | import java.io.IOException; 7 | import java.io.OutputStreamWriter; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | /** 12 | * Created by bushaopeng on 18/1/19. 13 | */ 14 | public class TextFileWritter { 15 | 16 | static final Map writterMap = new HashMap<>(); 17 | 18 | static { 19 | } 20 | 21 | private final String name; 22 | private final File file; 23 | private BufferedWriter fileWritter; 24 | 25 | public TextFileWritter(String name) { 26 | this.name = name; 27 | this.file = new File(name + ".txt"); 28 | if (file.exists()) { 29 | file.delete(); 30 | } else { 31 | try { 32 | file.createNewFile(); 33 | } catch (IOException e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | Utils.log("文件路径:" + file.getAbsolutePath()); 38 | try { 39 | this.fileWritter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); 40 | } catch (Exception e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | 45 | public static TextFileWritter getWritter(String name) { 46 | if (writterMap.get(name) == null) { 47 | writterMap.put(name, new TextFileWritter(name)); 48 | } 49 | return writterMap.get(name); 50 | } 51 | 52 | public void println(Object msg) { 53 | try { 54 | if (msg == null) { 55 | this.fileWritter.write("null"); 56 | } else { 57 | this.fileWritter.write(msg.toString()); 58 | } 59 | this.fileWritter.newLine(); 60 | this.fileWritter.flush(); 61 | } catch (IOException e) { 62 | e.printStackTrace(); 63 | } 64 | } 65 | 66 | public void close() { 67 | try { 68 | this.fileWritter.close(); 69 | } catch (IOException e) { 70 | e.printStackTrace(); 71 | } 72 | } 73 | 74 | public static TextFileWritter getDefaultWritter() { 75 | return getWritter("default"); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/analyze/AnalyzeAnnotationVisitor.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java.analyze; 2 | 3 | import java.util.List; 4 | 5 | import javax.lang.model.element.AnnotationMirror; 6 | import javax.lang.model.element.AnnotationValue; 7 | import javax.lang.model.element.AnnotationValueVisitor; 8 | import javax.lang.model.element.VariableElement; 9 | import javax.lang.model.type.TypeMirror; 10 | 11 | /** 12 | * Created by bushaopeng on 18/1/17. 13 | */ 14 | public class AnalyzeAnnotationVisitor implements AnnotationValueVisitor { 15 | @Override 16 | public Object visit(AnnotationValue av, Object o) { 17 | return null; 18 | } 19 | 20 | @Override 21 | public Object visit(AnnotationValue av) { 22 | return null; 23 | } 24 | 25 | @Override 26 | public Object visitBoolean(boolean b, Object o) { 27 | return null; 28 | } 29 | 30 | @Override 31 | public Object visitByte(byte b, Object o) { 32 | return null; 33 | } 34 | 35 | @Override 36 | public Object visitChar(char c, Object o) { 37 | return null; 38 | } 39 | 40 | @Override 41 | public Object visitDouble(double d, Object o) { 42 | return null; 43 | } 44 | 45 | @Override 46 | public Object visitFloat(float f, Object o) { 47 | return null; 48 | } 49 | 50 | @Override 51 | public Object visitInt(int i, Object o) { 52 | return null; 53 | } 54 | 55 | @Override 56 | public Object visitLong(long i, Object o) { 57 | return null; 58 | } 59 | 60 | @Override 61 | public Object visitShort(short s, Object o) { 62 | return null; 63 | } 64 | 65 | @Override 66 | public Object visitString(String s, Object o) { 67 | return null; 68 | } 69 | 70 | @Override 71 | public Object visitType(TypeMirror t, Object o) { 72 | return null; 73 | } 74 | 75 | @Override 76 | public Object visitEnumConstant(VariableElement c, Object o) { 77 | return null; 78 | } 79 | 80 | @Override 81 | public Object visitAnnotation(AnnotationMirror a, Object o) { 82 | return null; 83 | } 84 | 85 | @Override 86 | public Object visitUnknown(AnnotationValue av, Object o) { 87 | return null; 88 | } 89 | 90 | @Override 91 | public Object visitArray(List vals, Object o) { 92 | return null; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/AidlProcessor.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java; 2 | 3 | import org.apache.commons.io.IOUtils; 4 | 5 | import java.io.BufferedReader; 6 | import java.io.File; 7 | import java.io.FileOutputStream; 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.io.InputStreamReader; 11 | import java.util.Enumeration; 12 | import java.util.zip.ZipEntry; 13 | import java.util.zip.ZipFile; 14 | 15 | /** 16 | * Created by bushaopeng on 18/1/8. 17 | */ 18 | public class AidlProcessor { 19 | public boolean process(String path) { 20 | try { 21 | File file = new File(path); 22 | final Runtime runtime = Runtime.getRuntime(); 23 | final File parentFile = new File("/Users/bushaopeng/Desktop/androidSDK/build-tools/20.0.0"); 24 | processSubFiles(file, new ProcessSubAIDLFileCallback() { 25 | @Override 26 | public void processFile(File subFile) { 27 | 28 | String command = "./aidl -I/Users/bushaopeng/Desktop/myGit/JDroid/src/main/java " 29 | + subFile.getAbsolutePath(); 30 | System.out.println("正在处理: " + subFile.getAbsolutePath() + ",命令" + command); 31 | try { 32 | Process process = runtime.exec(command, null, parentFile); 33 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 34 | String line = null; 35 | StringBuilder builder = new StringBuilder(); 36 | while ((line = bufferedReader.readLine()) != null) { 37 | builder.append(line).append('\n'); 38 | } 39 | System.out.println(builder.toString()); 40 | process.destroy(); 41 | } catch (IOException e) { 42 | e.printStackTrace(); 43 | } 44 | } 45 | }); 46 | 47 | } catch (Exception e) { 48 | e.printStackTrace(); 49 | } 50 | return true; 51 | } 52 | 53 | 54 | private void processSubFiles(File file, ProcessSubAIDLFileCallback callback) { 55 | if (file == null) { 56 | return; 57 | } 58 | if (file.isDirectory()) { 59 | File[] files = file.listFiles(); 60 | if (files != null && files.length > 0) { 61 | for (File f : files) { 62 | processSubFiles(f, callback); 63 | } 64 | } 65 | } else { 66 | if (callback.matchFile(file)) { 67 | callback.processFile(file); 68 | } 69 | } 70 | } 71 | 72 | public interface ProcessSubFileCallback { 73 | boolean matchFile(File subFile); 74 | 75 | void processFile(File subFile); 76 | } 77 | 78 | public abstract class ProcessSubAIDLFileCallback implements ProcessSubFileCallback { 79 | @Override 80 | public boolean matchFile(File subFile) { 81 | return subFile.getName().endsWith(".aidl"); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/analyze/AnalyzeMethodVisitor.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java.analyze; 2 | 3 | import com.bryansharp.jar2java.Utils; 4 | 5 | import org.objectweb.asm.AnnotationVisitor; 6 | import org.objectweb.asm.Attribute; 7 | import org.objectweb.asm.Label; 8 | import org.objectweb.asm.MethodVisitor; 9 | 10 | /** 11 | * Created by bushaopeng on 17/9/14. 12 | */ 13 | public class AnalyzeMethodVisitor implements MethodVisitor { 14 | @Override 15 | public AnnotationVisitor visitAnnotationDefault() { 16 | return null; 17 | } 18 | 19 | @Override 20 | public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 21 | return null; 22 | } 23 | 24 | @Override 25 | public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) { 26 | return null; 27 | } 28 | 29 | @Override 30 | public void visitAttribute(Attribute attribute) { 31 | // Utils.logEach("visitAttribute", attribute); 32 | } 33 | 34 | @Override 35 | public void visitCode() { 36 | // Utils.logEach("visitCode"); 37 | } 38 | 39 | @Override 40 | public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) { 41 | // Utils.logEach("visitLabel", type); 42 | } 43 | 44 | @Override 45 | public void visitInsn(int opcode) { 46 | Utils.logEach("visitInsn", Utils.getOpName(opcode)); 47 | } 48 | 49 | @Override 50 | public void visitIntInsn(int opcode, int operand) { 51 | Utils.logEach("visitIntInsn", Utils.getOpName(opcode), operand); 52 | } 53 | 54 | @Override 55 | public void visitVarInsn(int opcode, int var) { 56 | Utils.logEach("visitVarInsn", Utils.getOpName(opcode), var); 57 | } 58 | 59 | @Override 60 | public void visitTypeInsn(int opcode, String type) { 61 | Utils.logEach("visitTypeInsn", Utils.getOpName(opcode), type); 62 | } 63 | 64 | @Override 65 | public void visitFieldInsn(int opcode, String owner, String name, String desc) { 66 | Utils.logEach("visitFieldInsn", Utils.getOpName(opcode), owner, name, desc); 67 | } 68 | 69 | @Override 70 | public void visitMethodInsn(int opcode, String owner, String name, String desc) { 71 | Utils.logEach("visitMethodInsn", Utils.getOpName(opcode), owner, name, desc); 72 | } 73 | 74 | @Override 75 | public void visitJumpInsn(int opcode, Label label) { 76 | Utils.logEach("visitJumpInsn", Utils.getOpName(opcode), label); 77 | } 78 | 79 | @Override 80 | public void visitLabel(Label label) { 81 | Utils.logEach("visitLabel", label); 82 | } 83 | 84 | @Override 85 | public void visitLdcInsn(Object cst) { 86 | Utils.logEach("visitLdcInsn", cst); 87 | } 88 | 89 | @Override 90 | public void visitIincInsn(int var, int increment) { 91 | 92 | } 93 | 94 | @Override 95 | public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) { 96 | Utils.logEach("visitTryCatchBlock", min, max, dflt, labels); 97 | } 98 | 99 | @Override 100 | public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) { 101 | 102 | } 103 | 104 | @Override 105 | public void visitMultiANewArrayInsn(String desc, int dims) { 106 | 107 | } 108 | 109 | @Override 110 | public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { 111 | Utils.logEach("visitTryCatchBlock", start, end, handler, type); 112 | } 113 | 114 | @Override 115 | public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { 116 | Utils.logEach("visitLocalVariable", name, desc, signature, start, end, index); 117 | } 118 | 119 | @Override 120 | public void visitLineNumber(int line, Label start) { 121 | Utils.logEach("visitTryCatchBlock", line, start); 122 | } 123 | 124 | @Override 125 | public void visitMaxs(int maxStack, int maxLocals) { 126 | Utils.logEach("visitMaxs", maxStack, maxLocals); 127 | } 128 | 129 | @Override 130 | public void visitEnd() { 131 | Utils.logEach("visitEnd"); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/Main.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java; 2 | 3 | import com.bryansharp.jar2java.analyze.JarAnalyzer; 4 | import com.bryansharp.jar2java.convert.Decompiler; 5 | 6 | import java.io.File; 7 | import java.io.FileOutputStream; 8 | import java.io.IOException; 9 | import java.util.ArrayList; 10 | import java.util.Enumeration; 11 | import java.util.Map; 12 | import java.util.zip.ZipEntry; 13 | import java.util.zip.ZipFile; 14 | 15 | /** 16 | * Created by bushaopeng on 17/1/6. 17 | */ 18 | public class Main { 19 | final static String pathname = "build/javaDirOutput"; 20 | private static File baseDir; 21 | 22 | public static void main(String[] args) { 23 | // AidlProcessor processor = new AidlProcessor(); 24 | // if (processor.process("/Users/bushaopeng/Desktop/myGit/JDroid/src/main/java")) { 25 | // return; 26 | // } 27 | // JavapJarParser jarParser = new JavapJarParser(); 28 | // if (jarParser.parse("/Users/bushaopeng/IdeaProjects/Jar2Java/classes.jar")) { 29 | // return; 30 | // } 31 | // JarAnalyzer jarAnalyzer = new JarAnalyzer(); 32 | // if (jarAnalyzer.getReproguardMapping(args[0]) != null) { 33 | // return; 34 | // } 35 | if (args == null || args.length < 1) { 36 | log("please specify a jar file"); 37 | return; 38 | } 39 | try { 40 | ArrayList paths = new ArrayList<>(); 41 | for (String jarFile : args) { 42 | if (checkJar(jarFile)) { 43 | paths.add(jarFile); 44 | } 45 | } 46 | if (paths.size() > 0) { 47 | initBuildPath(); 48 | for (String jarFile : paths) { 49 | decompileJar(jarFile); 50 | } 51 | } 52 | } catch (Exception e) { 53 | e.printStackTrace(); 54 | } 55 | } 56 | 57 | private static void initBuildPath() { 58 | String dirName = getOutputFileDirName(); 59 | baseDir = new File(dirName); 60 | baseDir.mkdirs(); 61 | } 62 | 63 | private static boolean checkJar(String jarPath) { 64 | return !(jarPath == null || !jarPath.endsWith(".jar") || !new File(jarPath).exists()); 65 | } 66 | 67 | private static void decompileJar(String jarFullPath) throws IOException { 68 | boolean needRename = true; 69 | if (needRename) { 70 | JarAnalyzer jarAnalyzer = new JarAnalyzer(); 71 | 72 | Map renameMap = jarAnalyzer.getRenameMap(jarFullPath); 73 | for (Map.Entry entry : renameMap.entrySet()) { 74 | Utils.log(entry.getKey() + "->" + entry.getValue()); 75 | } 76 | File file = jarAnalyzer.renameClassInJar(jarFullPath, renameMap); 77 | jarFullPath = file.getAbsolutePath(); 78 | } 79 | 80 | ZipFile zipFile = new ZipFile(jarFullPath); 81 | Enumeration entries = zipFile.entries(); 82 | while (entries.hasMoreElements()) { 83 | ZipEntry zipEntry = entries.nextElement(); 84 | String name = zipEntry.getName(); 85 | if (name.endsWith(".class")) { 86 | //内部类直接跳过 87 | if (name.contains("$")) { 88 | log("jump inner class " + name); 89 | continue; 90 | } 91 | String result = null; 92 | log("start to decompile class " + name); 93 | try { 94 | result = Decompiler.decompile(jarFullPath, name); 95 | stringToClassFile(name, result); 96 | } catch (Exception e) { 97 | logError("decompile " + name + " failed"); 98 | e.printStackTrace(); 99 | } 100 | } 101 | } 102 | } 103 | 104 | private static void stringToClassFile(String name, String result) throws IOException { 105 | if (name == null) return; 106 | if (result == null) return; 107 | int endIndex = name.lastIndexOf("/"); 108 | String dirName = baseDir.getAbsolutePath() + "/" + name.substring(0, endIndex); 109 | File dir = new File(dirName); 110 | dir.mkdirs(); 111 | 112 | String javaFilename = name.substring(endIndex + 1).replace(".class", ".java"); 113 | File javaFile = new File(dir, javaFilename); 114 | FileOutputStream fileOutputStream = new FileOutputStream(javaFile); 115 | fileOutputStream.write(result.getBytes()); 116 | fileOutputStream.flush(); 117 | javaFile.setWritable(true, false); 118 | } 119 | 120 | private static String getOutputFileDirName() { 121 | if (new File(pathname).exists()) { 122 | return getNewDirName(1); 123 | } 124 | return pathname; 125 | } 126 | 127 | private static String getNewDirName(int count) { 128 | if (new File(pathname + count).exists()) { 129 | return getNewDirName(++count); 130 | } 131 | return pathname + count; 132 | } 133 | 134 | public static void log(Object msg) { 135 | if (msg == null) { 136 | return; 137 | } 138 | System.out.println(msg.toString()); 139 | } 140 | 141 | public static void logError(Object msg) { 142 | if (msg == null) { 143 | return; 144 | } 145 | System.err.println(msg.toString()); 146 | } 147 | 148 | public static void logDiv() { 149 | System.out.println("=============================="); 150 | } 151 | 152 | public static void logEach(Object... msgs) { 153 | logDiv(); 154 | for (Object msg : msgs) { 155 | System.out.print(msg.toString()); 156 | System.out.print("\t"); 157 | } 158 | System.out.print("\n"); 159 | logDiv(); 160 | } 161 | 162 | } 163 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/convert/Jar2JavaDecompiler.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | package com.bryansharp.jar2java.convert; 6 | 7 | import com.bryansharp.jar2java.Utils; 8 | import com.bryansharp.jar2java.convert.decompiler.GuiPreferences; 9 | import com.bryansharp.jar2java.convert.decompiler.PlainTextPrinter; 10 | 11 | import java.io.ByteArrayOutputStream; 12 | import java.io.PrintStream; 13 | import java.lang.reflect.InvocationHandler; 14 | import java.lang.reflect.Method; 15 | import java.lang.reflect.Proxy; 16 | import java.util.ArrayList; 17 | 18 | import jd.common.loader.BaseLoader; 19 | import jd.common.loader.LoaderManager; 20 | import jd.common.util.CommonTypeNameUtil; 21 | import jd.common.util.VersionUtil; 22 | import jd.core.loader.LoaderException; 23 | import jd.core.model.classfile.ClassFile; 24 | import jd.core.model.layout.block.LayoutBlock; 25 | import jd.core.model.reference.ReferenceMap; 26 | import jd.core.printer.Printer; 27 | import jd.core.process.analyzer.classfile.ClassFileAnalyzer; 28 | import jd.core.process.analyzer.classfile.ReferenceAnalyzer; 29 | import jd.core.process.deserializer.ClassFileDeserializer; 30 | import jd.core.process.layouter.ClassFileLayouter; 31 | import jd.core.process.writer.ClassFileWriter; 32 | 33 | public class Jar2JavaDecompiler { 34 | private static LoaderManager loaderManager = new LoaderManager(); 35 | 36 | public static String decompile(Jar2JavaPreferences preferences, String basePath, String classPath) { 37 | try { 38 | BaseLoader loader = loaderManager.getLoader(basePath); 39 | ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 10); 40 | PrintStream ps = new PrintStream(baos); 41 | 42 | ClassFile classFile = ClassFileDeserializer.Deserialize(loader, classPath); 43 | if (classFile == null) { 44 | throw new LoaderException("Can not deserialize \'" + classPath + "\'."); 45 | } else { 46 | ReferenceMap referenceMap = new ReferenceMap(); 47 | ClassFileAnalyzer.Analyze(referenceMap, classFile); 48 | ReferenceAnalyzer.Analyze(referenceMap, classFile); 49 | 50 | String className = classFile.getThisClassName().replace('/', '.'); 51 | 52 | Utils.log("decompile classname is " + className); 53 | 54 | ArrayList layoutBlockList = new ArrayList<>(1024); 55 | int maxLineNumber = ClassFileLayouter.Layout(preferences, referenceMap, classFile, layoutBlockList); 56 | 57 | Printer printerProxy = getPrinter(preferences, ps, className); 58 | 59 | ClassFileWriter.Write(loader, printerProxy, referenceMap, maxLineNumber, classFile.getMajorVersion(), classFile.getMinorVersion(), layoutBlockList); 60 | if (preferences.isShowMetadata()) { 61 | printerProxy.endOfLine(); 62 | printerProxy.print("/* Location: "); 63 | printerProxy.print(loader.getCodebase()); 64 | printerProxy.endOfLine(); 65 | printerProxy.print(" * Qualified Name: "); 66 | String qualifiedName = CommonTypeNameUtil.InternalPathToQualifiedTypeName(classPath); 67 | printerProxy.print(qualifiedName); 68 | String jdkVersion = VersionUtil.getJDKVersion(classFile.getMajorVersion(), classFile.getMinorVersion()); 69 | if (jdkVersion.length() > 0) { 70 | printerProxy.endOfLine(); 71 | printerProxy.print(" * Java Class Version: "); 72 | printerProxy.print(jdkVersion); 73 | } 74 | 75 | printerProxy.endOfLine(); 76 | printerProxy.print(" * By Jar2Java"); 77 | printerProxy.endOfLine(); 78 | printerProxy.print(" * Using JD-Core Version: "); 79 | printerProxy.print("0.7.1"); 80 | printerProxy.endOfLine(); 81 | printerProxy.print(" */"); 82 | } 83 | 84 | ps.close(); 85 | return new String(baos.toByteArray()); 86 | } 87 | } catch (Throwable var13) { 88 | return null; 89 | } 90 | } 91 | 92 | private static Printer getPrinter(Jar2JavaPreferences preferences, PrintStream ps, String className) { 93 | final Printer printer = getInnerPrinter(preferences, ps); 94 | if (!"com.mobvista.msdk.a.a".equals(className)) { 95 | return printer; 96 | } 97 | InvocationHandler handler = new InvocationHandler() { 98 | @Override 99 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 100 | StringBuilder builder = new StringBuilder(); 101 | if (args != null && args.length > 0) { 102 | for (Object arg : args) { 103 | if (arg != null) { 104 | builder.append(arg.toString()); 105 | } else { 106 | builder.append("null"); 107 | } 108 | builder.append("\t"); 109 | } 110 | builder.setLength(builder.length() - 1); 111 | } 112 | Utils.log("调用:" + method.getName() + ": " + builder.toString()); 113 | return method.invoke(printer, args); 114 | } 115 | }; 116 | return (Printer) Proxy.newProxyInstance(Jar2JavaDecompiler.class.getClassLoader(), printer.getClass().getInterfaces(), handler); 117 | } 118 | 119 | private static Printer getInnerPrinter(Jar2JavaPreferences preferences, PrintStream ps) { 120 | // PlainTextPrinter plainTextPrinter = new PlainTextPrinter(); 121 | // plainTextPrinter.setPrintStream(ps); 122 | // GuiPreferences guiPreferences = new GuiPreferences(); 123 | // guiPreferences.setShowLineNumbers(preferences.isShowLineNumbers()); 124 | // plainTextPrinter.setPreferences(guiPreferences); 125 | // return plainTextPrinter; 126 | return new JavaSourceTextPrinter(preferences, ps); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/convert/JavapJarParser.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java.convert; 2 | 3 | import org.apache.commons.io.IOUtils; 4 | 5 | import java.io.BufferedOutputStream; 6 | import java.io.BufferedReader; 7 | import java.io.File; 8 | import java.io.FileOutputStream; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import java.io.InputStreamReader; 12 | import java.io.OutputStream; 13 | import java.util.Enumeration; 14 | import java.util.zip.ZipEntry; 15 | import java.util.zip.ZipFile; 16 | 17 | /** 18 | * Created by bushaopeng on 18/1/8. 19 | */ 20 | public class JavapJarParser { 21 | public boolean parse(String path) { 22 | try { 23 | File file = new File(path); 24 | String name = file.getName().split("\\.")[0]; 25 | String absolutePath = file.getParentFile().getAbsolutePath(); 26 | final String destDirPath = absolutePath + "/parseOutput/" + name; 27 | File destDir = new File(destDirPath); 28 | if (destDir.exists()) { 29 | boolean delete = destDir.delete(); 30 | System.out.println("删除文件:" + delete); 31 | } 32 | decompress(path, destDirPath); 33 | final Runtime runtime = Runtime.getRuntime(); 34 | processSubFiles(destDir, new ProcessSubClassFileCallback() { 35 | @Override 36 | public void processFile(File subFile) { 37 | String className = subFile.getName().split("\\.")[0]; 38 | File parentFile = subFile.getParentFile(); 39 | String command = "javap -v -p -l " + className; 40 | System.out.println("正在处理: " + subFile.getAbsolutePath() + ",命令" + command); 41 | try { 42 | Process process = runtime.exec(command, null, parentFile); 43 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 44 | String line = null; 45 | StringBuilder builder = new StringBuilder(); 46 | int lineNum = 1; 47 | while ((line = bufferedReader.readLine()) != null) { 48 | builder.append(line).append('\n'); 49 | filterProcessLine(subFile, line, lineNum); 50 | lineNum++; 51 | } 52 | File javapParser = new File(parentFile, className + ".javap.txt"); 53 | OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(javapParser)); 54 | outputStream.write(builder.toString().getBytes()); 55 | outputStream.close(); 56 | 57 | System.out.println("生成文件:" + javapParser.getAbsolutePath()); 58 | 59 | process.destroy(); 60 | } catch (IOException e) { 61 | e.printStackTrace(); 62 | } 63 | boolean delete = subFile.delete(); 64 | System.out.println("删除:" + delete); 65 | } 66 | }); 67 | 68 | } catch (Exception e) { 69 | e.printStackTrace(); 70 | } 71 | return true; 72 | } 73 | 74 | private void filterProcessLine(File subFile, String line, int lineNum) { 75 | if (line == null) { 76 | return; 77 | } 78 | line = line.trim(); 79 | if (line.startsWith("#")) { 80 | return; 81 | } 82 | if (line.toLowerCase().contains("android/view")) { 83 | return; 84 | } 85 | if (line.toLowerCase().contains("android/widget")) { 86 | return; 87 | } 88 | if (line.toLowerCase().contains("android/util")) { 89 | return; 90 | } 91 | if (line.toLowerCase().contains("android/graphics")) { 92 | return; 93 | } 94 | if (line.contains("android/text/TextUtils")) { 95 | return; 96 | } 97 | if (line.contains("Method android")&&line.contains("invoke")) { 98 | System.out.println("包含安卓系统调用:" + subFile.getName() + "文件第" + lineNum + "行: " + line); 99 | } 100 | } 101 | 102 | private void processSubFiles(File file, ProcessSubClassFileCallback callback) { 103 | if (file == null) { 104 | return; 105 | } 106 | if (file.isDirectory()) { 107 | File[] files = file.listFiles(); 108 | if (files != null && files.length > 0) { 109 | for (File f : files) { 110 | processSubFiles(f, callback); 111 | } 112 | } 113 | } else { 114 | if (callback.matchFile(file)) { 115 | callback.processFile(file); 116 | } 117 | } 118 | } 119 | 120 | public static void decompress(String srcPath, String dest) throws Exception { 121 | File file = new File(srcPath); 122 | if (!file.exists()) { 123 | throw new RuntimeException(srcPath + "所指文件不存在"); 124 | } 125 | ZipFile zf = new ZipFile(file); 126 | Enumeration entries = zf.entries(); 127 | ZipEntry entry = null; 128 | while (entries.hasMoreElements()) { 129 | entry = (ZipEntry) entries.nextElement(); 130 | System.out.println("解压" + entry.getName()); 131 | if (entry.isDirectory()) { 132 | String dirPath = dest + File.separator + entry.getName(); 133 | File dir = new File(dirPath); 134 | dir.mkdirs(); 135 | } else { 136 | // 表示文件 137 | File f = new File(dest + File.separator + entry.getName()); 138 | if (!f.exists()) { 139 | String dirs = f.getParentFile().getAbsolutePath(); 140 | File parentDir = new File(dirs); 141 | parentDir.mkdirs(); 142 | } 143 | f.createNewFile(); 144 | // 将压缩文件内容写入到这个文件中 145 | InputStream is = null; 146 | FileOutputStream fos = null; 147 | try { 148 | is = zf.getInputStream(entry); 149 | fos = new FileOutputStream(f); 150 | int count; 151 | byte[] buf = new byte[8192]; 152 | while ((count = is.read(buf)) != -1) { 153 | fos.write(buf, 0, count); 154 | } 155 | } catch (IOException e) { 156 | e.printStackTrace(); 157 | } finally { 158 | IOUtils.closeQuietly(is); 159 | IOUtils.closeQuietly(fos); 160 | } 161 | 162 | } 163 | } 164 | 165 | } 166 | 167 | public interface ProcessSubFileCallback { 168 | boolean matchFile(File subFile); 169 | 170 | void processFile(File subFile); 171 | } 172 | 173 | public abstract class ProcessSubClassFileCallback implements ProcessSubFileCallback { 174 | @Override 175 | public boolean matchFile(File subFile) { 176 | return subFile.getName().endsWith(".class"); 177 | } 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/convert/decompiler/ClassFileSourcePrinter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2015 Emmanuel Dupuy 3 | * This program is made available under the terms of the GPLv3 License. 4 | */ 5 | 6 | package com.bryansharp.jar2java.convert.decompiler; 7 | 8 | import jd.core.printer.Printer; 9 | 10 | public abstract class ClassFileSourcePrinter implements Printer { 11 | protected static final String TAB = " "; 12 | protected static final String NEWLINE = "\n"; 13 | 14 | protected int maxLineNumber = 0; 15 | protected int indentationCount; 16 | protected boolean display; 17 | 18 | protected abstract boolean getRealignmentLineNumber(); 19 | 20 | protected abstract boolean isShowPrefixThis(); 21 | 22 | protected abstract boolean isUnicodeEscape(); 23 | 24 | protected abstract void append(char c); 25 | 26 | protected abstract void append(String s); 27 | 28 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 29 | 30 | public void print(byte b) { 31 | append(String.valueOf(b)); 32 | } 33 | 34 | public void print(int i) { 35 | append(String.valueOf(i)); 36 | } 37 | 38 | public void print(char c) { 39 | if (this.display) 40 | append(c); 41 | } 42 | 43 | public void print(String s) { 44 | if (this.display) 45 | printEscape(s); 46 | } 47 | 48 | public void printNumeric(String s) { 49 | append(s); 50 | } 51 | 52 | public void printString(String s, String scopeInternalName) { 53 | append(s); 54 | } 55 | 56 | public void printKeyword(String s) { 57 | if (this.display) 58 | append(s); 59 | } 60 | 61 | public void printJavaWord(String s) { 62 | append(s); 63 | } 64 | 65 | public void printType(String internalName, String name, String scopeInternalName) { 66 | if (this.display) 67 | printEscape(name); 68 | } 69 | 70 | public void printTypeDeclaration(String internalName, String name) { 71 | printEscape(name); 72 | } 73 | 74 | public void printTypeImport(String internalName, String name) { 75 | printEscape(name); 76 | } 77 | 78 | public void printField(String internalName, String name, String descriptor, String scopeInternalName) { 79 | printEscape(name); 80 | } 81 | 82 | public void printFieldDeclaration(String internalName, String name, String descriptor) { 83 | printEscape(name); 84 | } 85 | 86 | public void printStaticField(String internalName, String name, String descriptor, String scopeInternalName) { 87 | printEscape(name); 88 | } 89 | 90 | public void printStaticFieldDeclaration(String internalName, String name, String descriptor) { 91 | printEscape(name); 92 | } 93 | 94 | public void printConstructor(String internalName, String name, String descriptor, String scopeInternalName) { 95 | printEscape(name); 96 | } 97 | 98 | public void printConstructorDeclaration(String internalName, String name, String descriptor) { 99 | printEscape(name); 100 | } 101 | 102 | public void printStaticConstructorDeclaration(String internalName, String name) { 103 | append(name); 104 | } 105 | 106 | public void printMethod(String internalName, String name, String descriptor, String scopeInternalName) { 107 | printEscape(name); 108 | } 109 | 110 | public void printMethodDeclaration(String internalName, String name, String descriptor) { 111 | printEscape(name); 112 | } 113 | 114 | public void printStaticMethod(String internalName, String name, String descriptor, String scopeInternalName) { 115 | printEscape(name); 116 | } 117 | 118 | public void printStaticMethodDeclaration(String internalName, String name, String descriptor) { 119 | printEscape(name); 120 | } 121 | 122 | public void start(int maxLineNumber, int majorVersion, int minorVersion) { 123 | this.indentationCount = 0; 124 | this.display = true; 125 | this.maxLineNumber = maxLineNumber; 126 | } 127 | 128 | public void end() { 129 | } 130 | 131 | public void indent() { 132 | this.indentationCount++; 133 | } 134 | 135 | public void desindent() { 136 | if (this.indentationCount > 0) 137 | this.indentationCount--; 138 | } 139 | 140 | public void startOfLine(int lineNumber) { 141 | for (int i = 0; i < indentationCount; i++) 142 | append(TAB); 143 | } 144 | 145 | public void endOfLine() { 146 | append(NEWLINE); 147 | } 148 | 149 | public void extraLine(int count) { 150 | if (getRealignmentLineNumber()) { 151 | while (count-- > 0) { 152 | append(NEWLINE); 153 | } 154 | } 155 | } 156 | 157 | public void startOfComment() { 158 | } 159 | 160 | public void endOfComment() { 161 | } 162 | 163 | public void startOfJavadoc() { 164 | } 165 | 166 | public void endOfJavadoc() { 167 | } 168 | 169 | public void startOfXdoclet() { 170 | } 171 | 172 | public void endOfXdoclet() { 173 | } 174 | 175 | public void startOfError() { 176 | } 177 | 178 | public void endOfError() { 179 | } 180 | 181 | public void startOfImportStatements() { 182 | } 183 | 184 | public void endOfImportStatements() { 185 | } 186 | 187 | public void startOfTypeDeclaration(String internalPath) { 188 | } 189 | 190 | public void endOfTypeDeclaration() { 191 | } 192 | 193 | public void startOfAnnotationName() { 194 | } 195 | 196 | public void endOfAnnotationName() { 197 | } 198 | 199 | public void startOfOptionalPrefix() { 200 | if (!isShowPrefixThis()) 201 | this.display = false; 202 | } 203 | 204 | public void endOfOptionalPrefix() { 205 | this.display = true; 206 | } 207 | 208 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 209 | 210 | public void debugStartOfLayoutBlock() { 211 | } 212 | 213 | public void debugEndOfLayoutBlock() { 214 | } 215 | 216 | public void debugStartOfSeparatorLayoutBlock() { 217 | } 218 | 219 | public void debugEndOfSeparatorLayoutBlock(int min, int value, int max) { 220 | } 221 | 222 | public void debugStartOfStatementsBlockLayoutBlock() { 223 | } 224 | 225 | public void debugEndOfStatementsBlockLayoutBlock(int min, int value, int max) { 226 | } 227 | 228 | public void debugStartOfInstructionBlockLayoutBlock() { 229 | } 230 | 231 | public void debugEndOfInstructionBlockLayoutBlock() { 232 | } 233 | 234 | public void debugStartOfCommentDeprecatedLayoutBlock() { 235 | } 236 | 237 | public void debugEndOfCommentDeprecatedLayoutBlock() { 238 | } 239 | 240 | public void debugMarker(String marker) { 241 | } 242 | 243 | public void debugStartOfCaseBlockLayoutBlock() { 244 | } 245 | 246 | public void debugEndOfCaseBlockLayoutBlock() { 247 | } 248 | 249 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 250 | 251 | protected void printEscape(String s) { 252 | if (isUnicodeEscape()) { 253 | int length = s.length(); 254 | 255 | for (int i = 0; i < length; i++) { 256 | char c = s.charAt(i); 257 | 258 | if (c == '\t') { 259 | append(c); 260 | } else if (c < 32) { 261 | // Write octal format 262 | append("\\0"); 263 | append((char) ('0' + (c >> 3))); 264 | append((char) ('0' + (c & 0x7))); 265 | } else if (c > 127) { 266 | // Write octal format 267 | append("\\u"); 268 | 269 | int z = (c >> 12); 270 | append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); 271 | z = ((c >> 8) & 0xF); 272 | append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); 273 | z = ((c >> 4) & 0xF); 274 | append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); 275 | z = (c & 0xF); 276 | append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); 277 | } else { 278 | append(c); 279 | } 280 | } 281 | } else { 282 | append(s); 283 | } 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/convert/decompiler/PlainTextPrinter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2015 Emmanuel Dupuy 3 | * This program is made available under the terms of the GPLv3 License. 4 | */ 5 | 6 | package com.bryansharp.jar2java.convert.decompiler; 7 | 8 | import java.io.PrintStream; 9 | 10 | import jd.core.model.instruction.bytecode.instruction.Instruction; 11 | import jd.core.printer.Printer; 12 | 13 | public class PlainTextPrinter implements Printer { 14 | protected static final String TAB = " "; 15 | protected static final String NEWLINE = "\n"; 16 | 17 | protected GuiPreferences preferences = null; 18 | protected PrintStream printStream = null; 19 | protected int maxLineNumber = 0; 20 | protected int majorVersion = 0; 21 | protected int minorVersion = 0; 22 | protected int digitCount = 0; 23 | 24 | protected String lineNumberBeginPrefix; 25 | protected String lineNumberEndPrefix; 26 | protected String unknownLineNumberPrefix; 27 | protected int indentationCount; 28 | protected boolean display; 29 | 30 | public void setPreferences(GuiPreferences preferences) { 31 | this.preferences = preferences; 32 | } 33 | 34 | public void setPrintStream(PrintStream printStream) { 35 | this.printStream = printStream; 36 | } 37 | 38 | public int getMajorVersion() { 39 | return majorVersion; 40 | } 41 | 42 | public int getMinorVersion() { 43 | return minorVersion; 44 | } 45 | 46 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 47 | 48 | public void print(byte b) { 49 | this.printStream.append(String.valueOf(b)); 50 | } 51 | 52 | public void print(int i) { 53 | this.printStream.append(String.valueOf(i)); 54 | } 55 | 56 | public void print(char c) { 57 | if (this.display) 58 | this.printStream.append(String.valueOf(c)); 59 | } 60 | 61 | public void print(String s) { 62 | if (this.display) 63 | printEscape(s); 64 | } 65 | 66 | public void printNumeric(String s) { 67 | this.printStream.append(s); 68 | } 69 | 70 | public void printString(String s, String scopeInternalName) { 71 | this.printStream.append(s); 72 | } 73 | 74 | public void printKeyword(String s) { 75 | if (this.display) 76 | this.printStream.append(s); 77 | } 78 | 79 | public void printJavaWord(String s) { 80 | this.printStream.append(s); 81 | } 82 | 83 | public void printType(String internalName, String name, String scopeInternalName) { 84 | if (this.display) 85 | printEscape(name); 86 | } 87 | 88 | public void printTypeDeclaration(String internalName, String name) { 89 | printEscape(name); 90 | } 91 | 92 | public void printTypeImport(String internalName, String name) { 93 | printEscape(name); 94 | } 95 | 96 | public void printField(String internalName, String name, String descriptor, String scopeInternalName) { 97 | printEscape(name); 98 | } 99 | 100 | public void printFieldDeclaration(String internalName, String name, String descriptor) { 101 | printEscape(name); 102 | } 103 | 104 | public void printStaticField(String internalName, String name, String descriptor, String scopeInternalName) { 105 | printEscape(name); 106 | } 107 | 108 | public void printStaticFieldDeclaration(String internalName, String name, String descriptor) { 109 | printEscape(name); 110 | } 111 | 112 | public void printConstructor(String internalName, String name, String descriptor, String scopeInternalName) { 113 | printEscape(name); 114 | } 115 | 116 | public void printConstructorDeclaration(String internalName, String name, String descriptor) { 117 | printEscape(name); 118 | } 119 | 120 | public void printStaticConstructorDeclaration(String internalName, String name) { 121 | this.printStream.append(name); 122 | } 123 | 124 | public void printMethod(String internalName, String name, String descriptor, String scopeInternalName) { 125 | printEscape(name); 126 | } 127 | 128 | public void printMethodDeclaration(String internalName, String name, String descriptor) { 129 | printEscape(name); 130 | } 131 | 132 | public void printStaticMethod(String internalName, String name, String descriptor, String scopeInternalName) { 133 | printEscape(name); 134 | } 135 | 136 | public void printStaticMethodDeclaration(String internalName, String name, String descriptor) { 137 | printEscape(name); 138 | } 139 | 140 | public void start(int maxLineNumber, int majorVersion, int minorVersion) { 141 | this.majorVersion = majorVersion; 142 | this.minorVersion = minorVersion; 143 | this.indentationCount = 0; 144 | this.display = true; 145 | 146 | if (this.preferences.isShowLineNumbers()) { 147 | this.maxLineNumber = maxLineNumber; 148 | 149 | if (maxLineNumber > 0) { 150 | this.digitCount = 1; 151 | this.unknownLineNumberPrefix = " "; 152 | int maximum = 9; 153 | 154 | while (maximum < maxLineNumber) { 155 | this.digitCount++; 156 | this.unknownLineNumberPrefix += ' '; 157 | maximum = maximum * 10 + 9; 158 | } 159 | 160 | this.lineNumberBeginPrefix = "/* "; 161 | this.lineNumberEndPrefix = " */ "; 162 | } else { 163 | this.unknownLineNumberPrefix = ""; 164 | this.lineNumberBeginPrefix = ""; 165 | this.lineNumberEndPrefix = ""; 166 | } 167 | } else { 168 | this.maxLineNumber = 0; 169 | this.unknownLineNumberPrefix = ""; 170 | this.lineNumberBeginPrefix = ""; 171 | this.lineNumberEndPrefix = ""; 172 | } 173 | } 174 | 175 | public void end() { 176 | } 177 | 178 | public void indent() { 179 | this.indentationCount++; 180 | } 181 | 182 | public void desindent() { 183 | if (this.indentationCount > 0) 184 | this.indentationCount--; 185 | } 186 | 187 | public void startOfLine(int lineNumber) { 188 | if (this.maxLineNumber > 0) { 189 | this.printStream.append(this.lineNumberBeginPrefix); 190 | 191 | if (lineNumber == Instruction.UNKNOWN_LINE_NUMBER) { 192 | this.printStream.append(this.unknownLineNumberPrefix); 193 | } else { 194 | int left = 0; 195 | 196 | left = printDigit(5, lineNumber, 10000, left); 197 | left = printDigit(4, lineNumber, 1000, left); 198 | left = printDigit(3, lineNumber, 100, left); 199 | left = printDigit(2, lineNumber, 10, left); 200 | this.printStream.append((char) ('0' + (lineNumber - left))); 201 | } 202 | 203 | this.printStream.append(this.lineNumberEndPrefix); 204 | } 205 | 206 | for (int i = 0; i < indentationCount; i++) 207 | this.printStream.append(TAB); 208 | } 209 | 210 | public void endOfLine() { 211 | this.printStream.append(NEWLINE); 212 | } 213 | 214 | public void extraLine(int count) { 215 | if (this.preferences.getRealignmentLineNumber()) { 216 | while (count-- > 0) { 217 | if (this.maxLineNumber > 0) { 218 | this.printStream.append(this.lineNumberBeginPrefix); 219 | this.printStream.append(this.unknownLineNumberPrefix); 220 | this.printStream.append(this.lineNumberEndPrefix); 221 | } 222 | 223 | this.printStream.append(NEWLINE); 224 | } 225 | } 226 | } 227 | 228 | public void startOfComment() { 229 | } 230 | 231 | public void endOfComment() { 232 | } 233 | 234 | public void startOfJavadoc() { 235 | } 236 | 237 | public void endOfJavadoc() { 238 | } 239 | 240 | public void startOfXdoclet() { 241 | } 242 | 243 | public void endOfXdoclet() { 244 | } 245 | 246 | public void startOfError() { 247 | } 248 | 249 | public void endOfError() { 250 | } 251 | 252 | public void startOfImportStatements() { 253 | } 254 | 255 | public void endOfImportStatements() { 256 | } 257 | 258 | public void startOfTypeDeclaration(String internalPath) { 259 | } 260 | 261 | public void endOfTypeDeclaration() { 262 | } 263 | 264 | public void startOfAnnotationName() { 265 | } 266 | 267 | public void endOfAnnotationName() { 268 | } 269 | 270 | public void startOfOptionalPrefix() { 271 | if (!this.preferences.isShowPrefixThis()) 272 | this.display = false; 273 | } 274 | 275 | public void endOfOptionalPrefix() { 276 | this.display = true; 277 | } 278 | 279 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 280 | 281 | public void debugStartOfLayoutBlock() { 282 | } 283 | 284 | public void debugEndOfLayoutBlock() { 285 | } 286 | 287 | public void debugStartOfSeparatorLayoutBlock() { 288 | } 289 | 290 | public void debugEndOfSeparatorLayoutBlock(int min, int value, int max) { 291 | } 292 | 293 | public void debugStartOfStatementsBlockLayoutBlock() { 294 | } 295 | 296 | public void debugEndOfStatementsBlockLayoutBlock(int min, int value, int max) { 297 | } 298 | 299 | public void debugStartOfInstructionBlockLayoutBlock() { 300 | } 301 | 302 | public void debugEndOfInstructionBlockLayoutBlock() { 303 | } 304 | 305 | public void debugStartOfCommentDeprecatedLayoutBlock() { 306 | } 307 | 308 | public void debugEndOfCommentDeprecatedLayoutBlock() { 309 | } 310 | 311 | public void debugMarker(String marker) { 312 | } 313 | 314 | public void debugStartOfCaseBlockLayoutBlock() { 315 | } 316 | 317 | public void debugEndOfCaseBlockLayoutBlock() { 318 | } 319 | 320 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 321 | 322 | protected void printEscape(String s) { 323 | if (this.preferences.isUnicodeEscape()) { 324 | int length = s.length(); 325 | 326 | for (int i = 0; i < length; i++) { 327 | char c = s.charAt(i); 328 | 329 | if (c == '\t') { 330 | this.printStream.append(c); 331 | } else if (c < 32) { 332 | // Write octal format 333 | this.printStream.append("\\0"); 334 | this.printStream.append((char) ('0' + (c >> 3))); 335 | this.printStream.append((char) ('0' + (c & 0x7))); 336 | } else if (c > 127) { 337 | // Write octal format 338 | this.printStream.append("\\u"); 339 | 340 | int z = (c >> 12); 341 | this.printStream.append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); 342 | z = ((c >> 8) & 0xF); 343 | this.printStream.append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); 344 | z = ((c >> 4) & 0xF); 345 | this.printStream.append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); 346 | z = (c & 0xF); 347 | this.printStream.append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); 348 | } else { 349 | this.printStream.append(c); 350 | } 351 | } 352 | } else { 353 | this.printStream.append(s); 354 | } 355 | } 356 | 357 | protected int printDigit(int dcv, int lineNumber, int divisor, int left) { 358 | if (this.digitCount >= dcv) { 359 | if (lineNumber < divisor) { 360 | this.printStream.append(' '); 361 | } else { 362 | int e = (lineNumber - left) / divisor; 363 | this.printStream.append((char) ('0' + e)); 364 | left += e * divisor; 365 | } 366 | } 367 | 368 | return left; 369 | } 370 | } 371 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/convert/JavaSourceTextPrinter.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java.convert; 2 | 3 | import java.io.PrintStream; 4 | 5 | import jd.common.preferences.CommonPreferences; 6 | import jd.core.model.instruction.bytecode.instruction.Instruction; 7 | import jd.core.printer.Printer; 8 | 9 | /** 10 | * Created by bsp on 18/1/13. 11 | */ 12 | public class JavaSourceTextPrinter implements Printer { 13 | protected static final String TAB = " "; 14 | protected static final String NEWLINE = "\n"; 15 | protected CommonPreferences preferences; 16 | protected PrintStream printStream; 17 | protected int maxLineNumber; 18 | protected int majorVersion; 19 | protected int minorVersion; 20 | protected int digitCount; 21 | protected String lineNumberBeginPrefix; 22 | protected String lineNumberEndPrefix; 23 | protected String unknownLineNumberPrefix; 24 | protected int indentationCount; 25 | protected boolean display; 26 | 27 | public JavaSourceTextPrinter(CommonPreferences preferences, PrintStream printStream) { 28 | this.preferences = preferences; 29 | this.printStream = printStream; 30 | this.maxLineNumber = 0; 31 | this.majorVersion = 0; 32 | this.minorVersion = 0; 33 | this.indentationCount = 0; 34 | } 35 | 36 | public int getMajorVersion() { 37 | return this.majorVersion; 38 | } 39 | 40 | public int getMinorVersion() { 41 | return this.minorVersion; 42 | } 43 | 44 | @Override 45 | public void print(byte b) { 46 | this.printStream.append(String.valueOf(b)); 47 | } 48 | 49 | @Override 50 | public void print(int i) { 51 | this.printStream.append(String.valueOf(i)); 52 | } 53 | 54 | @Override 55 | public void print(char c) { 56 | if (this.display) { 57 | this.printStream.append(String.valueOf(c)); 58 | } 59 | 60 | } 61 | 62 | @Override 63 | public void print(String s) { 64 | if (this.display) { 65 | this.printEscape(s); 66 | } 67 | 68 | } 69 | 70 | @Override 71 | public void printNumeric(String s) { 72 | this.printStream.append(s); 73 | } 74 | 75 | @Override 76 | public void printString(String s, String scopeInternalName) { 77 | this.printStream.append(s); 78 | } 79 | 80 | @Override 81 | public void printKeyword(String s) { 82 | if (this.display) { 83 | this.printStream.append(s); 84 | } 85 | 86 | } 87 | 88 | @Override 89 | public void printJavaWord(String s) { 90 | this.printStream.append(s); 91 | } 92 | 93 | @Override 94 | public void printType(String internalName, String name, String scopeInternalName) { 95 | if (this.display) { 96 | this.printEscape(name); 97 | } 98 | 99 | } 100 | 101 | @Override 102 | public void printTypeDeclaration(String internalName, String name) { 103 | this.printEscape(name); 104 | } 105 | 106 | @Override 107 | public void printTypeImport(String internalName, String name) { 108 | this.printEscape(name); 109 | } 110 | 111 | @Override 112 | public void printField(String internalName, String name, String descriptor, String scopeInternalName) { 113 | this.printEscape(name); 114 | } 115 | 116 | @Override 117 | public void printFieldDeclaration(String internalName, String name, String descriptor) { 118 | this.printEscape(name); 119 | } 120 | 121 | @Override 122 | public void printStaticField(String internalName, String name, String descriptor, String scopeInternalName) { 123 | this.printEscape(name); 124 | } 125 | 126 | @Override 127 | public void printStaticFieldDeclaration(String internalName, String name, String descriptor) { 128 | this.printEscape(name); 129 | } 130 | 131 | @Override 132 | public void printConstructor(String internalName, String name, String descriptor, String scopeInternalName) { 133 | this.printEscape(name); 134 | } 135 | 136 | @Override 137 | public void printConstructorDeclaration(String internalName, String name, String descriptor) { 138 | this.printEscape(name); 139 | } 140 | 141 | @Override 142 | public void printStaticConstructorDeclaration(String internalName, String name) { 143 | this.printStream.append(name); 144 | } 145 | 146 | @Override 147 | public void printMethod(String internalName, String name, String descriptor, String scopeInternalName) { 148 | this.printEscape(name); 149 | } 150 | 151 | @Override 152 | public void printMethodDeclaration(String internalName, String name, String descriptor) { 153 | this.printEscape(name); 154 | } 155 | 156 | @Override 157 | public void printStaticMethod(String internalName, String name, String descriptor, String scopeInternalName) { 158 | this.printEscape(name); 159 | } 160 | 161 | @Override 162 | public void printStaticMethodDeclaration(String internalName, String name, String descriptor) { 163 | this.printEscape(name); 164 | } 165 | 166 | @Override 167 | public void start(int maxLineNumber, int majorVersion, int minorVersion) { 168 | this.majorVersion = majorVersion; 169 | this.minorVersion = minorVersion; 170 | this.indentationCount = 0; 171 | this.display = true; 172 | if (this.preferences.isShowLineNumbers()) { 173 | this.maxLineNumber = maxLineNumber; 174 | if (maxLineNumber > 0) { 175 | this.digitCount = 1; 176 | this.unknownLineNumberPrefix = " "; 177 | 178 | for (int maximum = 9; maximum < maxLineNumber; maximum = maximum * 10 + 9) { 179 | ++this.digitCount; 180 | this.unknownLineNumberPrefix = this.unknownLineNumberPrefix + ' '; 181 | } 182 | 183 | this.lineNumberBeginPrefix = "/* "; 184 | this.lineNumberEndPrefix = " */ "; 185 | } else { 186 | this.unknownLineNumberPrefix = ""; 187 | this.lineNumberBeginPrefix = ""; 188 | this.lineNumberEndPrefix = ""; 189 | } 190 | } else { 191 | this.maxLineNumber = 0; 192 | this.unknownLineNumberPrefix = ""; 193 | this.lineNumberBeginPrefix = ""; 194 | this.lineNumberEndPrefix = ""; 195 | } 196 | 197 | } 198 | 199 | @Override 200 | public void end() { 201 | } 202 | 203 | @Override 204 | public void indent() { 205 | ++this.indentationCount; 206 | } 207 | 208 | @Override 209 | public void desindent() { 210 | if (this.indentationCount > 0) { 211 | --this.indentationCount; 212 | } 213 | 214 | } 215 | 216 | @Override 217 | public void startOfLine(int lineNumber) { 218 | int left = 0; 219 | if (this.maxLineNumber > 0) { 220 | this.printStream.append(this.lineNumberBeginPrefix); 221 | if (lineNumber == Instruction.UNKNOWN_LINE_NUMBER) { 222 | this.printStream.append(this.unknownLineNumberPrefix); 223 | } else { 224 | left = this.printDigit(5, lineNumber, 10000, left); 225 | left = this.printDigit(4, lineNumber, 1000, left); 226 | left = this.printDigit(3, lineNumber, 100, left); 227 | left = this.printDigit(2, lineNumber, 10, left); 228 | this.printStream.append((char) (48 + (lineNumber - left))); 229 | } 230 | 231 | this.printStream.append(this.lineNumberEndPrefix); 232 | } 233 | 234 | for (left = 0; left < this.indentationCount; ++left) { 235 | this.printStream.append(TAB); 236 | } 237 | 238 | } 239 | 240 | @Override 241 | public void endOfLine() { 242 | this.printStream.append(NEWLINE); 243 | } 244 | 245 | @Override 246 | public void extraLine(int count) { 247 | if (!this.preferences.isMergeEmptyLines()) { 248 | for (; count-- > 0; this.printStream.append(NEWLINE)) { 249 | if (this.maxLineNumber > 0) { 250 | this.printStream.append(this.lineNumberBeginPrefix); 251 | this.printStream.append(this.unknownLineNumberPrefix); 252 | this.printStream.append(this.lineNumberEndPrefix); 253 | } 254 | } 255 | } 256 | } 257 | 258 | @Override 259 | public void startOfComment() { 260 | } 261 | 262 | @Override 263 | public void endOfComment() { 264 | } 265 | 266 | @Override 267 | public void startOfJavadoc() { 268 | } 269 | 270 | @Override 271 | public void endOfJavadoc() { 272 | } 273 | 274 | @Override 275 | public void startOfXdoclet() { 276 | } 277 | 278 | @Override 279 | public void endOfXdoclet() { 280 | } 281 | 282 | @Override 283 | public void startOfError() { 284 | } 285 | 286 | @Override 287 | public void endOfError() { 288 | } 289 | 290 | @Override 291 | public void startOfImportStatements() { 292 | } 293 | 294 | @Override 295 | public void endOfImportStatements() { 296 | } 297 | 298 | @Override 299 | public void startOfTypeDeclaration(String internalPath) { 300 | } 301 | 302 | @Override 303 | public void endOfTypeDeclaration() { 304 | } 305 | 306 | @Override 307 | public void startOfAnnotationName() { 308 | } 309 | 310 | @Override 311 | public void endOfAnnotationName() { 312 | } 313 | 314 | @Override 315 | public void startOfOptionalPrefix() { 316 | if (!this.preferences.isShowPrefixThis()) { 317 | this.display = false; 318 | } 319 | 320 | } 321 | 322 | @Override 323 | public void endOfOptionalPrefix() { 324 | this.display = true; 325 | } 326 | 327 | @Override 328 | public void debugStartOfLayoutBlock() { 329 | } 330 | 331 | @Override 332 | public void debugEndOfLayoutBlock() { 333 | } 334 | 335 | @Override 336 | public void debugStartOfSeparatorLayoutBlock() { 337 | } 338 | 339 | @Override 340 | public void debugEndOfSeparatorLayoutBlock(int min, int value, int max) { 341 | } 342 | 343 | @Override 344 | public void debugStartOfStatementsBlockLayoutBlock() { 345 | } 346 | 347 | @Override 348 | public void debugEndOfStatementsBlockLayoutBlock(int min, int value, int max) { 349 | } 350 | 351 | @Override 352 | public void debugStartOfInstructionBlockLayoutBlock() { 353 | } 354 | 355 | @Override 356 | public void debugEndOfInstructionBlockLayoutBlock() { 357 | } 358 | 359 | @Override 360 | public void debugStartOfCommentDeprecatedLayoutBlock() { 361 | } 362 | 363 | @Override 364 | public void debugEndOfCommentDeprecatedLayoutBlock() { 365 | } 366 | 367 | @Override 368 | public void debugMarker(String marker) { 369 | } 370 | 371 | @Override 372 | public void debugStartOfCaseBlockLayoutBlock() { 373 | } 374 | 375 | @Override 376 | public void debugEndOfCaseBlockLayoutBlock() { 377 | } 378 | 379 | protected void printEscape(String s) { 380 | if (this.preferences.isUnicodeEscape()) { 381 | int length = s.length(); 382 | 383 | for (int i = 0; i < length; ++i) { 384 | char c = s.charAt(i); 385 | if (c == 9) { 386 | this.printStream.append(c); 387 | } else if (c < 32) { 388 | this.printStream.append("\\0"); 389 | this.printStream.append((char) (48 + (c >> 3))); 390 | this.printStream.append((char) (48 + (c & 7))); 391 | } else if (c > 127) { 392 | this.printStream.append("\\u"); 393 | int z = c >> 12; 394 | this.printStream.append((char) (z <= 9 ? 48 + z : 55 + z)); 395 | z = c >> 8 & 15; 396 | this.printStream.append((char) (z <= 9 ? 48 + z : 55 + z)); 397 | z = c >> 4 & 15; 398 | this.printStream.append((char) (z <= 9 ? 48 + z : 55 + z)); 399 | z = c & 15; 400 | this.printStream.append((char) (z <= 9 ? 48 + z : 55 + z)); 401 | } else { 402 | this.printStream.append(c); 403 | } 404 | } 405 | } else { 406 | this.printStream.append(s); 407 | } 408 | 409 | } 410 | 411 | protected int printDigit(int dcv, int lineNumber, int divisor, int left) { 412 | if (this.digitCount >= dcv) { 413 | if (lineNumber < divisor) { 414 | this.printStream.append(' '); 415 | } else { 416 | int e = (lineNumber - left) / divisor; 417 | this.printStream.append((char) (48 + e)); 418 | left += e * divisor; 419 | } 420 | } 421 | 422 | return left; 423 | } 424 | } 425 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/Utils.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java; 2 | 3 | import java.io.File; 4 | import java.lang.reflect.Array; 5 | import java.util.HashMap; 6 | import java.util.HashSet; 7 | import java.util.Map; 8 | 9 | /** 10 | * Created by bushaopeng on 17/1/24. 11 | */ 12 | public class Utils { 13 | public static HashMap accCodeMap = new HashMap<>(); 14 | static HashMap opCodeMap = new HashMap<>(); 15 | static HashSet keywords = new HashSet<>(); 16 | 17 | static { 18 | keywords.add("do"); 19 | keywords.add("if"); 20 | keywords.add("for"); 21 | keywords.add("int"); 22 | keywords.add("new"); 23 | keywords.add("try"); 24 | keywords.add("byte"); 25 | keywords.add("case"); 26 | keywords.add("char"); 27 | keywords.add("else"); 28 | keywords.add("goto"); 29 | keywords.add("long"); 30 | keywords.add("this"); 31 | keywords.add("void"); 32 | keywords.add("break"); 33 | keywords.add("catch"); 34 | keywords.add("class"); 35 | keywords.add("const"); 36 | keywords.add("final"); 37 | keywords.add("float"); 38 | keywords.add("short"); 39 | keywords.add("super"); 40 | keywords.add("throw"); 41 | keywords.add("while"); 42 | keywords.add("double"); 43 | keywords.add("import"); 44 | keywords.add("native"); 45 | keywords.add("public"); 46 | keywords.add("return"); 47 | keywords.add("static"); 48 | keywords.add("switch"); 49 | keywords.add("throws"); 50 | keywords.add("boolean"); 51 | keywords.add("default"); 52 | keywords.add("extends"); 53 | keywords.add("finally"); 54 | keywords.add("package"); 55 | keywords.add("private"); 56 | keywords.add("abstract"); 57 | keywords.add("continue"); 58 | keywords.add("strictfp"); 59 | keywords.add("volatile"); 60 | keywords.add("interface"); 61 | keywords.add("protected"); 62 | keywords.add("transient"); 63 | keywords.add("implements"); 64 | keywords.add("instanceof"); 65 | keywords.add("synchronized"); 66 | 67 | } 68 | 69 | public static String path2Classname(String entryName) { 70 | return entryName.replace(File.separator, ".").replace(".class", ""); 71 | } 72 | 73 | public static String classname2Path(String oldClassname) { 74 | return oldClassname.replace(".", "/"); 75 | } 76 | 77 | public static Map getOpMap() { 78 | if (opCodeMap.size() == 0) { 79 | HashMap map = new HashMap<>(); 80 | map.put("V1_1", 196653); 81 | map.put("V1_2", 46); 82 | map.put("V1_3", 47); 83 | map.put("V1_4", 48); 84 | map.put("V1_5", 49); 85 | map.put("V1_6", 50); 86 | map.put("V1_7", 51); 87 | map.put("ACC_PUBLIC", 1); 88 | map.put("ACC_PRIVATE", 2); 89 | map.put("ACC_PROTECTED", 4); 90 | map.put("ACC_STATIC", 8); 91 | map.put("ACC_FINAL", 16); 92 | map.put("ACC_SUPER", 32); 93 | map.put("ACC_SYNCHRONIZED", 32); 94 | map.put("ACC_VOLATILE", 64); 95 | map.put("ACC_BRIDGE", 64); 96 | map.put("ACC_VARARGS", 128); 97 | map.put("ACC_TRANSIENT", 128); 98 | map.put("ACC_NATIVE", 256); 99 | map.put("ACC_INTERFACE", 512); 100 | map.put("ACC_ABSTRACT", 1024); 101 | map.put("ACC_STRICT", 2048); 102 | map.put("ACC_SYNTHETIC", 4096); 103 | map.put("ACC_ANNOTATION", 8192); 104 | map.put("ACC_ENUM", 16384); 105 | map.put("ACC_DEPRECATED", 131072); 106 | map.put("T_BOOLEAN", 4); 107 | map.put("T_CHAR", 5); 108 | map.put("T_FLOAT", 6); 109 | map.put("T_DOUBLE", 7); 110 | map.put("T_BYTE", 8); 111 | map.put("T_SHORT", 9); 112 | map.put("T_INT", 10); 113 | map.put("T_LONG", 11); 114 | map.put("F_NEW", -1); 115 | map.put("F_FULL", 0); 116 | map.put("F_APPEND", 1); 117 | map.put("F_CHOP", 2); 118 | map.put("F_SAME", 3); 119 | map.put("F_SAME1", 4); 120 | map.put("TOP", 0); 121 | map.put("INTEGER", 1); 122 | map.put("FLOAT", 2); 123 | map.put("DOUBLE", 3); 124 | map.put("LONG", 4); 125 | map.put("NULL", 5); 126 | map.put("UNINITIALIZED_THIS", 6); 127 | map.put("NOP", 0); 128 | map.put("ACONST_NULL", 1); 129 | map.put("ICONST_M1", 2); 130 | map.put("ICONST_0", 3); 131 | map.put("ICONST_1", 4); 132 | map.put("ICONST_2", 5); 133 | map.put("ICONST_3", 6); 134 | map.put("ICONST_4", 7); 135 | map.put("ICONST_5", 8); 136 | map.put("LCONST_0", 9); 137 | map.put("LCONST_1", 10); 138 | map.put("FCONST_0", 11); 139 | map.put("FCONST_1", 12); 140 | map.put("FCONST_2", 13); 141 | map.put("DCONST_0", 14); 142 | map.put("DCONST_1", 15); 143 | map.put("BIPUSH", 16); 144 | map.put("SIPUSH", 17); 145 | map.put("LDC", 18); 146 | map.put("ILOAD", 21); 147 | map.put("LLOAD", 22); 148 | map.put("FLOAD", 23); 149 | map.put("DLOAD", 24); 150 | map.put("ALOAD", 25); 151 | map.put("IALOAD", 46); 152 | map.put("LALOAD", 47); 153 | map.put("FALOAD", 48); 154 | map.put("DALOAD", 49); 155 | map.put("AALOAD", 50); 156 | map.put("BALOAD", 51); 157 | map.put("CALOAD", 52); 158 | map.put("SALOAD", 53); 159 | map.put("ISTORE", 54); 160 | map.put("LSTORE", 55); 161 | map.put("FSTORE", 56); 162 | map.put("DSTORE", 57); 163 | map.put("ASTORE", 58); 164 | map.put("IASTORE", 79); 165 | map.put("LASTORE", 80); 166 | map.put("FASTORE", 81); 167 | map.put("DASTORE", 82); 168 | map.put("AASTORE", 83); 169 | map.put("BASTORE", 84); 170 | map.put("CASTORE", 85); 171 | map.put("SASTORE", 86); 172 | map.put("POP", 87); 173 | map.put("POP2", 88); 174 | map.put("DUP", 89); 175 | map.put("DUP_X1", 90); 176 | map.put("DUP_X2", 91); 177 | map.put("DUP2", 92); 178 | map.put("DUP2_X1", 93); 179 | map.put("DUP2_X2", 94); 180 | map.put("SWAP", 95); 181 | map.put("IADD", 96); 182 | map.put("LADD", 97); 183 | map.put("FADD", 98); 184 | map.put("DADD", 99); 185 | map.put("ISUB", 100); 186 | map.put("LSUB", 101); 187 | map.put("FSUB", 102); 188 | map.put("DSUB", 103); 189 | map.put("IMUL", 104); 190 | map.put("LMUL", 105); 191 | map.put("FMUL", 106); 192 | map.put("DMUL", 107); 193 | map.put("IDIV", 108); 194 | map.put("LDIV", 109); 195 | map.put("FDIV", 110); 196 | map.put("DDIV", 111); 197 | map.put("IREM", 112); 198 | map.put("LREM", 113); 199 | map.put("FREM", 114); 200 | map.put("DREM", 115); 201 | map.put("INEG", 116); 202 | map.put("LNEG", 117); 203 | map.put("FNEG", 118); 204 | map.put("DNEG", 119); 205 | map.put("ISHL", 120); 206 | map.put("LSHL", 121); 207 | map.put("ISHR", 122); 208 | map.put("LSHR", 123); 209 | map.put("IUSHR", 124); 210 | map.put("LUSHR", 125); 211 | map.put("IAND", 126); 212 | map.put("LAND", 127); 213 | map.put("IOR", 128); 214 | map.put("LOR", 129); 215 | map.put("IXOR", 130); 216 | map.put("LXOR", 131); 217 | map.put("IINC", 132); 218 | map.put("I2L", 133); 219 | map.put("I2F", 134); 220 | map.put("I2D", 135); 221 | map.put("L2I", 136); 222 | map.put("L2F", 137); 223 | map.put("L2D", 138); 224 | map.put("F2I", 139); 225 | map.put("F2L", 140); 226 | map.put("F2D", 141); 227 | map.put("D2I", 142); 228 | map.put("D2L", 143); 229 | map.put("D2F", 144); 230 | map.put("I2B", 145); 231 | map.put("I2C", 146); 232 | map.put("I2S", 147); 233 | map.put("LCMP", 148); 234 | map.put("FCMPL", 149); 235 | map.put("FCMPG", 150); 236 | map.put("DCMPL", 151); 237 | map.put("DCMPG", 152); 238 | map.put("IFEQ", 153); 239 | map.put("IFNE", 154); 240 | map.put("IFLT", 155); 241 | map.put("IFGE", 156); 242 | map.put("IFGT", 157); 243 | map.put("IFLE", 158); 244 | map.put("IF_ICMPEQ", 159); 245 | map.put("IF_ICMPNE", 160); 246 | map.put("IF_ICMPLT", 161); 247 | map.put("IF_ICMPGE", 162); 248 | map.put("IF_ICMPGT", 163); 249 | map.put("IF_ICMPLE", 164); 250 | map.put("IF_ACMPEQ", 165); 251 | map.put("IF_ACMPNE", 166); 252 | map.put("GOTO", 167); 253 | map.put("JSR", 168); 254 | map.put("RET", 169); 255 | map.put("TABLESWITCH", 170); 256 | map.put("LOOKUPSWITCH", 171); 257 | map.put("IRETURN", 172); 258 | map.put("LRETURN", 173); 259 | map.put("FRETURN", 174); 260 | map.put("DRETURN", 175); 261 | map.put("ARETURN", 176); 262 | map.put("RETURN", 177); 263 | map.put("GETSTATIC", 178); 264 | map.put("PUTSTATIC", 179); 265 | map.put("GETFIELD", 180); 266 | map.put("PUTFIELD", 181); 267 | map.put("INVOKEVIRTUAL", 182); 268 | map.put("INVOKESPECIAL", 183); 269 | map.put("INVOKESTATIC", 184); 270 | map.put("INVOKEINTERFACE", 185); 271 | map.put("INVOKEDYNAMIC", 186); 272 | map.put("NEW", 187); 273 | map.put("NEWARRAY", 188); 274 | map.put("ANEWARRAY", 189); 275 | map.put("ARRAYLENGTH", 190); 276 | map.put("ATHROW", 191); 277 | map.put("CHECKCAST", 192); 278 | map.put("INSTANCEOF", 193); 279 | map.put("MONITORENTER", 194); 280 | map.put("MONITOREXIT", 195); 281 | map.put("MULTIANEWARRAY", 197); 282 | map.put("IFNULL", 198); 283 | map.put("IFNONNULL", 199); 284 | for (Map.Entry entry : map.entrySet()) { 285 | opCodeMap.put(entry.getValue(), entry.getKey()); 286 | } 287 | } 288 | return opCodeMap; 289 | } 290 | 291 | public static Map getAccCodeMap() { 292 | if (accCodeMap.size() == 0) { 293 | HashMap map = new HashMap<>(); 294 | map.put("ACC_PUBLIC", 1); 295 | map.put("ACC_PRIVATE", 2); 296 | map.put("ACC_PROTECTED", 4); 297 | map.put("ACC_STATIC", 8); 298 | map.put("ACC_FINAL", 16); 299 | map.put("ACC_SUPER", 32); 300 | map.put("ACC_SYNCHRONIZED", 32); 301 | map.put("ACC_VOLATILE", 64); 302 | map.put("ACC_BRIDGE", 64); 303 | map.put("ACC_VARARGS", 128); 304 | map.put("ACC_TRANSIENT", 128); 305 | map.put("ACC_NATIVE", 256); 306 | map.put("ACC_INTERFACE", 512); 307 | map.put("ACC_ABSTRACT", 1024); 308 | map.put("ACC_STRICT", 2048); 309 | map.put("ACC_SYNTHETIC", 4096); 310 | map.put("ACC_ANNOTATION", 8192); 311 | map.put("ACC_ENUM", 16384); 312 | map.put("ACC_DEPRECATED", 131072); 313 | for (Map.Entry entry : map.entrySet()) { 314 | accCodeMap.put(entry.getValue(), entry.getKey()); 315 | } 316 | } 317 | return accCodeMap; 318 | } 319 | 320 | public static String accCode2String(int access) { 321 | StringBuilder builder = new StringBuilder(); 322 | Map map = getAccCodeMap(); 323 | for (Map.Entry entry : map.entrySet()) { 324 | if ((entry.getKey().intValue() & access) > 0) { 325 | //此处如果使用|作为分隔符会导致编译报错 因此改用斜杠 326 | builder.append('\\' + entry.getValue() + "/ "); 327 | } 328 | } 329 | return builder.toString(); 330 | } 331 | 332 | public static String getOpName(int opCode) { 333 | return getOpMap().get(opCode); 334 | } 335 | 336 | public static void logEach(Object... msg) { 337 | for (Object m : msg) { 338 | try { 339 | if (m != null) { 340 | if (m.getClass().isArray()) { 341 | logInline("["); 342 | int length = Array.getLength(m); 343 | if (length > 0) { 344 | for (int i = 0; i < length; i++) { 345 | Object get = Array.get(m, i); 346 | if (get != null) { 347 | logInline(get + "\t"); 348 | } else { 349 | logInline("null\t"); 350 | } 351 | } 352 | } 353 | logInline("]\t"); 354 | } else { 355 | logInline(m + "\t"); 356 | } 357 | } else { 358 | logInline("null\t"); 359 | } 360 | } catch (Exception e) { 361 | } 362 | } 363 | logInline("\n"); 364 | } 365 | 366 | public static void log(Object msg) { 367 | System.out.println(msg); 368 | } 369 | 370 | public static void logInline(Object msg) { 371 | System.out.print(msg); 372 | } 373 | 374 | public static boolean isProguardedName(String simpleName) { 375 | if (simpleName.matches("[a-z]{1,2}")) { 376 | return true; 377 | } 378 | if (Utils.isKeyWord(simpleName)) { 379 | return true; 380 | } 381 | return false; 382 | } 383 | 384 | public static boolean isKeyWord(String simpleName) { 385 | return keywords.contains(simpleName); 386 | } 387 | } 388 | -------------------------------------------------------------------------------- /src/main/java/com/bryansharp/jar2java/analyze/JarAnalyzer.java: -------------------------------------------------------------------------------- 1 | package com.bryansharp.jar2java.analyze; 2 | 3 | import com.bryansharp.jar2java.TextFileWritter; 4 | import com.bryansharp.jar2java.Utils; 5 | 6 | import org.apache.commons.codec.digest.DigestUtils; 7 | import org.apache.commons.io.IOUtils; 8 | import org.objectweb.asm.ClassReader; 9 | import org.objectweb.asm.ClassVisitor; 10 | import org.objectweb.asm.ClassWriter; 11 | 12 | import java.io.File; 13 | import java.io.FileOutputStream; 14 | import java.io.IOException; 15 | import java.io.InputStream; 16 | import java.lang.reflect.InvocationHandler; 17 | import java.lang.reflect.Method; 18 | import java.lang.reflect.Proxy; 19 | import java.util.Enumeration; 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | import java.util.Set; 23 | import java.util.jar.JarEntry; 24 | import java.util.jar.JarFile; 25 | import java.util.jar.JarOutputStream; 26 | import java.util.zip.ZipEntry; 27 | import java.util.zip.ZipFile; 28 | 29 | /** 30 | * Created by bushaopeng on 17/9/14. 31 | */ 32 | public class JarAnalyzer { 33 | public static File unzipEntryToTemp(ZipEntry element, ZipFile zipFile) throws IOException { 34 | InputStream stream = zipFile.getInputStream(element); 35 | byte[] array = IOUtils.toByteArray(stream); 36 | String hex = DigestUtils.md5Hex(element.getName()); 37 | final File tempDir = new File("/Users/bushaopeng/IdeaProjects/Jar2Java/build"); 38 | File targetFile = new File(tempDir, hex + ".jar"); 39 | if (targetFile.exists()) { 40 | targetFile.delete(); 41 | } 42 | new FileOutputStream(targetFile).write(array); 43 | return targetFile; 44 | } 45 | 46 | public boolean analyzeAar(String path) { 47 | try { 48 | ZipFile zipFile = new ZipFile(new File(path)); 49 | Enumeration entries = zipFile.entries(); 50 | while (entries.hasMoreElements()) { 51 | ZipEntry element = entries.nextElement(); 52 | String name = element.getName(); 53 | if (name.endsWith(".jar")) { 54 | File innerJar = unzipEntryToTemp(element, zipFile); 55 | // renameClassInJar(innerJar); 56 | } 57 | } 58 | } catch (Exception e) { 59 | e.printStackTrace(); 60 | } 61 | return true; 62 | } 63 | 64 | private File renameClassInJar(File jarFile, Map renameMap) throws IOException { 65 | File outputJar = new File(jarFile.getParentFile(), "new-" + jarFile.getName()); 66 | if (outputJar.exists()) { 67 | outputJar.delete(); 68 | } 69 | JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(outputJar)); 70 | 71 | JarFile file = new JarFile(jarFile); 72 | Enumeration enumeration = file.entries(); 73 | while (enumeration.hasMoreElements()) { 74 | JarEntry jarEntry = enumeration.nextElement(); 75 | InputStream inputStream = file.getInputStream(jarEntry); 76 | 77 | String entryName = jarEntry.getName(); 78 | String className; 79 | byte[] sourceClassBytes = IOUtils.toByteArray(inputStream); 80 | boolean entryNamePut = false; 81 | if (entryName.endsWith(".class")) { 82 | className = Utils.path2Classname(entryName); 83 | byte[] bytes = renameClass(className, sourceClassBytes, renameMap); 84 | if (renameMap.keySet().contains(className)) { 85 | String newEntryName = getNewEntryName(entryName, renameMap.get(className)); 86 | ZipEntry zipEntry = new ZipEntry(newEntryName); 87 | jarOutputStream.putNextEntry(zipEntry); 88 | } else { 89 | ZipEntry zipEntry = new ZipEntry(entryName); 90 | jarOutputStream.putNextEntry(zipEntry); 91 | } 92 | entryNamePut = true; 93 | jarOutputStream.write(bytes); 94 | } 95 | if (!entryNamePut) { 96 | ZipEntry zipEntry = new ZipEntry(entryName); 97 | jarOutputStream.putNextEntry(zipEntry); 98 | } 99 | jarOutputStream.closeEntry(); 100 | } 101 | jarOutputStream.close(); 102 | return outputJar; 103 | } 104 | 105 | private String getNewEntryName(String entryName, String newClassname) { 106 | return entryName.substring(0, entryName.lastIndexOf('/') + 1) 107 | + newClassname.substring(newClassname.lastIndexOf('.') + 1) 108 | + entryName.substring(entryName.lastIndexOf(".")); 109 | } 110 | 111 | public File renameClassInJar(String jarFilePath, final Map renameMap) { 112 | try { 113 | return renameClassInJar(new File(jarFilePath), renameMap); 114 | } catch (IOException e) { 115 | e.printStackTrace(); 116 | } 117 | return null; 118 | } 119 | 120 | private byte[] renameClass(final String className, byte[] sourceClassBytes, final Map renameMap) { 121 | 122 | Utils.log("className: " + className + "原大小:" + sourceClassBytes.length); 123 | final ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS); 124 | // ClassVisitor adapter = new AnalyzeClassVisitor(className, classWriter); 125 | 126 | final ClassLoader classLoader = getClass().getClassLoader(); 127 | ClassVisitor adapter = (ClassVisitor) Proxy.newProxyInstance(classLoader, classWriter.getClass().getInterfaces(), new InvocationHandler() { 128 | @Override 129 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 130 | boolean replaceInArgs = replaceInArgs(method, args, renameMap); 131 | final Object invoke = method.invoke(classWriter, args); 132 | if (replaceInArgs) { 133 | logProxy(method, args, invoke, 0); 134 | } 135 | if (invoke != null) { 136 | return Proxy.newProxyInstance(classLoader, invoke.getClass().getInterfaces(), new InvocationHandler() { 137 | @Override 138 | public Object invoke(Object proxy1, Method method1, Object[] args1) throws Throwable { 139 | boolean replaceInArgs1 = replaceInArgs(method1, args1, renameMap); 140 | Object ret = method1.invoke(invoke, args1); 141 | if (replaceInArgs1) { 142 | logProxy(method1, args1, ret, 1); 143 | } 144 | return ret; 145 | } 146 | }); 147 | } 148 | return null; 149 | } 150 | }); 151 | 152 | ClassReader cr = new ClassReader(sourceClassBytes); 153 | //cr.accept(visitor, ClassReader.SKIP_DEBUG); 154 | cr.accept(adapter, 0); 155 | Utils.log("className: " + className + "新类大小:" + classWriter.toByteArray().length); 156 | return classWriter.toByteArray(); 157 | } 158 | 159 | private boolean replaceInArgs(Method method, Object[] args, final Map renameMap) { 160 | boolean replaced = false; 161 | Set keySet = renameMap.keySet(); 162 | if (args != null && args.length > 0) { 163 | for (int i = 0; i < args.length; i++) { 164 | if (args[i] == null) { 165 | continue; 166 | } 167 | for (String classname : keySet) { 168 | String path = Utils.classname2Path(classname); 169 | String newPath = Utils.classname2Path(renameMap.get(classname)); 170 | if (path.equals(args[i])) { 171 | args[i] = newPath; 172 | replaced = true; 173 | } else { 174 | if (args[i] instanceof String) { 175 | String signiture = (String) args[i]; 176 | if (signiture.contains(path)) { 177 | StringBuilder builder = new StringBuilder(); 178 | boolean seeNext = false; 179 | Object appendObj = null; 180 | for (int charPos = 0; charPos < signiture.length(); charPos++) { 181 | if (seeNext) { 182 | seeNext = false; 183 | int endIndex = signiture.indexOf(';', charPos); 184 | if (endIndex < 0) { 185 | // fixme double L logic 186 | appendObj = signiture.charAt(charPos); 187 | } else { 188 | String maybeClassRef = signiture.substring(charPos, endIndex); 189 | charPos = endIndex - 1; 190 | if (!path.equals(maybeClassRef)) { 191 | appendObj = maybeClassRef; 192 | } else { 193 | appendObj = newPath; 194 | } 195 | } 196 | } else { 197 | char cCar = signiture.charAt(charPos); 198 | if (cCar == 'L') { 199 | seeNext = true; 200 | } 201 | appendObj = cCar; 202 | } 203 | builder.append(appendObj); 204 | } 205 | String newSignture = builder.toString(); 206 | replaced = !newSignture.equals(args[i]); 207 | args[i] = newSignture; 208 | } 209 | } 210 | } 211 | } 212 | } 213 | } 214 | return replaced; 215 | } 216 | 217 | private void logProxy(Method method, Object[] args, Object returnedVal, int indent) { 218 | StringBuilder builder = new StringBuilder(); 219 | if (args != null && args.length > 0) { 220 | for (Object arg : args) { 221 | if (arg != null) { 222 | builder.append(arg.toString()); 223 | } else { 224 | builder.append("null"); 225 | } 226 | builder.append("\t"); 227 | } 228 | builder.setLength(builder.length() - 1); 229 | } 230 | if (returnedVal != null) { 231 | builder.append(", 返回值:").append(returnedVal); 232 | } else { 233 | builder.append(",无返回值"); 234 | } 235 | Utils.log((indent == 0 ? "" : "\t") + "调用:" + method.getName() + ", 参数: " + builder.toString()); 236 | } 237 | 238 | public Map getRenameMap(String jarFilePath) { 239 | Map map = new HashMap<>(); 240 | try { 241 | File jarFile = new File(jarFilePath); 242 | JarFile file = new JarFile(jarFile); 243 | Enumeration enumeration = file.entries(); 244 | while (enumeration.hasMoreElements()) { 245 | JarEntry jarEntry = enumeration.nextElement(); 246 | String entryName = jarEntry.getName(); 247 | String className; 248 | if (entryName.endsWith(".class")) { 249 | className = Utils.path2Classname(entryName); 250 | String simpleName = className.substring(className.lastIndexOf('.') + 1); 251 | if (Utils.isProguardedName(simpleName)) { 252 | map.put(className, getNewClassName(className, simpleName)); 253 | } 254 | } 255 | } 256 | file.close(); 257 | } catch (IOException e) { 258 | e.printStackTrace(); 259 | } 260 | 261 | return map; 262 | } 263 | 264 | private String getNewClassName(String className, String simpleName) { 265 | String hex = DigestUtils.md5Hex(className + simpleName); 266 | hex = hex.substring(hex.length() - 5); 267 | return className.substring(0, className.lastIndexOf('.') + 1) + simpleName.toUpperCase() + hex; 268 | } 269 | 270 | public Map getReproguardMapping(String jarPath) { 271 | Map renameMap = new HashMap<>(); 272 | try { 273 | JarFile file = new JarFile(new File(jarPath)); 274 | Enumeration enumeration = file.entries(); 275 | while (enumeration.hasMoreElements()) { 276 | JarEntry jarEntry = enumeration.nextElement(); 277 | InputStream inputStream = file.getInputStream(jarEntry); 278 | String entryName = jarEntry.getName(); 279 | String className; 280 | byte[] sourceClassBytes = IOUtils.toByteArray(inputStream); 281 | if (entryName.endsWith(".class")) { 282 | className = Utils.path2Classname(entryName); 283 | String newClassname = getReproguardClassname(className); 284 | TextFileWritter.getDefaultWritter().println(className + (newClassname != null ? " -> " + newClassname : "")); 285 | // analyzeClassNames(className, sourceClassBytes); 286 | } 287 | } 288 | } catch (IOException e) { 289 | e.printStackTrace(); 290 | } 291 | TextFileWritter.getDefaultWritter().close(); 292 | return renameMap; 293 | } 294 | 295 | private String getReproguardClassname(String className) { 296 | StringBuilder stringBuilder = new StringBuilder(); 297 | boolean changed = false; 298 | String[] split = className.split("\\."); 299 | int count = 0; 300 | for (String s : split) { 301 | if (count == split.length - 1) { 302 | if (Utils.isProguardedName(s)) { 303 | changed = true; 304 | String suffix = DigestUtils.md5Hex(className).substring(0, 4); 305 | stringBuilder.append(s.toUpperCase()).append(suffix); 306 | } else { 307 | stringBuilder.append("."); 308 | } 309 | } else { 310 | if (Utils.isKeyWord(s)) { 311 | changed = true; 312 | String suffix = DigestUtils.md5Hex(className).substring(0, 4); 313 | stringBuilder.append(s).append(suffix); 314 | } else { 315 | stringBuilder.append(s); 316 | } 317 | stringBuilder.append("."); 318 | } 319 | count++; 320 | } 321 | if (changed) { 322 | return stringBuilder.toString(); 323 | } 324 | return null; 325 | } 326 | 327 | private void analyzeClassNames(String className, byte[] sourceClassBytes) { 328 | Utils.log("className: " + className + "原大小:" + sourceClassBytes.length); 329 | ClassVisitor adapter = new AnalyzeClassVisitor(className); 330 | ClassReader cr = new ClassReader(sourceClassBytes); 331 | //cr.accept(visitor, ClassReader.SKIP_DEBUG); 332 | cr.accept(adapter, 0); 333 | } 334 | } 335 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------