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);
}
}
};
}
Aggregations