Search in sources :

Example 1 with TypeDeclaration

use of lombok.ast.TypeDeclaration in project android by JetBrains.

the class LintIdeJavaParserTest method testResolve.

public void testResolve() {
    String source1Path = "src/p1/p2/Target.java";
    String source1 = "package p1.p2;\n" + "\n" + "public class Target {\n" + "    public void foo(int f) {\n" + "    }\n" + "    public int myField = 5;\n" + "    public static class Target2 extends java.io.File {\n" + "        public Target2() {\n" + "            super(null, null);\n" + "        }\n" + "    }\n" + "}";
    myFixture.addFileToProject(source1Path, source1);
    String source2Path = "src/p1/p2/Caller.java";
    String source2 = "package p1.p2;\n" + "\n" + "public final class Caller extends Target.Target2 {\n" + "    public String call(Target target) {\n" + "        target.foo(42);\n" + "        target.myField = 6;\n" + "    }\n" + "}";
    PsiFile file2 = myFixture.addFileToProject(source2Path, source2);
    PsiCall call = PsiTreeUtil.findChildrenOfType(file2, PsiCall.class).iterator().next();
    ResolvedNode resolved = LintIdeJavaParser.resolve(call);
    assertNotNull(resolved);
    assertTrue(resolved instanceof ResolvedMethod);
    ResolvedMethod method = (ResolvedMethod) resolved;
    assertEquals("foo", method.getName());
    assertEquals("p1.p2.Target", method.getContainingClass().getName());
    assertTrue(method.isInPackage("p1.p2", true));
    assertTrue(method.isInPackage("p1.p2", false));
    assertTrue(method.isInPackage("p1", true));
    assertFalse(method.isInPackage("p1", false));
    // Resolve "target" as a reference expression in the call() method
    PsiElement element = ((PsiMethodCallExpression) call).getMethodExpression().getQualifier();
    assertNotNull(element);
    resolved = LintIdeJavaParser.resolve(element);
    assertTrue(resolved instanceof ResolvedVariable);
    ResolvedVariable resolvedVariable = (ResolvedVariable) resolved;
    assertEquals("target", resolvedVariable.getName());
    assertEquals("p1.p2.Target", resolvedVariable.getType().getName());
    PsiMethod callMethod = PsiTreeUtil.findChildrenOfType(file2, PsiMethod.class).iterator().next();
    assertNotNull(callMethod);
    PsiVariable variable = callMethod.getParameterList().getParameters()[0];
    resolved = LintIdeJavaParser.resolve(variable);
    assertTrue(resolved instanceof ResolvedVariable);
    resolvedVariable = (ResolvedVariable) resolved;
    assertEquals("target", resolvedVariable.getName());
    assertEquals("p1.p2.Target", resolvedVariable.getType().getName());
    @SuppressWarnings("ConstantConditions") PsiStatement fieldReferenceStatement = callMethod.getBody().getStatements()[1];
    PsiExpression lExpression = ((PsiAssignmentExpression) ((PsiExpressionStatement) fieldReferenceStatement).getExpression()).getLExpression();
    resolved = LintIdeJavaParser.resolve(lExpression);
    assertTrue(resolved instanceof ResolvedField);
    ResolvedField field = (ResolvedField) resolved;
    assertEquals("myField", field.getName());
    assertEquals("int", field.getType().getName());
    assertEquals("p1.p2.Target", field.getContainingClass().getName());
    assertTrue(field.isInPackage("p1.p2", true));
    assertTrue(field.isInPackage("p1.p2", false));
    assertTrue(field.isInPackage("p1", true));
    assertFalse(field.isInPackage("p1", false));
    PsiClass cls = PsiTreeUtil.findChildrenOfType(file2, PsiClass.class).iterator().next();
    @SuppressWarnings("ConstantConditions") PsiJavaCodeReferenceElement superReference = cls.getExtendsList().getReferenceElements()[0];
    resolved = LintIdeJavaParser.resolve(superReference);
    assertTrue(resolved instanceof ResolvedClass);
    ResolvedClass rc = (ResolvedClass) resolved;
    assertTrue(rc.getName().equals("p1.p2.Target.Target2"));
    assertTrue(rc.isInPackage("p1.p2", true));
    assertTrue(rc.isInPackage("p1.p2", false));
    assertTrue(rc.isInPackage("p1", true));
    assertFalse(rc.isInPackage("p1", false));
    CompilationUnit unit = LombokPsiConverter.convert((PsiJavaFile) file2);
    //noinspection ConstantConditions
    TypeDeclaration first = unit.astTypeDeclarations().first();
    resolved = LintIdeJavaParser.resolve((PsiElement) first.getNativeNode());
    assertTrue(resolved instanceof ResolvedClass);
    rc = (ResolvedClass) resolved;
    assertEquals("p1.p2.Caller", rc.getName());
    //noinspection ConstantConditions
    assertEquals("p1.p2.Target.Target2", rc.getSuperClass().getName());
    assertNull(rc.getContainingClass());
}
Also used : CompilationUnit(lombok.ast.CompilationUnit) TypeDeclaration(lombok.ast.TypeDeclaration)

