use of lombok.javac.JavacNode in project lombok by rzwitserloot.
the class JavacHandlerUtil method createListOfNonExistentFields.
/**
* Given a list of field names and a node referring to a type, finds each name in the list that does not match a field within the type.
*/
public static List<Integer> createListOfNonExistentFields(List<String> list, JavacNode type, boolean excludeStandard, boolean excludeTransient) {
boolean[] matched = new boolean[list.size()];
for (JavacNode child : type.down()) {
if (list.isEmpty())
break;
if (child.getKind() != Kind.FIELD)
continue;
JCVariableDecl field = (JCVariableDecl) child.get();
if (excludeStandard) {
if ((field.mods.flags & Flags.STATIC) != 0)
continue;
if (field.name.toString().startsWith("$"))
continue;
}
if (excludeTransient && (field.mods.flags & Flags.TRANSIENT) != 0)
continue;
int idx = list.indexOf(child.getName());
if (idx > -1)
matched[idx] = true;
}
ListBuffer<Integer> problematic = new ListBuffer<Integer>();
for (int i = 0; i < list.size(); i++) {
if (!matched[i])
problematic.append(i);
}
return problematic.toList();
}
use of lombok.javac.JavacNode in project lombok by rzwitserloot.
the class JavacJavaUtilMapSingularizer method generateFields.
@Override
public java.util.List<JavacNode> generateFields(SingularData data, JavacNode builderType, JCTree source) {
if (useGuavaInstead(builderType)) {
return guavaMapSingularizer.generateFields(data, builderType, source);
}
JavacTreeMaker maker = builderType.getTreeMaker();
JCVariableDecl buildKeyField;
{
JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
type = addTypeArgs(1, false, builderType, type, data.getTypeArgs(), source);
buildKeyField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$key"), type, null);
}
JCVariableDecl buildValueField;
{
JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
List<JCExpression> tArgs = data.getTypeArgs();
if (tArgs != null && tArgs.size() > 1)
tArgs = tArgs.tail;
else
tArgs = List.nil();
type = addTypeArgs(1, false, builderType, type, tArgs, source);
buildValueField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$value"), type, null);
}
JavacNode valueFieldNode = injectFieldAndMarkGenerated(builderType, buildValueField);
JavacNode keyFieldNode = injectFieldAndMarkGenerated(builderType, buildKeyField);
return Arrays.asList(keyFieldNode, valueFieldNode);
}
use of lombok.javac.JavacNode in project lombok by rzwitserloot.
the class JavacHandlerUtil method sanityCheckForMethodGeneratingAnnotationsOnBuilderClass.
public static void sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(JavacNode typeNode, JavacNode errorNode) {
List<String> disallowed = List.nil();
for (JavacNode child : typeNode.down()) {
for (Class<? extends java.lang.annotation.Annotation> annType : INVALID_ON_BUILDERS) {
if (annotationTypeMatches(annType, child)) {
disallowed = disallowed.append(annType.getSimpleName());
}
}
}
int size = disallowed.size();
if (size == 0)
return;
if (size == 1) {
errorNode.addError("@" + disallowed.head + " is not allowed on builder classes.");
return;
}
StringBuilder out = new StringBuilder();
for (String a : disallowed) out.append("@").append(a).append(", ");
out.setLength(out.length() - 2);
errorNode.addError(out.append(" are not allowed on builder classes.").toString());
}
use of lombok.javac.JavacNode in project lombok by rzwitserloot.
the class JavacHandlerUtil method injectMethod.
/**
* Adds the given new method declaration to the provided type AST Node.
* Can also inject constructors.
*
* Also takes care of updating the JavacAST.
*/
public static void injectMethod(JavacNode typeNode, JCMethodDecl method, List<Type> paramTypes, Type returnType) {
JCClassDecl type = (JCClassDecl) typeNode.get();
if (method.getName().contentEquals("<init>")) {
//Scan for default constructor, and remove it.
int idx = 0;
for (JCTree def : type.defs) {
if (def instanceof JCMethodDecl) {
if ((((JCMethodDecl) def).mods.flags & Flags.GENERATEDCONSTR) != 0) {
JavacNode tossMe = typeNode.getNodeFor(def);
if (tossMe != null)
tossMe.up().removeChild(tossMe);
type.defs = addAllButOne(type.defs, idx);
ClassSymbolMembersField.remove(type.sym, ((JCMethodDecl) def).sym);
break;
}
}
idx++;
}
}
addSuppressWarningsAll(method.mods, typeNode, method.pos, getGeneratedBy(method), typeNode.getContext());
addGenerated(method.mods, typeNode, method.pos, getGeneratedBy(method), typeNode.getContext());
type.defs = type.defs.append(method);
fixMethodMirror(typeNode.getContext(), typeNode.getElement(), method.getModifiers().flags, method.getName(), paramTypes, returnType);
typeNode.add(method, Kind.METHOD);
}
use of lombok.javac.JavacNode in project lombok by rzwitserloot.
the class JavacHandlerUtil method findGetter.
private static GetterMethod findGetter(JavacNode field) {
JCVariableDecl decl = (JCVariableDecl) field.get();
JavacNode typeNode = field.up();
for (String potentialGetterName : toAllGetterNames(field)) {
for (JavacNode potentialGetter : typeNode.down()) {
if (potentialGetter.getKind() != Kind.METHOD)
continue;
JCMethodDecl method = (JCMethodDecl) potentialGetter.get();
if (!method.name.toString().equalsIgnoreCase(potentialGetterName))
continue;
/** static getX() methods don't count. */
if ((method.mods.flags & Flags.STATIC) != 0)
continue;
/** Nor do getters with a non-empty parameter list. */
if (method.params != null && method.params.size() > 0)
continue;
return new GetterMethod(method.name, method.restype);
}
}
// Check if the field has a @Getter annotation.
boolean hasGetterAnnotation = false;
for (JavacNode child : field.down()) {
if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
//Definitely WONT have a getter.
if (ann.getInstance().value() == AccessLevel.NONE)
return null;
hasGetterAnnotation = true;
}
}
if (!hasGetterAnnotation && new HandleGetter().fieldQualifiesForGetterGeneration(field)) {
//Check if the class has @Getter or @Data annotation.
JavacNode containingType = field.up();
if (containingType != null)
for (JavacNode child : containingType.down()) {
if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Data.class, child))
hasGetterAnnotation = true;
if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
//Definitely WONT have a getter.
if (ann.getInstance().value() == AccessLevel.NONE)
return null;
hasGetterAnnotation = true;
}
}
}
if (hasGetterAnnotation) {
String getterName = toGetterName(field);
if (getterName == null)
return null;
return new GetterMethod(field.toName(getterName), decl.vartype);
}
return null;
}
Aggregations