Search in sources :

Example 1 with JavaEvaluator

use of com.android.tools.lint.client.api.JavaEvaluator in project Conductor by bluelinelabs.

the class ControllerIssueDetector method checkClass.

@Override
public void checkClass(JavaContext context, PsiClass declaration) {
    final JavaEvaluator evaluator = context.getEvaluator();
    if (evaluator.isAbstract(declaration)) {
        return;
    }
    if (!evaluator.isPublic(declaration)) {
        String message = String.format("This Controller class should be public (%1$s)", declaration.getQualifiedName());
        context.report(ISSUE, declaration, context.getLocation(declaration), message);
        return;
    }
    if (declaration.getContainingClass() != null && !evaluator.isStatic(declaration)) {
        String message = String.format("This Controller inner class should be static (%1$s)", declaration.getQualifiedName());
        context.report(ISSUE, declaration, context.getLocation(declaration), message);
        return;
    }
    boolean hasDefaultConstructor = false;
    boolean hasBundleConstructor = false;
    PsiMethod[] constructors = declaration.getConstructors();
    for (PsiMethod constructor : constructors) {
        if (evaluator.isPublic(constructor)) {
            PsiParameter[] parameters = constructor.getParameterList().getParameters();
            if (parameters.length == 0) {
                hasDefaultConstructor = true;
                break;
            } else if (parameters.length == 1 && parameters[0].getType().equalsToText(SdkConstants.CLASS_BUNDLE) || parameters[0].getType().equalsToText("Bundle")) {
                hasBundleConstructor = true;
                break;
            }
        }
    }
    if (constructors.length > 0 && !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`)", declaration.getQualifiedName());
        context.report(ISSUE, declaration, context.getLocation(declaration), message);
    }
}
Also used : PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) JavaEvaluator(com.android.tools.lint.client.api.JavaEvaluator)

Example 2 with JavaEvaluator

use of com.android.tools.lint.client.api.JavaEvaluator in project Conductor by bluelinelabs.

the class ControllerChangeHandlerIssueDetector method checkClass.

@Override
public void checkClass(JavaContext context, PsiClass declaration) {
    final JavaEvaluator evaluator = context.getEvaluator();
    if (evaluator.isAbstract(declaration)) {
        return;
    }
    if (!evaluator.isPublic(declaration)) {
        String message = String.format("This ControllerChangeHandler class should be public (%1$s)", declaration.getQualifiedName());
        context.report(ISSUE, declaration, context.getLocation(declaration), message);
        return;
    }
    if (declaration.getContainingClass() != null && !evaluator.isStatic(declaration)) {
        String message = String.format("This ControllerChangeHandler inner class should be static (%1$s)", declaration.getQualifiedName());
        context.report(ISSUE, declaration, context.getLocation(declaration), message);
        return;
    }
    boolean hasDefaultConstructor = false;
    PsiMethod[] constructors = declaration.getConstructors();
    for (PsiMethod constructor : constructors) {
        if (evaluator.isPublic(constructor)) {
            if (constructor.getParameterList().getParametersCount() == 0) {
                hasDefaultConstructor = true;
                break;
            }
        }
    }
    if (constructors.length > 0 && !hasDefaultConstructor) {
        String message = String.format("This ControllerChangeHandler needs to have a public default constructor (`%1$s`)", declaration.getQualifiedName());
        context.report(ISSUE, declaration, context.getLocation(declaration), message);
    }
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) JavaEvaluator(com.android.tools.lint.client.api.JavaEvaluator)

Aggregations

JavaEvaluator (com.android.tools.lint.client.api.JavaEvaluator)2 PsiMethod (com.intellij.psi.PsiMethod)2 PsiParameter (com.intellij.psi.PsiParameter)1