Skip to content

Commit

Permalink
* added support for JDK9 runtime, changes picked up from [Soot-j9 bra…
Browse files Browse the repository at this point in the history
…nch](https://github.com/sable/soot/tree/java9) of soot repo. implemented `java >= 9 && USE_CLASSPATH` case.  Check their [readme](https://github.com/sable/soot/tree/java9)
  • Loading branch information
dkimitsa committed Jul 18, 2019
1 parent a0ffbeb commit 6fa8247
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -10,7 +10,7 @@
</parent>

<groupId>com.mobidevelop.robovm</groupId>
<version>2.5.0-6</version>
<version>2.5.0-7</version>
<artifactId>robovm-soot</artifactId>
<name>Soot</name>
<packaging>jar</packaging>
Expand Down
61 changes: 61 additions & 0 deletions src/soot/CoffiJava9ClassProvider.java
@@ -0,0 +1,61 @@
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 2014 Raja Vallee-Rai and others
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package soot;

import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Objectweb ASM class provider.
*
* @author Andreas Dann
*/
public class CoffiJava9ClassProvider implements ClassProvider {

public ClassSource find(String cls) {
String clsFile = cls.replace('.', '/') + ".class";
SourceLocator.FoundFile file = null;
// here we go through all modules, since we are in classpath mode

Path p = Paths.get(URI.create("jrt:/modules"));
try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) {
for (Path entry : stream) {
// check each module folder for the class
file = SourceLocator.v().lookUpInVirtualFileSystem(entry.toUri().toString(), clsFile);
if (file != null) {
break;
}
}
} catch (FileSystemNotFoundException ex) {
System.out.println("Could not read my modules (perhaps not Java 9?).");
} catch (IOException e) {
e.printStackTrace();
}
return file == null ? null : new CoffiClassSource(cls, file.inputStream());

}
}
47 changes: 42 additions & 5 deletions src/soot/SourceLocator.java
Expand Up @@ -20,9 +20,12 @@
package soot;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
Expand All @@ -44,6 +47,9 @@ public class SourceLocator
public SourceLocator( Singletons.Global g ) {}
public static SourceLocator v() { return G.v().soot_SourceLocator(); }

public static final String DUMMY_CLASSPATH_JDK9_FS = "VIRTUAL_FS_FOR_JDK";
private boolean java9Mode = false;

protected Set<ClassLoader> additionalClassLoaders = new HashSet<ClassLoader>();
protected Set<String> classesToLoad;

Expand Down Expand Up @@ -97,7 +103,10 @@ public void additionalClassLoader(ClassLoader c) {

private void setupClassProviders() {
classProviders = new LinkedList<ClassProvider>();
classProviders.add(new CoffiClassProvider());
classProviders.add(new CoffiClassProvider());
if (this.java9Mode) {
classProviders.add(new CoffiJava9ClassProvider());
}
}

private List<ClassProvider> classProviders;
Expand Down Expand Up @@ -359,6 +368,13 @@ protected List<String> explodeClassPath( String classPath ) {
String canonicalDir;
try {
canonicalDir = new File(originalDir).getCanonicalPath();
// FIXME: make this nice in the future
// currently, we do not add it to NOT break backward compatibility
// instead, we add the CoffiJava9ClassProvider in setupClassProvider()
if (originalDir.equals(DUMMY_CLASSPATH_JDK9_FS)) {
SourceLocator.v().java9Mode = true;
continue;
}
ret.add(canonicalDir);
} catch( IOException e ) {
throw new CompilationDeathException( "Couldn't resolve classpath entry "+originalDir+": "+e );
Expand All @@ -372,14 +388,17 @@ public static class FoundFile {
this.entry = entry;
}
FoundFile( File file ) {
this.file = file;
this.path = file.toPath();
}
FoundFile( Path path ) {
this.path = path;
}
public File file;
public Path path;
public ZipFile zipFile;
public ZipEntry entry;
public InputStream inputStream() {
try {
if( file != null ) return new FileInputStream(file);
if( path != null ) return Files.newInputStream(path);
return doJDKBugWorkaround(zipFile.getInputStream(entry),
entry.getSize());
} catch( IOException e ) {
Expand Down Expand Up @@ -436,6 +455,24 @@ private FoundFile lookupInJar(String jar, String fileName) {
throw new RuntimeException( "Caught IOException "+e+" looking in jar file "+jar+" for file "+fileName );
}
}
/**
* Looks up classes in Java 9's virtual filesystem jrt:/
*
* @param archivePath
* path to the filesystem
* @param fileName
* the file to search
* @return the FoundFile
*/
public FoundFile lookUpInVirtualFileSystem(String archivePath, String fileName) {
// FileSystem fs = FileSystems.getFileSystem(URI.create(archivePath));
Path foundFile = Paths.get(URI.create(archivePath)).resolve(fileName);
if (foundFile != null && Files.isRegularFile(foundFile)) {
return new FoundFile(foundFile);
}

return null;
}
private HashMap<String, String> sourceToClassMap;

public HashMap<String, String> getSourceToClassMap(){
Expand Down

0 comments on commit 6fa8247

Please sign in to comment.