mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 12:15:00 +00:00
optimized the creation for singular lists/collections.
This commit is contained in:
1 parent
75da1e2481
commit
30beb498ca
9 files changed
+37
-77
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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));
|
||||
constructorCall.arguments = new Expression[] {thisDotPluralName};
|
||||
argToUnmodifiable = constructorCall;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in new issue
Block a user