use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project error-prone by google.
the class NonOverridingEquals method matchMethod.
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
if (!MATCHER.matches(methodTree, state)) {
return Description.NO_MATCH;
}
// this is a type-specific helper method and give advice to either inline it or rename it.
if (enclosingClassOverridesEquals.matches(methodTree, state)) {
return buildDescription(methodTree).setMessage(MESSAGE_BASE + "; if this is a type-specific helper for a method that does" + " override Object.equals, either inline it into the callers or rename it to" + " avoid ambiguity").build();
}
// Don't provide a fix if the method is static, non-public, or returns a boxed Boolean
if (noFixMatcher.matches(methodTree, state)) {
return describeMatch(methodTree);
}
JCClassDecl cls = (JCClassDecl) state.findEnclosing(ClassTree.class);
if ((cls.getModifiers().flags & ENUM) != 0) {
/* If the enclosing class is an enum, then just delete the equals method since enums
* should always be compared for reference equality. Enum defines a final equals method for
* just this reason. */
return buildDescription(methodTree).setMessage(MESSAGE_BASE + "; enum instances can safely be compared by reference " + "equality, so please delete this").addFix(SuggestedFix.delete(methodTree)).build();
} else {
/* Otherwise, change the covariant equals method to override Object.equals. */
SuggestedFix.Builder fix = SuggestedFix.builder();
// Add @Override annotation if not present.
if (ASTHelpers.getAnnotation(methodTree, Override.class) == null) {
fix.prefixWith(methodTree, "@Override\n");
}
// Change method signature, substituting Object for parameter type.
JCTree parameterType = (JCTree) methodTree.getParameters().get(0).getType();
Name parameterName = ((JCVariableDecl) methodTree.getParameters().get(0)).getName();
fix.replace(parameterType, "Object");
// If there is a method body...
if (methodTree.getBody() != null) {
// Add type check at start
String typeCheckStmt = "if (!(" + parameterName + " instanceof " + parameterType + ")) {\n" + " return false;\n" + "}\n";
fix.prefixWith(methodTree.getBody().getStatements().get(0), typeCheckStmt);
// Cast all uses of the parameter name using a recursive TreeScanner.
new CastScanner().scan(methodTree.getBody(), new CastState(parameterName, parameterType.toString(), fix));
}
return describeMatch(methodTree, fix.build());
}
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class JavacHandlerUtil method cloneSelfType.
public static JCExpression cloneSelfType(JavacNode childOfType) {
JavacNode typeNode = childOfType;
JavacTreeMaker maker = childOfType.getTreeMaker();
while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up();
if (typeNode != null && typeNode.get() instanceof JCClassDecl) {
JCClassDecl type = (JCClassDecl) typeNode.get();
ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>();
if (!type.typarams.isEmpty()) {
for (JCTypeParameter tp : type.typarams) {
typeArgs.append(maker.Ident(tp.name));
}
return maker.TypeApply(maker.Ident(type.name), typeArgs.toList());
} else {
return maker.Ident(type.name);
}
} else {
return null;
}
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class JavacHandlerUtil method isClassAndDoesNotHaveFlags.
public static boolean isClassAndDoesNotHaveFlags(JavacNode typeNode, int flags) {
JCClassDecl typeDecl = null;
if (typeNode.get() instanceof JCClassDecl)
typeDecl = (JCClassDecl) typeNode.get();
else
return false;
long typeDeclflags = typeDecl == null ? 0 : typeDecl.mods.flags;
return (typeDeclflags & flags) == 0;
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class HandleUtilityClass method changeModifiersAndGenerateConstructor.
private void changeModifiersAndGenerateConstructor(JavacNode typeNode, JavacNode errorNode) {
JCClassDecl classDecl = (JCClassDecl) typeNode.get();
boolean makeConstructor = true;
classDecl.mods.flags |= Flags.FINAL;
boolean markStatic = true;
if (typeNode.up().getKind() == Kind.COMPILATION_UNIT)
markStatic = false;
if (markStatic && typeNode.up().getKind() == Kind.TYPE) {
JCClassDecl typeDecl = (JCClassDecl) typeNode.up().get();
if ((typeDecl.mods.flags & Flags.INTERFACE) != 0)
markStatic = false;
}
if (markStatic)
classDecl.mods.flags |= Flags.STATIC;
for (JavacNode element : typeNode.down()) {
if (element.getKind() == Kind.FIELD) {
JCVariableDecl fieldDecl = (JCVariableDecl) element.get();
fieldDecl.mods.flags |= Flags.STATIC;
} else if (element.getKind() == Kind.METHOD) {
JCMethodDecl methodDecl = (JCMethodDecl) element.get();
if (methodDecl.name.contentEquals("<init>")) {
if (getGeneratedBy(methodDecl) == null && (methodDecl.mods.flags & Flags.GENERATEDCONSTR) == 0) {
element.addError("@UtilityClasses cannot have declared constructors.");
makeConstructor = false;
continue;
}
}
methodDecl.mods.flags |= Flags.STATIC;
} else if (element.getKind() == Kind.TYPE) {
JCClassDecl innerClassDecl = (JCClassDecl) element.get();
innerClassDecl.mods.flags |= Flags.STATIC;
}
}
if (makeConstructor)
createPrivateDefaultConstructor(typeNode);
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class JavacHandlerUtil method createFieldAccessor.
static JCExpression createFieldAccessor(JavacTreeMaker maker, JavacNode field, FieldAccess fieldAccess, JCExpression receiver) {
boolean lookForGetter = lookForGetter(field, fieldAccess);
GetterMethod getter = lookForGetter ? findGetter(field) : null;
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
if (getter == null) {
if (receiver == null) {
if ((fieldDecl.mods.flags & Flags.STATIC) == 0) {
receiver = maker.Ident(field.toName("this"));
} else {
JavacNode containerNode = field.up();
if (containerNode != null && containerNode.get() instanceof JCClassDecl) {
JCClassDecl container = (JCClassDecl) field.up().get();
receiver = maker.Ident(container.name);
}
}
}
return receiver == null ? maker.Ident(fieldDecl.name) : maker.Select(receiver, fieldDecl.name);
}
if (receiver == null)
receiver = maker.Ident(field.toName("this"));
JCMethodInvocation call = maker.Apply(List.<JCExpression>nil(), maker.Select(receiver, getter.name), List.<JCExpression>nil());
return call;
}
Aggregations