Search in sources :

Example 1 with UParameter

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

the class ControllerIssueDetector 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 Controller 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 Controller 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;
            boolean hasBundleConstructor = false;
            for (UMethod method : node.getMethods()) {
                if (method.isConstructor()) {
                    hasConstructor = true;
                    if (evaluator.isPublic(method)) {
                        List<UParameter> parameters = method.getUastParameters();
                        if (parameters.size() == 0) {
                            hasDefaultConstructor = true;
                            break;
                        } else if (parameters.size() == 1 && (parameters.get(0).getType().equalsToText(SdkConstants.CLASS_BUNDLE)) || parameters.get(0).getType().equalsToText("Bundle")) {
                            hasBundleConstructor = true;
                        }
                    }
                }
            }
            if (hasConstructor && !hasDefaultConstructor && !hasBundleConstructor) {
                String message = String.format("This Controller needs to have either a public default constructor or a" + " public single-argument constructor that takes a Bundle. (`%1$s`)", node.getQualifiedName());
                context.report(ISSUE, node, context.getLocation((UElement) node), message);
            }
        }
    };
}
Also used : UParameter(org.jetbrains.uast.UParameter) 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

JavaEvaluator (com.android.tools.lint.client.api.JavaEvaluator)1 UElementHandler (com.android.tools.lint.client.api.UElementHandler)1 UClass (org.jetbrains.uast.UClass)1 UElement (org.jetbrains.uast.UElement)1 UMethod (org.jetbrains.uast.UMethod)1 UParameter (org.jetbrains.uast.UParameter)1 UTypeReferenceExpression (org.jetbrains.uast.UTypeReferenceExpression)1