Search in sources :

Example 1 with RuntimeVisibleAnnotations_attribute

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"));
}
Also used : ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) ZipFile(java.util.zip.ZipFile) RuntimeVisibleAnnotations_attribute(org.eclipse.ceylon.langtools.classfile.RuntimeVisibleAnnotations_attribute) Annotation(org.eclipse.ceylon.langtools.classfile.Annotation) Test(org.junit.Test)

Aggregations

File (java.io.File)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 Annotation (org.eclipse.ceylon.langtools.classfile.Annotation)1 ClassFile (org.eclipse.ceylon.langtools.classfile.ClassFile)1 RuntimeVisibleAnnotations_attribute (org.eclipse.ceylon.langtools.classfile.RuntimeVisibleAnnotations_attribute)1 Test (org.junit.Test)1