Search in sources :

Example 1 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class MethodLookup method lookup.

private static MethodLookup lookup(String className, String signature, SquidClassLoader classLoader, LookupMethodVisitor methodVisitor) {
    byte[] bytes = classLoader.getBytesForClass(className);
    if (bytes == null) {
        return null;
    }
    ClassReader cr = new ClassReader(bytes);
    LookupClassVisitor lookupVisitor = new LookupClassVisitor(methodVisitor, signature);
    cr.accept(lookupVisitor, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    if (lookupVisitor.methodFound) {
        return new MethodLookup(lookupVisitor.isStatic, lookupVisitor.isVarArgs, lookupVisitor.declaredExceptions);
    }
    // we didn't succeed to find the method in the class, try recursively on superclasses and interfaces
    if (lookupVisitor.superClassName != null) {
        MethodLookup result = lookup(lookupVisitor.superClassName, signature, classLoader, methodVisitor);
        if (result != null) {
            return result;
        }
    }
    if (lookupVisitor.interfaces != null) {
        return Arrays.stream(lookupVisitor.interfaces).map(iface -> lookup(iface, signature, classLoader, methodVisitor)).filter(Objects::nonNull).findAny().orElse(null);
    }
    return null;
}
Also used : Arrays(java.util.Arrays) Opcodes(org.objectweb.asm.Opcodes) MethodVisitor(org.objectweb.asm.MethodVisitor) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Collectors(java.util.stream.Collectors) Type(org.objectweb.asm.Type) Objects(java.util.Objects) List(java.util.List) ClassReader(org.objectweb.asm.ClassReader) Flags(org.sonar.java.resolve.Flags) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) ClassVisitor(org.objectweb.asm.ClassVisitor) Nullable(javax.annotation.Nullable) JSRInlinerAdapter(org.objectweb.asm.commons.JSRInlinerAdapter) Objects(java.util.Objects) ClassReader(org.objectweb.asm.ClassReader)

Example 2 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class ExportsDirectiveTreeImplTest method exportsDirective.

private ExportsDirectiveTree exportsDirective(String exportsDirective) {
    CompilationUnitTree compilationUnitTree = createTree("module org.foo {\n  " + exportsDirective + "\n}");
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return (ExportsDirectiveTree) compilationUnitTree.moduleDeclaration().moduleDirectives().get(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExportsDirectiveTree(org.sonar.plugins.java.api.tree.ExportsDirectiveTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 3 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class ExportsDirectiveTreeImplTest method createTree.

private CompilationUnitTree createTree(String... lines) {
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse(Arrays.stream(lines).collect(Collectors.joining("\n")));
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return compilationUnitTree;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 4 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class ProvidesDirectiveTreeImplTest method providesDirective.

private ProvidesDirectiveTree providesDirective(String exportsDirective) {
    CompilationUnitTree compilationUnitTree = createTree("module org.foo {\n  " + exportsDirective + "\n}");
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return (ProvidesDirectiveTree) compilationUnitTree.moduleDeclaration().moduleDirectives().get(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ProvidesDirectiveTree(org.sonar.plugins.java.api.tree.ProvidesDirectiveTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 5 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class UsesDirectiveTreeImplTest method moduleDirective.

private UsesDirectiveTree moduleDirective(String exportsDirective) {
    CompilationUnitTree compilationUnitTree = createTree("module org.foo {\n  " + exportsDirective + "\n}");
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return (UsesDirectiveTree) compilationUnitTree.moduleDeclaration().moduleDirectives().get(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) UsesDirectiveTree(org.sonar.plugins.java.api.tree.UsesDirectiveTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Aggregations

SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)53 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)37 File (java.io.File)24 Test (org.junit.Test)18 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)12 ArrayList (java.util.ArrayList)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 SemanticModel (org.sonar.java.resolve.SemanticModel)9 Tree (org.sonar.plugins.java.api.tree.Tree)9 List (java.util.List)7 Collectors (java.util.stream.Collectors)6 BeforeClass (org.junit.BeforeClass)6 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Opcodes (org.objectweb.asm.Opcodes)5 JavaParser (org.sonar.java.ast.parser.JavaParser)5 IOException (java.io.IOException)4 Collections (java.util.Collections)4 Before (org.junit.Before)4 Label (org.objectweb.asm.Label)4