Search in sources :

Example 11 with ClassReader

use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.

the class OutputFilesSink method save.

public void save(@NotNull final OutputFileObject fileObject) {
    final BinaryContent content = fileObject.getContent();
    final File srcFile = fileObject.getSourceFile();
    boolean isTemp = false;
    final JavaFileObject.Kind outKind = fileObject.getKind();
    if (srcFile != null && content != null) {
        final String sourcePath = FileUtil.toSystemIndependentName(srcFile.getPath());
        final JavaSourceRootDescriptor rootDescriptor = myContext.getProjectDescriptor().getBuildRootIndex().findJavaRootDescriptor(myContext, srcFile);
        try {
            if (rootDescriptor != null) {
                isTemp = rootDescriptor.isTemp;
                if (!isTemp) {
                    // first, handle [src->output] mapping and register paths for files_generated event
                    if (outKind == JavaFileObject.Kind.CLASS) {
                        // todo: avoid array copying?
                        myOutputConsumer.registerCompiledClass(rootDescriptor.target, new CompiledClass(fileObject.getFile(), srcFile, fileObject.getClassName(), content));
                    } else {
                        myOutputConsumer.registerOutputFile(rootDescriptor.target, fileObject.getFile(), Collections.<String>singleton(sourcePath));
                    }
                }
            } else {
                // was not able to determine the source root descriptor or the source root is excluded from compilation (e.g. for annotation processors)
                if (outKind == JavaFileObject.Kind.CLASS) {
                    myOutputConsumer.registerCompiledClass(null, new CompiledClass(fileObject.getFile(), srcFile, fileObject.getClassName(), content));
                }
            }
        } catch (IOException e) {
            myContext.processMessage(new CompilerMessage(JavaBuilder.BUILDER_NAME, e));
        }
        if (!isTemp && outKind == JavaFileObject.Kind.CLASS) {
            // register in mappings any non-temp class file
            try {
                final ClassReader reader = new FailSafeClassReader(content.getBuffer(), content.getOffset(), content.getLength());
                myMappingsCallback.associate(FileUtil.toSystemIndependentName(fileObject.getFile().getPath()), sourcePath, reader);
            } catch (Throwable e) {
                // need this to make sure that unexpected errors in, for example, ASM will not ruin the compilation  
                final String message = "Class dependency information may be incomplete! Error parsing generated class " + fileObject.getFile().getPath();
                LOG.info(message, e);
                myContext.processMessage(new CompilerMessage(JavaBuilder.BUILDER_NAME, BuildMessage.Kind.WARNING, message + "\n" + CompilerMessage.getTextFromThrowable(e), sourcePath));
            }
        }
    }
    if (outKind == JavaFileObject.Kind.CLASS) {
        myContext.processMessage(new ProgressMessage("Writing classes... " + myChunkName));
        if (!isTemp && srcFile != null) {
            mySuccessfullyCompiled.add(srcFile);
        }
    }
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) CompiledClass(org.jetbrains.jps.incremental.CompiledClass) IOException(java.io.IOException) BinaryContent(org.jetbrains.jps.incremental.BinaryContent) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) JavaSourceRootDescriptor(org.jetbrains.jps.builders.java.JavaSourceRootDescriptor) File(java.io.File) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader)

Example 12 with ClassReader

use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.

the class CompilingEvaluator method changeSuperToMagicAccessor.

private static byte[] changeSuperToMagicAccessor(byte[] bytes) {
    ClassWriter classWriter = new ClassWriter(0);
    ClassVisitor classVisitor = new ClassVisitor(Opcodes.API_VERSION, classWriter) {

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            if ("java/lang/Object".equals(superName)) {
                superName = "sun/reflect/MagicAccessorImpl";
            }
            super.visit(version, access, name, signature, superName, interfaces);
        }
    };
    new ClassReader(bytes).accept(classVisitor, 0);
    return classWriter.toByteArray();
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter)

Example 13 with ClassReader

use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.

the class BytecodeAnalysisIndex method collectKeys.

@NotNull
private static Map<Bytes, Void> collectKeys(byte[] content) throws NoSuchAlgorithmException {
    HashMap<Bytes, Void> map = new HashMap<>();
    MessageDigest md = BytecodeAnalysisConverter.getMessageDigest();
    new ClassReader(content).accept(new KeyedMethodVisitor() {

        @Nullable
        @Override
        MethodVisitor visitMethod(MethodNode node, Key key) {
            map.put(ClassDataIndexer.compressKey(md, key), null);
            return null;
        }
    }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    return map;
}
Also used : MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) MessageDigest(java.security.MessageDigest) Nullable(org.jetbrains.annotations.Nullable) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with ClassReader

