diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2015-01-26 13:52:50 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2015-01-26 13:52:50 +0100 |
commit | 30beb498ca46772e60dd321a07e19be1d08d1e9a (patch) | |
tree | 7fe984d4c745408b1dd2cfc9f51d3205a258bbf1 | |
parent | 75da1e2481d077d6c39b91aa9d364a397a41461f (diff) | |
download | lombok-30beb498ca46772e60dd321a07e19be1d08d1e9a.tar.gz lombok-30beb498ca46772e60dd321a07e19be1d08d1e9a.tar.bz2 lombok-30beb498ca46772e60dd321a07e19be1d08d1e9a.zip |
optimized the creation for singular lists/collections.
9 files changed, 37 insertions, 77 deletions
diff --git a/doc/changelog.markdown b/doc/changelog.markdown index d50089af..55a75fa0 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -10,9 +10,7 @@ Lombok Changelog * Builder __TODO TODO TODO TODO DO NOT SHIP YET__: - * features web page - * Check if the shadowed localvar names are properly typed; if compatible subtypes, we should cast these to avoid accidentally calling an overload. - * Review if there are nay potentially breaking changes in the pipeline for builder BEFORE moving it out of experimental. + * Review if there are any potentially breaking changes in the pipeline for builder BEFORE moving it out of experimental. * Make sure you cover the fact that builder has moved on from experimental in this issue, and on the features page /doc! * __TODO TODO TODO TODO DO NOT SHIP YET__: At least modify the deprecated warning of the constructorProperties thing that we're gonna kill it soon. diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java index 0784ac4f..0a9eaf75 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSingularizer.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.List; import lombok.core.LombokImmutableList; +import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseNode; import lombok.eclipse.handlers.EclipseSingularsRecipes.EclipseSingularizer; import lombok.eclipse.handlers.EclipseSingularsRecipes.SingularData; @@ -90,34 +91,26 @@ public class EclipseJavaUtilListSingularizer extends EclipseJavaUtilListSetSingu switchContents.add(new BreakStatement(null, 0, 0)); } - /* default: Create with right size, then add all */ { + /* default: Create by passing builder field to constructor. */ { switchContents.add(new CaseStatement(null, 0, 0)); - /* pluralName = new j.u.ArrayList<Generics>(this.pluralName.size()); */ { - Expression[] args = new Expression[] {getSize(builderType, data.getPluralName(), false)}; + Expression argToUnmodifiable; + /* new j.u.ArrayList<Generics>(this.pluralName); */ { + FieldReference thisDotPluralName = new FieldReference(data.getPluralName(), 0L); + thisDotPluralName.receiver = new ThisReference(0, 0); TypeReference targetTypeExpr = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS); targetTypeExpr = addTypeArgs(1, false, builderType, targetTypeExpr, data.getTypeArgs()); AllocationExpression constructorCall = new AllocationExpression(); constructorCall.type = targetTypeExpr; - constructorCall.arguments = args; - switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0L), constructorCall, 0)); - } - - /* pluralname.addAll(this.pluralname); */ { - FieldReference thisDotPluralName = new FieldReference(data.getPluralName(), 0L); - thisDotPluralName.receiver = new ThisReference(0, 0); - MessageSend addAllInvoke = new MessageSend(); - addAllInvoke.receiver = new SingleNameReference(data.getPluralName(), 0L); - addAllInvoke.selector = new char[] { 'a', 'd', 'd', 'A', 'l', 'l' }; - addAllInvoke.arguments = new Expression[] {thisDotPluralName}; - switchContents.add(addAllInvoke); + constructorCall.arguments = new Expression[] {thisDotPluralName}; + argToUnmodifiable = constructorCall; } - /* pluralname = Collections.unmodifiableList(pluralname); */ { + /* pluralname = Collections.unmodifiableList(-newlist-); */ { MessageSend unmodInvoke = new MessageSend(); unmodInvoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0); unmodInvoke.selector = "unmodifiableList".toCharArray(); - unmodInvoke.arguments = new Expression[] {new SingleNameReference(data.getPluralName(), 0L)}; + unmodInvoke.arguments = new Expression[] {argToUnmodifiable}; switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), unmodInvoke, 0)); } } @@ -126,7 +119,7 @@ public class EclipseJavaUtilListSingularizer extends EclipseJavaUtilListSetSingu switchStat.statements = switchContents.toArray(new Statement[switchContents.size()]); switchStat.expression = getSize(builderType, data.getPluralName(), true); - TypeReference localShadowerType = new QualifiedTypeReference(JAVA_UTIL_LIST, NULL_POSS); + TypeReference localShadowerType = new QualifiedTypeReference(Eclipse.fromQualifiedName(data.getTargetFqn()), NULL_POSS); localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs()); LocalDeclaration varDefStat = new LocalDeclaration(data.getPluralName(), 0, 0); varDefStat.type = localShadowerType; diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSingularizer.java index 2cf34cf7..65e91fa0 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSingularizer.java @@ -88,7 +88,7 @@ public class JavacJavaUtilListSingularizer extends JavacJavaUtilListSetSingulari } JCStatement switchStat = maker.Switch(getSize(maker, builderType, data.getPluralName(), true), cases.toList()); - JCExpression localShadowerType = chainDots(builderType, "java", "util", "List"); + JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn()); localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs(), source); JCStatement varDefStat = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, null); statements.append(varDefStat); @@ -99,29 +99,22 @@ public class JavacJavaUtilListSingularizer extends JavacJavaUtilListSetSingulari List<JCExpression> jceBlank = List.nil(); Name thisName = builderType.toName("this"); - JCStatement createStat; { - // pluralName = new java.util.ArrayList<Generics>(this.pluralName.size()); + JCExpression argToUnmodifiable; { + // new java.util.ArrayList<Generics>(this.pluralName); List<JCExpression> constructorArgs = List.nil(); - constructorArgs = List.<JCExpression>of(getSize(maker, builderType, data.getPluralName(), false)); + JCExpression thisDotPluralName = maker.Select(maker.Ident(thisName), data.getPluralName()); + constructorArgs = List.<JCExpression>of(thisDotPluralName); JCExpression targetTypeExpr = chainDots(builderType, "java", "util", "ArrayList"); targetTypeExpr = addTypeArgs(1, false, builderType, targetTypeExpr, data.getTypeArgs(), source); - JCExpression constructorCall = maker.NewClass(null, jceBlank, targetTypeExpr, constructorArgs, null); - createStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), constructorCall)); - } - - JCStatement fillStat; { - // pluralname.addAll(this.pluralname); - JCExpression thisDotPluralName = maker.Select(maker.Ident(thisName), data.getPluralName()); - fillStat = maker.Exec(maker.Apply(jceBlank, maker.Select(maker.Ident(data.getPluralName()), builderType.toName("addAll")), List.of(thisDotPluralName))); + argToUnmodifiable = maker.NewClass(null, jceBlank, targetTypeExpr, constructorArgs, null); } JCStatement unmodifiableStat; { - // pluralname = Collections.unmodifiableInterfaceType(pluralname); - JCExpression arg = maker.Ident(data.getPluralName()); - JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "unmodifiableList"), List.of(arg)); + // pluralname = Collections.unmodifiableInterfaceType(-newlist-); + JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "unmodifiableList"), List.of(argToUnmodifiable)); unmodifiableStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke)); } - return List.of(createStat, fillStat, unmodifiableStat); + return List.of(unmodifiableStat); } } diff --git a/test/transform/resource/after-delombok/BuilderSingularLists.java b/test/transform/resource/after-delombok/BuilderSingularLists.java index aa5382c1..0d074e92 100644 --- a/test/transform/resource/after-delombok/BuilderSingularLists.java +++ b/test/transform/resource/after-delombok/BuilderSingularLists.java @@ -66,11 +66,9 @@ class BuilderSingularLists<T> { children = java.util.Collections.singletonList(this.children.get(0)); break; default: - children = new java.util.ArrayList<T>(this.children.size()); - children.addAll(this.children); - children = java.util.Collections.unmodifiableList(children); + children = java.util.Collections.unmodifiableList(new java.util.ArrayList<T>(this.children)); } - java.util.List<Number> scarves; + java.util.Collection<Number> scarves; switch (this.scarves == null ? 0 : this.scarves.size()) { case 0: scarves = java.util.Collections.emptyList(); @@ -79,9 +77,7 @@ class BuilderSingularLists<T> { scarves = java.util.Collections.singletonList(this.scarves.get(0)); break; default: - scarves = new java.util.ArrayList<Number>(this.scarves.size()); - scarves.addAll(this.scarves); - scarves = java.util.Collections.unmodifiableList(scarves); + scarves = java.util.Collections.unmodifiableList(new java.util.ArrayList<Number>(this.scarves)); } java.util.List<java.lang.Object> rawList; switch (this.rawList == null ? 0 : this.rawList.size()) { @@ -92,9 +88,7 @@ class BuilderSingularLists<T> { rawList = java.util.Collections.singletonList(this.rawList.get(0)); break; default: - rawList = new java.util.ArrayList<java.lang.Object>(this.rawList.size()); - rawList.addAll(this.rawList); - rawList = java.util.Collections.unmodifiableList(rawList); + rawList = java.util.Collections.unmodifiableList(new java.util.ArrayList<java.lang.Object>(this.rawList)); } return new BuilderSingularLists<T>(children, scarves, rawList); } diff --git a/test/transform/resource/after-delombok/BuilderSingularNoAutosingularize.java b/test/transform/resource/after-delombok/BuilderSingularNoAutosingularize.java index 07bbef9c..5f985e16 100644 --- a/test/transform/resource/after-delombok/BuilderSingularNoAutosingularize.java +++ b/test/transform/resource/after-delombok/BuilderSingularNoAutosingularize.java @@ -64,9 +64,7 @@ class BuilderSingularNoAutosingularize { things = java.util.Collections.singletonList(this.things.get(0)); break; default: - things = new java.util.ArrayList<String>(this.things.size()); - things.addAll(this.things); - things = java.util.Collections.unmodifiableList(things); + things = java.util.Collections.unmodifiableList(new java.util.ArrayList<String>(this.things)); } java.util.List<String> widgets; switch (this.widgets == null ? 0 : this.widgets.size()) { @@ -77,9 +75,7 @@ class BuilderSingularNoAutosingularize { widgets = java.util.Collections.singletonList(this.widgets.get(0)); break; default: - widgets = new java.util.ArrayList<String>(this.widgets.size()); - widgets.addAll(this.widgets); - widgets = java.util.Collections.unmodifiableList(widgets); + widgets = java.util.Collections.unmodifiableList(new java.util.ArrayList<String>(this.widgets)); } java.util.List<String> items; switch (this.items == null ? 0 : this.items.size()) { @@ -90,9 +86,7 @@ class BuilderSingularNoAutosingularize { items = java.util.Collections.singletonList(this.items.get(0)); break; default: - items = new java.util.ArrayList<String>(this.items.size()); - items.addAll(this.items); - items = java.util.Collections.unmodifiableList(items); + items = java.util.Collections.unmodifiableList(new java.util.ArrayList<String>(this.items)); } return new BuilderSingularNoAutosingularize(things, widgets, items); } diff --git a/test/transform/resource/after-ecj/BuilderSingularLists.java b/test/transform/resource/after-ecj/BuilderSingularLists.java index c0a3ef9a..9e4bb894 100644 --- a/test/transform/resource/after-ecj/BuilderSingularLists.java +++ b/test/transform/resource/after-ecj/BuilderSingularLists.java @@ -56,11 +56,9 @@ import lombok.Singular; children = java.util.Collections.singletonList(this.children.get(0)); break; default : - children = new java.util.ArrayList<T>(this.children.size()); - children.addAll(this.children); - children = java.util.Collections.unmodifiableList(children); + children = java.util.Collections.unmodifiableList(new java.util.ArrayList<T>(this.children)); } - java.util.List<Number> scarves; + java.util.Collection<Number> scarves; switch (((this.scarves == null) ? 0 : this.scarves.size())) { case 0 : scarves = java.util.Collections.emptyList(); @@ -69,9 +67,7 @@ import lombok.Singular; scarves = java.util.Collections.singletonList(this.scarves.get(0)); break; default : - scarves = new java.util.ArrayList<Number>(this.scarves.size()); - scarves.addAll(this.scarves); - scarves = java.util.Collections.unmodifiableList(scarves); + scarves = java.util.Collections.unmodifiableList(new java.util.ArrayList<Number>(this.scarves)); } java.util.List<java.lang.Object> rawList; switch (((this.rawList == null) ? 0 : this.rawList.size())) { @@ -82,9 +78,7 @@ import lombok.Singular; rawList = java.util.Collections.singletonList(this.rawList.get(0)); break; default : - rawList = new java.util.ArrayList<java.lang.Object>(this.rawList.size()); - rawList.addAll(this.rawList); - rawList = java.util.Collections.unmodifiableList(rawList); + rawList = java.util.Collections.unmodifiableList(new java.util.ArrayList<java.lang.Object>(this.rawList)); } return new BuilderSingularLists<T>(children, scarves, rawList); } diff --git a/test/transform/resource/after-ecj/BuilderSingularNoAutosingularize.java b/test/transform/resource/after-ecj/BuilderSingularNoAutosingularize.java index 53bed757..16febc2e 100644 --- a/test/transform/resource/after-ecj/BuilderSingularNoAutosingularize.java +++ b/test/transform/resource/after-ecj/BuilderSingularNoAutosingularize.java @@ -54,9 +54,7 @@ import lombok.Singular; things = java.util.Collections.singletonList(this.things.get(0)); break; default : - things = new java.util.ArrayList<String>(this.things.size()); - things.addAll(this.things); - things = java.util.Collections.unmodifiableList(things); + things = java.util.Collections.unmodifiableList(new java.util.ArrayList<String>(this.things)); } java.util.List<String> widgets; switch (((this.widgets == null) ? 0 : this.widgets.size())) { @@ -67,9 +65,7 @@ import lombok.Singular; widgets = java.util.Collections.singletonList(this.widgets.get(0)); break; default : - widgets = new java.util.ArrayList<String>(this.widgets.size()); - widgets.addAll(this.widgets); - widgets = java.util.Collections.unmodifiableList(widgets); + widgets = java.util.Collections.unmodifiableList(new java.util.ArrayList<String>(this.widgets)); } java.util.List<String> items; switch (((this.items == null) ? 0 : this.items.size())) { @@ -80,9 +76,7 @@ import lombok.Singular; items = java.util.Collections.singletonList(this.items.get(0)); break; default : - items = new java.util.ArrayList<String>(this.items.size()); - items.addAll(this.items); - items = java.util.Collections.unmodifiableList(items); + items = java.util.Collections.unmodifiableList(new java.util.ArrayList<String>(this.items)); } return new BuilderSingularNoAutosingularize(things, widgets, items); } diff --git a/test/transform/resource/before/BuilderSingularRedirectToGuava.java b/test/transform/resource/before/BuilderSingularRedirectToGuava.java index 7e1e2c23..2dfa7684 100644 --- a/test/transform/resource/before/BuilderSingularRedirectToGuava.java +++ b/test/transform/resource/before/BuilderSingularRedirectToGuava.java @@ -1,4 +1,4 @@ -//CONF: lombok.builder.useGuava = true +//CONF: lombok.singular.useGuava = true import java.util.Set; import java.util.NavigableMap; import java.util.Collection; diff --git a/website/features/Builder.html b/website/features/Builder.html index b4b7fb55..5cf7c23e 100644 --- a/website/features/Builder.html +++ b/website/features/Builder.html @@ -91,7 +91,7 @@ <code>@Singular</code> can only be applied to collection types for which lombok has a recipe to produce the singular methods. Currently, the supported types are: <ul> <li><a href="http://docs.oracle.com/javase/8/docs/api/java/util/package-summary.html"><code>java.util</code></a>:<ul> - <li><code>Collection</code> and <code>List</code> (backed by a compacted unmodifiable <code>ArrayList</code> in the general case).</li> + <li><code>Iterable</code>, <code>Collection</code>, and <code>List</code> (backed by a compacted unmodifiable <code>ArrayList</code> in the general case).</li> <li><code>Set</code>, <code>SortedSet</code>, and <code>NavigableSet</code> (backed by a smartly sized unmodifiable <code>HashSet</code> or <code>TreeSet</code> in the general case).</li> <li><code>Map</code>, <code>SortedMap</code>, and <code>NavigableMap</code> (backed by a smartly sized unmodifiable <code>HashMap</code> or <code>TreeMap</code> in the general case).</li> </ul></li> |