其他
当老板让我从 Java8 升到 Java11
The following article is from 低并发编程 Author 闪客
来源丨低并发编程(ID:dibingfa)
它是什么
它怎么用
emt4j-0.3
├── bin
│ ├── analysis.bat
│ └── analysis.sh
└── lib
├── agent
│ ├── emt4j-agent-jdk11-0.3.jar
│ └── emt4j-agent-jdk8-0.3.jar
├── analysis
│ ├── asm-9.2.jar
│ ├── commons-io-2.4.jar
│ ├── commons-lang3-3.8.jar
│ ├── emt4j-analysis-0.3.jar
│ ├── emt4j-common-0.3.jar
│ ├── gson-2.9.0.jar
│ ├── lombok-1.18.8.jar
│ ├── mvel2-2.4.12.Final.jar
│ ├── slf4j-api-1.7.30.jar
│ └── velocity-engine-core-2.3.jar
└── maven-plugin
└── emt4j-maven-plugin-0.3.jar
public static void main( String[] args ) {
System.out.println( "Hello World!" );
}
}
拿一个复杂的项目举例
private static boolean isPreJava8() {
String version = System.getProperty("java.version");
String[] parts = version.split("\\.");
try {
int major = Integer.parseInt(parts[1]);
return major < 8;
} catch (Exception var3) {
return true;
}
}
什么原理
public static void main(String[] var0) {
System.out.println("hello");
System.out.println(System.getProperty("java.version"));
}
}
sh analysis.sh -f 8 -t 11 -o report.html classFiles
// org/eclipse/emt4j/analysis/AnalysisMain.java
public class AnalysisMain {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
...
ReportConfig config = doAnalysis(args, ...);
doReport(config, ...);
}
...
}
// org/eclipse/emt4j/analysis/analyzer/ClassAnalyzer.java
protected static void processClass(byte[] classFileContent ...) {
ClassSymbol symbol = ClassInspectorInstance.getInstance().getSymbolInClass(classFileContent);
...
}
@RuleImpl(type = "whole-class")
public class WholeClassRule extends ExecutableRule {
...
@Override
protected CheckResult check(Dependency dependency) {
Map<String, Object> mvelMap = new HashMap<>();
mvelMap.put("typeSet", dependency.getClassSymbol().getTypeSet());
mvelMap.put("methodSet", toMethodIdentifierSet(dependency.getClassSymbol().getCallMethodSet()));
mvelMap.put("cpSet", dependency.getClassSymbol().getConstantPoolSet());
Object result = MVEL.eval(mvel2Rule, mvelMap);
if (result instanceof Boolean) {
return ((Boolean) result) ? CheckResult.FAIL : CheckResult.PASS;
} else {
throw new JdkMigrationException("Mvel2 rule file" + mvel2RuleFile + " must return a boolean result!Now result type is : " + result.getClass());
}
}
}
// emt4j-common/src/main/resources/default/rule/8to11/data/mvel2-rule-getjavaversion.cfg
methodSet.contains('java.lang.System.getProperty') &&
(
cpSet.contains('java.version') ||
cpSet.contains('java.specification.version') ||
cpSet.contains('java.runtime.version')
)
再说两句
【☝🏼点击查看更多详情】