Search in sources :

Example 26 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class JavaTreeModelTest method local_class_declaration.

/**
 * 14.3. Local Class Declarations
 */
@Test
public void local_class_declaration() {
    BlockTree block = ((MethodTree) firstTypeMember("class T { void m() { abstract class Local { } } }")).block();
    ClassTree tree = (ClassTree) block.body().get(0);
    assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
    assertThat(tree.simpleName().identifierToken().text()).isEqualTo("Local");
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.ABSTRACT);
    assertThat(tree).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 7);
    block = ((MethodTree) firstTypeMember("class T { void m() { static enum Local { ; } } }")).block();
    tree = (ClassTree) block.body().get(0);
    assertThat(tree.is(Tree.Kind.ENUM)).isTrue();
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.STATIC);
    assertThat(tree).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 8);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Example 27 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class BytecodeCFGBuilderTest method test_all_instructions_are_part_of_CFG.

@Test
public void test_all_instructions_are_part_of_CFG() throws Exception {
    SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
    File file = new File("src/test/java/org/sonar/java/bytecode/cfg/testdata/CFGTestData.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    SemanticModel.createFor(tree, squidClassLoader);
    Symbol.TypeSymbol testClazz = ((ClassTree) tree.types().get(0)).symbol();
    ClassReader cr = new ClassReader(squidClassLoader.getResourceAsStream(Convert.bytecodeName(CFGTestData.class.getCanonicalName()) + ".class"));
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    cr.accept(classNode, 0);
    for (MethodNode method : classNode.methods) {
        Multiset<String> opcodes = Arrays.stream(method.instructions.toArray()).map(AbstractInsnNode::getOpcode).filter(opcode -> opcode != -1).map(opcode -> Printer.OPCODES[opcode]).collect(Collectors.toCollection(HashMultiset::create));
        Symbol methodSymbol = Iterables.getOnlyElement(testClazz.lookupSymbols(method.name));
        BytecodeCFG bytecodeCFG = SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) methodSymbol).completeSignature(), squidClassLoader);
        Multiset<String> cfgOpcodes = cfgOpcodes(bytecodeCFG);
        assertThat(cfgOpcodes).isEqualTo(opcodes);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) JavaSymbol(org.sonar.java.resolve.JavaSymbol) H_INVOKESTATIC(org.objectweb.asm.Opcodes.H_INVOKESTATIC) Multiset(com.google.common.collect.Multiset) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SETestUtils(org.sonar.java.se.SETestUtils) NO_OPERAND_INSN(org.sonar.java.bytecode.cfg.Instructions.NO_OPERAND_INSN) Label(org.objectweb.asm.Label) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) VAR_INSN(org.sonar.java.bytecode.cfg.Instructions.VAR_INSN) MethodNode(org.objectweb.asm.tree.MethodNode) INVOKEINTERFACE(org.objectweb.asm.Opcodes.INVOKEINTERFACE) Lists(com.google.common.collect.Lists) CFGTestData(org.sonar.java.bytecode.cfg.testdata.CFGTestData) HashMultiset(com.google.common.collect.HashMultiset) FIELD_INSN(org.sonar.java.bytecode.cfg.Instructions.FIELD_INSN) JUMP_INSN(org.sonar.java.bytecode.cfg.Instructions.JUMP_INSN) METHOD_INSN(org.sonar.java.bytecode.cfg.Instructions.METHOD_INSN) JavaParser(org.sonar.java.ast.parser.JavaParser) NOP(org.objectweb.asm.Opcodes.NOP) Opcodes(org.objectweb.asm.Opcodes) Predicate(java.util.function.Predicate) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) IOException(java.io.IOException) Test(org.junit.Test) TYPE_INSN(org.sonar.java.bytecode.cfg.Instructions.TYPE_INSN) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) Handle(org.objectweb.asm.Handle) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) ClassReader(org.objectweb.asm.ClassReader) Printer(org.objectweb.asm.util.Printer) SemanticModel(org.sonar.java.resolve.SemanticModel) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) JSR(org.objectweb.asm.Opcodes.JSR) ClassNode(org.objectweb.asm.tree.ClassNode) Convert(org.sonar.java.resolve.Convert) INT_INSN(org.sonar.java.bytecode.cfg.Instructions.INT_INSN) Symbol(org.sonar.plugins.java.api.semantic.Symbol) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassNode(org.objectweb.asm.tree.ClassNode) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MethodNode(org.objectweb.asm.tree.MethodNode) JavaSymbol(org.sonar.java.resolve.JavaSymbol) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) Test(org.junit.Test)

