diff options
5 files changed, 251 insertions, 4 deletions
diff --git a/src/core/lombok/javac/handlers/singulars/JavacGuavaMapSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacGuavaMapSingularizer.java new file mode 100644 index 00000000..af39c3ba --- /dev/null +++ b/src/core/lombok/javac/handlers/singulars/JavacGuavaMapSingularizer.java @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2015 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.javac.handlers.singulars; + +import static lombok.javac.Javac.*; +import static lombok.javac.handlers.JavacHandlerUtil.*; + +import java.util.Collections; + +import lombok.core.LombokImmutableList; +import lombok.core.handlers.HandlerUtil; +import lombok.javac.JavacNode; +import lombok.javac.JavacTreeMaker; +import lombok.javac.handlers.JavacHandlerUtil; +import lombok.javac.handlers.JavacSingularsRecipes.JavacSingularizer; +import lombok.javac.handlers.JavacSingularsRecipes.SingularData; + +import org.mangosdk.spi.ProviderFor; + +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.JCMethodDecl; +import com.sun.tools.javac.tree.JCTree.JCModifiers; +import com.sun.tools.javac.tree.JCTree.JCStatement; +import com.sun.tools.javac.tree.JCTree.JCTypeParameter; +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; +import com.sun.tools.javac.util.List; +import com.sun.tools.javac.util.ListBuffer; +import com.sun.tools.javac.util.Name; + +@ProviderFor(JavacSingularizer.class) +public class JavacGuavaMapSingularizer extends JavacGuavaSingularizer { + // TODO cgcc.ImmutableMultimap, cgcc.ImmutableListMultimap, cgcc.ImmutableSetMultimap + // TODO cgcc.ImmutableClassToInstanceMap + // TODO cgcc.ImmutableRangeMap + + @Override public LombokImmutableList<String> getSupportedTypes() { + return LombokImmutableList.of( + "com.google.common.collect.ImmutableMap", + "com.google.common.collect.ImmutableBiMap", + "com.google.common.collect.ImmutableSortedMap"); + } + + @Override public java.util.List<JavacNode> generateFields(SingularData data, JavacNode builderType, JCTree source) { + JavacTreeMaker maker = builderType.getTreeMaker(); + JCExpression type = JavacHandlerUtil.chainDots(builderType, "com", "google", "common", "collect", getSimpleTargetTypeName(data), "Builder"); + type = addTypeArgs(2, false, builderType, type, data.getTypeArgs(), source); + + JCVariableDecl buildField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), data.getPluralName(), type, null); + return Collections.singletonList(injectField(builderType, buildField)); + } + + @Override public void generateMethods(SingularData data, JavacNode builderType, JCTree source, boolean fluent, boolean chain) { + JavacTreeMaker maker = builderType.getTreeMaker(); + JCExpression returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(maker, CTC_VOID)); + JCStatement returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null; + generateSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent); + + returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(maker, CTC_VOID)); + returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null; + generatePluralMethod(maker, returnType, returnStatement, data, builderType, source, fluent); + } + + private void generateSingularMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { + List<JCTypeParameter> typeParams = List.nil(); + List<JCExpression> thrown = List.nil(); + + Name keyName = builderType.toName(data.getSingularName() + "$key"); + Name valueName = builderType.toName(data.getSingularName() + "$value"); + + JCModifiers mods = maker.Modifiers(Flags.PUBLIC); + ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); + statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, true, source)); + JCExpression thisDotFieldDotPut = chainDots(builderType, "this", data.getPluralName().toString(), "put"); + JCExpression invokePut = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotPut, List.<JCExpression>of(maker.Ident(keyName), maker.Ident(valueName))); + statements.append(maker.Exec(invokePut)); + if (returnStatement != null) statements.append(returnStatement); + JCBlock body = maker.Block(0, statements.toList()); + Name name = data.getSingularName(); + long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); + if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("put", name.toString())); + JCExpression keyType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source); + JCExpression valueType = cloneParamType(1, maker, data.getTypeArgs(), builderType, source); + JCVariableDecl paramKey = maker.VarDef(maker.Modifiers(paramFlags), keyName, keyType, null); + JCVariableDecl paramValue = maker.VarDef(maker.Modifiers(paramFlags), valueName, valueType, null); + JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(paramKey, paramValue), thrown, body, null); + injectMethod(builderType, method); + } + + private void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { + List<JCTypeParameter> typeParams = List.nil(); + List<JCExpression> thrown = List.nil(); + JCModifiers mods = maker.Modifiers(Flags.PUBLIC); + ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); + statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, true, source)); + JCExpression thisDotFieldDotPutAll = chainDots(builderType, "this", data.getPluralName().toString(), "putAll"); + JCExpression invokePutAll = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotPutAll, List.<JCExpression>of(maker.Ident(data.getPluralName()))); + statements.append(maker.Exec(invokePutAll)); + if (returnStatement != null) statements.append(returnStatement); + JCBlock body = maker.Block(0, statements.toList()); + Name name = data.getPluralName(); + long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); + if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("putAll", name.toString())); + JCExpression paramType = chainDots(builderType, "java", "util", "Map"); + paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs(), source); + JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null); + JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null); + injectMethod(builderType, method); + } + + @Override public void appendBuildCode(SingularData data, JavacNode builderType, JCTree source, ListBuffer<JCStatement> statements, Name targetVariableName) { + JavacTreeMaker maker = builderType.getTreeMaker(); + List<JCExpression> jceBlank = List.nil(); + + JCExpression varType = chainDotsString(builderType, data.getTargetFqn()); + varType = addTypeArgs(2, false, builderType, varType, data.getTypeArgs(), source); + + JCExpression empty; { + //ImmutableX.of() + JCExpression emptyMethod = chainDots(builderType, "com", "google", "common", "collect", getSimpleTargetTypeName(data), "of"); + empty = maker.Apply(jceBlank, emptyMethod, jceBlank); + } + + JCExpression invokeBuild; { + //this.pluralName.build(); + invokeBuild = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName().toString(), "build"), jceBlank); + } + + JCExpression isNull; { + //this.pluralName == null + isNull = maker.Binary(CTC_EQUAL, maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName()), maker.Literal(CTC_BOT, null)); + } + + JCExpression init = maker.Conditional(isNull, empty, invokeBuild); // this.pluralName == null ? ImmutableX.of() : this.pluralName.build() + + JCStatement jcs = maker.VarDef(maker.Modifiers(0), data.getPluralName(), varType, init); + statements.append(jcs); + } +} diff --git a/src/core/lombok/javac/handlers/singulars/JavacGuavaSetListSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacGuavaSetListSingularizer.java index 3e050199..54ba091b 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacGuavaSetListSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacGuavaSetListSingularizer.java @@ -51,9 +51,8 @@ import com.sun.tools.javac.util.Name; @ProviderFor(JavacSingularizer.class) public class JavacGuavaSetListSingularizer extends JavacGuavaSingularizer { - // TODO all maps - // TODO com.google.common.collect.ImmutableTable. - // TODO com.google.common.collect.ImmutableRangeSet. + // TODO com.google.common.collect.ImmutableTable + // TODO com.google.common.collect.ImmutableRangeSet // TODO com.google.common.collect.ImmutableMultiset and com.google.common.collect.ImmutableSortedMultiset @Override public LombokImmutableList<String> getSupportedTypes() { return LombokImmutableList.of( diff --git a/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java index 45450603..ae9f34c5 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java @@ -42,7 +42,8 @@ abstract class JavacGuavaSingularizer extends JavacSingularizer { } protected String getBuilderMethodName(SingularData data) { - if (data.getTargetSimpleType().equals("ImmutableSortedSet")) return "naturalOrder"; + String simpleTypeName = data.getTargetSimpleType(); + if ("ImmutableSortedSet".equals(simpleTypeName) || "ImmutableSortedMap".equals(simpleTypeName)) return "naturalOrder"; return "builder"; } diff --git a/test/transform/resource/after-delombok/BuilderSingletonGuavaMaps.java b/test/transform/resource/after-delombok/BuilderSingletonGuavaMaps.java new file mode 100644 index 00000000..14525ece --- /dev/null +++ b/test/transform/resource/after-delombok/BuilderSingletonGuavaMaps.java @@ -0,0 +1,75 @@ +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableBiMap; +import com.google.common.collect.ImmutableSortedMap; +class BuilderSingletonGuavaMaps<K, V> { + private ImmutableMap<K, V> battleaxes; + private ImmutableSortedMap<Integer, ? extends V> vertices; + private ImmutableBiMap rawMap; + @java.lang.SuppressWarnings("all") + BuilderSingletonGuavaMaps(final ImmutableMap<K, V> battleaxes, final ImmutableSortedMap<Integer, ? extends V> vertices, final ImmutableBiMap rawMap) { + this.battleaxes = battleaxes; + this.vertices = vertices; + this.rawMap = rawMap; + } + @java.lang.SuppressWarnings("all") + public static class BuilderSingletonGuavaMapsBuilder<K, V> { + private com.google.common.collect.ImmutableMap.Builder<K, V> battleaxes; + private com.google.common.collect.ImmutableSortedMap.Builder<Integer, V> vertices; + private com.google.common.collect.ImmutableBiMap.Builder<java.lang.Object, java.lang.Object> rawMap; + @java.lang.SuppressWarnings("all") + BuilderSingletonGuavaMapsBuilder() { + } + @java.lang.SuppressWarnings("all") + public BuilderSingletonGuavaMapsBuilder<K, V> battleaxe(final K battleaxe$key, final V battleaxe$value) { + if (this.battleaxes == null) this.battleaxes = com.google.common.collect.ImmutableMap.builder(); + this.battleaxes.put(battleaxe$key, battleaxe$value); + return this; + } + @java.lang.SuppressWarnings("all") + public BuilderSingletonGuavaMapsBuilder<K, V> battleaxes(final java.util.Map<? extends K, ? extends V> battleaxes) { + if (this.battleaxes == null) this.battleaxes = com.google.common.collect.ImmutableMap.builder(); + this.battleaxes.putAll(battleaxes); + return this; + } + @java.lang.SuppressWarnings("all") + public BuilderSingletonGuavaMapsBuilder<K, V> vertex(final Integer vertex$key, final V vertex$value) { + if (this.vertices == null) this.vertices = com.google.common.collect.ImmutableSortedMap.naturalOrder(); + this.vertices.put(vertex$key, vertex$value); + return this; + } + @java.lang.SuppressWarnings("all") + public BuilderSingletonGuavaMapsBuilder<K, V> vertices(final java.util.Map<? extends Integer, ? extends V> vertices) { + if (this.vertices == null) this.vertices = com.google.common.collect.ImmutableSortedMap.naturalOrder(); + this.vertices.putAll(vertices); + return this; + } + @java.lang.SuppressWarnings("all") + public BuilderSingletonGuavaMapsBuilder<K, V> rawMap(final java.lang.Object rawMap$key, final java.lang.Object rawMap$value) { + if (this.rawMap == null) this.rawMap = com.google.common.collect.ImmutableBiMap.builder(); + this.rawMap.put(rawMap$key, rawMap$value); + return this; + } + @java.lang.SuppressWarnings("all") + public BuilderSingletonGuavaMapsBuilder<K, V> rawMap(final java.util.Map<?, ?> rawMap) { + if (this.rawMap == null) this.rawMap = com.google.common.collect.ImmutableBiMap.builder(); + this.rawMap.putAll(rawMap); + return this; + } + @java.lang.SuppressWarnings("all") + public BuilderSingletonGuavaMaps<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(); + return new BuilderSingletonGuavaMaps<K, V>(battleaxes, vertices, rawMap); + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + public java.lang.String toString() { + return "BuilderSingletonGuavaMaps.BuilderSingletonGuavaMapsBuilder(battleaxes=" + this.battleaxes + ", vertices=" + this.vertices + ", rawMap=" + this.rawMap + ")"; + } + } + @java.lang.SuppressWarnings("all") + public static <K, V> BuilderSingletonGuavaMapsBuilder<K, V> builder() { + return new BuilderSingletonGuavaMapsBuilder<K, V>(); + } +}
\ No newline at end of file diff --git a/test/transform/resource/before/BuilderSingletonGuavaMaps.java b/test/transform/resource/before/BuilderSingletonGuavaMaps.java new file mode 100644 index 00000000..2271b9d9 --- /dev/null +++ b/test/transform/resource/before/BuilderSingletonGuavaMaps.java @@ -0,0 +1,12 @@ +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableBiMap; +import com.google.common.collect.ImmutableSortedMap; + +import lombok.Singular; + +@lombok.Builder +class BuilderSingletonGuavaMaps<K, V> { + @Singular private ImmutableMap<K, V> battleaxes; + @Singular private ImmutableSortedMap<Integer, ? extends V> vertices; + @Singular("rawMap") private ImmutableBiMap rawMap; +} |