Wither support + tests (javac only; ecj tests are currently set to ignore).

This commit is contained in:
Reinier Zwitserloot committed 2012-07-12 02:58:26 +02:00
1 parent 1865bd7309
commit 3cf9ffed29
40 files changed
+952 -102

No files matched your search

+63 -44
View File
@@ -104,20 +104,7 @@ public class TransformationsUtil {
* @return The getter name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
*/
public static String toGetterName(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
if (fieldName.length() == 0) return null;
Accessors ac = accessors.getInstance();
fieldName = removePrefix(fieldName, ac.prefix());
if (fieldName == null) return null;
if (ac.fluent()) return fieldName.toString();
if (isBoolean && fieldName.toString().startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fieldName.charAt(2))) {
// The field is for example named 'isRunning'.
return fieldName.toString();
}
return buildName(isBoolean ? "is" : "get", fieldName.toString());
return toAccessorName(accessors, fieldName, isBoolean, "is", "get", true);
}
/**
@@ -141,20 +128,50 @@ public class TransformationsUtil {
* @return The setter name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
*/
public static String toSetterName(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
return toAccessorName(accessors, fieldName, isBoolean, "set", "set", true);
}
/**
* Generates a wither name from a given field name.
*
* Strategy:
* <ul>
* <li>Reduce the field's name to its base name by stripping off any prefix (from {@code Accessors}). If the field name does not fit
* the prefix list, this method immediately returns {@code null}.</li>
* <li>Only if {@code isBoolean} is true: Check if the field starts with {@code is} followed by a non-lowercase character.
* If so, replace {@code is} with {@code with} and return that.</li>
* <li>Check if the first character of the field is lowercase. If so, check if the second character
* exists and is title or upper case. If so, uppercase the first character. If not, titlecase the first character.</li>
* <li>Return {@code "with"} plus the possibly title/uppercased first character, and the rest of the field name.</li>
* </ul>
*
* @param accessors Accessors configuration.
* @param fieldName the name of the field.
* @param isBoolean if the field is of type 'boolean'. For fields of type {@code java.lang.Boolean}, you should provide {@code false}.
* @return The wither name for this field, or {@code null} if this field does not fit expected patterns and therefore cannot be turned into a getter name.
*/
public static String toWitherName(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
return toAccessorName(accessors, fieldName, isBoolean, "with", "with", false);
}
private static String toAccessorName(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean,
String booleanPrefix, String normalPrefix, boolean adhereToFluent) {
if (fieldName.length() == 0) return null;
Accessors ac = accessors.getInstance();
fieldName = removePrefix(fieldName, ac.prefix());
if (fieldName == null) return null;
String fName = fieldName.toString();
if (ac.fluent()) return fName;
if (adhereToFluent && ac.fluent()) return fName;
if (isBoolean && fName.startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fName.charAt(2))) {
if (isBoolean && fName.startsWith("is") && fieldName.length() > 2 && !Character.isLowerCase(fieldName.charAt(2))) {
// The field is for example named 'isRunning'.
return "set" + fName.substring(2);
return booleanPrefix + fName.substring(2);
}
return buildName("set", fName);
return buildName(isBoolean ? booleanPrefix : normalPrefix, fName);
}
/**
@@ -168,28 +185,7 @@ public class TransformationsUtil {
* @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
*/
public static List<String> toAllGetterNames(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
if (!isBoolean) {
String getterName = toGetterName(accessors, fieldName, false);
return (getterName == null) ? Collections.<String>emptyList() : Collections.singletonList(getterName);
}
Accessors acc = accessors.getInstance();
fieldName = removePrefix(fieldName, acc.prefix());
if (fieldName == null) return Collections.emptyList();
List<String> baseNames = toBaseNames(fieldName, isBoolean, acc.fluent());
Set<String> names = new HashSet<String>();
for (String baseName : baseNames) {
if (acc.fluent()) {
names.add(baseName);
} else {
names.add(buildName("is", baseName));
names.add(buildName("get", baseName));
}
}
return new ArrayList<String>(names);
return toAllAccessorNames(accessors, fieldName, isBoolean, "is", "get", true);
}
/**
@@ -198,13 +194,34 @@ public class TransformationsUtil {
* For example if {@code isBoolean} is true, then a field named {@code isRunning} would produce:<br />
* {@code [setRunning, setIsRunning]}
*
* @param accessors Accessors configuration.
* @param fieldName the name of the field.
* @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
*/
public static List<String> toAllSetterNames(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
return toAllAccessorNames(accessors, fieldName, isBoolean, "set", "set", true);
}
/**
* Returns all names of methods that would represent the wither for a field with the provided name.
*
* For example if {@code isBoolean} is true, then a field named {@code isRunning} would produce:<br />
* {@code [withRunning, withIsRunning]}
*
* @param accessors Accessors configuration.
* @param fieldName the name of the field.
* @param isBoolean if the field is of type 'boolean'. For fields of type 'java.lang.Boolean', you should provide {@code false}.
*/
public static List<String> toAllWitherNames(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean) {
return toAllAccessorNames(accessors, fieldName, isBoolean, "with", "with", false);
}
private static List<String> toAllAccessorNames(AnnotationValues<Accessors> accessors, CharSequence fieldName, boolean isBoolean,
String booleanPrefix, String normalPrefix, boolean adhereToFluent) {
if (!isBoolean) {
String setterName = toSetterName(accessors, fieldName, false);
return (setterName == null) ? Collections.<String>emptyList() : Collections.singletonList(setterName);
String accessorName = toAccessorName(accessors, fieldName, false, booleanPrefix, normalPrefix, adhereToFluent);
return (accessorName == null) ? Collections.<String>emptyList() : Collections.singletonList(accessorName);
}
Accessors acc = accessors.getInstance();
@@ -215,14 +232,16 @@ public class TransformationsUtil {
Set<String> names = new HashSet<String>();
for (String baseName : baseNames) {
if (acc.fluent()) {
if (adhereToFluent && acc.fluent()) {
names.add(baseName);
} else {
names.add(buildName("set", baseName));
names.add(buildName(normalPrefix, baseName));
if (!normalPrefix.equals(booleanPrefix)) names.add(buildName(booleanPrefix, baseName));
}
}
return new ArrayList<String>(names);
}
private static List<String> toBaseNames(CharSequence fieldName, boolean isBoolean, boolean fluent) {
@@ -945,10 +945,7 @@ public class EclipseHandlerUtil {
* Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static List<String> toAllGetterNames(EclipseNode field, boolean isBoolean) {
String fieldName = field.getName();
AnnotationValues<Accessors> accessors = getAccessorsForField(field);
return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean);
return TransformationsUtil.toAllGetterNames(getAccessorsForField(field), field.getName(), isBoolean);
}
/**
@@ -957,10 +954,7 @@ public class EclipseHandlerUtil {
* Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static String toGetterName(EclipseNode field, boolean isBoolean) {
String fieldName = field.getName();
AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
return TransformationsUtil.toGetterName(accessors, fieldName, isBoolean);
return TransformationsUtil.toGetterName(getAccessorsForField(field), field.getName(), isBoolean);
}
/**
@@ -968,10 +962,7 @@ public class EclipseHandlerUtil {
* Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static java.util.List<String> toAllSetterNames(EclipseNode field, boolean isBoolean) {
String fieldName = field.getName();
AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
return TransformationsUtil.toAllSetterNames(accessors, fieldName, isBoolean);
return TransformationsUtil.toAllSetterNames(getAccessorsForField(field), field.getName(), isBoolean);
}
/**
@@ -980,10 +971,24 @@ public class EclipseHandlerUtil {
* Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static String toSetterName(EclipseNode field, boolean isBoolean) {
String fieldName = field.getName();
AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field);
return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean);
return TransformationsUtil.toSetterName(getAccessorsForField(field), field.getName(), isBoolean);
}
/**
* Translates the given field into all possible wither names.
* Convenient wrapper around {@link TransformationsUtil#toAllWitherNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static java.util.List<String> toAllWitherNames(EclipseNode field, boolean isBoolean) {
return TransformationsUtil.toAllWitherNames(getAccessorsForField(field), field.getName(), isBoolean);
}
/**
* @return the likely wither name for the stated field. (e.g. private boolean foo; to withFoo).
*
* Convenient wrapper around {@link TransformationsUtil#toWitherName(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static String toWitherName(EclipseNode field, boolean isBoolean) {
return TransformationsUtil.toWitherName(getAccessorsForField(field), field.getName(), isBoolean);
}
/**
+60
View File
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2012 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.experimental;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import lombok.AccessLevel;
/**
* Put on any field to make lombok build a 'wither' - a withX method which produces a clone of this object (except for 1 field which gets a new value).
* <p>
* Example:
* <pre>
* private &#64;Wither final int foo;
* </pre>
*
* will generate:
*
* <pre>
* public SELF_TYPE withFoo(int foo) {
* return this.foo == foo ? this : new SELF_TYPE(otherField1, otherField2, foo);
* }
* </pre>
*
* If any method named {@code withFoo} (case insensitive) exists, regardless of return type or parameters,
* no method is generated, and instead a compiler warning is emitted.
* <p>
* This annotation can also be applied to a class, in which case it'll be as if all non-static fields that don't already have
* a {@code Wither} annotation have the annotation.
*/
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Wither {
/**
* If you want your wither to be non-public, you can specify an alternate access level here.
*/
AccessLevel value() default AccessLevel.PUBLIC;
}
@@ -21,8 +21,8 @@
*/
package lombok.javac.handlers;
import static lombok.javac.Javac.getCtcInt;
import static lombok.javac.handlers.JavacHandlerUtil.*;
import static lombok.javac.Javac.*;
import java.util.Collection;
@@ -37,6 +37,7 @@ import lombok.core.AnnotationValues;
import lombok.core.TransformationsUtil;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
import lombok.javac.handlers.JavacHandlerUtil.FieldAccess;
import org.mangosdk.spi.ProviderFor;
@@ -171,6 +172,11 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
return;
}
if ((fieldDecl.mods.flags & Flags.FINAL) != 0) {
source.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:
@@ -224,20 +230,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
JCExpression methodType = null;
if (returnThis) {
JavacNode typeNode = field;
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 = ListBuffer.lb();
if (!type.typarams.isEmpty()) {
for (JCTypeParameter tp : type.typarams) {
typeArgs.append(treeMaker.Ident(tp.name));
}
methodType = treeMaker.TypeApply(treeMaker.Ident(type.name), typeArgs.toList());
} else {
methodType = treeMaker.Ident(type.name);
}
}
methodType = cloneSelfType(field);
}
if (methodType == null) {
@@ -0,0 +1,274 @@
/*
* Copyright (C) 2012 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.javac.handlers;
import static lombok.javac.Javac.getCtcInt;
import static lombok.javac.handlers.JavacHandlerUtil.*;
import java.util.Collection;
import lombok.AccessLevel;
import lombok.core.AST.Kind;
import lombok.core.AnnotationValues;
import lombok.core.TransformationsUtil;
import lombok.experimental.Wither;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
import lombok.javac.handlers.JavacHandlerUtil.FieldAccess;
import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCConditional;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCNewClass;
import com.sun.tools.javac.tree.JCTree.JCReturn;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
/**
* Handles the {@code lombok.experimental.Wither} annotation for javac.
*/
@ProviderFor(JavacAnnotationHandler.class)
public class HandleWither extends JavacAnnotationHandler<Wither> {
public void generateWitherForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelWither) {
if (checkForTypeLevelWither) {
if (typeNode != null) for (JavacNode child : typeNode.down()) {
if (child.getKind() == Kind.ANNOTATION) {
if (annotationTypeMatches(Wither.class, child)) {
//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);
}
}
/**
* 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.
*
* @param fieldNode The node representing the field you want a wither for.
* @param pos The node responsible for generating the wither (the {@code @Value} or {@code @Wither} annotation).
*/
public void generateWitherForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level) {
for (JavacNode 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;
}
}
}
createWitherForField(level, fieldNode, fieldNode, false);
}
@Override public void handle(AnnotationValues<Wither> annotation, JCAnnotation ast, JavacNode annotationNode) {
Collection<JavacNode> fields = annotationNode.upFromAnnotationToFields();
deleteAnnotationIfNeccessary(annotationNode, Wither.class);
deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
JavacNode node = annotationNode.up();
AccessLevel level = annotation.getInstance().value();
if (level == AccessLevel.NONE || node == null) return;
switch (node.getKind()) {
case FIELD:
createWitherForFields(level, fields, annotationNode, true);
break;
case TYPE:
generateWitherForType(node, annotationNode, level, false);
break;
}
}
private void createWitherForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists) {
for (JavacNode fieldNode : fieldNodes) {
createWitherForField(level, fieldNode, errorNode, whineIfExists);
}
}
private void createWitherForField(AccessLevel level,
JavacNode fieldNode, JavacNode source, boolean whineIfExists) {
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) {
source.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list.");
return;
}
if ((fieldDecl.mods.flags & Flags.STATIC) != 0) {
source.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) {
source.addWarning("Not generating wither for this field: Withers cannot be generated for final, initialized fields.");
return;
}
if (fieldDecl.name.toString().startsWith("$")) {
source.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 (whineIfExists) {
String altNameExpl = "";
if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName);
source.addWarning(
String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
}
return;
default:
case NOT_EXISTS:
//continue scanning the other alt names.
}
}
long access = toJavacModifier(level);
JCMethodDecl createdWither = createWither(access, fieldNode, fieldNode.getTreeMaker(), source.get());
injectMethod(fieldNode.up(), createdWither);
}
private JCMethodDecl createWither(long access, JavacNode field, TreeMaker treeMaker, JCTree source) {
String witherName = toWitherName(field);
if (witherName == null) return null;
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
ListBuffer<JCStatement> statements = ListBuffer.lb();
List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN);
Name methodName = field.toName(witherName);
List<JCAnnotation> annsOnParam = nonNulls.appendList(nullables);
JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null);
JCExpression selfType = cloneSelfType(field);
if (selfType == null) return null;
TreeMaker maker = field.getTreeMaker();
ListBuffer<JCExpression> args = ListBuffer.lb();
for (JavacNode child : field.up().down()) {
if (child.getKind() != Kind.FIELD) continue;
JCVariableDecl childDecl = (JCVariableDecl) child.get();
// Skip fields that start with $
if (childDecl.name.toString().startsWith("$")) continue;
long fieldFlags = childDecl.mods.flags;
// Skip static fields.
if ((fieldFlags & Flags.STATIC) != 0) continue;
// Skip initialized final fields.
if (((fieldFlags & Flags.FINAL) != 0) && childDecl.init != null) continue;
if (child.get() == field.get()) {
args.append(maker.Ident(fieldDecl.name));
} else {
args.append(createFieldAccessor(maker, child, FieldAccess.ALWAYS_FIELD));
}
}
JCNewClass newClass = maker.NewClass(null, List.<JCExpression>nil(), selfType, args.toList(), null);
JCExpression identityCheck = maker.Binary(getCtcInt(JCTree.class, "EQ"), createFieldAccessor(maker, field, FieldAccess.ALWAYS_FIELD), maker.Ident(fieldDecl.name));
JCConditional conditional = maker.Conditional(identityCheck, maker.Ident(field.toName("this")), newClass);
JCReturn returnStatement = maker.Return(conditional);
if (nonNulls.isEmpty()) {
statements.append(returnStatement);
} else {
JCStatement nullCheck = generateNullCheck(treeMaker, field);
if (nullCheck != null) statements.append(nullCheck);
statements.append(returnStatement);
}
JCExpression returnType = cloneSelfType(field);
JCBlock methodBody = treeMaker.Block(0, statements.toList());
List<JCTypeParameter> methodGenericParams = List.nil();
List<JCVariableDecl> parameters = List.of(param);
List<JCExpression> throwsClauses = List.nil();
JCExpression annotationMethodDefaultValue = null;
List<JCAnnotation> annsOnMethod = List.nil();
if (isFieldDeprecated(field)) {
annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(chainDots(field, "java", "lang", "Deprecated"), List.<JCExpression>nil()));
}
return recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, returnType,
methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source);
}
}
@@ -61,6 +61,7 @@ import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
import com.sun.tools.javac.tree.JCTree.JCNewArray;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.TreeScanner;
@@ -307,11 +308,7 @@ public class JavacHandlerUtil {
* Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static java.util.List<String> toAllGetterNames(JavacNode field) {
String fieldName = field.getName();
boolean isBoolean = isBoolean(field);
AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean);
return TransformationsUtil.toAllGetterNames(getAccessorsForField(field), field.getName(), isBoolean(field));
}
/**
@@ -320,11 +317,7 @@ public class JavacHandlerUtil {
* Convenient wrapper around {@link TransformationsUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static String toGetterName(JavacNode field) {
String fieldName = field.getName();
boolean isBoolean = isBoolean(field);
AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
return TransformationsUtil.toGetterName(accessors, fieldName, isBoolean);
return TransformationsUtil.toGetterName(getAccessorsForField(field), field.getName(), isBoolean(field));
}
/**
@@ -332,11 +325,7 @@ public class JavacHandlerUtil {
* Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static java.util.List<String> toAllSetterNames(JavacNode field) {
String fieldName = field.getName();
boolean isBoolean = isBoolean(field);
AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
return TransformationsUtil.toAllSetterNames(accessors, fieldName, isBoolean);
return TransformationsUtil.toAllSetterNames(getAccessorsForField(field), field.getName(), isBoolean(field));
}
/**
@@ -345,11 +334,24 @@ public class JavacHandlerUtil {
* Convenient wrapper around {@link TransformationsUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static String toSetterName(JavacNode field) {
String fieldName = field.getName();
boolean isBoolean = isBoolean(field);
AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean);
return TransformationsUtil.toSetterName(getAccessorsForField(field), field.getName(), isBoolean(field));
}
/**
* Translates the given field into all possible wither names.
* Convenient wrapper around {@link TransformationsUtil#toAllWitherNames(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static java.util.List<String> toAllWitherNames(JavacNode field) {
return TransformationsUtil.toAllWitherNames(getAccessorsForField(field), field.getName(), isBoolean(field));
}
/**
* @return the likely wither name for the stated field. (e.g. private boolean foo; to withFoo).
*
* Convenient wrapper around {@link TransformationsUtil#toWitherName(lombok.core.AnnotationValues, CharSequence, boolean)}.
*/
public static String toWitherName(JavacNode field) {
return TransformationsUtil.toWitherName(getAccessorsForField(field), field.getName(), isBoolean(field));
}
/**
@@ -366,6 +368,26 @@ public class JavacHandlerUtil {
return instance.chain() || (instance.fluent() && !forced);
}
public static JCExpression cloneSelfType(JavacNode field) {
JavacNode typeNode = field;
TreeMaker maker = field.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 = ListBuffer.lb();
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;
}
}
private static boolean isBoolean(JavacNode field) {
JCExpression varType = ((JCVariableDecl) field.get()).vartype;
return varType != null && varType.toString().equals("boolean");
@@ -0,0 +1,30 @@
class WitherAccessLevel {
boolean isNone;
boolean isPrivate;
boolean isPackage;
boolean isProtected;
boolean isPublic;
boolean value;
WitherAccessLevel(boolean isNone, boolean isPrivate, boolean isPackage, boolean isProtected, boolean isPublic, boolean value) {
}
@java.lang.SuppressWarnings("all")
private WitherAccessLevel withPrivate(final boolean isPrivate) {
return this.isPrivate == isPrivate ? this : new WitherAccessLevel(this.isNone, isPrivate, this.isPackage, this.isProtected, this.isPublic, this.value);
}
@java.lang.SuppressWarnings("all")
WitherAccessLevel withPackage(final boolean isPackage) {
return this.isPackage == isPackage ? this : new WitherAccessLevel(this.isNone, this.isPrivate, isPackage, this.isProtected, this.isPublic, this.value);
}
@java.lang.SuppressWarnings("all")
protected WitherAccessLevel withProtected(final boolean isProtected) {
return this.isProtected == isProtected ? this : new WitherAccessLevel(this.isNone, this.isPrivate, this.isPackage, isProtected, this.isPublic, this.value);
}
@java.lang.SuppressWarnings("all")
public WitherAccessLevel withPublic(final boolean isPublic) {
return this.isPublic == isPublic ? this : new WitherAccessLevel(this.isNone, this.isPrivate, this.isPackage, this.isProtected, isPublic, this.value);
}
@java.lang.SuppressWarnings("all")
public WitherAccessLevel withValue(final boolean value) {
return this.value == value ? this : new WitherAccessLevel(this.isNone, this.isPrivate, this.isPackage, this.isProtected, this.isPublic, value);
}
}
@@ -0,0 +1,71 @@
class Wither1 {
boolean foo;
void withFoo(boolean foo) {
}
Wither1(boolean foo) {
}
}
class Wither2 {
boolean foo;
void withFoo(String foo) {
}
Wither2(boolean foo) {
}
}
class Wither3 {
String foo;
void withFoo(boolean foo) {
}
Wither3(String foo) {
}
}
class Wither4 {
String foo;
void withFoo(String foo) {
}
Wither4(String foo) {
}
}
class Wither5 {
String foo;
void withFoo() {
}
Wither5(String foo) {
}
@java.lang.SuppressWarnings("all")
public Wither5 withFoo(final String foo) {
return this.foo == foo ? this : new Wither5(foo);
}
}
class Wither6 {
String foo;
void withFoo(String foo, int x) {
}
Wither6(String foo) {
}
@java.lang.SuppressWarnings("all")
public Wither6 withFoo(final String foo) {
return this.foo == foo ? this : new Wither6(foo);
}
}
class Wither7 {
String foo;
void withFoo(String foo, Object... x) {
}
Wither7(String foo) {
}
}
class Wither8 {
boolean isFoo;
void withIsFoo(boolean foo) {
}
Wither8(boolean foo) {
}
}
class Wither9 {
boolean isFoo;
void withFoo(boolean foo) {
}
Wither9(boolean foo) {
}
}
@@ -0,0 +1,23 @@
class WitherAndAllArgsConstructor<T, J extends T, L extends java.lang.Number> {
J test;
java.util.List<L> test2;
final int x = 10;
int y = 20;
final int z;
@java.beans.ConstructorProperties({"test", "test2", "y", "z"})
@java.lang.SuppressWarnings("all")
public WitherAndAllArgsConstructor(final J test, final java.util.List<L> test2, final int y, final int z) {
this.test = test;
this.test2 = test2;
this.y = y;
this.z = z;
}
@java.lang.SuppressWarnings("all")
public WitherAndAllArgsConstructor<T, J, L> withTest(final J test) {
return this.test == test ? this : new WitherAndAllArgsConstructor<T, J, L>(test, this.test2, this.y, this.z);
}
@java.lang.SuppressWarnings("all")
public WitherAndAllArgsConstructor<T, J, L> withTest2(final java.util.List<L> test2) {
return this.test2 == test2 ? this : new WitherAndAllArgsConstructor<T, J, L>(this.test, test2, this.y, this.z);
}
}
@@ -0,0 +1,20 @@
class WitherDeprecated {
@Deprecated
int annotation;
/**
* @deprecated
*/
int javadoc;
WitherDeprecated(int annotation, int javadoc) {
}
@java.lang.Deprecated
@java.lang.SuppressWarnings("all")
public WitherDeprecated withAnnotation(final int annotation) {
return this.annotation == annotation ? this : new WitherDeprecated(annotation, this.javadoc);
}
@java.lang.Deprecated
@java.lang.SuppressWarnings("all")
public WitherDeprecated withJavadoc(final int javadoc) {
return this.javadoc == javadoc ? this : new WitherDeprecated(this.annotation, javadoc);
}
}
@@ -0,0 +1,52 @@
class WitherOnClass1 {
boolean isNone;
boolean isPublic;
WitherOnClass1(boolean isNone, boolean isPublic) {
}
@java.lang.SuppressWarnings("all")
public WitherOnClass1 withPublic(final boolean isPublic) {
return this.isPublic == isPublic ? this : new WitherOnClass1(this.isNone, isPublic);
}
}
class WitherOnClass2 {
boolean isNone;
boolean isProtected;
boolean isPackage;
WitherOnClass2(boolean isNone, boolean isProtected, boolean isPackage) {
}
@java.lang.SuppressWarnings("all")
protected WitherOnClass2 withProtected(final boolean isProtected) {
return this.isProtected == isProtected ? this : new WitherOnClass2(this.isNone, isProtected, this.isPackage);
}
@java.lang.SuppressWarnings("all")
WitherOnClass2 withPackage(final boolean isPackage) {
return this.isPackage == isPackage ? this : new WitherOnClass2(this.isNone, this.isProtected, isPackage);
}
}
class WitherOnClass3 {
String couldBeNull;
@lombok.NonNull
String nonNull;
WitherOnClass3(String couldBeNull, String nonNull) {
}
@java.lang.SuppressWarnings("all")
public WitherOnClass3 withCouldBeNull(final String couldBeNull) {
return this.couldBeNull == couldBeNull ? this : new WitherOnClass3(couldBeNull, this.nonNull);
}
@java.lang.SuppressWarnings("all")
public WitherOnClass3 withNonNull(@lombok.NonNull final String nonNull) {
if (nonNull == null) throw new java.lang.NullPointerException("nonNull");
return this.nonNull == nonNull ? this : new WitherOnClass3(this.couldBeNull, nonNull);
}
}
class WitherOnClass4 {
final int fX = 10;
final int fY;
WitherOnClass4(int y) {
this.fY = y;
}
@java.lang.SuppressWarnings("all")
public WitherOnClass4 withY(final int fY) {
return this.fY == fY ? this : new WitherOnClass4(fY);
}
}
@@ -0,0 +1,4 @@
class WitherOnStatic {
static boolean foo;
static int bar;
}
@@ -0,0 +1,14 @@
class WitherPlain {
int i;
final int foo;
WitherPlain(int i, int foo) {
}
@java.lang.SuppressWarnings("all")
public WitherPlain withI(final int i) {
return this.i == i ? this : new WitherPlain(i, this.foo);
}
@java.lang.SuppressWarnings("all")
public WitherPlain withFoo(final int foo) {
return this.foo == foo ? this : new WitherPlain(this.i, foo);
}
}
@@ -0,0 +1,3 @@
class WitherWithDollar {
int $i;
}
@@ -0,0 +1,15 @@
class WitherWithGenerics<T, J extends T, L extends java.lang.Number> {
J test;
java.util.List<L> test2;
int $i;
public WitherWithGenerics(J test, java.util.List<L> test2) {
}
@java.lang.SuppressWarnings("all")
public WitherWithGenerics<T, J, L> withTest(final J test) {
return this.test == test ? this : new WitherWithGenerics<T, J, L>(test, this.test2);
}
@java.lang.SuppressWarnings("all")
public WitherWithGenerics<T, J, L> withTest2(final java.util.List<L> test2) {
return this.test2 == test2 ? this : new WitherWithGenerics<T, J, L>(this.test, test2);
}
}
@@ -0,0 +1 @@
//ignore
@@ -0,0 +1 @@
//ignore
@@ -0,0 +1 @@
//ignore
@@ -0,0 +1 @@
//ignore
@@ -0,0 +1 @@
//ignore
@@ -0,0 +1 @@
//ignore
@@ -0,0 +1 @@
//ignore
@@ -0,0 +1 @@
//ignore
@@ -0,0 +1 @@
//ignore
@@ -1,14 +1,19 @@
class SetterAccessLevel {
@lombok.Setter(lombok.AccessLevel.NONE)
boolean isNone;
@lombok.Setter(lombok.AccessLevel.PRIVATE)
boolean isPrivate;
@lombok.Setter(lombok.AccessLevel.PACKAGE)
boolean isPackage;
@lombok.Setter(lombok.AccessLevel.PROTECTED)
boolean isProtected;
@lombok.Setter(lombok.AccessLevel.PUBLIC)
boolean isPublic;
@lombok.Setter(value=lombok.AccessLevel.PUBLIC)
boolean value;
}
@@ -3,43 +3,51 @@ class Setter1 {
void setFoo(boolean foo) {
}
}
class Setter2 {
@lombok.Setter boolean foo;
void setFoo(String foo) {
}
}
class Setter3 {
@lombok.Setter String foo;
void setFoo(boolean foo) {
}
}
class Setter4 {
@lombok.Setter String foo;
void setFoo(String foo) {
}
}
class Setter5 {
@lombok.Setter String foo;
void setFoo() {
}
}
class Setter6 {
@lombok.Setter String foo;
void setFoo(String foo, int x) {
}
}
class Setter7 {
@lombok.Setter String foo;
void setFoo(String foo, Object... x) {
}
}
class Setter8 {
@lombok.Setter boolean isFoo;
void setIsFoo(boolean foo) {
}
}
class Setter9 {
@lombok.Setter boolean isFoo;
void setFoo(boolean foo) {
}
}
}
@@ -4,6 +4,7 @@ class SetterOnClass1 {
boolean isNone;
boolean isPublic;
}
@lombok.Setter(lombok.AccessLevel.PROTECTED)
class SetterOnClass2 {
@lombok.Setter(lombok.AccessLevel.NONE)
@@ -12,24 +13,28 @@ class SetterOnClass2 {
@lombok.Setter(lombok.AccessLevel.PACKAGE)
boolean isPackage;
}
@lombok.Setter(lombok.AccessLevel.PACKAGE)
class SetterOnClass3 {
@lombok.Setter(lombok.AccessLevel.NONE)
boolean isNone;
boolean isPackage;
}
@lombok.Setter(lombok.AccessLevel.PRIVATE)
class SetterOnClass4 {
@lombok.Setter(lombok.AccessLevel.NONE)
boolean isNone;
boolean isPrivate;
}
@lombok.Setter(lombok.AccessLevel.PUBLIC)
class SetterOnClass5 {
@lombok.Setter(lombok.AccessLevel.NONE)
boolean isNone;
boolean isPublic;
}
@lombok.Setter
class SetterOnClass6 {
String couldBeNull;
@@ -0,0 +1,24 @@
import lombok.AccessLevel;
class WitherAccessLevel {
@lombok.experimental.Wither(lombok.AccessLevel.NONE)
boolean isNone;
@lombok.experimental.Wither(AccessLevel.PRIVATE)
boolean isPrivate;
@lombok.experimental.Wither(lombok.AccessLevel.PACKAGE)
boolean isPackage;
@lombok.experimental.Wither(AccessLevel.PROTECTED)
boolean isProtected;
@lombok.experimental.Wither(lombok.AccessLevel.PUBLIC)
boolean isPublic;
@lombok.experimental.Wither(value=lombok.AccessLevel.PUBLIC)
boolean value;
WitherAccessLevel(boolean isNone, boolean isPrivate, boolean isPackage, boolean isProtected, boolean isPublic, boolean value) {
}
}
@@ -0,0 +1,89 @@
class Wither1 {
@lombok.experimental.Wither boolean foo;
void withFoo(boolean foo) {
}
Wither1(boolean foo) {
}
}
class Wither2 {
@lombok.experimental.Wither boolean foo;
void withFoo(String foo) {
}
Wither2(boolean foo) {
}
}
class Wither3 {
@lombok.experimental.Wither String foo;
void withFoo(boolean foo) {
}
Wither3(String foo) {
}
}
class Wither4 {
@lombok.experimental.Wither String foo;
void withFoo(String foo) {
}
Wither4(String foo) {
}
}
class Wither5 {
@lombok.experimental.Wither String foo;
void withFoo() {
}
Wither5(String foo) {
}
}
class Wither6 {
@lombok.experimental.Wither String foo;
void withFoo(String foo, int x) {
}
Wither6(String foo) {
}
}
class Wither7 {
@lombok.experimental.Wither String foo;
void withFoo(String foo, Object... x) {
}
Wither7(String foo) {
}
}
class Wither8 {
@lombok.experimental.Wither boolean isFoo;
void withIsFoo(boolean foo) {
}
Wither8(boolean foo) {
}
}
class Wither9 {
@lombok.experimental.Wither boolean isFoo;
void withFoo(boolean foo) {
}
Wither9(boolean foo) {
}
}
@@ -0,0 +1,12 @@
@lombok.AllArgsConstructor
class WitherAndAllArgsConstructor<T, J extends T, L extends java.lang.Number> {
@lombok.experimental.Wither J test;
@lombok.experimental.Wither java.util.List<L> test2;
final int x = 10;
int y = 20;
final int z;
}
@@ -0,0 +1,15 @@
import lombok.experimental.Wither;
class WitherDeprecated {
@Deprecated
@Wither int annotation;
/**
* @deprecated
*/
@Wither int javadoc;
WitherDeprecated(int annotation, int javadoc) {
}
}
@@ -0,0 +1,45 @@
@lombok.experimental.Wither
class WitherOnClass1 {
@lombok.experimental.Wither(lombok.AccessLevel.NONE)
boolean isNone;
boolean isPublic;
WitherOnClass1(boolean isNone, boolean isPublic) {
}
}
@lombok.experimental.Wither(lombok.AccessLevel.PROTECTED)
class WitherOnClass2 {
@lombok.experimental.Wither(lombok.AccessLevel.NONE)
boolean isNone;
boolean isProtected;
@lombok.experimental.Wither(lombok.AccessLevel.PACKAGE)
boolean isPackage;
WitherOnClass2(boolean isNone, boolean isProtected, boolean isPackage) {
}
}
@lombok.experimental.Wither
class WitherOnClass3 {
String couldBeNull;
@lombok.NonNull String nonNull;
WitherOnClass3(String couldBeNull, String nonNull) {
}
}
@lombok.experimental.Wither @lombok.experimental.Accessors(prefix="f")
class WitherOnClass4 {
final int fX = 10;
final int fY;
WitherOnClass4(int y) {
this.fY = y;
}
}
@@ -0,0 +1,4 @@
class WitherOnStatic {
@lombok.experimental.Wither static boolean foo;
@lombok.experimental.Wither static int bar;
}
@@ -0,0 +1,8 @@
import lombok.experimental.Wither;
class WitherPlain {
@lombok.experimental.Wither int i;
@Wither final int foo;
WitherPlain(int i, int foo) {
}
}
@@ -0,0 +1,3 @@
class WitherWithDollar {
@lombok.experimental.Wither int $i;
}
@@ -0,0 +1,8 @@
class WitherWithGenerics<T, J extends T, L extends java.lang.Number> {
@lombok.experimental.Wither J test;
@lombok.experimental.Wither java.util.List<L> test2;
int $i;
public WitherWithGenerics(J test, java.util.List<L> test2) {
}
}
@@ -1,7 +1,7 @@
2:9 WARNING Not generating setFoo(): A method with that name already exists
7:9 WARNING Not generating setFoo(): A method with that name already exists
12:9 WARNING Not generating setFoo(): A method with that name already exists
17:9 WARNING Not generating setFoo(): A method with that name already exists
32:9 WARNING Not generating setFoo(): A method with that name already exists
37:9 WARNING Not generating setFoo(): A method with that name already exists (setIsFoo)
42:9 WARNING Not generating setFoo(): A method with that name already exists
8:9 WARNING Not generating setFoo(): A method with that name already exists
14:9 WARNING Not generating setFoo(): A method with that name already exists
20:9 WARNING Not generating setFoo(): A method with that name already exists
38:9 WARNING Not generating setFoo(): A method with that name already exists
44:9 WARNING Not generating setFoo(): A method with that name already exists (setIsFoo)
50:9 WARNING Not generating setFoo(): A method with that name already exists
@@ -0,0 +1,7 @@
2:9 WARNING Not generating withFoo(): A method with that name already exists
12:9 WARNING Not generating withFoo(): A method with that name already exists
22:9 WARNING Not generating withFoo(): A method with that name already exists
32:9 WARNING Not generating withFoo(): A method with that name already exists
62:9 WARNING Not generating withFoo(): A method with that name already exists
72:9 WARNING Not generating withFoo(): A method with that name already exists (withIsFoo)
82:9 WARNING Not generating withFoo(): A method with that name already exists
@@ -0,0 +1,2 @@
2:9 WARNING Not generating wither for this field: Withers cannot be generated for static fields.
3:9 WARNING Not generating wither for this field: Withers cannot be generated for static fields.
@@ -0,0 +1 @@
2:9 WARNING Not generating wither for this field: Withers cannot be generated for fields starting with $.