Search in sources :

Example 21 with ClassReader

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

the class Mappings method getCallback.

public Callbacks.Backend getCallback() {
    return new Callbacks.Backend() {

        public void associate(String classFileName, Collection<String> sources, ClassReader cr) {
            synchronized (myLock) {
                final int classFileNameS = myContext.get(classFileName);
                final Pair<ClassRepr, Set<UsageRepr.Usage>> result = new ClassfileAnalyzer(myContext).analyze(classFileNameS, cr);
                final ClassRepr repr = result.first;
                if (repr != null) {
                    final Set<UsageRepr.Usage> localUsages = result.second;
                    final int className = repr.name;
                    for (String sourceFileName : sources) {
                        final File sourceFile = new File(sourceFileName);
                        myClassToSourceFile.put(className, sourceFile);
                        mySourceFileToClasses.put(sourceFile, repr);
                    }
                    for (final int s : repr.getSupers()) {
                        myClassToSubclasses.put(s, className);
                    }
                    for (final UsageRepr.Usage u : localUsages) {
                        final int owner = u.getOwner();
                        if (owner != className) {
                            myClassToClassDependency.put(owner, className);
                        }
                    }
                }
            }
        }

        public void associate(final String classFileName, final String sourceFileName, final ClassReader cr) {
            associate(classFileName, Collections.singleton(sourceFileName), cr);
        }

        @Override
        public void registerImports(final String className, final Collection<String> imports, Collection<String> staticImports) {
            final List<String> allImports = new ArrayList<>();
            for (String anImport : imports) {
                if (!anImport.endsWith("*")) {
                    // filter out wildcard imports
                    allImports.add(anImport);
                }
            }
            for (final String s : staticImports) {
                int i = s.length() - 1;
                while (s.charAt(i) != '.') {
                    i--;
                }
                final String anImport = s.substring(0, i);
                if (!anImport.endsWith("*")) {
                    // filter out wildcard imports
                    allImports.add(anImport);
                }
            }
            if (!allImports.isEmpty()) {
                myPostPasses.offer(() -> {
                    final int rootClassName = myContext.get(className.replace(".", "/"));
                    final Collection<File> fileNames = myClassToSourceFile.get(rootClassName);
                    final ClassRepr repr = fileNames != null && !fileNames.isEmpty() ? getReprByName(fileNames.iterator().next(), rootClassName) : null;
                    for (final String i : allImports) {
                        final int iname = myContext.get(i.replace('.', '/'));
                        myClassToClassDependency.put(iname, rootClassName);
                        if (repr != null && repr.addUsage(UsageRepr.createClassUsage(myContext, iname))) {
                            for (File fileName : fileNames) {
                                mySourceFileToClasses.put(fileName, repr);
                            }
                        }
                    }
                });
            }
        }
    };
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) File(java.io.File)

Example 22 with ClassReader

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

the class SourceLineCounterUtil method collectSrcLinesForUntouchedFiles.

public static void collectSrcLinesForUntouchedFiles(final List<Integer> uncoveredLines, byte[] content, final boolean excludeLines) {
    final ClassReader reader = new ClassReader(content);
    final SourceLineCounter collector = new SourceLineCounter(null, excludeLines, null);
    reader.accept(collector, 0);
    final TIntObjectHashMap lines = collector.getSourceLines();
    lines.forEachKey(new TIntProcedure() {

        public boolean execute(int line) {
            line--;
            uncoveredLines.add(line);
            return true;
        }
    });
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) SourceLineCounter(com.intellij.rt.coverage.instrumentation.SourceLineCounter)

Example 23 with ClassReader

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

the class ByteCodeViewerManager method processClassFile.

private static String processClassFile(byte[] bytes) {
    final ClassReader classReader = new ClassReader(bytes);
    final StringWriter writer = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(writer);
    try {
        classReader.accept(new TraceClassVisitor(null, new Textifier(), printWriter), 0);
    } finally {
        printWriter.close();
    }
    return writer.toString();
}
Also used : TraceClassVisitor(org.jetbrains.org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) Textifier(org.jetbrains.org.objectweb.asm.util.Textifier) PrintWriter(java.io.PrintWriter)

Example 24 with ClassReader

use of org.jetbrains.org.objectweb.asm.ClassReader in project kotlin by JetBrains.

the class InnerClassInfoGenTest method extractInnerClasses.

@NotNull
private List<InnerClassAttribute> extractInnerClasses(@NotNull String className) {
    OutputFile outputFile = generateClassesInFile().get(className + ".class");
    assertNotNull(outputFile);
    byte[] bytes = outputFile.asByteArray();
    ClassReader reader = new ClassReader(bytes);
    final List<InnerClassAttribute> result = new ArrayList<InnerClassAttribute>();
    reader.accept(new ClassVisitor(ASM5) {

        @Override
        public void visitInnerClass(@NotNull String name, String outerName, String innerName, int access) {
            result.add(new InnerClassAttribute(name, outerName, innerName, access));
        }
    }, ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
    return result;
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) ArrayList(java.util.ArrayList) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with ClassReader

use of org.jetbrains.org.objectweb.asm.ClassReader in project kotlin by JetBrains.

the class OuterClassGenTest method doCustomTest.

private void doCustomTest(@Language("RegExp") @NotNull String internalNameRegexp, @NotNull OuterClassInfo expectedInfo, @NotNull String testDataFile) {
    ClassReader kotlinReader = getKotlinClassReader(internalNameRegexp, testDataFile);
    OuterClassInfo kotlinInfo = readOuterClassInfo(kotlinReader);
    String message = "Error in enclosingMethodInfo info for class: " + kotlinReader.getClassName();
    if (kotlinInfo == null) {
        assertNull(expectedInfo.getOwner());
    } else {
        assertTrue(message + "\n" + kotlinInfo.getOwner() + " doesn't start with " + expectedInfo.getOwner(), kotlinInfo.getOwner().startsWith(expectedInfo.getOwner()));
    }
    assertEquals(message, expectedInfo.getMethodName(), kotlinInfo.getMethodName());
    assertEquals(message, expectedInfo.getMethodDesc(), kotlinInfo.getMethodDesc());
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader)

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