use of lombok.eclipse.EclipseNode in project lombok by rzwitserloot.
the class HandleWither method generateWitherForType.
public boolean generateWitherForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean checkForTypeLevelWither) {
if (checkForTypeLevelWither) {
if (hasAnnotation(Wither.class, typeNode)) {
//The annotation will make it happen, so we can skip it.
return true;
}
}
TypeDeclaration typeDecl = null;
if (typeNode.get() instanceof TypeDeclaration)
typeDecl = (TypeDeclaration) typeNode.get();
int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0;
if (typeDecl == null || notAClass) {
pos.addError("@Wither is only supported on a class or a field.");
return false;
}
for (EclipseNode field : typeNode.down()) {
if (field.getKind() != Kind.FIELD)
continue;
FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
if (!filterField(fieldDecl))
continue;
//Skip final fields.
if ((fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0 && fieldDecl.initialization != null)
continue;
generateWitherForField(field, pos, level);
}
return true;
}
use of lombok.eclipse.EclipseNode in project lombok by rzwitserloot.
the class HandleWither method generateWitherForField.
/**
* Generates a wither on the stated field.
*
* Used by {@link HandleValue}.
*
* The difference between this call and the handle method is as follows:
*
* If there is a {@code lombok.experimental.Wither} annotation on the field, it is used and the
* same rules apply (e.g. warning if the method already exists, stated access level applies).
* If not, the wither is still generated if it isn't already there, though there will not
* be a warning if its already there. The default access level is used.
*/
public void generateWitherForField(EclipseNode fieldNode, EclipseNode sourceNode, AccessLevel level) {
for (EclipseNode child : fieldNode.down()) {
if (child.getKind() == Kind.ANNOTATION) {
if (annotationTypeMatches(Wither.class, child)) {
//The annotation will make it happen, so we can skip it.
return;
}
}
}
List<Annotation> empty = Collections.emptyList();
createWitherForField(level, fieldNode, sourceNode, false, empty, empty);
}
use of lombok.eclipse.EclipseNode in project lombok by rzwitserloot.
the class EclipseHandlerUtil method findGetter.
private static GetterMethod findGetter(EclipseNode field) {
FieldDeclaration fieldDeclaration = (FieldDeclaration) field.get();
boolean forceBool = FieldDeclaration_booleanLazyGetter.get(fieldDeclaration);
TypeReference fieldType = fieldDeclaration.type;
boolean isBoolean = forceBool || isBoolean(fieldType);
EclipseNode typeNode = field.up();
for (String potentialGetterName : toAllGetterNames(field, isBoolean)) {
for (EclipseNode potentialGetter : typeNode.down()) {
if (potentialGetter.getKind() != Kind.METHOD)
continue;
if (!(potentialGetter.get() instanceof MethodDeclaration))
continue;
MethodDeclaration method = (MethodDeclaration) potentialGetter.get();
if (!potentialGetterName.equalsIgnoreCase(new String(method.selector)))
continue;
/** static getX() methods don't count. */
if ((method.modifiers & ClassFileConstants.AccStatic) != 0)
continue;
/** Nor do getters with a non-empty parameter list. */
if (method.arguments != null && method.arguments.length > 0)
continue;
return new GetterMethod(method.selector, method.returnType);
}
}
// Check if the field has a @Getter annotation.
boolean hasGetterAnnotation = false;
for (EclipseNode 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.
EclipseNode containingType = field.up();
if (containingType != null)
for (EclipseNode 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, isBoolean);
if (getterName == null)
return null;
return new GetterMethod(getterName.toCharArray(), fieldType);
}
return null;
}
use of lombok.eclipse.EclipseNode in project lombok by rzwitserloot.
the class EclipseHandlerUtil method sanityCheckForMethodGeneratingAnnotationsOnBuilderClass.
public static void sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(EclipseNode typeNode, EclipseNode errorNode) {
List<String> disallowed = null;
for (EclipseNode child : typeNode.down()) {
if (child.getKind() != Kind.ANNOTATION)
continue;
for (Class<? extends java.lang.annotation.Annotation> annType : INVALID_ON_BUILDERS) {
if (annotationTypeMatches(annType, child)) {
if (disallowed == null)
disallowed = new ArrayList<String>();
disallowed.add(annType.getSimpleName());
}
}
}
int size = disallowed == null ? 0 : disallowed.size();
if (size == 0)
return;
if (size == 1) {
errorNode.addError("@" + disallowed.get(0) + " 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.eclipse.EclipseNode in project lombok by rzwitserloot.
the class EclipseHandlerUtil method cloneSelfType.
public static TypeReference cloneSelfType(EclipseNode context, ASTNode source) {
int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd;
long p = (long) pS << 32 | pE;
EclipseNode type = context;
TypeReference result = null;
while (type != null && type.getKind() != Kind.TYPE) type = type.up();
if (type != null && type.get() instanceof TypeDeclaration) {
TypeDeclaration typeDecl = (TypeDeclaration) type.get();
if (typeDecl.typeParameters != null && typeDecl.typeParameters.length > 0) {
TypeReference[] refs = new TypeReference[typeDecl.typeParameters.length];
int idx = 0;
for (TypeParameter param : typeDecl.typeParameters) {
TypeReference typeRef = new SingleTypeReference(param.name, (long) param.sourceStart << 32 | param.sourceEnd);
if (source != null)
setGeneratedBy(typeRef, source);
refs[idx++] = typeRef;
}
result = new ParameterizedSingleTypeReference(typeDecl.name, refs, 0, p);
} else {
result = new SingleTypeReference(((TypeDeclaration) type.get()).name, p);
}
}
if (result != null && source != null)
setGeneratedBy(result, source);
return result;
}
Aggregations