[@Singular @Builder] Our recipe for guava builders did not work in javac 1.7 and below; fixed.

This commit is contained in:
Reinier Zwitserloot
2015-01-21 01:11:26 +01:00
parent ef60d1af00
commit cd139f4635
10 changed files with 52 additions and 36 deletions
@@ -239,8 +239,24 @@ public class EclipseSingularsRecipes {
* @param source The source annotation that is the root cause of this code generation.
*/
protected TypeReference addTypeArgs(int count, boolean addExtends, EclipseNode node, TypeReference type, List<TypeReference> typeArgs) {
TypeReference[] clonedAndFixedArgs = createTypeArgs(count, addExtends, node, typeArgs);
if (type instanceof SingleTypeReference) {
type = new ParameterizedSingleTypeReference(((SingleTypeReference) type).token, clonedAndFixedArgs, 0, 0L);
} else if (type instanceof QualifiedTypeReference) {
QualifiedTypeReference qtr = (QualifiedTypeReference) type;
TypeReference[][] trs = new TypeReference[qtr.tokens.length][];
trs[qtr.tokens.length - 1] = clonedAndFixedArgs;
type = new ParameterizedQualifiedTypeReference(((QualifiedTypeReference) type).tokens, trs, 0, NULL_POSS);
} else {
node.addError("Don't know how to clone-and-parameterize type: " + type);
}
return type;
}
protected TypeReference[] createTypeArgs(int count, boolean addExtends, EclipseNode node, List<TypeReference> typeArgs) {
if (count < 0) throw new IllegalArgumentException("count is negative");
if (count == 0) return type;
if (count == 0) return null;
List<TypeReference> arguments = new ArrayList<TypeReference>();
if (typeArgs != null) for (TypeReference orig : typeArgs) {
@@ -280,18 +296,8 @@ public class EclipseSingularsRecipes {
}
}
if (type instanceof SingleTypeReference) {
type = new ParameterizedSingleTypeReference(((SingleTypeReference) type).token, arguments.toArray(new TypeReference[arguments.size()]), 0, 0L);
} else if (type instanceof QualifiedTypeReference) {
QualifiedTypeReference qtr = (QualifiedTypeReference) type;
TypeReference[][] trs = new TypeReference[qtr.tokens.length][];
trs[qtr.tokens.length - 1] = arguments.toArray(new TypeReference[arguments.size()]);
type = new ParameterizedQualifiedTypeReference(((QualifiedTypeReference) type).tokens, trs, 0, NULL_POSS);
} else {
node.addError("Don't know how to clone-and-parameterize type: " + type);
}
return type;
if (arguments.isEmpty()) return null;
return arguments.toArray(new TypeReference[arguments.size()]);
}
private static final char[] SIZE_TEXT = new char[] {'s', 'i', 'z', 'e'};
@@ -205,6 +205,7 @@ abstract class EclipseGuavaSingularizer extends EclipseSingularizer {
emptyInvoke = new MessageSend();
emptyInvoke.selector = new char[] {'o', 'f'};
emptyInvoke.receiver = new QualifiedNameReference(makeGuavaTypeName(getSimpleTargetTypeName(data), false), NULL_POSS, 0, 0);
emptyInvoke.typeArguments = createTypeArgs(mapMode ? 2 : 1, false, builderType, data.getTypeArgs());
}
MessageSend invokeBuild; {
@@ -214,11 +214,18 @@ public class JavacSingularsRecipes {
* @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();
List<JCExpression> clonedAndFixedTypeArgs = createTypeArgs(count, addExtends, node, typeArgs, source);
return maker.TypeApply(type, clonedAndFixedTypeArgs);
}
protected List<JCExpression> createTypeArgs(int count, boolean addExtends, JavacNode node, 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;
if (count == 0) return List.nil();
ListBuffer<JCExpression> arguments = new ListBuffer<JCExpression>();
if (typeArgs != null) for (JCExpression orig : typeArgs) {
@@ -255,7 +262,8 @@ public class JavacSingularsRecipes {
arguments.append(chainDots(node, "java", "lang", "Object"));
}
}
return maker.TypeApply(type, arguments.toList());
return arguments.toList();
}
/** Generates 'this.<em>name</em>.size()' as an expression; if nullGuard is true, it's this.name == null ? 0 : this.name.size(). */
@@ -159,7 +159,8 @@ abstract class JavacGuavaSingularizer extends JavacSingularizer {
JCExpression empty; {
//ImmutableX.of()
JCExpression emptyMethod = chainDots(builderType, "com", "google", "common", "collect", getSimpleTargetTypeName(data), "of");
empty = maker.Apply(jceBlank, emptyMethod, jceBlank);
List<JCExpression> invokeTypeArgs = createTypeArgs(mapMode ? 2 : 1, false, builderType, data.getTypeArgs(), source);
empty = maker.Apply(invokeTypeArgs, emptyMethod, jceBlank);
}
JCExpression invokeBuild; {
@@ -74,10 +74,10 @@ class BuilderSingularGuavaListsSets<T> {
}
@java.lang.SuppressWarnings("all")
public BuilderSingularGuavaListsSets<T> build() {
com.google.common.collect.ImmutableList<T> cards = this.cards == null ? com.google.common.collect.ImmutableList.of() : this.cards.build();
com.google.common.collect.ImmutableCollection<Number> frogs = this.frogs == null ? com.google.common.collect.ImmutableList.of() : this.frogs.build();
com.google.common.collect.ImmutableSet<java.lang.Object> rawSet = this.rawSet == null ? com.google.common.collect.ImmutableSet.of() : this.rawSet.build();
com.google.common.collect.ImmutableSortedSet<String> passes = this.passes == null ? com.google.common.collect.ImmutableSortedSet.of() : this.passes.build();
com.google.common.collect.ImmutableList<T> cards = this.cards == null ? com.google.common.collect.ImmutableList.<T>of() : this.cards.build();
com.google.common.collect.ImmutableCollection<Number> frogs = this.frogs == null ? com.google.common.collect.ImmutableList.<Number>of() : this.frogs.build();
com.google.common.collect.ImmutableSet<java.lang.Object> rawSet = this.rawSet == null ? com.google.common.collect.ImmutableSet.<java.lang.Object>of() : this.rawSet.build();
com.google.common.collect.ImmutableSortedSet<String> passes = this.passes == null ? com.google.common.collect.ImmutableSortedSet.<String>of() : this.passes.build();
return new BuilderSingularGuavaListsSets<T>(cards, frogs, rawSet, passes);
}
@java.lang.Override
@@ -58,9 +58,9 @@ class BuilderSingularGuavaMaps<K, V> {
}
@java.lang.SuppressWarnings("all")
public BuilderSingularGuavaMaps<K, V> build() {
com.google.common.collect.ImmutableMap<K, V> battleaxes = this.battleaxes == null ? com.google.common.collect.ImmutableMap.of() : this.battleaxes.build();
com.google.common.collect.ImmutableSortedMap<Integer, V> vertices = this.vertices == null ? com.google.common.collect.ImmutableSortedMap.of() : this.vertices.build();
com.google.common.collect.ImmutableBiMap<java.lang.Object, java.lang.Object> rawMap = this.rawMap == null ? com.google.common.collect.ImmutableBiMap.of() : this.rawMap.build();
com.google.common.collect.ImmutableMap<K, V> battleaxes = this.battleaxes == null ? com.google.common.collect.ImmutableMap.<K, V>of() : this.battleaxes.build();
com.google.common.collect.ImmutableSortedMap<Integer, V> vertices = this.vertices == null ? com.google.common.collect.ImmutableSortedMap.<Integer, V>of() : this.vertices.build();
com.google.common.collect.ImmutableBiMap<java.lang.Object, java.lang.Object> rawMap = this.rawMap == null ? com.google.common.collect.ImmutableBiMap.<java.lang.Object, java.lang.Object>of() : this.rawMap.build();
return new BuilderSingularGuavaMaps<K, V>(battleaxes, vertices, rawMap);
}
@java.lang.Override
@@ -57,9 +57,9 @@ class BuilderSingularRedirectToGuava {
}
@java.lang.SuppressWarnings("all")
public BuilderSingularRedirectToGuava build() {
java.util.Set<String> dangerMice = this.dangerMice == null ? com.google.common.collect.ImmutableSet.of() : this.dangerMice.build();
java.util.NavigableMap<Integer, Number> things = this.things == null ? com.google.common.collect.ImmutableSortedMap.of() : this.things.build();
java.util.Collection<Class<?>> doohickeys = this.doohickeys == null ? com.google.common.collect.ImmutableList.of() : this.doohickeys.build();
java.util.Set<String> dangerMice = this.dangerMice == null ? com.google.common.collect.ImmutableSet.<String>of() : this.dangerMice.build();
java.util.NavigableMap<Integer, Number> things = this.things == null ? com.google.common.collect.ImmutableSortedMap.<Integer, Number>of() : this.things.build();
java.util.Collection<Class<?>> doohickeys = this.doohickeys == null ? com.google.common.collect.ImmutableList.<Class<?>>of() : this.doohickeys.build();
return new BuilderSingularRedirectToGuava(dangerMice, things, doohickeys);
}
@java.lang.Override
@@ -61,10 +61,10 @@ import lombok.Singular;
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSets<T> build() {
com.google.common.collect.ImmutableList<T> cards = ((this.cards == null) ? com.google.common.collect.ImmutableList.of() : this.cards.build());
com.google.common.collect.ImmutableCollection<Number> frogs = ((this.frogs == null) ? com.google.common.collect.ImmutableList.of() : this.frogs.build());
com.google.common.collect.ImmutableSet<java.lang.Object> rawSet = ((this.rawSet == null) ? com.google.common.collect.ImmutableSet.of() : this.rawSet.build());
com.google.common.collect.ImmutableSortedSet<String> passes = ((this.passes == null) ? com.google.common.collect.ImmutableSortedSet.of() : this.passes.build());
com.google.common.collect.ImmutableList<T> cards = ((this.cards == null) ? com.google.common.collect.ImmutableList.<T>of() : this.cards.build());
com.google.common.collect.ImmutableCollection<Number> frogs = ((this.frogs == null) ? com.google.common.collect.ImmutableList.<Number>of() : this.frogs.build());
com.google.common.collect.ImmutableSet<java.lang.Object> rawSet = ((this.rawSet == null) ? com.google.common.collect.ImmutableSet.<java.lang.Object>of() : this.rawSet.build());
com.google.common.collect.ImmutableSortedSet<String> passes = ((this.passes == null) ? com.google.common.collect.ImmutableSortedSet.<String>of() : this.passes.build());
return new BuilderSingularGuavaListsSets<T>(cards, frogs, rawSet, passes);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
@@ -47,9 +47,9 @@ import lombok.Singular;
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMaps<K, V> build() {
com.google.common.collect.ImmutableMap<K, V> battleaxes = ((this.battleaxes == null) ? com.google.common.collect.ImmutableMap.of() : this.battleaxes.build());
com.google.common.collect.ImmutableSortedMap<Integer, V> vertices = ((this.vertices == null) ? com.google.common.collect.ImmutableSortedMap.of() : this.vertices.build());
com.google.common.collect.ImmutableBiMap<java.lang.Object, java.lang.Object> rawMap = ((this.rawMap == null) ? com.google.common.collect.ImmutableBiMap.of() : this.rawMap.build());
com.google.common.collect.ImmutableMap<K, V> battleaxes = ((this.battleaxes == null) ? com.google.common.collect.ImmutableMap.<K, V>of() : this.battleaxes.build());
com.google.common.collect.ImmutableSortedMap<Integer, V> vertices = ((this.vertices == null) ? com.google.common.collect.ImmutableSortedMap.<Integer, V>of() : this.vertices.build());
com.google.common.collect.ImmutableBiMap<java.lang.Object, java.lang.Object> rawMap = ((this.rawMap == null) ? com.google.common.collect.ImmutableBiMap.<java.lang.Object, java.lang.Object>of() : this.rawMap.build());
return new BuilderSingularGuavaMaps<K, V>(battleaxes, vertices, rawMap);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
@@ -47,9 +47,9 @@ import lombok.Singular;
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSingularRedirectToGuava build() {
java.util.Set<String> dangerMice = ((this.dangerMice == null) ? com.google.common.collect.ImmutableSet.of() : this.dangerMice.build());
java.util.NavigableMap<Integer, Number> things = ((this.things == null) ? com.google.common.collect.ImmutableSortedMap.of() : this.things.build());
java.util.Collection<Class<?>> doohickeys = ((this.doohickeys == null) ? com.google.common.collect.ImmutableList.of() : this.doohickeys.build());
java.util.Set<String> dangerMice = ((this.dangerMice == null) ? com.google.common.collect.ImmutableSet.<String>of() : this.dangerMice.build());
java.util.NavigableMap<Integer, Number> things = ((this.things == null) ? com.google.common.collect.ImmutableSortedMap.<Integer, Number>of() : this.things.build());
java.util.Collection<Class<?>> doohickeys = ((this.doohickeys == null) ? com.google.common.collect.ImmutableList.<Class<?>>of() : this.doohickeys.build());
return new BuilderSingularRedirectToGuava(dangerMice, things, doohickeys);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {