lombok.ast.ForwardingAstVisitor Java Examples

The following examples show how to use lombok.ast.ForwardingAstVisitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HashMapForJDK7Detector.java    From MeituanLintDemo with Apache License 2.0 6 votes vote down vote up
@Override
public AstVisitor createJavaVisitor(final @NonNull JavaContext context) {
    return new ForwardingAstVisitor() {

        @Override
        public boolean visitConstructorInvocation(ConstructorInvocation node) {
            TypeReference reference = node.astTypeReference();
            String typeName = reference.astParts().last().astIdentifier().astValue();
            // TODO: Should we handle factory method constructions of HashMaps as well,
            // e.g. via Guava? This is a bit trickier since we need to infer the type
            // arguments from the calling context.
            if (typeName.equals(HASH_MAP)) {
                checkHashMap(context, node, reference);
            }
            return super.visitConstructorInvocation(node);
        }
    };
}
 
Example #2
Source File: LogDetector.java    From MeituanLintDemo with Apache License 2.0 6 votes vote down vote up
@Override
public AstVisitor createJavaVisitor(final JavaContext context) {
    return new ForwardingAstVisitor() {
        @Override
        public boolean visitMethodInvocation(MethodInvocation node) {
            JavaParser.ResolvedNode resolve = context.resolve(node);
            if (resolve instanceof JavaParser.ResolvedMethod) {
                JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolve;
                // 方法所在的类校验
                JavaParser.ResolvedClass containingClass = method.getContainingClass();
                if (containingClass.matches("android.util.Log")) {
                    context.report(ISSUE, node, context.getLocation(node),
                                   "请使用Ln,避免使用Log");
                    return true;
                }
                if (node.toString().startsWith("System.out.println")) {
                    context.report(ISSUE, node, context.getLocation(node),
                                   "请使用Ln,避免使用System.out.println");
                    return true;
                }
            }
            return super.visitMethodInvocation(node);
        }
    };
}
 
Example #3
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
@Override
public AstVisitor createJavaVisitor(@NonNull JavaContext context) {
    if (mApiDatabase == null) {
        return new ForwardingAstVisitor() {
        };
    }
    return new ApiVisitor(context);
}
 
Example #4
Source File: RtlDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AstVisitor createJavaVisitor(@NonNull JavaContext context) {
    if (rtlApplies(context)) {
        return new IdentifierChecker(context);
    }

    return new ForwardingAstVisitor() { };
}
 
Example #5
Source File: AssertDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AstVisitor createJavaVisitor(@NonNull final JavaContext context) {
    return new ForwardingAstVisitor() {
        @Override
        public boolean visitAssert(Assert node) {
            if (!context.getMainProject().isAndroidProject()) {
                return true;
            }

            Expression assertion = node.astAssertion();
            // Allow "assert true"; it's basically a no-op
            if (assertion instanceof BooleanLiteral) {
                Boolean b = ((BooleanLiteral) assertion).astValue();
                if (b != null && b) {
                    return false;
                }
            } else {
                // Allow assertions of the form "assert foo != null" because they are often used
                // to make statements to tools about known nullness properties. For example,
                // findViewById() may technically return null in some cases, but a developer
                // may know that it won't be when it's called correctly, so the assertion helps
                // to clear nullness warnings.
                if (isNullCheck(assertion)) {
                    return false;
                }
            }
            String message
                    = "Assertions are unreliable. Use `BuildConfig.DEBUG` conditional checks instead.";
            context.report(ISSUE, node, context.getLocation(node), message);
            return false;
        }
    };
}
 
Example #6
Source File: CallSuperDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AstVisitor createJavaVisitor(@NonNull final JavaContext context) {
    return new ForwardingAstVisitor() {
        @Override
        public boolean visitMethodDeclaration(MethodDeclaration node) {
            ResolvedNode resolved = context.resolve(node);
            if (resolved instanceof ResolvedMethod) {
                ResolvedMethod method = (ResolvedMethod) resolved;
                checkCallSuper(context, node, method);
            }

            return false;
        }
    };
}
 
Example #7
Source File: ConstantEvaluatorTest.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static void check(Object expected, String source, final String targetVariable) {
    JavaContext context = LintUtilsTest.parse(source, new File("src/test/pkg/Test.java"));
    assertNotNull(context);
    CompilationUnit unit = (CompilationUnit) context.getCompilationUnit();
    assertNotNull(unit);

    // Find the expression
    final AtomicReference<Expression> reference = new AtomicReference<Expression>();
    unit.accept(new ForwardingAstVisitor() {
        @Override
        public boolean visitVariableDefinitionEntry(VariableDefinitionEntry node) {
            if (node.astName().astValue().equals(targetVariable)) {
                reference.set(node.astInitializer());
            }
            return super.visitVariableDefinitionEntry(node);
        }
    });
    Expression expression = reference.get();
    Object actual = ConstantEvaluator.evaluate(context, expression);
    if (expected == null) {
        assertNull(actual);
    } else {
        assertNotNull("Couldn't compute value for " + source + ", expected " + expected,
                actual);
        assertEquals(expected.getClass(), actual.getClass());
        assertEquals(expected.toString(), actual.toString());
    }
    assertEquals(expected, actual);
    if (expected instanceof String) {
        assertEquals(expected, ConstantEvaluator.evaluateString(context, expression,
                false));
    }
}
 
