Search in sources :

Example 6 with UElement

use of org.jetbrains.uast.UElement in project Conductor by bluelinelabs.

the class ControllerChangeHandlerIssueDetector method createUastHandler.

@Override
public UElementHandler createUastHandler(final JavaContext context) {
    final JavaEvaluator evaluator = context.getEvaluator();
    return new UElementHandler() {

        @Override
        public void visitClass(UClass node) {
            if (evaluator.isAbstract(node)) {
                return;
            }
            boolean hasSuperType = false;
            for (UTypeReferenceExpression superType : node.getUastSuperTypes()) {
                if (CLASS_NAME.equals(superType.asRenderString())) {
                    hasSuperType = true;
                    break;
                }
            }
            if (!hasSuperType) {
                return;
            }
            if (!evaluator.isPublic(node)) {
                String message = String.format("This ControllerChangeHandler class should be public (%1$s)", node.getQualifiedName());
                context.report(ISSUE, node, context.getLocation((UElement) node), message);
                return;
            }
            if (node.getContainingClass() != null && !evaluator.isStatic(node)) {
                String message = String.format("This ControllerChangeHandler inner class should be static (%1$s)", node.getQualifiedName());
                context.report(ISSUE, node, context.getLocation((UElement) node), message);
                return;
            }
            boolean hasConstructor = false;
            boolean hasDefaultConstructor = false;
            for (UMethod method : node.getMethods()) {
                if (method.isConstructor()) {
                    hasConstructor = true;
                    if (evaluator.isPublic(method) && method.getUastParameters().size() == 0) {
                        hasDefaultConstructor = true;
                        break;
                    }
                }
            }
            if (hasConstructor && !hasDefaultConstructor) {
                String message = String.format("This ControllerChangeHandler needs to have a public default constructor (`%1$s`)", node.getQualifiedName());
                context.report(ISSUE, node, context.getLocation((UElement) node), message);
            }
        }
    };
}
Also used : UElement(org.jetbrains.uast.UElement) UTypeReferenceExpression(org.jetbrains.uast.UTypeReferenceExpression) UMethod(org.jetbrains.uast.UMethod) JavaEvaluator(com.android.tools.lint.client.api.JavaEvaluator) UClass(org.jetbrains.uast.UClass) UElementHandler(com.android.tools.lint.client.api.UElementHandler)

Aggregations

UElement (org.jetbrains.uast.UElement)6 PsiElement (com.intellij.psi.PsiElement)3 UExpression (org.jetbrains.uast.UExpression)3 UReferenceExpression (org.jetbrains.uast.UReferenceExpression)3 Nullable (com.android.annotations.Nullable)2 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2 JavaEvaluator (com.android.tools.lint.client.api.JavaEvaluator)2 UElementHandler (com.android.tools.lint.client.api.UElementHandler)2 PsiClass (com.intellij.psi.PsiClass)2 PsiMethod (com.intellij.psi.PsiMethod)2 PsiVariable (com.intellij.psi.PsiVariable)2 UCallExpression (org.jetbrains.uast.UCallExpression)2 UClass (org.jetbrains.uast.UClass)2 UIfExpression (org.jetbrains.uast.UIfExpression)2 UMethod (org.jetbrains.uast.UMethod)2 UParenthesizedExpression (org.jetbrains.uast.UParenthesizedExpression)2 UQualifiedReferenceExpression (org.jetbrains.uast.UQualifiedReferenceExpression)2 UTypeReferenceExpression (org.jetbrains.uast.UTypeReferenceExpression)2 ResourceType (com.android.resources.ResourceType)1 JavaEvaluator (com.android.tools.klint.client.api.JavaEvaluator)1