diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2015-01-20 15:01:30 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2015-01-20 15:01:30 +0100 |
commit | 5b4ab9dae8940973ce8b87c8c2a0388b768dac89 (patch) | |
tree | 1c6b9f00f7e1ae5e7e0ec72b2b6e4d823ee911a8 /src/core/lombok/eclipse | |
parent | 857da88e3e2af440876bf79bebd6e32e55895cf8 (diff) | |
download | lombok-5b4ab9dae8940973ce8b87c8c2a0388b768dac89.tar.gz lombok-5b4ab9dae8940973ce8b87c8c2a0388b768dac89.tar.bz2 lombok-5b4ab9dae8940973ce8b87c8c2a0388b768dac89.zip |
added the ‘always use guava’ lombok.config option for @Singular @Builder.
Diffstat (limited to 'src/core/lombok/eclipse')
7 files changed, 78 insertions, 10 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java b/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java index eca918d7..df8bd665 100644 --- a/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java +++ b/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java @@ -175,7 +175,7 @@ public class EclipseSingularsRecipes { char[] name = fd.name; if (name == null) continue; if (getGeneratedBy(fd) != null) continue; - for (char[] fieldToBeGenerated : listFieldsToBeGenerated(data)) { + for (char[] fieldToBeGenerated : listFieldsToBeGenerated(data, builderType)) { if (!Arrays.equals(name, fieldToBeGenerated)) continue; child.addError("Manually adding a field that @Singular @Builder would generate is not supported. If you want to manually manage the builder aspect for this field/parameter, don't use @Singular."); return true; @@ -187,7 +187,7 @@ public class EclipseSingularsRecipes { char[] name = method.selector; if (name == null) continue; if (getGeneratedBy(method) != null) continue; - for (char[] methodToBeGenerated : listMethodsToBeGenerated(data)) { + for (char[] methodToBeGenerated : listMethodsToBeGenerated(data, builderType)) { if (!Arrays.equals(name, methodToBeGenerated)) continue; child.addError("Manually adding a method that @Singular @Builder would generate is not supported. If you want to manually manage the builder aspect for this field/parameter, don't use @Singular."); return true; @@ -199,11 +199,11 @@ public class EclipseSingularsRecipes { return false; } - public List<char[]> listFieldsToBeGenerated(SingularData data) { + public List<char[]> listFieldsToBeGenerated(SingularData data, EclipseNode builderType) { return Collections.singletonList(data.pluralName); } - public List<char[]> listMethodsToBeGenerated(SingularData data) { + public List<char[]> listMethodsToBeGenerated(SingularData data, EclipseNode builderType) { char[] p = data.pluralName; char[] s = data.singularName; if (Arrays.equals(p, s)) return Collections.singletonList(p); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java index 775d4ed5..8d54da6f 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import lombok.core.GuavaTypeMap; import lombok.core.handlers.HandlerUtil; import lombok.eclipse.EclipseNode; import lombok.eclipse.handlers.EclipseSingularsRecipes.EclipseSingularizer; @@ -64,13 +65,11 @@ abstract class EclipseGuavaSingularizer extends EclipseSingularizer { }; protected String getSimpleTargetTypeName(SingularData data) { - String simpleTypeName = data.getTargetSimpleType(); - if ("ImmutableCollection".equals(simpleTypeName)) return "ImmutableList"; - return simpleTypeName; + return GuavaTypeMap.getGuavaTypeName(data.getTargetFqn()); } protected char[] getBuilderMethodName(SingularData data) { - String simpleTypeName = data.getTargetSimpleType(); + String simpleTypeName = getSimpleTargetTypeName(data); if ("ImmutableSortedSet".equals(simpleTypeName) || "ImmutableSortedMap".equals(simpleTypeName)) return "naturalOrder".toCharArray(); return "builder".toCharArray(); } diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java index f64748a8..1d1c4dbd 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java @@ -50,7 +50,27 @@ import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; abstract class EclipseJavaUtilListSetSingularizer extends EclipseJavaUtilSingularizer { + @Override public List<char[]> listFieldsToBeGenerated(SingularData data, EclipseNode builderType) { + if (useGuavaInstead(builderType)) { + return guavaListSetSingularizer.listFieldsToBeGenerated(data, builderType); + } + + return super.listFieldsToBeGenerated(data, builderType); + } + + @Override public List<char[]> listMethodsToBeGenerated(SingularData data, EclipseNode builderType) { + if (useGuavaInstead(builderType)) { + return guavaListSetSingularizer.listMethodsToBeGenerated(data, builderType); + } + + return super.listMethodsToBeGenerated(data, builderType); + } + @Override public List<EclipseNode> generateFields(SingularData data, EclipseNode builderType) { + if (useGuavaInstead(builderType)) { + return guavaListSetSingularizer.generateFields(data, builderType); + } + TypeReference type = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS); type = addTypeArgs(1, false, builderType, type, data.getTypeArgs()); @@ -64,6 +84,11 @@ abstract class EclipseJavaUtilListSetSingularizer extends EclipseJavaUtilSingula } @Override public void generateMethods(SingularData data, EclipseNode builderType, boolean fluent, boolean chain) { + if (useGuavaInstead(builderType)) { + guavaListSetSingularizer.generateMethods(data, builderType, fluent, chain); + return; + } + TypeReference returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0); Statement returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null; generateSingularMethod(returnType, returnStatement, data, builderType, fluent); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java index 4f67b5c0..0784ac4f 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java @@ -56,6 +56,11 @@ public class EclipseJavaUtilListSingularizer extends EclipseJavaUtilListSetSingu } @Override public void appendBuildCode(SingularData data, EclipseNode builderType, List<Statement> statements, char[] targetVariableName) { + if (useGuavaInstead(builderType)) { + guavaListSetSingularizer.appendBuildCode(data, builderType, statements, targetVariableName); + return; + } + List<Statement> switchContents = new ArrayList<Statement>(); /* case 0: (empty) break; */ { diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java index ff23823b..640bd396 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java @@ -61,7 +61,11 @@ public class EclipseJavaUtilMapSingularizer extends EclipseJavaUtilSingularizer return LombokImmutableList.of("java.util.Map", "java.util.SortedMap", "java.util.NavigableMap"); } - @Override public List<char[]> listFieldsToBeGenerated(SingularData data) { + @Override public List<char[]> listFieldsToBeGenerated(SingularData data, EclipseNode builderType) { + if (useGuavaInstead(builderType)) { + return guavaMapSingularizer.listFieldsToBeGenerated(data, builderType); + } + char[] p = data.getPluralName(); int len = p.length; char[] k = new char[len + 4]; @@ -81,7 +85,19 @@ public class EclipseJavaUtilMapSingularizer extends EclipseJavaUtilSingularizer return Arrays.asList(k, v); } - @Override public java.util.List<EclipseNode> generateFields(SingularData data, EclipseNode builderType) { + @Override public List<char[]> listMethodsToBeGenerated(SingularData data, EclipseNode builderType) { + if (useGuavaInstead(builderType)) { + return guavaMapSingularizer.listFieldsToBeGenerated(data, builderType); + } else { + return super.listMethodsToBeGenerated(data, builderType); + } + } + + @Override public List<EclipseNode> generateFields(SingularData data, EclipseNode builderType) { + if (useGuavaInstead(builderType)) { + return guavaMapSingularizer.generateFields(data, builderType); + } + char[] keyName = (new String(data.getPluralName()) + "$key").toCharArray(); char[] valueName = (new String(data.getPluralName()) + "$value").toCharArray(); FieldDeclaration buildKeyField; { @@ -113,6 +129,11 @@ public class EclipseJavaUtilMapSingularizer extends EclipseJavaUtilSingularizer } @Override public void generateMethods(SingularData data, EclipseNode builderType, boolean fluent, boolean chain) { + if (useGuavaInstead(builderType)) { + guavaMapSingularizer.generateMethods(data, builderType, fluent, chain); + return; + } + TypeReference returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0); Statement returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null; generateSingularMethod(returnType, returnStatement, data, builderType, fluent); @@ -235,6 +256,11 @@ public class EclipseJavaUtilMapSingularizer extends EclipseJavaUtilSingularizer } @Override public void appendBuildCode(SingularData data, EclipseNode builderType, List<Statement> statements, char[] targetVariableName) { + if (useGuavaInstead(builderType)) { + guavaMapSingularizer.appendBuildCode(data, builderType, statements, targetVariableName); + return; + } + if (data.getTargetFqn().equals("java.util.Map")) { statements.addAll(createJavaUtilSetMapInitialCapacitySwitchStatements(data, builderType, true, "emptyMap", "singletonMap", "LinkedHashMap")); } else { diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSetSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSetSingularizer.java index d4d87879..2d16eae0 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSetSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSetSingularizer.java @@ -38,6 +38,11 @@ public class EclipseJavaUtilSetSingularizer extends EclipseJavaUtilListSetSingul } @Override public void appendBuildCode(SingularData data, EclipseNode builderType, List<Statement> statements, char[] targetVariableName) { + if (useGuavaInstead(builderType)) { + guavaListSetSingularizer.appendBuildCode(data, builderType, statements, targetVariableName); + return; + } + if (data.getTargetFqn().equals("java.util.Set")) { statements.addAll(createJavaUtilSetMapInitialCapacitySwitchStatements(data, builderType, false, "emptySet", "singleton", "LinkedHashSet")); } else { diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSingularizer.java index 60c0f3ad..e4c399ed 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilSingularizer.java @@ -57,6 +57,7 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; +import lombok.ConfigurationKeys; import lombok.eclipse.EclipseNode; import lombok.eclipse.handlers.EclipseSingularsRecipes.EclipseSingularizer; import lombok.eclipse.handlers.EclipseSingularsRecipes.SingularData; @@ -82,6 +83,13 @@ abstract class EclipseJavaUtilSingularizer extends EclipseSingularizer { {'j', 'a', 'v', 'a'}, {'u', 't', 'i', 'l'}, {'C', 'o', 'l', 'l', 'e', 'c', 't', 'i', 'o', 'n', 's'} }; + protected final EclipseSingularizer guavaListSetSingularizer = new EclipseGuavaSetListSingularizer(); + protected final EclipseSingularizer guavaMapSingularizer = new EclipseGuavaMapSingularizer(); + + protected boolean useGuavaInstead(EclipseNode node) { + return Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.BUILDER_USE_GUAVA)); + } + protected List<Statement> createJavaUtilSetMapInitialCapacitySwitchStatements(SingularData data, EclipseNode builderType, boolean mapMode, String emptyCollectionMethod, String singletonCollectionMethod, String targetType) { List<Statement> switchContents = new ArrayList<Statement>(); char[] keyName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName(); |