use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.
the class Java9ModuleReader method getJava9Module.
public static Java9Module getJava9Module(InputStream is) {
ClassFile classFile = getClassFile(is);
if (classFile == null)
return null;
Module_attribute moduleAttribute = (Module_attribute) classFile.getAttribute(Attribute.Module);
if (moduleAttribute != null) {
return new Java9Module(moduleAttribute, classFile);
}
return null;
}
use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.
the class Java9Util method writeModuleDescriptor.
private static void writeModuleDescriptor(ZipOutputStream zos, Java9ModuleDescriptor module) {
ClassWriter classWriter = new ClassWriter();
ClassFile classFile = generateModuleDescriptor(module);
try {
zos.putNextEntry(new ZipEntry("module-info.class"));
classWriter.write(classFile, zos);
zos.flush();
zos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.
the class Java9Util method writeModuleDescriptor.
public static void writeModuleDescriptor(File outputFolder, Java9ModuleDescriptor module) {
ClassWriter classWriter = new ClassWriter();
ClassFile classFile = generateModuleDescriptor(module);
try (OutputStream os = new FileOutputStream(new File(outputFolder, "module-info.class"))) {
classWriter.write(classFile, os);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.
the class Java9ModuleReader method getJava9Module.
public static Java9Module getJava9Module(ZipFile zipFile, ZipEntry entry) {
ClassFile classFile = getClassFile(zipFile, entry);
if (classFile == null)
return null;
Module_attribute moduleAttribute = (Module_attribute) classFile.getAttribute(Attribute.Module);
if (moduleAttribute != null) {
return new Java9Module(moduleAttribute, classFile);
}
return null;
}
use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.
the class ClassFileUtilTest method test.
@Test
public void test() throws IOException, ConstantPoolException {
String location = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File(location);
String classPath = ClassFileUtilTest.class.getName().replace('.', '/') + ".class";
ClassFile classFile;
if (f.isDirectory()) {
// folder
File klassFile = new File(f, classPath);
classFile = ClassFile.read(klassFile);
} else {
// zip
try (ZipFile zf = new ZipFile(f)) {
ZipEntry entry = zf.getEntry(classPath);
try (InputStream is = zf.getInputStream(entry)) {
classFile = ClassFile.read(is);
}
}
}
RuntimeVisibleAnnotations_attribute attribute = (RuntimeVisibleAnnotations_attribute) classFile.getAttribute(Attribute.RuntimeVisibleAnnotations);
Annotation annotation = ClassFileUtil.findAnnotation(classFile, attribute, TestInterface.class);
Assert.assertNotNull(annotation);
Assert.assertEquals(true, ClassFileUtil.getAnnotationValue(classFile, annotation, "bool"));
Assert.assertEquals((byte) 1, ClassFileUtil.getAnnotationValue(classFile, annotation, "b"));
Assert.assertEquals((short) 2, ClassFileUtil.getAnnotationValue(classFile, annotation, "s"));
Assert.assertEquals((int) 3, ClassFileUtil.getAnnotationValue(classFile, annotation, "i"));
Assert.assertEquals((long) 4, ClassFileUtil.getAnnotationValue(classFile, annotation, "l"));
Assert.assertEquals('a', ClassFileUtil.getAnnotationValue(classFile, annotation, "c"));
Assert.assertEquals((float) 5.0, ClassFileUtil.getAnnotationValue(classFile, annotation, "f"));
Assert.assertEquals((double) 6.0, ClassFileUtil.getAnnotationValue(classFile, annotation, "d"));
Assert.assertEquals("str", ClassFileUtil.getAnnotationValue(classFile, annotation, "string"));
Assert.assertArrayEquals(new Object[] { "a", "b" }, (Object[]) ClassFileUtil.getAnnotationValue(classFile, annotation, "array"));
Annotation subAnnotation = (Annotation) ClassFileUtil.getAnnotationValue(classFile, annotation, "annot");
Assert.assertNotNull(subAnnotation);
Assert.assertEquals("annot", ClassFileUtil.getAnnotationValue(classFile, subAnnotation, "val"));
}
Aggregations