use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class HandleUtilityClass method checkLegality.
private static boolean checkLegality(JavacNode typeNode, JavacNode errorNode) {
JCClassDecl typeDecl = null;
if (typeNode.get() instanceof JCClassDecl)
typeDecl = (JCClassDecl) typeNode.get();
long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0;
if (typeDecl == null || notAClass) {
errorNode.addError("@UtilityClass is only supported on a class (can't be an interface, enum, or annotation).");
return false;
}
// It might be an inner class. This is okay, but only if it is / can be a static inner class. Thus, all of its parents have to be static inner classes until the top-level.
JavacNode typeWalk = typeNode;
while (true) {
typeWalk = typeWalk.up();
switch(typeWalk.getKind()) {
case TYPE:
JCClassDecl typeDef = (JCClassDecl) typeWalk.get();
if ((typeDef.mods.flags & (Flags.STATIC | Flags.ANNOTATION | Flags.ENUM | Flags.INTERFACE)) != 0)
continue;
if (typeWalk.up().getKind() == Kind.COMPILATION_UNIT)
return true;
errorNode.addError("@UtilityClass automatically makes the class static, however, this class cannot be made static.");
return false;
case COMPILATION_UNIT:
return true;
default:
errorNode.addError("@UtilityClass cannot be placed on a method local or anonymous inner class, or any class nested in such a class.");
return false;
}
}
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class HandleWither method createWitherForField.
public void createWitherForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean strictMode, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
JavacNode typeNode = fieldNode.up();
boolean makeAbstract = typeNode != null && typeNode.getKind() == Kind.TYPE && (((JCClassDecl) typeNode.get()).mods.flags & Flags.ABSTRACT) != 0;
if (fieldNode.getKind() != Kind.FIELD) {
fieldNode.addError("@Wither is only supported on a class or a field.");
return;
}
JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get();
String methodName = toWitherName(fieldNode);
if (methodName == null) {
fieldNode.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list.");
return;
}
if ((fieldDecl.mods.flags & Flags.STATIC) != 0) {
if (strictMode) {
fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for static fields.");
}
return;
}
if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null) {
if (strictMode) {
fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for final, initialized fields.");
}
return;
}
if (fieldDecl.name.toString().startsWith("$")) {
if (strictMode) {
fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for fields starting with $.");
}
return;
}
for (String altName : toAllWitherNames(fieldNode)) {
switch(methodExists(altName, fieldNode, false, 1)) {
case EXISTS_BY_LOMBOK:
return;
case EXISTS_BY_USER:
if (strictMode) {
String altNameExpl = "";
if (!altName.equals(methodName))
altNameExpl = String.format(" (%s)", altName);
fieldNode.addWarning(String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
}
return;
default:
case NOT_EXISTS:
}
}
long access = toJavacModifier(level);
JCMethodDecl createdWither = createWither(access, fieldNode, fieldNode.getTreeMaker(), source, onMethod, onParam, makeAbstract);
ClassSymbol sym = ((JCClassDecl) fieldNode.up().get()).sym;
Type returnType = sym == null ? null : sym.type;
injectMethod(typeNode, createdWither, List.<Type>of(getMirrorForFieldType(fieldNode)), returnType);
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class JavacHandlerUtil method deleteAnnotationIfNeccessary0.
private static void deleteAnnotationIfNeccessary0(JavacNode annotation, Class<? extends Annotation>... annotationTypes) {
if (inNetbeansEditor(annotation))
return;
if (!annotation.shouldDeleteLombokAnnotations())
return;
JavacNode parentNode = annotation.directUp();
switch(parentNode.getKind()) {
case FIELD:
case ARGUMENT:
case LOCAL:
JCVariableDecl variable = (JCVariableDecl) parentNode.get();
variable.mods.annotations = filterList(variable.mods.annotations, annotation.get());
break;
case METHOD:
JCMethodDecl method = (JCMethodDecl) parentNode.get();
method.mods.annotations = filterList(method.mods.annotations, annotation.get());
break;
case TYPE:
try {
JCClassDecl type = (JCClassDecl) parentNode.get();
type.mods.annotations = filterList(type.mods.annotations, annotation.get());
} catch (ClassCastException e) {
//something rather odd has been annotated. Better to just break only delombok instead of everything.
}
break;
default:
//This really shouldn't happen, but if it does, better just break delombok instead of breaking everything.
return;
}
parentNode.getAst().setChanged();
for (Class<?> annotationType : annotationTypes) {
deleteImportFromCompilationUnit(annotation, annotationType.getName());
}
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class PrettyPrinter method printClassMembers.
private void printClassMembers(List<JCTree> members, boolean isEnum, boolean isInterface) {
Class<?> prefType = null;
// 1 = normal, 2 = with body, 3 = no enum field yet.
int typeOfPrevEnumMember = isEnum ? 3 : 0;
boolean prevWasEnumMember = isEnum;
for (JCTree member : members) {
if (typeOfPrevEnumMember == 3 && member instanceof JCMethodDecl && (((JCMethodDecl) member).mods.flags & GENERATEDCONSTR) != 0)
continue;
boolean isEnumVar = isEnum && member instanceof JCVariableDecl && (((JCVariableDecl) member).mods.flags & ENUM) != 0;
if (!isEnumVar && prevWasEnumMember) {
prevWasEnumMember = false;
if (typeOfPrevEnumMember == 3)
align();
println(";");
}
if (isEnumVar) {
if (prefType != null && prefType != JCVariableDecl.class)
println();
switch(typeOfPrevEnumMember) {
case 1:
print(", ");
break;
case 2:
println(",");
align();
break;
}
print(member);
JCTree init = ((JCVariableDecl) member).init;
typeOfPrevEnumMember = init instanceof JCNewClass && ((JCNewClass) init).def != null ? 2 : 1;
} else if (member instanceof JCVariableDecl) {
if (prefType != null && prefType != JCVariableDecl.class)
println();
if (isInterface)
flagMod = -1L & ~(PUBLIC | STATIC | FINAL);
print(member);
} else if (member instanceof JCMethodDecl) {
if ((((JCMethodDecl) member).mods.flags & GENERATEDCONSTR) != 0)
continue;
if (prefType != null)
println();
if (isInterface)
flagMod = -1L & ~(PUBLIC | ABSTRACT);
print(member);
} else if (member instanceof JCClassDecl) {
if (prefType != null)
println();
if (isInterface)
flagMod = -1L & ~(PUBLIC | STATIC);
print(member);
} else {
if (prefType != null)
println();
print(member);
}
prefType = member.getClass();
}
if (prevWasEnumMember) {
prevWasEnumMember = false;
if (typeOfPrevEnumMember == 3)
align();
println(";");
}
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class JavacHandlerUtil method injectField.
private static JavacNode injectField(JavacNode typeNode, JCVariableDecl field, boolean addGenerated) {
JCClassDecl type = (JCClassDecl) typeNode.get();
if (addGenerated) {
addSuppressWarningsAll(field.mods, typeNode, field.pos, getGeneratedBy(field), typeNode.getContext());
addGenerated(field.mods, typeNode, field.pos, getGeneratedBy(field), typeNode.getContext());
}
List<JCTree> insertAfter = null;
List<JCTree> insertBefore = type.defs;
while (true) {
boolean skip = false;
if (insertBefore.head instanceof JCVariableDecl) {
JCVariableDecl f = (JCVariableDecl) insertBefore.head;
if (isEnumConstant(f) || isGenerated(f))
skip = true;
} else if (insertBefore.head instanceof JCMethodDecl) {
if ((((JCMethodDecl) insertBefore.head).mods.flags & GENERATEDCONSTR) != 0)
skip = true;
}
if (skip) {
insertAfter = insertBefore;
insertBefore = insertBefore.tail;
continue;
}
break;
}
List<JCTree> fieldEntry = List.<JCTree>of(field);
fieldEntry.tail = insertBefore;
if (insertAfter == null) {
type.defs = fieldEntry;
} else {
insertAfter.tail = fieldEntry;
}
return typeNode.add(field, Kind.FIELD);
}
Aggregations