mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-15 22:15:11 +00:00
added singleton builder support for javac: guava lists and sets.
This commit is contained in:
1 parent
acd0097662
commit
65b97f74af
4 files changed
+323
No files matched your search
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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 org.mangosdk.spi.ProviderFor;
|
||||
|
||||
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 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 JavacGuavaSetListSingularizer extends JavacGuavaSingularizer {
|
||||
// TODO all maps
|
||||
// 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(
|
||||
"com.google.common.collect.ImmutableCollection",
|
||||
"com.google.common.collect.ImmutableList",
|
||||
"com.google.common.collect.ImmutableSet",
|
||||
"com.google.common.collect.ImmutableSortedSet");
|
||||
}
|
||||
|
||||
@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(1, 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();
|
||||
|
||||
JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
|
||||
ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
|
||||
statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source));
|
||||
JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "add");
|
||||
JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getSingularName())));
|
||||
statements.append(maker.Exec(invokeAdd));
|
||||
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("add", name.toString()));
|
||||
JCExpression paramType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
|
||||
JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getSingularName(), paramType, null);
|
||||
JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), 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, false, source));
|
||||
JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "addAll");
|
||||
JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getPluralName())));
|
||||
statements.append(maker.Exec(invokeAdd));
|
||||
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("addAll", name.toString()));
|
||||
JCExpression paramType = chainDots(builderType, "java", "lang", "Iterable");
|
||||
paramType = addTypeArgs(1, 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(1, 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.chainDots;
|
||||
|
||||
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.util.List;
|
||||
|
||||
import lombok.javac.JavacNode;
|
||||
import lombok.javac.JavacTreeMaker;
|
||||
import lombok.javac.handlers.JavacSingularsRecipes.JavacSingularizer;
|
||||
import lombok.javac.handlers.JavacSingularsRecipes.SingularData;
|
||||
|
||||
abstract class JavacGuavaSingularizer extends JavacSingularizer {
|
||||
protected String getSimpleTargetTypeName(SingularData data) {
|
||||
String simpleTypeName = data.getTargetSimpleType();
|
||||
if ("ImmutableCollection".equals(simpleTypeName)) return "ImmutableList";
|
||||
return simpleTypeName;
|
||||
}
|
||||
|
||||
protected String getBuilderMethodName(SingularData data) {
|
||||
if (data.getTargetSimpleType().equals("ImmutableSortedSet")) return "naturalOrder";
|
||||
return "builder";
|
||||
}
|
||||
|
||||
protected JCStatement createConstructBuilderVarIfNeeded(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, JCTree source) {
|
||||
List<JCExpression> jceBlank = List.nil();
|
||||
|
||||
JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
|
||||
JCExpression thisDotField2 = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
|
||||
JCExpression cond = maker.Binary(CTC_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
|
||||
|
||||
JCExpression create = maker.Apply(jceBlank, chainDots(builderType, "com", "google", "common", "collect", getSimpleTargetTypeName(data), getBuilderMethodName(data)), jceBlank);
|
||||
JCStatement thenPart = maker.Exec(maker.Assign(thisDotField2, create));
|
||||
|
||||
return maker.If(cond, thenPart, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableCollection;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.ImmutableSortedSet;
|
||||
class BuilderSingletonGuavaListsSets<T> {
|
||||
private ImmutableList<T> cards;
|
||||
private ImmutableCollection<? extends Number> frogs;
|
||||
private ImmutableSet rawSet;
|
||||
private ImmutableSortedSet<String> passes;
|
||||
@java.lang.SuppressWarnings("all")
|
||||
BuilderSingletonGuavaListsSets(final ImmutableList<T> cards, final ImmutableCollection<? extends Number> frogs, final ImmutableSet rawSet, final ImmutableSortedSet<String> passes) {
|
||||
this.cards = cards;
|
||||
this.frogs = frogs;
|
||||
this.rawSet = rawSet;
|
||||
this.passes = passes;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public static class BuilderSingletonGuavaListsSetsBuilder<T> {
|
||||
private com.google.common.collect.ImmutableList.Builder<T> cards;
|
||||
private com.google.common.collect.ImmutableList.Builder<Number> frogs;
|
||||
private com.google.common.collect.ImmutableSet.Builder<java.lang.Object> rawSet;
|
||||
private com.google.common.collect.ImmutableSortedSet.Builder<String> passes;
|
||||
@java.lang.SuppressWarnings("all")
|
||||
BuilderSingletonGuavaListsSetsBuilder() {
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public BuilderSingletonGuavaListsSetsBuilder<T> card(final T card) {
|
||||
if (this.cards == null) this.cards = com.google.common.collect.ImmutableList.builder();
|
||||
this.cards.add(card);
|
||||
return this;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public BuilderSingletonGuavaListsSetsBuilder<T> cards(final java.lang.Iterable<? extends T> cards) {
|
||||
if (this.cards == null) this.cards = com.google.common.collect.ImmutableList.builder();
|
||||
this.cards.addAll(cards);
|
||||
return this;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public BuilderSingletonGuavaListsSetsBuilder<T> frog(final Number frog) {
|
||||
if (this.frogs == null) this.frogs = com.google.common.collect.ImmutableList.builder();
|
||||
this.frogs.add(frog);
|
||||
return this;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public BuilderSingletonGuavaListsSetsBuilder<T> frogs(final java.lang.Iterable<? extends Number> frogs) {
|
||||
if (this.frogs == null) this.frogs = com.google.common.collect.ImmutableList.builder();
|
||||
this.frogs.addAll(frogs);
|
||||
return this;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public BuilderSingletonGuavaListsSetsBuilder<T> rawSet(final java.lang.Object rawSet) {
|
||||
if (this.rawSet == null) this.rawSet = com.google.common.collect.ImmutableSet.builder();
|
||||
this.rawSet.add(rawSet);
|
||||
return this;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public BuilderSingletonGuavaListsSetsBuilder<T> rawSet(final java.lang.Iterable<?> rawSet) {
|
||||
if (this.rawSet == null) this.rawSet = com.google.common.collect.ImmutableSet.builder();
|
||||
this.rawSet.addAll(rawSet);
|
||||
return this;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public BuilderSingletonGuavaListsSetsBuilder<T> pass(final String pass) {
|
||||
if (this.passes == null) this.passes = com.google.common.collect.ImmutableSortedSet.naturalOrder();
|
||||
this.passes.add(pass);
|
||||
return this;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public BuilderSingletonGuavaListsSetsBuilder<T> passes(final java.lang.Iterable<? extends String> passes) {
|
||||
if (this.passes == null) this.passes = com.google.common.collect.ImmutableSortedSet.naturalOrder();
|
||||
this.passes.addAll(passes);
|
||||
return this;
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public BuilderSingletonGuavaListsSets<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();
|
||||
return new BuilderSingletonGuavaListsSets<T>(cards, frogs, rawSet, passes);
|
||||
}
|
||||
@java.lang.Override
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public java.lang.String toString() {
|
||||
return "BuilderSingletonGuavaListsSets.BuilderSingletonGuavaListsSetsBuilder(cards=" + this.cards + ", frogs=" + this.frogs + ", rawSet=" + this.rawSet + ", passes=" + this.passes + ")";
|
||||
}
|
||||
}
|
||||
@java.lang.SuppressWarnings("all")
|
||||
public static <T> BuilderSingletonGuavaListsSetsBuilder<T> builder() {
|
||||
return new BuilderSingletonGuavaListsSetsBuilder<T>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableCollection;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.ImmutableSortedSet;
|
||||
|
||||
import lombok.Singular;
|
||||
|
||||
@lombok.Builder
|
||||
class BuilderSingletonGuavaListsSets<T> {
|
||||
@Singular private ImmutableList<T> cards;
|
||||
@Singular private ImmutableCollection<? extends Number> frogs;
|
||||
@Singular("rawSet") private ImmutableSet rawSet;
|
||||
@Singular private ImmutableSortedSet<String> passes;
|
||||
}
|
||||
Reference in new issue
Block a user