java反编译器cfr
介绍
cfr是一个非常强大的java反编译器,支持java5,java8的新特性,如枚举,lambda表达式等。
下载
地址
下载下来是一个jar包,可以使用java命令来运行。
使用
public enum UserGenderEnum {
MALE("0", "男"),
FEMALE("1", "女"),
NOT_KNOWN("2", "未知");
private final String code;
private final String info;
UserGenderEnum(String code, String info) {
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public String getInfo() {
return info;
}
}
反编译命令为
java -jar cfr-0.150.jar UserGenderEnum.class --sugerenums false
参数--sugerenums false表示会解析枚举语法糖,结果为
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public final class UserGenderEnum extends Enum<UserGenderEnum> {
public static final /* enum */ UserGenderEnum MALE = new UserGenderEnum("MALE", 0, "0", "\u7537");
public static final /* enum */ UserGenderEnum FEMALE = new UserGenderEnum("FEMALE", 1, "1", "\u5973");
public static final /* enum */ UserGenderEnum NOT_KNOWN = new UserGenderEnum("NOT_KNOWN", 2, "2", "\u672a\u77e5");
private final String code;
private final String info;
private static final /* synthetic */ UserGenderEnum[] $VALUES;
public static UserGenderEnum[] values() {
return (UserGenderEnum[])$VALUES.clone();
}
public static UserGenderEnum valueOf(String name) {
return Enum.valueOf(UserGenderEnum.class, name);
}
private UserGenderEnum(String string, int n, String code, String info) {
super(string, n);
this.code = code;
this.info = info;
}
public String getCode() {
return this.code;
}
public String getInfo() {
return this.info;
}
static {
$VALUES = new UserGenderEnum[]{MALE, FEMALE, NOT_KNOWN};
}
}
再看一个lambda的例子
public class Client {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("zhangsan");
names.add("lisi");
names.add("wangwu");
String ret = names.stream()
.filter(name -> name.length() > 4)
.collect(Collectors.joining(","));
System.out.println(ret);
}
}
java -jar cfr-0.150.jar Client.class --decodelambdas false
输出结果为
public class Client {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("zhangsan");
names.add("lisi");
names.add("wangwu");
String ret = names.stream().filter((Predicate<String>)LambdaMetafactory.metafactory(null, null, null, (Ljava/lang/Object;)Z, lambda$main$0(java.lang.String ), (Ljava/lang/String;)Z)()).collect(Collectors.joining(","));
System.out.println(ret);
}
private static /* synthetic */ boolean lambda$main$0(String name) {
return name.length() > 4;
}
}
cfr还有大量的参数可以让我们灵活的设置
java -jar cfr-0.150.jar --help
--aexagg (boolean)
--aexagg2 (boolean)
--aggressivedoextension (boolean)
--aggressivesizethreshold (int >= 0) default: 15000
--allowcorrecting (boolean) default: true
--analyseas (One of [DETECT, JAR, WAR, CLASS])
--arrayiter (boolean) default: true if class file from version 49.0 (Java 5) or greater
--caseinsensitivefs (boolean) default: true
--clobber (boolean)
--collectioniter (boolean) default: true if class file from version 49.0 (Java 5) or greater
--commentmonitors (boolean) default: false
--comments (boolean) default: true
--decodeenumswitch (boolean) default: true if class file from version 49.0 (Java 5) or greater
去除switch对枚举支持的语法糖 --decodeenumswitch false
--decodefinally (boolean) default: true
--decodelambdas (boolean) default: true if class file from version 52.0 (Java 8) or greater
去除lambda表达式的语法糖 --decodelambdas false
--decodestringswitch (boolean) default: true if class file from version 51.0 (Java 7) or greater
--dumpclasspath (boolean) default: false
--eclipse (boolean) default: true
--elidescala (boolean) default: false
--extraclasspath (string)
--forbidanonymousclasses (boolean) default: false
--forbidmethodscopedclasses (boolean) default: false
--forceclassfilever (string, specifying either java version as 'j6', 'j1.0', or classfile as '56', '56.65535')
--forcecondpropagate (boolean)
--forceexceptionprune (boolean)
--forcereturningifs (boolean)
--forcetopsort (boolean)
--forcetopsortaggress (boolean)
--forcetopsortnopull (boolean)
--forloopaggcapture (boolean)
--hidebridgemethods (boolean) default: true
--hidelangimports (boolean) default: true
--hidelongstrings (boolean) default: false
--hideutf (boolean) default: true
--ignoreexceptions (boolean) default: false
--ignoreexceptionsalways (boolean) default: false
--importfilter (string)
--innerclasses (boolean) default: true
--instanceofpattern (boolean) default: true if class file from version 58.0 (Java 14) or greater, or experimental in 58.0 (Java 14)
--j14classobj (boolean) default: false if class file from version 49.0 (Java 5) or greater
--jarfilter (string)
--labelledblocks (boolean) default: true
--lenient (boolean) default: false
--liftconstructorinit (boolean) default: true
--lomem (boolean) default: false
--methodname (string)
--obfuscationpath (string)
--outputdir (string)
结果输出目录
--outputpath (string)
--override (boolean) default: true if class file from version 50.0 (Java 6) or greater
--previewfeatures (boolean) default: true
--pullcodecase (boolean) default: false
--recordtypes (boolean) default: true if class file from version 58.0 (Java 14) or greater, or experimental in 58.0 (Java 14)
--recover (boolean) default: true
--recovertypeclash (boolean)
--recovertypehints (boolean)
--relinkconststring (boolean) default: true
--removebadgenerics (boolean) default: true
去除泛型的语法糖 --removebadgenerics false
--removeboilerplate (boolean) default: true
--removedeadconditionals (boolean)
--removedeadmethods (boolean) default: true
--removeinnerclasssynthetics (boolean) default: true
--rename (boolean) default: false
--renamedupmembers (boolean) default: Value of option 'rename'
--renameenumidents (boolean) default: Value of option 'rename'
--renameillegalidents (boolean) default: Value of option 'rename'
--renamesmallmembers (int >= 0) default: 0
--showinferrable (boolean) default: false if class file from version 51.0 (Java 7) or greater
--showversion (boolean) default: true
--silent (boolean) default: false
--skipbatchinnerclasses (boolean) default: true
--staticinitreturn (boolean) default: true
--stringbuffer (boolean) default: false if class file from version 49.0 (Java 5) or greater
--stringbuilder (boolean) default: true if class file from version 49.0 (Java 5) or greater
--stringconcat (boolean) default: true if class file from version 53.0 (Java 9) or greater
--sugarasserts (boolean) default: true
--sugarboxing (boolean) default: true
去除自动装箱和拆箱的语法糖 --sugarboxing false
--sugarenums (boolean) default: true if class file from version 49.0 (Java 5) or greater
去除枚举的语法糖 --sugarenums false
--switchexpression (boolean) default: true if class file from version 57.0 (Java 13) or greater, or experimental in 56.0 (Java 12)
--tidymonitors (boolean) default: true
--tryresources (boolean) default: true if class file from version 51.0 (Java 7) or greater
--usenametable (boolean) default: true
--usesignatures (boolean) default: true
--help (string)
也支持反编译整个 jar 包
java -jar cfr-0.150.jar test.jar --outputdir ./