< prev index next >
test/jdk/com/sun/tools/jextract/Runner.java
Print this page
@@ -41,13 +41,17 @@
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
+import java.util.stream.Collectors;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
+
+import com.sun.tools.jextract.JextractTool;
+import com.sun.tools.jextract.Writer;
import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.tree.ClassNode;
import jdk.internal.org.objectweb.asm.util.ASMifier;
import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
import jdk.internal.org.objectweb.asm.util.Textifier;
@@ -80,28 +84,28 @@
private InMemoryFileManager<StandardJavaFileManager> mfm;
private ClassLoader expectedCL;
private Map<String, byte[]> actualClz;
private ClassLoader actualCL;
+ private Writer writer;
private Object[][] clz_data;
public Runner(Path nativeSrc, String pkg, Path[] javaSrcFiles) {
this.ctx = new Context();
this.nativeSrc = nativeSrc;
this.pkg = pkg;
this.javaSrcFiles = javaSrcFiles;
}
- private Map<String, byte[]> extract(String pkg) throws IOException {
+ private Writer extract() throws IOException {
if (!Files.isReadable(nativeSrc)) {
throw new IllegalArgumentException("Cannot read the file: " + nativeSrc);
}
Path p = nativeSrc.toAbsolutePath();
- ctx.usePackageForFolder(p.getParent(), pkg);
+ ctx.setTargetPackage(pkg);
ctx.addSource(p);
- ctx.parse();
- return ctx.collectClasses(pkg);
+ return new JextractTool(ctx).processHeaders();
}
private InMemoryFileManager<StandardJavaFileManager> compileJavaCode() {
JavaCompiler cl = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sfm = cl.getStandardFileManager(null, null, null);
@@ -114,11 +118,11 @@
@Test
public void testJarManifest() throws IOException {
// Get the jar
ByteArrayOutputStream bos = new ByteArrayOutputStream();
- ctx.collectJarFile(new JarOutputStream(bos), new String[0], pkg);
+ writer.writeJarFile(new JarOutputStream(bos), new String[0]);
System.out.println("Jar built, verifying...");
JarInputStream jis = new JarInputStream(new ByteArrayInputStream(bos.toByteArray()));
// List all classes in the jar
@@ -282,25 +286,37 @@
@BeforeClass
public void compile() throws IOException {
System.out.println("Compiling...");
mfm = compileJavaCode();
- actualClz = extract(pkg);
+ writer = extract();
+ actualClz = writer.results();
expectedCL = mfm.getTheClassLoader();
actualCL = new ClassLoader() {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
- byte[] byteCode = actualClz.get(name);
+ byte[] byteCode = actualClz.get(canonicalize(name));
if (byteCode == null) throw new ClassNotFoundException(name);
return defineClass(name, byteCode, 0, byteCode.length);
}
};
System.out.println("Done compile, ready for test");
- assertEquals(actualClz.keySet(), mfm.listClasses());
+ assertEquals(actualClz.keySet().stream()
+ .map(Runner::normalize)
+ .collect(Collectors.toSet()),
+ mfm.listClasses());
System.out.println("Compile result validated.");
}
+ private static String normalize(String classname) {
+ return classname.replace(File.separatorChar, '.');
+ }
+
+ private static String canonicalize(String classname) {
+ return classname.replace('.', File.separatorChar);
+ }
+
private static Path[] paths(String testDir, String[] files) {
return Arrays.stream(files)
.map(f -> Paths.get(testDir, f))
.toArray(Path[]::new);
}
< prev index next >