Example #8
Source File: JavaVariablesDetector.java    From lewis with Apache License 2.0 5 votes vote down vote up
@Override
public AstVisitor createJavaVisitor(@NonNull final JavaContext context) {
    return new ForwardingAstVisitor() {

        @Override
        public boolean visitVariableDeclaration(VariableDeclaration node) {

            if (hasClassParent(node) && !PackageManager.isGenerated(context, node)) {

                Node classDeclaration = node.getParent();

                String nodeString = node.toString();

                VariableDefinitionEntry variableDefinition = node.astDefinition().astVariables().first();
                String name = variableDefinition.astName().astValue();

                if (!isStaticOrFinal(node) && !isModel(context, classDeclaration) && !isWidget(nodeString)) {
                    if (!instanceVariableCorrectFormat(name)) {
                        context.report(ISSUE_INSTANCE_VARIABLE_NAME, context.getLocation(node),
                                "Expecting " + name + " to begin with 'm' and be written in camelCase.");
                    }
                } else if (isStaticAndFinal(node)) {
                    if (!staticFinalCorrectFormat(name)) {
                        context.report(ISSUE_CLASS_CONSTANT_NAME, context.getLocation(node),
                                "Expecting " + name + " to be named using UPPER_SNAKE_CASE.");
                    }
                }
            }

            return super.visitVariableDeclaration(node);
        }

    };
}
 
Example #9
Source File: EcjParserTest.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("ClassNameDiffersFromFileName")
public void testGetFields() throws Exception {
    @Language("JAVA")
    String source = ""
            + "public class FieldTest {\n"
            + "    public int field1 = 1;\n"
            + "    public int field2 = 3;\n"
            + "    public int field3 = 5;\n"
            + "    \n"
            + "    public static class Inner extends FieldTest {\n"
            + "        public int field2 = 5;\n"
            + "    }\n"
            + "}\n";

    final JavaContext context = LintUtilsTest.parse(source,
            new File("src/test/pkg/FieldTest.java"));
    assertNotNull(context);

    Node compilationUnit = context.getCompilationUnit();
    assertNotNull(compilationUnit);
    final AtomicBoolean found = new AtomicBoolean();
    compilationUnit.accept(new ForwardingAstVisitor() {
        @Override
        public boolean visitClassDeclaration(ClassDeclaration node) {
            if (node.astName().astValue().equals("Inner")) {
                found.set(true);
                ResolvedNode resolved = context.resolve(node);
                assertNotNull(resolved);
                ResolvedClass cls = (ResolvedClass) resolved;
                List<ResolvedField> declaredFields = Lists.newArrayList(cls.getFields(false));
                assertEquals(1, declaredFields.size());
                assertEquals("field2", declaredFields.get(0).getName());

                declaredFields = Lists.newArrayList(cls.getFields(true));
                assertEquals(3, declaredFields.size());
                assertEquals("field2", declaredFields.get(0).getName());
                assertEquals("FieldTest.Inner", declaredFields.get(0).getContainingClassName());
                assertEquals("field1", declaredFields.get(1).getName());
                assertEquals("FieldTest", declaredFields.get(1).getContainingClassName());
                assertEquals("field3", declaredFields.get(2).getName());
                assertEquals("FieldTest", declaredFields.get(2).getContainingClassName());
            }

            return super.visitClassDeclaration(node);
        }
    });
    assertTrue(found.get());
}
 
Example #10
Source File: ExternalAnnotationRepositoryTest.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void testMergeParameters() throws Exception {
    try {
        ExternalAnnotationRepository manager = getExternalAnnotations("test.pkg", ""
                + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                + "<root>\n"
                + "  <item name=\"test.pkg.Test.Parent void testMethod(int)\">\n"
                + "    <annotation name=\"android.support.annotation.Annotation1\" />\n"
                + "  </item>\n"
                + "  <item name=\"test.pkg.Test.Child void testMethod(int)\">\n"
                + "    <annotation name=\"android.support.annotation.Annotation2\" />\n"
                + "  </item>\n"
                + "</root>\n");
        assertNotNull(manager);
        ExternalAnnotationRepository.set(manager);

        String source = ""
                + "package test.pkg;\n"
                + "\n"
                + "public class Test {\n"
                + "    public void test(Child child) {\n"
                + "        child.testMethod(5);\n"
                + "    }\n"
                + "\n"
                + "    public static class Parent {\n"
                + "        public void testMethod(int parameter) {\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    public static class Child extends Parent {\n"
                + "        @Override\n"
                + "        public void testMethod(int parameter) {\n"
                + "        }\n"
                + "    }\n"
                + "}\n";

        final JavaContext context = LintUtilsTest.parse(source,
                new File("src/test/pkg/Test.java"));
        assertNotNull(context);
        Node unit = context.getCompilationUnit();
        assertNotNull(unit);
        final AtomicBoolean foundMethod = new AtomicBoolean();
        unit.accept(new ForwardingAstVisitor() {
            @Override
            public boolean visitMethodInvocation(MethodInvocation node) {
                foundMethod.set(true);
                assertEquals("testMethod", node.astName().astValue());
                ResolvedNode resolved = context.resolve(node);
                assertTrue(resolved instanceof ResolvedMethod);
                ResolvedMethod method = (ResolvedMethod)resolved;
                List<ResolvedAnnotation> annotations =
                        Lists.newArrayList(method.getAnnotations());
                assertEquals(3, annotations.size());
                Collections.sort(annotations,
                        new Comparator<ResolvedAnnotation>() {
                            @Override
                            public int compare(ResolvedAnnotation a1,
                                    ResolvedAnnotation a2) {
                                return a1.getName().compareTo(a2.getName());
                            }
                        });
                assertEquals("android.support.annotation.Annotation1", annotations.get(0).getName());
                assertEquals("android.support.annotation.Annotation2", annotations.get(1).getName());
                assertEquals("java.lang.Override", annotations.get(2).getName());
                return super.visitMethodInvocation(node);
            }
        });
        assertTrue(foundMethod.get());
    } finally {
        ExternalAnnotationRepository.set(null);
    }
}