Search in sources :

Example 26 with SquidClassLoader

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

the class ExceptionalYieldTest method test_equals.

@Test
public void test_equals() {
    MethodBehavior methodBehavior = mockMethodBehavior();
    ExceptionalYield yield = new ExceptionalYield(methodBehavior);
    ExceptionalYield otherYield = new ExceptionalYield(methodBehavior);
    assertThat(yield).isNotEqualTo(null);
    assertThat(yield).isEqualTo(yield);
    assertThat(yield).isEqualTo(otherYield);
    otherYield.setExceptionType("java.lang.Exception");
    assertThat(yield).isNotEqualTo(otherYield);
    yield.setExceptionType("java.lang.Exception");
    assertThat(yield).isEqualTo(otherYield);
    SemanticModel semanticModel = SemanticModel.createFor((CompilationUnitTree) JavaParser.createParser().parse("class A{}"), new SquidClassLoader(new ArrayList<>()));
    assertThat(yield.exceptionType(semanticModel)).isEqualTo(otherYield.exceptionType(semanticModel));
    // same arity and parameters but happy yield
    assertThat(yield).isNotEqualTo(new HappyPathYield(methodBehavior));
}
Also used : SemanticModel(org.sonar.java.resolve.SemanticModel) SETestUtils.mockMethodBehavior(org.sonar.java.se.SETestUtils.mockMethodBehavior) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) ArrayList(java.util.ArrayList) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 27 with SquidClassLoader

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

the class UCFGJavaVisitorTest method setUp.

@BeforeClass
public static void setUp() {
    File testJarsDir = new File("target/test-jars/");
    squidClassLoader = new SquidClassLoader(Arrays.asList(testJarsDir.listFiles()));
}
Also used : File(java.io.File) LocationInFile(org.sonar.ucfg.LocationInFile) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) BeforeClass(org.junit.BeforeClass)

Example 28 with SquidClassLoader

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

the class BytecodeCFGBuilderTest method getBytecodeCFG.

public static BytecodeCFG getBytecodeCFG(String methodName, String filename) {
    SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
    File file = new File(filename);
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    SemanticModel.createFor(tree, squidClassLoader);
    List<Tree> classMembers = ((ClassTree) tree.types().get(0)).members();
    Symbol.MethodSymbol symbol = classMembers.stream().filter(m -> m instanceof MethodTree).map(m -> ((MethodTree) m).symbol()).filter(s -> methodName.equals(s.name())).findFirst().orElseThrow(IllegalStateException::new);
    return SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) symbol).completeSignature(), squidClassLoader);
}
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) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 29 with SquidClassLoader

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

the class BytecodeCFGBuilderTest method isNotBlank_goto_followed_by_label.

@Test
public void isNotBlank_goto_followed_by_label() throws Exception {
    SquidClassLoader classLoader = new SquidClassLoader(Lists.newArrayList(new File("src/test/commons-lang-2.1")));
    // apache commons 2.1 isNotBlank has a goto followed by an unreferenced label : see SONARJAVA-2461
    BytecodeCFG bytecodeCFG = SETestUtils.bytecodeCFG("org.apache.commons.lang.StringUtils#isNotBlank(Ljava/lang/String;)Z", classLoader);
    assertThat(bytecodeCFG).isNotNull();
    assertThat(bytecodeCFG.blocks).hasSize(11);
    assertThat(bytecodeCFG.blocks.get(4).successors).containsExactly(bytecodeCFG.blocks.get(6));
}
Also used : File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 30 with SquidClassLoader

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

the class BytecodeCFGBuilderTest method supportJSRandRET.

@Test
public void supportJSRandRET() throws Exception {
    SquidClassLoader classLoader = new SquidClassLoader(Lists.newArrayList(new File("src/test/JsrRet")));
    BytecodeCFG bytecodeCFG = SETestUtils.bytecodeCFG("jdk3.AllInstructions#jsrAndRetInstructions(I)I", classLoader);
    assertThat(bytecodeCFG).isNotNull();
    bytecodeCFG.blocks.stream().map(b -> b.terminator).filter(Objects::nonNull).forEach(t -> assertThat(t.opcode).isNotEqualTo(JSR));
}
Also used : File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

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