Search in sources :

Example 6 with ClassFile

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;
}
Also used : ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) Module_attribute(org.eclipse.ceylon.langtools.classfile.Module_attribute)

Example 7 with ClassFile

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);
    }
}
Also used : ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ClassWriter(org.eclipse.ceylon.langtools.classfile.ClassWriter)

Example 8 with ClassFile

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);
    }
}
Also used : ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File) ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) ClassWriter(org.eclipse.ceylon.langtools.classfile.ClassWriter)

Example 9 with ClassFile

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;
}
Also used : ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) Module_attribute(org.eclipse.ceylon.langtools.classfile.Module_attribute)

Example 10 with ClassFile

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"));
}
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

ClassFile (org.eclipse.ceylon.langtools.classfile.ClassFile)12 Annotation (org.eclipse.ceylon.langtools.classfile.Annotation)4 Module_attribute (org.eclipse.ceylon.langtools.classfile.Module_attribute)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ZipEntry (java.util.zip.ZipEntry)3 ZipFile (java.util.zip.ZipFile)3 File (java.io.File)2 ModuleDependencyInfo (org.eclipse.ceylon.cmr.api.ModuleDependencyInfo)2 ClassWriter (org.eclipse.ceylon.langtools.classfile.ClassWriter)2 ConstantPoolException (org.eclipse.ceylon.langtools.classfile.ConstantPoolException)2 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ModuleInfo (org.eclipse.ceylon.cmr.api.ModuleInfo)1 ModuleVersionArtifact (org.eclipse.ceylon.cmr.api.ModuleVersionArtifact)1 ModuleVersionDetails (org.eclipse.ceylon.cmr.api.ModuleVersionDetails)1