Closed lehvolk closed 1 year ago
Hi,
yes, we support Java 11. Could you try again by adding these two things:
builder.addInputLocation(new JrtFileSystemAnalysisInputLocation());
identifierFactory.getClassType("java.base/java.lang.String");
But this requires running on Java 11, right? Otherwise JrtFileSystemAnalysisInputLocation
throws Exception.
I stuck a bit with second option but seems that code do a trick:
fun main() {
val javaHome = "C:\\Program Files\\ojdkbuild\\java-11-openjdk-11.0.15-1"
println(javaHome)
val classpath = System.getProperty("java.class.path")
val cp = classpath.split(File.pathSeparator.toRegex()).map { File(it) }
val builder: JavaProject.JavaProjectBuilder = JavaProject.builder(JavaLanguage(11))
allRuntimeLocations.forEach {
println(it.absolutePath)
builder.addInputLocation(JavaModulePathAnalysisInputLocation(it.absolutePath))
}
for (s in cp) {
if (s.exists()) {
builder.addInputLocation(JavaModulePathAnalysisInputLocation(s.absolutePath))
}
}
val javaProject: JavaProject = builder.build()
val view = javaProject.createFullView()
val identifierFactory = JavaModuleIdentifierFactory.getInstance()
val type = identifierFactory.getClassType("java.lang.String")
val sootClass = view.getClass(type)
println(sootClass.get().methods.first().body)
}
Because Java11 uses modules, the fully quailfied name of a class is defined by the module and the path. java.base is the module and java.lang the package. To get the correct ClassType, you have to use "java.base/java.lang.String"
But this requires running on Java 11, right? Otherwise
JrtFileSystemAnalysisInputLocation
throws Exception.
It needs at least Java9 where JavaModules was introduced (so we could make use of the java class library to access the modules etc.).
To get the correct ClassType, you have to use "java.base/java.lang.String"
@JonasKlauke It was my first variant but I got empty Optional for this code:
val identifierFactory = JavaModuleIdentifierFactory.getInstance()
val type = identifierFactory.getClassType("java.base/java.lang.String")
Here is a full gist for this https://gist.github.com/lehvolk/25e906a414bb5054df28bcb2f2152c2f
Then I removed "java.base/" and everything works. Current behavior contradicts your statement about modules names.
PS Just in case I'm running this code on Java 8
ok running from Java8 we cant access the module specific functionality. Without specifying a module signatue (like java.base/...) a usual (read: like <Java9) ClassType is created in the javaidentifierfactory. Every input that is not assigned to a module is collected into the unnamed module so it is accessed without the module package name in the signature.
Beware: It seems java.lang.String
comes from the rt.jar of your java8 which could have a different implementations to the java11 code you want to analyze
Is Java 11 supported?
I run this code under Java 11 and it's returned empty
Optional