Example 2 with TypeDeclaration

use of lombok.ast.TypeDeclaration in project kotlin by JetBrains.

the class LintDriver method isSuppressed.

/**
     * Returns whether the given issue is suppressed in the given parse tree node.
     *
     * @param context the context for the source being scanned
     * @param issue the issue to be checked, or null to just check for "all"
     * @param scope the AST node containing the issue
     * @return true if there is a suppress annotation covering the specific
     *         issue in this class
     */
public boolean isSuppressed(@Nullable JavaContext context, @NonNull Issue issue, @Nullable Node scope) {
    boolean checkComments = mClient.checkForSuppressComments() && context != null && context.containsCommentSuppress();
    while (scope != null) {
        Class<? extends Node> type = scope.getClass();
        // so no need to do instanceof stuff here.
        if (type == VariableDefinition.class) {
            // Variable
            VariableDefinition declaration = (VariableDefinition) scope;
            if (isSuppressed(issue, declaration.astModifiers())) {
                return true;
            }
        } else if (type == MethodDeclaration.class) {
            // Method
            // Look for annotations on the method
            MethodDeclaration declaration = (MethodDeclaration) scope;
            if (isSuppressed(issue, declaration.astModifiers())) {
                return true;
            }
        } else if (type == ConstructorDeclaration.class) {
            // Constructor
            // Look for annotations on the method
            ConstructorDeclaration declaration = (ConstructorDeclaration) scope;
            if (isSuppressed(issue, declaration.astModifiers())) {
                return true;
            }
        } else if (TypeDeclaration.class.isAssignableFrom(type)) {
            // Class, annotation, enum, interface
            TypeDeclaration declaration = (TypeDeclaration) scope;
            if (isSuppressed(issue, declaration.astModifiers())) {
                return true;
            }
        } else if (type == AnnotationMethodDeclaration.class) {
            // Look for annotations on the method
            AnnotationMethodDeclaration declaration = (AnnotationMethodDeclaration) scope;
            if (isSuppressed(issue, declaration.astModifiers())) {
                return true;
            }
        }
        if (checkComments && context.isSuppressedWithComment(scope, issue)) {
            return true;
        }
        scope = scope.getParent();
    }
    return false;
}
Also used : AnnotationMethodDeclaration(lombok.ast.AnnotationMethodDeclaration) VariableDefinition(lombok.ast.VariableDefinition) AnnotationMethodDeclaration(lombok.ast.AnnotationMethodDeclaration) MethodDeclaration(lombok.ast.MethodDeclaration) ConstructorDeclaration(lombok.ast.ConstructorDeclaration) TypeDeclaration(lombok.ast.TypeDeclaration)

Aggregations

TypeDeclaration (lombok.ast.TypeDeclaration)2 AnnotationMethodDeclaration (lombok.ast.AnnotationMethodDeclaration)1 CompilationUnit (lombok.ast.CompilationUnit)1 ConstructorDeclaration (lombok.ast.ConstructorDeclaration)1 MethodDeclaration (lombok.ast.MethodDeclaration)1 VariableDefinition (lombok.ast.VariableDefinition)1