aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok
diff options
context:
space:
mode:
authorJan Rieke <it@janrieke.de>2019-12-13 23:32:16 +0100
committerJan <it@janrieke.de>2020-02-06 16:05:56 +0100
commit4a34ed75b49f259823739df5691f1d81f37c12ca (patch)
tree42cd0fbab82872f3ba8379253908bf181636a782 /src/core/lombok
parenta678e7effbe4f98e8cecc523e060951d443c0ab4 (diff)
downloadlombok-4a34ed75b49f259823739df5691f1d81f37c12ca.tar.gz
lombok-4a34ed75b49f259823739df5691f1d81f37c12ca.tar.bz2
lombok-4a34ed75b49f259823739df5691f1d81f37c12ca.zip
avoid more name clashes for builder type param; fixes #2297
Diffstat (limited to 'src/core/lombok')
-rw-r--r--[-rwxr-xr-x]src/core/lombok/eclipse/handlers/HandleSuperBuilder.java37
-rw-r--r--src/core/lombok/javac/handlers/HandleSuperBuilder.java35
2 files changed, 59 insertions, 13 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java
index 306c6439..12fa9b2c 100755..100644
--- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java
+++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java
@@ -29,6 +29,7 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
@@ -222,12 +223,12 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
// <C, B> are the generics for our builder.
String classGenericName = "C";
String builderGenericName = "B";
- // If these generics' names collide with any generics on the annotated class, modify them.
+ // We have to make sure that the generics' names do not collide with any generics on the annotated class,
+ // the classname itself, or any member type name of the annotated class.
// For instance, if there are generics <B, B2, C> on the annotated class, use "C2" and "B3" for our builder.
- java.util.List<String> typeParamStrings = new ArrayList<String>();
- for (TypeParameter typeParam : typeParams) typeParamStrings.add(typeParam.toString());
- classGenericName = generateNonclashingNameFor(classGenericName, typeParamStrings);
- builderGenericName = generateNonclashingNameFor(builderGenericName, typeParamStrings);
+ java.util.Set<String> usedNames = gatherUsedTypeNames(typeParams, td);
+ classGenericName = generateNonclashingNameFor(classGenericName, usedNames);
+ builderGenericName = generateNonclashingNameFor(builderGenericName, usedNames);
TypeReference extendsClause = td.superclass;
TypeReference superclassBuilderClass = null;
@@ -415,7 +416,7 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
}
}
}
-
+
private EclipseNode generateBuilderAbstractClass(EclipseNode tdParent, String builderClass,
TypeReference superclassBuilderClass, TypeParameter[] typeParams,
ASTNode source, String classGenericName, String builderGenericName) {
@@ -1052,7 +1053,29 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> {
return null;
}
- private String generateNonclashingNameFor(String classGenericName, java.util.List<String> typeParamStrings) {
+ private java.util.Set<String> gatherUsedTypeNames(TypeParameter[] typeParams, TypeDeclaration td) {
+ java.util.HashSet<String> usedNames = new HashSet<String>();
+
+ // 1. Add type parameter names.
+ for (TypeParameter typeParam : typeParams)
+ usedNames.add(typeParam.toString());
+
+ // 2. Add class name.
+ usedNames.add(String.valueOf(td.name));
+
+ // 3. Add used type names.
+ if (td.fields != null) {
+ for (FieldDeclaration field : td.fields) {
+ char[][] typeName = field.type.getTypeName();
+ if (typeName.length >= 1) // Add the first token, because only that can collide.
+ usedNames.add(String.valueOf(typeName[0]));
+ }
+ }
+
+ return usedNames;
+ }
+
+ private String generateNonclashingNameFor(String classGenericName, java.util.Set<String> typeParamStrings) {
if (!typeParamStrings.contains(classGenericName)) return classGenericName;
int counter = 2;
while (typeParamStrings.contains(classGenericName + counter)) counter++;
diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java
index ba330325..650b0964 100644
--- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java
+++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java
@@ -27,6 +27,7 @@ import static lombok.javac.handlers.JavacHandlerUtil.*;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashSet;
import javax.lang.model.element.Modifier;
@@ -209,12 +210,12 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> {
// <C, B> are the generics for our builder.
String classGenericName = "C";
String builderGenericName = "B";
- // If these generics' names collide with any generics on the annotated class, modify them.
+ // We have to make sure that the generics' names do not collide with any generics on the annotated class,
+ // the classname itself, or any member type name of the annotated class.
// For instance, if there are generics <B, B2, C> on the annotated class, use "C2" and "B3" for our builder.
- java.util.List<String> typeParamStrings = new ArrayList<String>();
- for (JCTypeParameter typeParam : typeParams) typeParamStrings.add(typeParam.getName().toString());
- classGenericName = generateNonclashingNameFor(classGenericName, typeParamStrings);
- builderGenericName = generateNonclashingNameFor(builderGenericName, typeParamStrings);
+ java.util.HashSet<String> usedNames = gatherUsedTypeNames(typeParams, td);
+ classGenericName = generateNonclashingNameFor(classGenericName, usedNames);
+ builderGenericName = generateNonclashingNameFor(builderGenericName, usedNames);
thrownExceptions = List.nil();
@@ -987,7 +988,29 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> {
return null;
}
- private String generateNonclashingNameFor(String classGenericName, java.util.List<String> typeParamStrings) {
+ private java.util.HashSet<String> gatherUsedTypeNames(List<JCTypeParameter> typeParams, JCClassDecl td) {
+ java.util.HashSet<String> usedNames = new HashSet<String>();
+
+ // 1. Add type parameter names.
+ for (JCTypeParameter typeParam : typeParams)
+ usedNames.add(typeParam.getName().toString());
+
+ // 2. Add class name.
+ usedNames.add(td.name.toString());
+
+ // 3. Add used type names.
+ for (JCTree member : td.getMembers()) {
+ if (member.getKind() == com.sun.source.tree.Tree.Kind.VARIABLE && member instanceof JCVariableDecl) {
+ JCTree type = ((JCVariableDecl)member).getType();
+ if (type instanceof JCIdent)
+ usedNames.add(((JCIdent)type).getName().toString());
+ }
+ }
+
+ return usedNames;
+ }
+
+ private String generateNonclashingNameFor(String classGenericName, java.util.HashSet<String> typeParamStrings) {
if (!typeParamStrings.contains(classGenericName)) return classGenericName;
int counter = 2;
while (typeParamStrings.contains(classGenericName + counter)) counter++;