Search in sources :

Example 1 with Scanner

use of com.google.errorprone.scanner.Scanner in project error-prone by google.

the class CompileTimeConstantExpressionMatcherTest method assertCompilerMatchesOnAssignment.

// Helper methods.
private void assertCompilerMatchesOnAssignment(final Map<String, Boolean> expectedMatches, String... lines) {
    final Matcher<ExpressionTree> matcher = new CompileTimeConstantExpressionMatcher();
    final Scanner scanner = new Scanner() {

        @Override
        public Void visitAssignment(AssignmentTree t, VisitorState state) {
            ExpressionTree lhs = t.getVariable();
            if (expectedMatches.containsKey(lhs.toString())) {
                boolean matches = matcher.matches(t.getExpression(), state);
                if (expectedMatches.get(lhs.toString())) {
                    assertTrue("Matcher should match expression" + t.getExpression(), matches);
                } else {
                    assertFalse("Matcher should not match expression" + t.getExpression(), matches);
                }
            }
            return super.visitAssignment(t, state);
        }
    };
    CompilationTestHelper.newInstance(ScannerSupplier.fromScanner(scanner), getClass()).expectResult(Result.OK).addSourceLines("test/CompileTimeConstantExpressionMatcherTestCase.java", lines).doTest();
}
Also used : Scanner(com.google.errorprone.scanner.Scanner) VisitorState(com.google.errorprone.VisitorState) ExpressionTree(com.sun.source.tree.ExpressionTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 2 with Scanner

use of com.google.errorprone.scanner.Scanner in project error-prone by google.

the class DescendantOfTransitiveTest method shouldMatchTransitively.

@Test
public void shouldMatchTransitively() throws Exception {
    writeFileToLocalDisk("I1.java", "package i;", "public interface I1 {", "  void count();", "}");
    writeFileToLocalDisk("I2.java", "package i;", "public interface I2 extends I1 {", "}");
    writeFileToLocalDisk("B.java", "package b;", "public class B implements i.I2 {", "  public void count() {", "  }", "}");
    assertCompilesWithLocalDisk(new Scanner());
    clearSourceFiles();
    writeFileToLocalDisk("C.java", "public class C {", "  public void test(b.B b) {", "    b.count();", "  }", "}");
    assertCompilesWithLocalDisk(memberSelectMatches(/* shouldMatch= */
    true, new DescendantOf("i.I1", "count()")));
}
Also used : Scanner(com.google.errorprone.scanner.Scanner) Test(org.junit.Test)

Aggregations

Scanner (com.google.errorprone.scanner.Scanner)2 VisitorState (com.google.errorprone.VisitorState)1 AssignmentTree (com.sun.source.tree.AssignmentTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 Test (org.junit.Test)1