aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2015-01-05 12:42:48 +0100
committerReinier Zwitserloot <reinier@zwitserloot.com>2015-01-05 12:42:48 +0100
commit8bfd1a2767b2021b65be6d29fdb2a512be8fe05b (patch)
tree0afbf379851f6ffb16a2eca2883dbcda09b6bec3 /src
parent4618097d986f08a922191332c87a7d8c73e34f96 (diff)
downloadlombok-8bfd1a2767b2021b65be6d29fdb2a512be8fe05b.tar.gz
lombok-8bfd1a2767b2021b65be6d29fdb2a512be8fe05b.tar.bz2
lombok-8bfd1a2767b2021b65be6d29fdb2a512be8fe05b.zip
[javac] smeared the code for java.util set singularizer out across the appropriate supertypes in preparation for code reuse when implementing for map and list.
Diffstat (limited to 'src')
-rw-r--r--src/core/lombok/javac/handlers/JavacSingularsRecipes.java72
-rw-r--r--src/core/lombok/javac/handlers/singulars/JavacJavaUtilSetSingularizer.java25
-rw-r--r--src/core/lombok/javac/handlers/singulars/JavacJavaUtilSingularizer.java84
3 files changed, 100 insertions, 81 deletions
diff --git a/src/core/lombok/javac/handlers/JavacSingularsRecipes.java b/src/core/lombok/javac/handlers/JavacSingularsRecipes.java
index 6a8a8f7c..f1804ac8 100644
--- a/src/core/lombok/javac/handlers/JavacSingularsRecipes.java
+++ b/src/core/lombok/javac/handlers/JavacSingularsRecipes.java
@@ -21,6 +21,8 @@
*/
package lombok.javac.handlers;
+import static lombok.javac.handlers.JavacHandlerUtil.*;
+
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -29,10 +31,15 @@ import lombok.core.LombokImmutableList;
import lombok.core.SpiLoadUtil;
import lombok.core.TypeLibrary;
import lombok.javac.JavacNode;
+import lombok.javac.JavacTreeMaker;
+import com.sun.source.tree.Tree.Kind;
+import com.sun.tools.javac.code.BoundKind;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCStatement;
+import com.sun.tools.javac.tree.JCTree.JCWildcard;
+import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
@@ -138,5 +145,70 @@ public class JavacSingularsRecipes {
public void appendCleaningCode(SingularData data, JavacNode builderType, JCTree source, ListBuffer<JCStatement> statements) {
}
+
+ // -- Utility methods --
+
+ /**
+ * Adds the requested number of type arguments to the provided type, copying each argument in {@code typeArgs}. If typeArgs is too long, the extra elements are ignored.
+ * If {@code typeArgs} is null or too short, {@code java.lang.Object} will be substituted for each missing type argument.
+ *
+ * @param count The number of type arguments requested.
+ * @param addExtends If {@code true}, all bounds are either '? extends X' or just '?'. If false, the reverse is applied, and '? extends Foo' is converted to Foo, '?' to Object, etc.
+ * @param node Some node in the same AST. Just used to obtain makers and contexts and such.
+ * @param type The type to add generics to.
+ * @param typeArgs the list of type args to clone.
+ * @param source The source annotation that is the root cause of this code generation.
+ */
+ protected JCExpression addTypeArgs(int count, boolean addExtends, JavacNode node, JCExpression type, List<JCExpression> typeArgs, JCTree source) {
+ JavacTreeMaker maker = node.getTreeMaker();
+ Context context = node.getContext();
+
+ if (count < 0) throw new IllegalArgumentException("count is negative");
+ if (count == 0) return type;
+ ListBuffer<JCExpression> arguments = new ListBuffer<JCExpression>();
+
+ if (typeArgs != null) for (JCExpression orig : typeArgs) {
+ if (!addExtends) {
+ if (orig.getKind() == Kind.UNBOUNDED_WILDCARD || orig.getKind() == Kind.SUPER_WILDCARD) {
+ arguments.append(chainDots(node, "java", "lang", "Object"));
+ } else if (orig.getKind() == Kind.EXTENDS_WILDCARD) {
+ JCExpression inner;
+ try {
+ inner = (JCExpression) ((JCWildcard) orig).inner;
+ } catch (Exception e) {
+ inner = chainDots(node, "java", "lang", "Object");
+ }
+ arguments.append(cloneType(maker, inner, source, context));
+ } else {
+ arguments.append(cloneType(maker, orig, source, context));
+ }
+ } else {
+ if (orig.getKind() == Kind.UNBOUNDED_WILDCARD || orig.getKind() == Kind.SUPER_WILDCARD) {
+ arguments.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
+ } else if (orig.getKind() == Kind.EXTENDS_WILDCARD) {
+ arguments.append(cloneType(maker, orig, source, context));
+ } else {
+ arguments.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), cloneType(maker, orig, source, context)));
+ }
+ }
+ if (--count == 0) break;
+ }
+
+ while (count-- > 0) {
+ if (addExtends) {
+ arguments.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
+ } else {
+ arguments.append(chainDots(node, "java", "lang", "Object"));
+ }
+ }
+ return maker.TypeApply(type, arguments.toList());
+ }
+
+ /** Generates 'this.<em>name</em>.size()' as an expression. */
+ protected JCExpression getSize(JavacTreeMaker maker, JavacNode builderType, Name name) {
+ JCExpression fn = maker.Select(maker.Select(maker.Ident(builderType.toName("this")), name), builderType.toName("size"));
+ return maker.Apply(List.<JCExpression>nil(), fn, List.<JCExpression>nil());
+ }
+
}
}
diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSetSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSetSingularizer.java
index c0f79275..1168b559 100644
--- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSetSingularizer.java
+++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSetSingularizer.java
@@ -39,7 +39,6 @@ import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCExpression;
-import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
import com.sun.tools.javac.tree.JCTree.JCStatement;
@@ -130,28 +129,16 @@ public class JavacJavaUtilSetSingularizer extends JavacJavaUtilSingularizer {
injectMethod(builderType, method);
}
- private JCExpression getSize(JavacTreeMaker maker, JavacNode builderType, Name name) {
- JCExpression fn = maker.Select(maker.Select(maker.Ident(builderType.toName("this")), name), builderType.toName("size"));
- return maker.Apply(List.<JCExpression>nil(), fn, List.<JCExpression>nil());
- }
-
@Override public void appendBuildCode(SingularData data, JavacNode builderType, JCTree source, ListBuffer<JCStatement> statements, Name targetVariableName) {
JavacTreeMaker maker = builderType.getTreeMaker();
JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs(), source);
JCExpression constructTargetType; {
if (data.getTargetFqn().equals("java.util.Set")) {
+ JCExpression loadFactor = maker.Literal(CTC_FLOAT, 0.75f);
JCExpression internalType = chainDots(builderType, "java", "util", "LinkedHashSet");
internalType = addTypeArgs(1, false, builderType, internalType, data.getTypeArgs(), source);
- JCExpression loadFactor = maker.Literal(CTC_FLOAT, 0.75f);
- JCExpression lessThanCutoff = maker.Binary(CTC_LESS_THAN, getSize(maker, builderType, data.getPluralName()), maker.Literal(CTC_INT, 0x40000000));
- JCExpression maxInt = chainDots(builderType, "java", "lang", "Integer", "MAX_VALUE");
- JCExpression belowThree = maker.Binary(CTC_LESS_THAN, getSize(maker, builderType, data.getPluralName()), maker.Literal(CTC_INT, 3));
- JCExpression sizePlusOne = maker.Binary(CTC_PLUS, getSize(maker, builderType, data.getPluralName()), maker.Literal(CTC_INT, 1));
- JCExpression sizeDivThree = maker.Binary(CTC_DIV, getSize(maker, builderType, data.getPluralName()), maker.Literal(CTC_INT, 3));
- JCExpression sizePlusSizeDivThree = maker.Binary(CTC_PLUS, getSize(maker, builderType, data.getPluralName()), sizeDivThree);
- JCExpression rest = maker.Conditional(belowThree, sizePlusOne, sizePlusSizeDivThree);
- JCExpression initialCapacity = maker.Conditional(lessThanCutoff, rest, maxInt);
+ JCExpression initialCapacity = createJavaUtilSetMapInitialCapacityExpression(maker, data, builderType);
constructTargetType = maker.NewClass(null, List.<JCExpression>nil(), internalType, List.<JCExpression>of(initialCapacity, loadFactor), null);
} else {
JCExpression internalType = chainDots(builderType, "java", "util", "TreeSet");
@@ -159,13 +146,9 @@ public class JavacJavaUtilSetSingularizer extends JavacJavaUtilSingularizer {
constructTargetType = maker.NewClass(null, List.<JCExpression>nil(), internalType, List.<JCExpression>nil(), null);
}
}
+
JCVariableDecl varDef = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, constructTargetType);
statements.append(varDef);
- JCFieldAccess varDotAddAll = maker.Select(maker.Ident(data.getPluralName()), builderType.toName("addAll"));
- JCExpression thisDotFieldName = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
- statements.append(maker.Exec(maker.Apply(List.<JCExpression>nil(), varDotAddAll, List.of(thisDotFieldName))));
- String singletonMaker = "unmodifiable" + data.getTargetFqn().substring(data.getTargetFqn().lastIndexOf(".") + 1);
- JCExpression javaUtilCollectionsInvoke = maker.Apply(List.<JCExpression>nil(), chainDots(builderType, "java", "util", "Collections", singletonMaker), List.<JCExpression>of(maker.Ident(data.getPluralName())));
- statements.append(maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), javaUtilCollectionsInvoke)));
+ stuffJavaUtilCollectionAndWrapWithUnmodifiable(data, builderType, statements, maker, "addAll");
}
}
diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSingularizer.java
index 83348c28..c6880663 100644
--- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSingularizer.java
+++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilSingularizer.java
@@ -21,75 +21,39 @@
*/
package lombok.javac.handlers.singulars;
-import com.sun.source.tree.Tree.Kind;
-import com.sun.tools.javac.code.BoundKind;
-import com.sun.tools.javac.tree.JCTree;
+import static lombok.javac.Javac.*;
+import static lombok.javac.handlers.JavacHandlerUtil.chainDots;
+
import com.sun.tools.javac.tree.JCTree.JCExpression;
-import com.sun.tools.javac.tree.JCTree.JCWildcard;
-import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
+import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import lombok.javac.JavacNode;
import lombok.javac.JavacTreeMaker;
-import static lombok.javac.handlers.JavacHandlerUtil.*;
import lombok.javac.handlers.JavacSingularsRecipes.JavacSingularizer;
+import lombok.javac.handlers.JavacSingularsRecipes.SingularData;
public abstract class JavacJavaUtilSingularizer extends JavacSingularizer {
- /**
- * Adds the requested number of type arguments to the provided type, copying each argument in {@code typeArgs}. If typeArgs is too long, the extra elements are ignored.
- * If {@code typeArgs} is null or too short, {@code java.lang.Object} will be substituted for each missing type argument.
- *
- * @param count The number of type arguments requested.
- * @param addExtends If {@code true}, all bounds are either '? extends X' or just '?'. If false, the reverse is applied, and '? extends Foo' is converted to Foo, '?' to Object, etc.
- * @param node Some node in the same AST. Just used to obtain makers and contexts and such.
- * @param type The type to add generics to.
- * @param typeArgs the list of type args to clone.
- * @param source The source annotation that is the root cause of this code generation.
- */
- protected JCExpression addTypeArgs(int count, boolean addExtends, JavacNode node, JCExpression type, List<JCExpression> typeArgs, JCTree source) {
- JavacTreeMaker maker = node.getTreeMaker();
- Context context = node.getContext();
-
- if (count < 0) throw new IllegalArgumentException("count is negative");
- if (count == 0) return type;
- ListBuffer<JCExpression> arguments = new ListBuffer<JCExpression>();
-
- if (typeArgs != null) for (JCExpression orig : typeArgs) {
- if (!addExtends) {
- if (orig.getKind() == Kind.UNBOUNDED_WILDCARD || orig.getKind() == Kind.SUPER_WILDCARD) {
- arguments.append(chainDots(node, "java", "lang", "Object"));
- } else if (orig.getKind() == Kind.EXTENDS_WILDCARD) {
- JCExpression inner;
- try {
- inner = (JCExpression) ((JCWildcard) orig).inner;
- } catch (Exception e) {
- inner = chainDots(node, "java", "lang", "Object");
- }
- arguments.append(cloneType(maker, inner, source, context));
- } else {
- arguments.append(cloneType(maker, orig, source, context));
- }
- } else {
- if (orig.getKind() == Kind.UNBOUNDED_WILDCARD || orig.getKind() == Kind.SUPER_WILDCARD) {
- arguments.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
- } else if (orig.getKind() == Kind.EXTENDS_WILDCARD) {
- arguments.append(cloneType(maker, orig, source, context));
- } else {
- arguments.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), cloneType(maker, orig, source, context)));
- }
- }
- if (--count == 0) break;
- }
-
- while (count-- > 0) {
- if (addExtends) {
- arguments.append(maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null));
- } else {
- arguments.append(chainDots(node, "java", "lang", "Object"));
- }
- }
- return maker.TypeApply(type, arguments.toList());
+ protected JCExpression createJavaUtilSetMapInitialCapacityExpression(JavacTreeMaker maker, SingularData data, JavacNode builderType) {
+ JCExpression lessThanCutoff = maker.Binary(CTC_LESS_THAN, getSize(maker, builderType, data.getPluralName()), maker.Literal(CTC_INT, 0x40000000));
+ JCExpression maxInt = chainDots(builderType, "java", "lang", "Integer", "MAX_VALUE");
+ JCExpression belowThree = maker.Binary(CTC_LESS_THAN, getSize(maker, builderType, data.getPluralName()), maker.Literal(CTC_INT, 3));
+ JCExpression sizePlusOne = maker.Binary(CTC_PLUS, getSize(maker, builderType, data.getPluralName()), maker.Literal(CTC_INT, 1));
+ JCExpression sizeDivThree = maker.Binary(CTC_DIV, getSize(maker, builderType, data.getPluralName()), maker.Literal(CTC_INT, 3));
+ JCExpression sizePlusSizeDivThree = maker.Binary(CTC_PLUS, getSize(maker, builderType, data.getPluralName()), sizeDivThree);
+ JCExpression rest = maker.Conditional(belowThree, sizePlusOne, sizePlusSizeDivThree);
+ JCExpression initialCapacity = maker.Conditional(lessThanCutoff, rest, maxInt);
+ return initialCapacity;
}
+ protected void stuffJavaUtilCollectionAndWrapWithUnmodifiable(SingularData data, JavacNode builderType, ListBuffer<JCStatement> statements, JavacTreeMaker maker, String addAllName) {
+ JCFieldAccess varDotAddAll = maker.Select(maker.Ident(data.getPluralName()), builderType.toName(addAllName));
+ JCExpression thisDotFieldName = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
+ statements.append(maker.Exec(maker.Apply(List.<JCExpression>nil(), varDotAddAll, List.of(thisDotFieldName))));
+ String singletonMaker = "unmodifiable" + data.getTargetFqn().substring(data.getTargetFqn().lastIndexOf(".") + 1);
+ JCExpression javaUtilCollectionsInvoke = maker.Apply(List.<JCExpression>nil(), chainDots(builderType, "java", "util", "Collections", singletonMaker), List.<JCExpression>of(maker.Ident(data.getPluralName())));
+ statements.append(maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), javaUtilCollectionsInvoke)));
+ }
}