use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.
the class JavacGuavaSingularizer method generateFields.
@Override
public java.util.List<JavacNode> generateFields(SingularData data, JavacNode builderType, JavacNode source) {
JavacTreeMaker maker = builderType.getTreeMaker();
String simpleTypeName = getSimpleTargetTypeName(data);
JCExpression type = JavacHandlerUtil.chainDots(builderType, "com", "google", "common", "collect", simpleTypeName, "Builder");
type = addTypeArgs(getTypeArgumentsCount(), false, builderType, type, data.getTypeArgs(), source);
JCVariableDecl buildField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), data.getPluralName(), type, null);
return Collections.singletonList(injectFieldAndMarkGenerated(builderType, buildField));
}
use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.
the class JavacGuavaSingularizer method appendBuildCode.
@Override
public void appendBuildCode(SingularData data, JavacNode builderType, JavacNode source, ListBuffer<JCStatement> statements, Name targetVariableName, String builderVariable) {
JavacTreeMaker maker = builderType.getTreeMaker();
List<JCExpression> jceBlank = List.nil();
JCExpression varType = chainDotsString(builderType, data.getTargetFqn());
int argumentsCount = getTypeArgumentsCount();
varType = addTypeArgs(argumentsCount, false, builderType, varType, data.getTypeArgs(), source);
JCExpression empty;
{
// ImmutableX.of()
JCExpression emptyMethod = chainDots(builderType, "com", "google", "common", "collect", getSimpleTargetTypeName(data), "of");
List<JCExpression> invokeTypeArgs = createTypeArgs(argumentsCount, false, builderType, data.getTypeArgs(), source);
empty = maker.Apply(invokeTypeArgs, emptyMethod, jceBlank);
}
JCExpression invokeBuild;
{
// this.pluralName.build();
invokeBuild = maker.Apply(jceBlank, chainDots(builderType, builderVariable, data.getPluralName().toString(), "build"), jceBlank);
}
JCExpression isNull;
{
// this.pluralName == null
isNull = maker.Binary(CTC_EQUAL, maker.Select(maker.Ident(builderType.toName(builderVariable)), data.getPluralName()), maker.Literal(CTC_BOT, null));
}
// this.pluralName == null ? ImmutableX.of() : this.pluralName.build()
JCExpression init = maker.Conditional(isNull, empty, invokeBuild);
JCStatement jcs = maker.VarDef(maker.Modifiers(0L), data.getPluralName(), varType, init);
statements.append(jcs);
}
use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.
the class JavacJavaUtilMapSingularizer method generateFields.
@Override
public java.util.List<JavacNode> generateFields(SingularData data, JavacNode builderType, JavacNode source) {
JavacTreeMaker maker = builderType.getTreeMaker();
JCVariableDecl buildKeyField;
{
JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
type = addTypeArgs(1, false, builderType, type, data.getTypeArgs(), source);
buildKeyField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$key"), type, null);
}
JCVariableDecl buildValueField;
{
JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
List<JCExpression> tArgs = data.getTypeArgs();
if (tArgs != null && tArgs.size() > 1)
tArgs = tArgs.tail;
else
tArgs = List.nil();
type = addTypeArgs(1, false, builderType, type, tArgs, source);
buildValueField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$value"), type, null);
}
JavacNode valueFieldNode = injectFieldAndMarkGenerated(builderType, buildValueField);
JavacNode keyFieldNode = injectFieldAndMarkGenerated(builderType, buildKeyField);
return Arrays.asList(keyFieldNode, valueFieldNode);
}
use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.
the class JavacJavaUtilListSingularizer method appendBuildCode.
@Override
public void appendBuildCode(SingularData data, JavacNode builderType, JavacNode source, ListBuffer<JCStatement> statements, Name targetVariableName, String builderVariable) {
JavacTreeMaker maker = builderType.getTreeMaker();
List<JCExpression> jceBlank = List.nil();
ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
/* case 0: (empty); break; */
{
JCStatement assignStat;
{
// pluralName = java.util.Collections.emptyList();
JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "emptyList"), jceBlank);
assignStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
}
JCStatement breakStat = maker.Break(null);
JCCase emptyCase = maker.Case(maker.Literal(CTC_INT, 0), List.of(assignStat, breakStat));
cases.append(emptyCase);
}
/* case 1: (singletonList); break; */
{
JCStatement assignStat;
{
// pluralName = java.util.Collections.singletonList(this.pluralName.get(0));
JCExpression zeroLiteral = maker.Literal(CTC_INT, 0);
JCExpression arg = maker.Apply(jceBlank, chainDots(builderType, builderVariable, data.getPluralName().toString(), "get"), List.of(zeroLiteral));
List<JCExpression> args = List.of(arg);
JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "singletonList"), args);
assignStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
}
JCStatement breakStat = maker.Break(null);
JCCase singletonCase = maker.Case(maker.Literal(CTC_INT, 1), List.of(assignStat, breakStat));
cases.append(singletonCase);
}
/* default: Create with right size, then addAll */
{
List<JCStatement> defStats = createListCopy(maker, data, builderType, source, builderVariable);
JCCase defaultCase = maker.Case(null, defStats);
cases.append(defaultCase);
}
JCStatement switchStat = maker.Switch(getSize(maker, builderType, data.getPluralName(), true, false, builderVariable), cases.toList());
JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs(), source);
JCStatement varDefStat = maker.VarDef(maker.Modifiers(0L), data.getPluralName(), localShadowerType, null);
statements.append(varDefStat);
statements.append(switchStat);
}
Aggregations