use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.

the class ClassFileViewProvider method detectInnerClass.

private static boolean detectInnerClass(VirtualFile file, @Nullable byte[] content) {
    String name = file.getNameWithoutExtension();
    int p = name.lastIndexOf('$', name.length() - 2);
    if (p <= 0)
        return false;
    Boolean isInner = IS_INNER_CLASS.get(file);
    if (isInner != null)
        return isInner;
    if (content == null) {
        try {
            content = file.contentsToByteArray(false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    ClassReader reader = new ClassReader(content);
    final Ref<Boolean> ref = Ref.create(Boolean.FALSE);
    final String className = reader.getClassName();
    reader.accept(new ClassVisitor(Opcodes.API_VERSION) {

        @Override
        public void visitOuterClass(String owner, String name, String desc) {
            ref.set(Boolean.TRUE);
        }

        @Override
        public void visitInnerClass(String name, String outer, String inner, int access) {
            if (className.equals(name)) {
                ref.set(Boolean.TRUE);
            }
        }
    }, EMPTY_ATTRIBUTES, ClassReader.SKIP_DEBUG | ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
    isInner = ref.get();
    IS_INNER_CLASS.set(file, isInner);
    return isInner;
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) IOException(java.io.IOException) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor)

Example 15 with ClassReader

use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.

the class ClsMirrorBuildingTest method testStrayInnersFiltering.

public void testStrayInnersFiltering() throws IOException {
    String path = JavaTestUtil.getJavaTestDataPath() + "/../../mockJDK-1.8/jre/lib/rt.jar!/java/lang/Class.class";
    VirtualFile file = StandardFileSystems.jar().findFileByPath(path);
    assertNotNull(path, file);
    InnerClassSourceStrategy<VirtualFile> strategy = new InnerClassSourceStrategy<VirtualFile>() {

        @Override
        public VirtualFile findInnerClass(String innerName, VirtualFile outerClass) {
            String baseName = outerClass.getNameWithoutExtension();
            VirtualFile child = outerClass.getParent().findChild(baseName + "$" + innerName + ".class");
            // stray inner classes should be filtered out
            assert child != null : innerName + " is not an inner class of " + outerClass;
            return child;
        }

        @Override
        public void accept(VirtualFile innerClass, StubBuildingVisitor<VirtualFile> visitor) {
            try {
                byte[] bytes = innerClass.contentsToByteArray();
                new ClassReader(bytes).accept(visitor, ClassReader.SKIP_FRAMES);
            } catch (IOException ignored) {
            }
        }
    };
    PsiJavaFileStubImpl stub = new PsiJavaFileStubImpl("java.lang", true);
    StubBuildingVisitor<VirtualFile> visitor = new StubBuildingVisitor<>(file, strategy, stub, 0, null);
    new ClassReader(file.contentsToByteArray()).accept(visitor, ClassReader.SKIP_FRAMES);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiJavaFileStubImpl(com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) InnerClassSourceStrategy(com.intellij.psi.impl.compiled.InnerClassSourceStrategy) IOException(java.io.IOException) StubBuildingVisitor(com.intellij.psi.impl.compiled.StubBuildingVisitor)

Aggregations

ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)30 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)12 File (java.io.File)6 IOException (java.io.IOException)6 OutputFile (org.jetbrains.kotlin.backend.common.output.OutputFile)5 ClassWriter (org.jetbrains.org.objectweb.asm.ClassWriter)4 FailSafeClassReader (com.intellij.compiler.instrumentation.FailSafeClassReader)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)3 ReadKotlinClassHeaderAnnotationVisitor (org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor)3 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)3 ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)3 InstrumenterClassWriter (com.intellij.compiler.instrumentation.InstrumenterClassWriter)2 PsiJavaFileStubImpl (com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl)2 SourceLineCounter (com.intellij.rt.coverage.instrumentation.SourceLineCounter)2 PrintWriter (java.io.PrintWriter)2 ProgressMessage (org.jetbrains.jps.incremental.messages.ProgressMessage)2 OutputFileCollection (org.jetbrains.kotlin.backend.common.output.OutputFileCollection)2 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)2