Search in sources :

Example 16 with ClassReader

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

the class SourceLineCounterUtil method collectNonCoveredClassInfo.

public static boolean collectNonCoveredClassInfo(final PackageAnnotator.ClassCoverageInfo classCoverageInfo, final PackageAnnotator.PackageCoverageInfo packageCoverageInfo, byte[] content, final boolean excludeLines, final PsiClass psiClass) {
    if (content == null)
        return false;
    ClassReader reader = new ClassReader(content, 0, content.length);
    SourceLineCounter counter = new SourceLineCounter(null, excludeLines, null);
    reader.accept(counter, 0);
    classCoverageInfo.totalLineCount += counter.getNSourceLines();
    packageCoverageInfo.totalLineCount += counter.getNSourceLines();
    for (Object nameAndSig : counter.getMethodsWithSourceCode()) {
        if (!PackageAnnotator.isGeneratedDefaultConstructor(psiClass, (String) nameAndSig)) {
            classCoverageInfo.totalMethodCount++;
            packageCoverageInfo.totalMethodCount++;
        }
    }
    if (!counter.isInterface()) {
        packageCoverageInfo.totalClassCount++;
    }
    return !counter.isInterface();
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) SourceLineCounter(com.intellij.rt.coverage.instrumentation.SourceLineCounter)

Example 17 with ClassReader

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

the class DefaultOutputConsumer method readClassName.

private static String readClassName(byte[] classBytes) throws IOException {
    final Ref<String> nameRef = Ref.create(null);
    new ClassReader(classBytes).accept(new ClassVisitor(Opcodes.API_VERSION) {

        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            nameRef.set(name.replace('/', '.'));
        }
    }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    return nameRef.get();
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor)

Example 18 with ClassReader

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

the class ClsFileImpl method buildFileStub.

@Nullable
public static PsiJavaFileStub buildFileStub(@NotNull VirtualFile file, @NotNull byte[] bytes) throws ClsFormatException {
    try {
        if (ClassFileViewProvider.isInnerClass(file, bytes)) {
            return null;
        }
        ClassReader reader = new ClassReader(bytes);
        String className = file.getNameWithoutExtension();
        String internalName = reader.getClassName();
        boolean module = internalName.equals("module-info") && BitUtil.isSet(reader.getAccess(), Opcodes.ACC_MODULE);
        LanguageLevel level = ClsParsingUtil.getLanguageLevelByVersion(reader.readShort(6));
        if (module) {
            PsiJavaFileStub stub = new PsiJavaFileStubImpl(null, "", level, true);
            ModuleStubBuildingVisitor visitor = new ModuleStubBuildingVisitor(stub);
            reader.accept(visitor, EMPTY_ATTRIBUTES, ClassReader.SKIP_FRAMES);
            if (visitor.getResult() != null)
                return stub;
        } else {
            PsiJavaFileStub stub = new PsiJavaFileStubImpl(null, getPackageName(internalName), level, true);
            try {
                FileContentPair source = new FileContentPair(file, bytes);
                StubBuildingVisitor<FileContentPair> visitor = new StubBuildingVisitor<>(source, STRATEGY, stub, 0, className);
                reader.accept(visitor, EMPTY_ATTRIBUTES, ClassReader.SKIP_FRAMES);
                if (visitor.getResult() != null)
                    return stub;
            } catch (OutOfOrderInnerClassException ignored) {
            }
        }
        return null;
    } catch (Exception e) {
        throw new ClsFormatException(file.getPath() + ": " + e.getMessage(), e);
    }
}
Also used : PsiJavaFileStub(com.intellij.psi.impl.java.stubs.PsiJavaFileStub) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) ClsFormatException(com.intellij.util.cls.ClsFormatException) PluginException(com.intellij.diagnostic.PluginException) IOException(java.io.IOException) PsiJavaFileStubImpl(com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl) LanguageLevel(com.intellij.pom.java.LanguageLevel) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClsFormatException(com.intellij.util.cls.ClsFormatException) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with ClassReader

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

the class InstrumentationClassFinder method loadPseudoClass.

private PseudoClass loadPseudoClass(InputStream is) throws IOException {
    final ClassReader reader = new ClassReader(is);
    final V visitor = new V();
    reader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    return new PseudoClass(this, visitor.myName, visitor.mySuperclassName, visitor.myInterfaces, visitor.myModifiers, visitor.myMethods);
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader)

Example 20 with ClassReader

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

the class BaseInstrumentingBuilder method performBuild.

@Override
protected final ExitCode performBuild(CompileContext context, ModuleChunk chunk, InstrumentationClassFinder finder, OutputConsumer outputConsumer) {
    ExitCode exitCode = ExitCode.NOTHING_DONE;
    for (CompiledClass compiledClass : outputConsumer.getCompiledClasses().values()) {
        if (Utils.IS_TEST_MODE || LOG.isDebugEnabled()) {
            LOG.info("checking " + compiledClass + " by " + getClass());
        }
        final BinaryContent originalContent = compiledClass.getContent();
        final ClassReader reader = new FailSafeClassReader(originalContent.getBuffer(), originalContent.getOffset(), originalContent.getLength());
        final int version = getClassFileVersion(reader);
        if (IS_INSTRUMENTED_KEY.get(compiledClass, Boolean.FALSE) || !canInstrument(compiledClass, version)) {
            // do not instrument the same content twice
            continue;
        }
        final ClassWriter writer = new InstrumenterClassWriter(reader, getAsmClassWriterFlags(version), finder);
        try {
            if (Utils.IS_TEST_MODE || LOG.isDebugEnabled()) {
                LOG.info("instrumenting " + compiledClass + " by " + getClass());
            }
            final BinaryContent instrumented = instrument(context, compiledClass, reader, writer, finder);
            if (instrumented != null) {
                compiledClass.setContent(instrumented);
                finder.cleanCachedData(compiledClass.getClassName());
                IS_INSTRUMENTED_KEY.set(compiledClass, Boolean.TRUE);
                exitCode = ExitCode.OK;
            }
        } catch (Throwable e) {
            LOG.info(e);
            final String message = e.getMessage();
            if (message != null) {
                context.processMessage(new CompilerMessage(getPresentableName(), BuildMessage.Kind.ERROR, message, ContainerUtil.getFirstItem(compiledClass.getSourceFilesPaths())));
            } else {
                context.processMessage(new CompilerMessage(getPresentableName(), e));
            }
        }
    }
    return exitCode;
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) InstrumenterClassWriter(com.intellij.compiler.instrumentation.InstrumenterClassWriter) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter) InstrumenterClassWriter(com.intellij.compiler.instrumentation.InstrumenterClassWriter)

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