diff options
Diffstat (limited to 'src/core/lombok/javac')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleWith.java (renamed from src/core/lombok/javac/handlers/HandleWither.java) | 88 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/JavacHandlerUtil.java | 32 |
2 files changed, 63 insertions, 57 deletions
diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWith.java index 9c95cb78..fd14e06a 100644 --- a/src/core/lombok/javac/handlers/HandleWither.java +++ b/src/core/lombok/javac/handlers/HandleWith.java @@ -29,10 +29,10 @@ import java.util.Collection; import lombok.AccessLevel; import lombok.ConfigurationKeys; +import lombok.With; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.configuration.CheckerFrameworkVersion; -import lombok.experimental.Wither; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; import lombok.javac.JavacTreeMaker; @@ -60,13 +60,13 @@ import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.Name; /** - * Handles the {@code lombok.experimental.Wither} annotation for javac. + * Handles the {@code lombok.With} 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 (hasAnnotation(Wither.class, typeNode)) { +public class HandleWith extends JavacAnnotationHandler<With> { + public void generateWithForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelWith) { + if (checkForTypeLevelWith) { + if (hasAnnotation(With.class, typeNode)) { //The annotation will make it happen, so we can skip it. return; } @@ -78,7 +78,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { 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."); + errorNode.addError("@With is only supported on a class or a field."); return; } @@ -92,105 +92,105 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { //Skip final initialized fields. if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null) continue; - generateWitherForField(field, errorNode.get(), level); + generateWithForField(field, errorNode.get(), level); } } /** - * Generates a wither on the stated field. + * Generates a with 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 + * If there is a {@code lombok.experimental.With} 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 + * If not, the with 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). + * @param fieldNode The node representing the field you want a with for. + * @param pos The node responsible for generating the with (the {@code @Value} or {@code @With} annotation). */ - public void generateWitherForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level) { - if (hasAnnotation(Wither.class, fieldNode)) { + public void generateWithForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level) { + if (hasAnnotation(With.class, fieldNode)) { //The annotation will make it happen, so we can skip it. return; } - createWitherForField(level, fieldNode, fieldNode, false, List.<JCAnnotation>nil(), List.<JCAnnotation>nil()); + createWithForField(level, fieldNode, fieldNode, false, List.<JCAnnotation>nil(), List.<JCAnnotation>nil()); } - @Override public void handle(AnnotationValues<Wither> annotation, JCAnnotation ast, JavacNode annotationNode) { - handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.WITHER_FLAG_USAGE, "@Wither"); + @Override public void handle(AnnotationValues<With> annotation, JCAnnotation ast, JavacNode annotationNode) { + handleFlagUsage(annotationNode, ConfigurationKeys.WITH_FLAG_USAGE, "@With"); Collection<JavacNode> fields = annotationNode.upFromAnnotationToFields(); - deleteAnnotationIfNeccessary(annotationNode, Wither.class); + deleteAnnotationIfNeccessary(annotationNode, With.class, "lombok.experimental.Wither"); deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel"); JavacNode node = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); if (level == AccessLevel.NONE || node == null) return; - List<JCAnnotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Wither(onMethod", annotationNode); - List<JCAnnotation> onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@Wither(onParam", annotationNode); + List<JCAnnotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@With(onMethod", annotationNode); + List<JCAnnotation> onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@With(onParam", annotationNode); switch (node.getKind()) { case FIELD: - createWitherForFields(level, fields, annotationNode, true, onMethod, onParam); + createWithForFields(level, fields, annotationNode, true, onMethod, onParam); break; case TYPE: - if (!onMethod.isEmpty()) annotationNode.addError("'onMethod' is not supported for @Wither on a type."); - if (!onParam.isEmpty()) annotationNode.addError("'onParam' is not supported for @Wither on a type."); - generateWitherForType(node, annotationNode, level, false); + if (!onMethod.isEmpty()) annotationNode.addError("'onMethod' is not supported for @With on a type."); + if (!onParam.isEmpty()) annotationNode.addError("'onParam' is not supported for @With on a type."); + generateWithForType(node, annotationNode, level, false); break; } } - public void createWitherForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { + public void createWithForFields(AccessLevel level, Collection<JavacNode> fieldNodes, JavacNode errorNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { for (JavacNode fieldNode : fieldNodes) { - createWitherForField(level, fieldNode, errorNode, whineIfExists, onMethod, onParam); + createWithForField(level, fieldNode, errorNode, whineIfExists, onMethod, onParam); } } - public void createWitherForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean strictMode, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { + public void createWithForField(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."); + fieldNode.addError("@With is only supported on a class or a field."); return; } - JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); - String methodName = toWitherName(fieldNode); + JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get(); + String methodName = toWithName(fieldNode); if (methodName == null) { - fieldNode.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list."); + fieldNode.addWarning("Not generating a withX method 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."); + fieldNode.addWarning("Not generating " + methodName + " for this field: With methods 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."); + fieldNode.addWarning("Not generating " + methodName + " for this field: With methods 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 $."); + fieldNode.addWarning("Not generating " + methodName + " for this field: With methods cannot be generated for fields starting with $."); } return; } - for (String altName : toAllWitherNames(fieldNode)) { + for (String altName : toAllWithNames(fieldNode)) { switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: return; @@ -210,22 +210,22 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { long access = toJavacModifier(level); - JCMethodDecl createdWither = createWither(access, fieldNode, fieldNode.getTreeMaker(), source, onMethod, onParam, makeAbstract); + JCMethodDecl createdWith = createWith(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); + injectMethod(typeNode, createdWith, List.<Type>of(getMirrorForFieldType(fieldNode)), returnType); } - public JCMethodDecl createWither(long access, JavacNode field, JavacTreeMaker maker, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam, boolean makeAbstract) { - String witherName = toWitherName(field); - if (witherName == null) return null; + public JCMethodDecl createWith(long access, JavacNode field, JavacTreeMaker maker, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam, boolean makeAbstract) { + String withName = toWithName(field); + if (withName == null) return null; JCVariableDecl fieldDecl = (JCVariableDecl) field.get(); List<JCAnnotation> copyableAnnotations = findCopyableAnnotations(field); - Name methodName = field.toName(witherName); + Name methodName = field.toName(withName); JCExpression returnType = cloneSelfType(field); @@ -287,8 +287,8 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { if (makeAbstract) access = access | Flags.ABSTRACT; JCMethodDecl decl = recursiveSetGeneratedBy(maker.MethodDef(maker.Modifiers(access, annsOnMethod), methodName, returnType, - methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source.get(), field.getContext()); - copyJavadoc(field, decl, CopyJavadoc.WITHER); + methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source.get(), field.getContext()); + copyJavadoc(field, decl, CopyJavadoc.WITH); return decl; } } diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index f23a894a..d12a980f 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -590,20 +590,20 @@ public class JavacHandlerUtil { } /** - * Translates the given field into all possible wither names. - * Convenient wrapper around {@link TransformationsUtil#toAllWitherNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + * Translates the given field into all possible with names. + * Convenient wrapper around {@link TransformationsUtil#toAllWithNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ - public static java.util.List<String> toAllWitherNames(JavacNode field) { - return HandlerUtil.toAllWitherNames(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); + public static java.util.List<String> toAllWithNames(JavacNode field) { + return HandlerUtil.toAllWithNames(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); } /** - * @return the likely wither name for the stated field. (e.g. private boolean foo; to withFoo). + * @return the likely with name for the stated field. (e.g. private boolean foo; to withFoo). * - * Convenient wrapper around {@link TransformationsUtil#toWitherName(lombok.core.AnnotationValues, CharSequence, boolean)}. + * Convenient wrapper around {@link TransformationsUtil#toWithName(lombok.core.AnnotationValues, CharSequence, boolean)}. */ - public static String toWitherName(JavacNode field) { - return HandlerUtil.toWitherName(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); + public static String toWithName(JavacNode field) { + return HandlerUtil.toWithName(field.getAst(), getAccessorsForField(field), field.getName(), isBoolean(field)); } /** @@ -1883,7 +1883,7 @@ public class JavacHandlerUtil { return (JCExpression) in; } - private static final Pattern SECTION_FINDER = Pattern.compile("^\\s*\\**\\s*[-*][-*]+\\s*([GS]ETTER|WITHER)\\s*[-*][-*]+\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); + private static final Pattern SECTION_FINDER = Pattern.compile("^\\s*\\**\\s*[-*][-*]+\\s*([GS]ETTER|WITH(?:ER)?)\\s*[-*][-*]+\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); public static String stripLinesWithTagFromJavadoc(String javadoc, String regexpFragment) { Pattern p = Pattern.compile("^\\s*\\**\\s*" + regexpFragment + "\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); @@ -1898,12 +1898,18 @@ public class JavacHandlerUtil { return javadoc.substring(0, m.start()); } - public static String getJavadocSection(String javadoc, String sectionName) { + public static String getJavadocSection(String javadoc, String sectionNameSpec) { + String[] sectionNames = sectionNameSpec.split("\\|"); Matcher m = SECTION_FINDER.matcher(javadoc); int sectionStart = -1; int sectionEnd = -1; while (m.find()) { - if (m.group(1).equalsIgnoreCase(sectionName)) { + boolean found = false; + for (String sectionName : sectionNames) if (m.group(1).equalsIgnoreCase(sectionName)) { + found = true; + break; + } + if (found) { sectionStart = m.end() + 1; } else if (sectionStart != -1) { sectionEnd = m.start(); @@ -1953,9 +1959,9 @@ public class JavacHandlerUtil { return applySetter(cu, node, "SETTER"); } }, - WITHER { + WITH { @Override public String apply(final JCCompilationUnit cu, final JavacNode node) { - return applySetter(cu, node, "WITHER"); + return applySetter(cu, node, "WITH|WITHER"); } }; |