Search in sources :

Example 6 with ClassFile

use of com.sun.tools.classfile.ClassFile in project jdk8u_jdk by JetBrains.

the class CheckDeps method analyzeDependencies.

/**
     * Analyze the dependencies of all classes in the given JAR file. The
     * method updates knownTypes and unknownRefs as part of the analysis.
     */
static void analyzeDependencies(Path jarpath) throws Exception {
    System.out.format("Analyzing %s%n", jarpath);
    try (JarFile jf = new JarFile(jarpath.toFile())) {
        Enumeration<JarEntry> entries = jf.entries();
        while (entries.hasMoreElements()) {
            JarEntry e = entries.nextElement();
            String name = e.getName();
            if (name.endsWith(".class")) {
                ClassFile cf = ClassFile.read(jf.getInputStream(e));
                for (Dependency d : finder.findDependencies(cf)) {
                    String origin = toClassName(d.getOrigin().getName());
                    String target = toClassName(d.getTarget().getName());
                    // origin is now known
                    unknownRefs.remove(origin);
                    knownTypes.add(origin);
                    // if the target is not known then record the reference
                    if (!knownTypes.contains(target)) {
                        Set<String> refs = unknownRefs.get(target);
                        if (refs == null) {
                            // first time seeing this unknown type
                            refs = new HashSet<>();
                            unknownRefs.put(target, refs);
                        }
                        refs.add(origin);
                    }
                }
            }
        }
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) Dependency(com.sun.tools.classfile.Dependency) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 7 with ClassFile

use of com.sun.tools.classfile.ClassFile in project ceylon-compiler by ceylon.

the class OverrideBridge method compile.

static Map<ClassFile, List<Method>> compile(Implementation implB, Implementation implC, Implementation implD, String destPath) throws Exception {
    File destDir = new File(workDir, destPath);
    destDir.mkdir();
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavaSource source = new JavaSource(implB, implC, implD);
    JavacTask ct = (JavacTask) tool.getTask(null, null, null, Arrays.asList("-d", destPath), null, Arrays.asList(source));
    ct.generate();
    Map<ClassFile, List<Method>> members = new HashMap<>();
    addMembers(destDir, members);
    return members;
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) HashMap(java.util.HashMap) JavaCompiler(javax.tools.JavaCompiler) ArrayList(java.util.ArrayList) List(java.util.List) JavacTask(com.sun.source.util.JavacTask) ClassFile(com.sun.tools.classfile.ClassFile)

Example 8 with ClassFile

use of com.sun.tools.classfile.ClassFile in project jdk8u_jdk by JetBrains.

the class LambdaAsm method verifyASM.

static void verifyASM() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_8, ACC_PUBLIC, "X", null, "java/lang/Object", null);
    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "foo", "()V", null, null);
    mv.visitMaxs(2, 1);
    mv.visitMethodInsn(INVOKESTATIC, "java/util/function/Function.class", "identity", "()Ljava/util/function/Function;", true);
    mv.visitInsn(RETURN);
    cw.visitEnd();
    byte[] carray = cw.toByteArray();
    // for debugging
    // write((new File("X.class")).toPath(), carray, CREATE, TRUNCATE_EXISTING);
    // verify using javap/classfile reader
    ClassFile cf = ClassFile.read(new ByteArrayInputStream(carray));
    int mcount = checkMethod(cf, "foo");
    if (mcount < 1) {
        throw new RuntimeException("unexpected method count, expected 1" + "but got " + mcount);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter) MethodVisitor(jdk.internal.org.objectweb.asm.MethodVisitor)

Example 9 with ClassFile

use of com.sun.tools.classfile.ClassFile in project jdk8u_jdk by JetBrains.

the class LambdaAsm method verifyInvokerBytecodeGenerator.

static void verifyInvokerBytecodeGenerator() throws Exception {
    int count = 0;
    int mcount = 0;
    try (DirectoryStream<Path> ds = newDirectoryStream(new File(".").toPath(), // filter in lambda proxy classes
    "A$I$$Lambda$?.class")) {
        for (Path p : ds) {
            System.out.println(p.toFile());
            ClassFile cf = ClassFile.read(p.toFile());
            // Check those methods implementing Supplier.get
            mcount += checkMethod(cf, "get");
            count++;
        }
    }
    if (count < 3) {
        throw new RuntimeException("unexpected number of files, " + "expected atleast 3 files, but got only " + count);
    }
    if (mcount < 3) {
        throw new RuntimeException("unexpected number of methods, " + "expected atleast 3 methods, but got only " + mcount);
    }
}
Also used : Path(java.nio.file.Path) ClassFile(com.sun.tools.classfile.ClassFile) File(java.io.File) ClassFile(com.sun.tools.classfile.ClassFile)

Example 10 with ClassFile

use of com.sun.tools.classfile.ClassFile in project jdk8u_jdk by JetBrains.

the class AnnotationsElementVisitor method readFrom.

public Element readFrom(InputStream in) throws IOException {
    try {
        this.in = in;
        ClassFile c = ClassFile.read(in);
        // read the file header
        if (c.magic != 0xCAFEBABE) {
            throw new RuntimeException("bad magic number " + Integer.toHexString(c.magic));
        }
        cfile.setAttr("magic", "" + c.magic);
        int minver = c.minor_version;
        int majver = c.major_version;
        cfile.setAttr("minver", "" + minver);
        cfile.setAttr("majver", "" + majver);
        readCP(c);
        readClass(c);
        return result();
    } catch (InvalidDescriptor | ConstantPoolException ex) {
        throw new IOException("Fatal error", ex);
    }
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) ConstantPoolException(com.sun.tools.classfile.ConstantPoolException) InvalidDescriptor(com.sun.tools.classfile.Descriptor.InvalidDescriptor)

Aggregations

ClassFile (com.sun.tools.classfile.ClassFile)16 Method (com.sun.tools.classfile.Method)8 Code_attribute (com.sun.tools.classfile.Code_attribute)5 ConstantPoolException (com.sun.tools.classfile.ConstantPoolException)3 Instruction (com.sun.tools.classfile.Instruction)3 File (java.io.File)3 InputStream (java.io.InputStream)3 ConstantPool (com.sun.tools.classfile.ConstantPool)2 Path (java.nio.file.Path)2 JavacTask (com.sun.source.util.JavacTask)1 AccessFlags (com.sun.tools.classfile.AccessFlags)1 Attributes (com.sun.tools.classfile.Attributes)1 ClassWriter (com.sun.tools.classfile.ClassWriter)1 Exception_data (com.sun.tools.classfile.Code_attribute.Exception_data)1 Dependency (com.sun.tools.classfile.Dependency)1 InvalidDescriptor (com.sun.tools.classfile.Descriptor.InvalidDescriptor)1 Field (com.sun.tools.classfile.Field)1 LocalVariableTypeTable_attribute (com.sun.tools.classfile.LocalVariableTypeTable_attribute)1 SourceFile_attribute (com.sun.tools.classfile.SourceFile_attribute)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1