Example 28 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class GenericsTest method declaredTypesFromFile.

private static List<Type> declaredTypesFromFile(String path) {
    CompilationUnitTree tree = treeOf(new File(path));
    List<Type> results = Lists.newLinkedList();
    for (Tree classTree : tree.types()) {
        Type type = ((ClassTree) classTree).symbol().type();
        results.add(type);
    }
    return results;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Type(org.sonar.plugins.java.api.semantic.Type) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) File(java.io.File)

Example 29 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class LambdaBlockReturnVisitorTest method test.

@Test
public void test() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {\n" + "        java.util.function.Consumer<String> c = s -> {\n" + "          if(s.length()>0) {\n" + "            return;\n" + "          }\n" + "          System.out.println(s);\n" + "        };\n" + "\n" + "        java.util.function.Function<String, String> f = s -> {\n" + "          if(s.length() > 0) {\n" + "            return s.replace('a', 'b');\n" + "          }\n" + "          return unknownSymbol;\n" + "        };\n" + "      }");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    List<VariableTree> vars = ((ClassTree) cut.types().get(0)).members().stream().map(m -> (VariableTree) m).collect(Collectors.toList());
    LambdaBlockReturnVisitor visitor = new LambdaBlockReturnVisitor();
    ((LambdaExpressionTree) vars.get(0).initializer()).body().accept(visitor);
    assertThat(visitor.types).isEmpty();
    visitor = new LambdaBlockReturnVisitor();
    ((LambdaExpressionTree) vars.get(1).initializer()).body().accept(visitor);
    assertThat(visitor.types).hasSize(1);
}
Also used : JavaParser(org.sonar.java.ast.parser.JavaParser) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ActionParser(com.sonar.sslr.api.typed.ActionParser) List(java.util.List) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Collections(java.util.Collections) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 30 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.

the class JavaTreeModelTest method enum_declaration.

/*
   * 8.9. Enums
   */
@Test
public void enum_declaration() {
    ClassTree tree = firstType("public enum T implements I1, I2 { }");
    assertThat(tree.is(Tree.Kind.ENUM)).isTrue();
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(tree.simpleName().name()).isEqualTo("T");
    assertThat(tree.superClass()).isNull();
    assertThat(tree.superInterfaces()).hasSize(2);
    assertThat(tree.declarationKeyword().text()).isEqualTo("enum");
    assertThatChildrenIteratorHasSize(tree, 8);
    tree = firstType("public enum T { }");
    assertThat(tree.is(Tree.Kind.ENUM)).isTrue();
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(tree.simpleName().name()).isEqualTo("T");
    assertThat(tree.superClass()).isNull();
    assertThat(tree.openBraceToken().text()).isEqualTo("{");
    assertThat(tree.closeBraceToken().text()).isEqualTo("}");
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("enum");
    assertThatChildrenIteratorHasSize(tree, 7);
}
Also used : NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Aggregations

ClassTree (org.sonar.plugins.java.api.tree.ClassTree)116 Tree (org.sonar.plugins.java.api.tree.Tree)53 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)47 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)45 Test (org.junit.Test)41 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)37 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)32 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)23 List (java.util.List)19 Type (org.sonar.plugins.java.api.semantic.Type)18 File (java.io.File)14 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)13 Rule (org.sonar.check.Rule)12 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)12 Collectors (java.util.stream.Collectors)10 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)10 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)10 ImmutableList (com.google.common.collect.ImmutableList)9 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)9