Search in sources :

Example 1 with ClassFile

use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.

the class BytecodeUtils method matchesModuleInfo.

public boolean matchesModuleInfo(String moduleName, String moduleVersion, File moduleArchive, String query, Overrides overrides) {
    ClassFile moduleInfo = readModuleInfo(moduleName, moduleArchive);
    if (moduleInfo == null)
        return false;
    Annotation moduleAnnotation = ClassFileUtil.findAnnotation(moduleInfo, MODULE_ANNOTATION);
    if (moduleAnnotation == null)
        return false;
    String groupId, artifactId;
    groupId = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "group");
    if (groupId == null || groupId.isEmpty()) {
        String[] coordinates = ModuleUtil.getMavenCoordinates(moduleName);
        groupId = coordinates[0];
        artifactId = coordinates[1];
    } else {
        artifactId = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "artifact");
        if (artifactId == null || artifactId.isEmpty())
            artifactId = moduleName;
    }
    String version = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "version");
    if (version == null)
        return false;
    String doc = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "doc");
    if (doc != null && matches(doc, query))
        return true;
    String label = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "label");
    if (label != null && matches(label, query))
        return true;
    String license = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "license");
    if (license != null && matches(license, query))
        return true;
    Object[] by = (Object[]) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "by");
    if (by != null) {
        for (Object author : by) {
            if (matches((String) author, query))
                return true;
        }
    }
    Object[] dependencies = (Object[]) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "dependencies");
    if (dependencies != null) {
        for (ModuleDependencyInfo dep : getDependencies(moduleInfo, dependencies, moduleName, version, groupId, artifactId, overrides)) {
            if (matches(dep.getModuleName(), query))
                return true;
        }
    }
    return false;
}
Also used : ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) ModuleDependencyInfo(org.eclipse.ceylon.cmr.api.ModuleDependencyInfo) Annotation(org.eclipse.ceylon.langtools.classfile.Annotation)

Example 2 with ClassFile

use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.

the class BytecodeUtils method readClassFiles.

