use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class HandleSetter method createSetterForField.
public void createSetterForField(AccessLevel level, JavacNode fieldNode, JavacNode sourceNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
if (fieldNode.getKind() != Kind.FIELD) {
fieldNode.addError("@Setter is only supported on a class or a field.");
return;
}
JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get();
String methodName = toSetterName(fieldNode);
if (methodName == null) {
fieldNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
return;
}
if ((fieldDecl.mods.flags & Flags.FINAL) != 0) {
fieldNode.addWarning("Not generating setter for this field: Setters cannot be generated for final fields.");
return;
}
for (String altName : toAllSetterNames(fieldNode)) {
switch(methodExists(altName, fieldNode, false, 1)) {
case EXISTS_BY_LOMBOK:
return;
case EXISTS_BY_USER:
if (whineIfExists) {
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) | (fieldDecl.mods.flags & Flags.STATIC);
JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), sourceNode, onMethod, onParam);
Type fieldType = getMirrorForFieldType(fieldNode);
Type returnType;
if (shouldReturnThis(fieldNode)) {
ClassSymbol sym = ((JCClassDecl) fieldNode.up().get()).sym;
returnType = sym == null ? null : sym.type;
} else {
returnType = Javac.createVoidType(fieldNode.getSymbolTable(), CTC_VOID);
}
injectMethod(fieldNode.up(), createdSetter, fieldType == null ? null : List.of(fieldType), returnType);
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class HandleLog method selfType.
public static JCFieldAccess selfType(JavacNode typeNode) {
JavacTreeMaker maker = typeNode.getTreeMaker();
Name name = ((JCClassDecl) typeNode.get()).name;
return maker.Select(maker.Ident(name), typeNode.toName("class"));
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class HandleConstructor method checkLegality.
public static boolean checkLegality(JavacNode typeNode, JavacNode errorNode, String name) {
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)) != 0;
if (typeDecl == null || notAClass) {
errorNode.addError(name + " is only supported on a class or an enum.");
return false;
}
return true;
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class HandleConstructor method generateConstructor.
public void generateConstructor(JavacNode typeNode, AccessLevel level, List<JCAnnotation> onConstructor, List<JavacNode> fields, boolean allToDefault, String staticName, SkipIfConstructorExists skipIfConstructorExists, JavacNode source) {
boolean staticConstrRequired = staticName != null && !staticName.equals("");
if (skipIfConstructorExists != SkipIfConstructorExists.NO && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS)
return;
if (skipIfConstructorExists != SkipIfConstructorExists.NO) {
for (JavacNode child : typeNode.down()) {
if (child.getKind() == Kind.ANNOTATION) {
boolean skipGeneration = annotationTypeMatches(NoArgsConstructor.class, child) || annotationTypeMatches(AllArgsConstructor.class, child) || annotationTypeMatches(RequiredArgsConstructor.class, child);
if (!skipGeneration && skipIfConstructorExists == SkipIfConstructorExists.YES) {
skipGeneration = annotationTypeMatches(Builder.class, child);
}
if (skipGeneration) {
if (staticConstrRequired) {
// @Data has asked us to generate a constructor, but we're going to skip this instruction, as an explicit 'make a constructor' annotation
// will take care of it. However, @Data also wants a specific static name; this will be ignored; the appropriate way to do this is to use
// the 'staticName' parameter of the @XArgsConstructor you've stuck on your type.
// We should warn that we're ignoring @Data's 'staticConstructor' param.
source.addWarning("Ignoring static constructor name: explicit @XxxArgsConstructor annotation present; its `staticName` parameter will be used.");
}
return;
}
}
}
}
JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, onConstructor, typeNode, fields, allToDefault, source);
ListBuffer<Type> argTypes = new ListBuffer<Type>();
for (JavacNode fieldNode : fields) {
Type mirror = getMirrorForFieldType(fieldNode);
if (mirror == null) {
argTypes = null;
break;
}
argTypes.append(mirror);
}
List<Type> argTypes_ = argTypes == null ? null : argTypes.toList();
injectMethod(typeNode, constr, argTypes_, Javac.createVoidType(typeNode.getSymbolTable(), CTC_VOID));
if (staticConstrRequired) {
ClassSymbol sym = ((JCClassDecl) typeNode.get()).sym;
Type returnType = sym == null ? null : sym.type;
JCMethodDecl staticConstr = createStaticConstructor(staticName, level, typeNode, allToDefault ? List.<JavacNode>nil() : fields, source.get());
injectMethod(typeNode, staticConstr, argTypes_, returnType);
}
}
use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project lombok by rzwitserloot.
the class HandleWither method generateWitherForType.
public void generateWitherForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelWither) {
if (checkForTypeLevelWither) {
if (hasAnnotation(Wither.class, typeNode)) {
// The annotation will make it happen, so we can skip it.
return;
}
}
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("@Wither is only supported on a class or a field.");
return;
}
for (JavacNode field : typeNode.down()) {
if (field.getKind() != Kind.FIELD)
continue;
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
// Skip fields that start with $
if (fieldDecl.name.toString().startsWith("$"))
continue;
// Skip static fields.
if ((fieldDecl.mods.flags & Flags.STATIC) != 0)
continue;
// Skip final initialized fields.
if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null)
continue;
generateWitherForField(field, errorNode.get(), level);
}
}
Aggregations