< prev index next >
test/jdk/com/sun/tools/jextract/Runner.java
Print this page
*** 41,53 ****
--- 41,57 ----
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,107 ****
private InMemoryFileManager<StandardJavaFileManager> mfm;
private ClassLoader expectedCL;
private Map<String, byte[]> actualClz;
private ClassLoader actualCL;
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 {
if (!Files.isReadable(nativeSrc)) {
throw new IllegalArgumentException("Cannot read the file: " + nativeSrc);
}
Path p = nativeSrc.toAbsolutePath();
! ctx.usePackageForFolder(p.getParent(), pkg);
ctx.addSource(p);
! ctx.parse();
! return ctx.collectClasses(pkg);
}
private InMemoryFileManager<StandardJavaFileManager> compileJavaCode() {
JavaCompiler cl = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sfm = cl.getStandardFileManager(null, null, null);
--- 84,111 ----
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 Writer extract() throws IOException {
if (!Files.isReadable(nativeSrc)) {
throw new IllegalArgumentException("Cannot read the file: " + nativeSrc);
}
Path p = nativeSrc.toAbsolutePath();
! ctx.setTargetPackage(pkg);
ctx.addSource(p);
! return new JextractTool(ctx).processHeaders();
}
private InMemoryFileManager<StandardJavaFileManager> compileJavaCode() {
JavaCompiler cl = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sfm = cl.getStandardFileManager(null, null, null);
*** 114,124 ****
@Test
public void testJarManifest() throws IOException {
// Get the jar
ByteArrayOutputStream bos = new ByteArrayOutputStream();
! ctx.collectJarFile(new JarOutputStream(bos), new String[0], pkg);
System.out.println("Jar built, verifying...");
JarInputStream jis = new JarInputStream(new ByteArrayInputStream(bos.toByteArray()));
// List all classes in the jar
--- 118,128 ----
@Test
public void testJarManifest() throws IOException {
// Get the jar
ByteArrayOutputStream bos = new ByteArrayOutputStream();
! 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,306 ****
@BeforeClass
public void compile() throws IOException {
System.out.println("Compiling...");
mfm = compileJavaCode();
! actualClz = extract(pkg);
expectedCL = mfm.getTheClassLoader();
actualCL = new ClassLoader() {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
! byte[] byteCode = actualClz.get(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());
System.out.println("Compile result validated.");
}
private static Path[] paths(String testDir, String[] files) {
return Arrays.stream(files)
.map(f -> Paths.get(testDir, f))
.toArray(Path[]::new);
}
--- 286,322 ----
@BeforeClass
public void compile() throws IOException {
System.out.println("Compiling...");
mfm = compileJavaCode();
! writer = extract();
! actualClz = writer.results();
expectedCL = mfm.getTheClassLoader();
actualCL = new ClassLoader() {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
! 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().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 >