use of org.eclipse.ceylon.langtools.classfile.RuntimeVisibleAnnotations_attribute 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