private static List<ClassFile> readClassFiles(final File jarFile) {
    try {
        try (JarFile jar = new JarFile(jarFile)) {
            List<ClassFile> ret = new ArrayList<ClassFile>(jar.size());
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                String name = entry.getName().toLowerCase();
                if (name.endsWith(".class")) {
                    try (InputStream stream = jar.getInputStream(entry)) {
                        try {
                            ret.add(ClassFile.read(stream));
                        } catch (ConstantPoolException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
            return ret;
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to read class file for module " + jarFile.getPath(), e);
    }
}
Also used : ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ConstantPoolException(org.eclipse.ceylon.langtools.classfile.ConstantPoolException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 3 with ClassFile

use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.

the class ClassFileScanner method scan.

public void scan(ModuleInfo moduleInfo) throws IOException {
    PathFilter pathFilter = null;
    if (moduleInfo != null && moduleInfo.getFilter() != null) {
        pathFilter = PathFilterParser.parse(moduleInfo.getFilter());
    }
    try (ZipFile zf = new ZipFile(jarFile)) {
        Enumeration<? extends ZipEntry> entries = zf.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory() || !entry.getName().toLowerCase().endsWith(".class"))
                continue;
            if (pathFilter != null && !pathFilter.accept(entry.getName()))
                continue;
            try (InputStream is = zf.getInputStream(entry)) {
                try {
                    ClassFile classFile = ClassFile.read(is);
                    isPublicApi = false;
                    checkPublicApi(classFile);
                } catch (ConstantPoolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvalidDescriptor e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
Also used : PathFilter(org.eclipse.ceylon.model.cmr.PathFilter) ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ConstantPoolException(org.eclipse.ceylon.langtools.classfile.ConstantPoolException) InvalidDescriptor(org.eclipse.ceylon.langtools.classfile.Descriptor.InvalidDescriptor)

Example 4 with ClassFile

use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.

the class Java9Util method generateModuleDescriptor.

private static ClassFile generateModuleDescriptor(Java9ModuleDescriptor module) {
    CPInfo[] pool = new ConstantPool.CPInfo[1 + 7 + (module.imports.size() * 2) + (module.getPackagesSize() * 2) + (module.main != null ? 3 : 0)];
    ConstantPool constantPool = new ConstantPool(pool);
    int cp = 1;
    // 1: this_class name
    pool[cp++] = new ConstantPool.CONSTANT_Utf8_info("module-info");
    // 2: this_class
    pool[cp++] = new ConstantPool.CONSTANT_Class_info(constantPool, 1);
    // 3: module attr
    pool[cp++] = new ConstantPool.CONSTANT_Utf8_info(Attribute.Module);
    // 4: module pkgs attr
    pool[cp++] = new ConstantPool.CONSTANT_Utf8_info(Attribute.ModulePackages);
    // 5: version
    pool[cp++] = new ConstantPool.CONSTANT_Utf8_info(module.version);
    // 6: module name
    pool[cp++] = new ConstantPool.CONSTANT_Utf8_info(module.name);
    // 7: module info
    pool[cp++] = new ConstantPool.CONSTANT_Module_info(constantPool, 6);
    if (module.main != null) {
        // 8: main attr
        pool[cp++] = new ConstantPool.CONSTANT_Utf8_info(Attribute.ModuleMainClass);
        // 9: main class name
        pool[cp++] = new ConstantPool.CONSTANT_Utf8_info(module.main.replace('.', '/'));
        // 10: main class
        pool[cp++] = new ConstantPool.CONSTANT_Class_info(constantPool, 9);
    }
    int i = 0;
    // now imports
    Module_attribute.RequiresEntry[] requires = new Module_attribute.RequiresEntry[module.imports.size()];
    for (Java9ModuleImport imp : module.imports) {
        pool[cp] = new ConstantPool.CONSTANT_Utf8_info(imp.name);
        pool[cp + 1] = new ConstantPool.CONSTANT_Module_info(constantPool, cp);
        int flag = 0;
        if (imp.exported)
            flag |= Module_attribute.ACC_TRANSITIVE;
        // FIXME: add version info!
        requires[i] = new Module_attribute.RequiresEntry(cp + 1, flag, 0);
        i++;
        cp += 2;
    }
    Module_attribute.ExportsEntry[] exports = new Module_attribute.ExportsEntry[module.exportedPackages.size()];
    i = 0;
    int[] modulePackages = new int[module.exportedPackages.size() + module.concealedPackages.size()];
    int m = 0;
    for (String pkg : module.exportedPackages) {
        pool[cp] = new ConstantPool.CONSTANT_Utf8_info(pkg.replace('.', '/'));
        pool[cp + 1] = new ConstantPool.CONSTANT_Package_info(constantPool, cp);
        exports[i++] = new Module_attribute.ExportsEntry(cp + 1, 0, new int[0]);
        modulePackages[m++] = cp + 1;
        cp += 2;
    }
    for (String pkg : module.concealedPackages) {
        pool[cp] = new ConstantPool.CONSTANT_Utf8_info(pkg.replace('.', '/'));
        pool[cp + 1] = new ConstantPool.CONSTANT_Package_info(constantPool, cp);
        modulePackages[m++] = cp + 1;
        cp += 2;
    }
    Attribute[] attributesArray = new Attribute[2 + (module.main != null ? 1 : 0)];
    attributesArray[0] = new Module_attribute(3, // module name index
    7, // flags
    0, // version
    5, requires, exports, new Module_attribute.OpensEntry[0], new int[0], new Module_attribute.ProvidesEntry[0]);
    attributesArray[1] = new ModulePackages_attribute(4, modulePackages);
    if (module.main != null)
        attributesArray[2] = new ModuleMainClass_attribute(8, 10);
    Attributes attributes = new Attributes(constantPool, attributesArray);
    return new ClassFile(org.eclipse.ceylon.langtools.tools.javac.jvm.ClassFile.JAVA_MAGIC, org.eclipse.ceylon.langtools.tools.javac.jvm.ClassFile.Version.V53.minor, org.eclipse.ceylon.langtools.tools.javac.jvm.ClassFile.Version.V53.major, constantPool, new org.eclipse.ceylon.langtools.classfile.AccessFlags(org.eclipse.ceylon.langtools.classfile.AccessFlags.ACC_MODULE), 2, 0, new int[0], new org.eclipse.ceylon.langtools.classfile.Field[0], new org.eclipse.ceylon.langtools.classfile.Method[0], attributes);
}
Also used : Attribute(org.eclipse.ceylon.langtools.classfile.Attribute) Attributes(org.eclipse.ceylon.langtools.classfile.Attributes) Module_attribute(org.eclipse.ceylon.langtools.classfile.Module_attribute) ModuleMainClass_attribute(org.eclipse.ceylon.langtools.classfile.ModuleMainClass_attribute) ClassFile(org.eclipse.ceylon.langtools.classfile.ClassFile) CPInfo(org.eclipse.ceylon.langtools.classfile.ConstantPool.CPInfo) ConstantPool(org.eclipse.ceylon.langtools.classfile.ConstantPool) ModulePackages_attribute(org.eclipse.ceylon.langtools.classfile.ModulePackages_attribute)

Example 5 with ClassFile

use of org.eclipse.ceylon.langtools.classfile.ClassFile in project ceylon by eclipse.

the class Java9ModuleReader method getJava9Module.

public static Java9Module getJava9Module(File jar) {
    ClassFile classFile = getClassFile(jar);
    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)

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