use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class JavacJavaUtilListSingularizer method createListCopy.
private List<JCStatement> createListCopy(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) {
List<JCExpression> jceBlank = List.nil();
Name thisName = builderType.toName("this");
JCExpression argToUnmodifiable;
{
// new java.util.ArrayList<Generics>(this.pluralName);
List<JCExpression> constructorArgs = List.nil();
JCExpression thisDotPluralName = maker.Select(maker.Ident(thisName), data.getPluralName());
constructorArgs = List.<JCExpression>of(thisDotPluralName);
JCExpression targetTypeExpr = chainDots(builderType, "java", "util", "ArrayList");
targetTypeExpr = addTypeArgs(1, false, builderType, targetTypeExpr, data.getTypeArgs(), source);
argToUnmodifiable = maker.NewClass(null, jceBlank, targetTypeExpr, constructorArgs, null);
}
JCStatement unmodifiableStat;
{
// pluralname = Collections.unmodifiableInterfaceType(-newlist-);
JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "unmodifiableList"), List.of(argToUnmodifiable));
unmodifiableStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
}
return List.of(unmodifiableStat);
}
use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class JavacJavaUtilMapSingularizer method generateClearMethod.
private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) {
JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
List<JCTypeParameter> typeParams = List.nil();
List<JCExpression> thrown = List.nil();
List<JCVariableDecl> params = List.nil();
List<JCExpression> jceBlank = List.nil();
JCExpression thisDotKeyField = chainDots(builderType, "this", data.getPluralName() + "$key");
JCExpression thisDotKeyFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$key", "clear");
JCExpression thisDotValueFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$value", "clear");
JCStatement clearKeyCall = maker.Exec(maker.Apply(jceBlank, thisDotKeyFieldDotClear, jceBlank));
JCStatement clearValueCall = maker.Exec(maker.Apply(jceBlank, thisDotValueFieldDotClear, jceBlank));
JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotKeyField, maker.Literal(CTC_BOT, null));
JCBlock clearCalls = maker.Block(0, List.of(clearKeyCall, clearValueCall));
JCStatement ifSetCallClear = maker.If(cond, clearCalls, null);
List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear);
JCBlock body = maker.Block(0, statements);
Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString()));
JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null);
injectMethod(builderType, method);
}
use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class JavacJavaUtilSingularizer method createJavaUtilSetMapInitialCapacitySwitchStatements.
protected List<JCStatement> createJavaUtilSetMapInitialCapacitySwitchStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, String emptyCollectionMethod, String singletonCollectionMethod, String targetType, JCTree source) {
List<JCExpression> jceBlank = List.nil();
ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
if (emptyCollectionMethod != null) {
// case 0: (empty); break;
JCStatement assignStat;
{
// pluralName = java.util.Collections.emptyCollectionMethod();
JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", emptyCollectionMethod), 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);
}
if (singletonCollectionMethod != null) {
// case 1: (singleton); break;
JCStatement assignStat;
{
// !mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName.get(0));
// mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName$key.get(0), this.pluralName$value.get(0));
JCExpression zeroLiteral = maker.Literal(CTC_INT, 0);
JCExpression arg = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + (mapMode ? "$key" : ""), "get"), List.of(zeroLiteral));
List<JCExpression> args;
if (mapMode) {
JCExpression zeroLiteralClone = maker.Literal(CTC_INT, 0);
JCExpression arg2 = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + (mapMode ? "$value" : ""), "get"), List.of(zeroLiteralClone));
args = List.of(arg, arg2);
} else {
args = List.of(arg);
}
JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", singletonCollectionMethod), 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:
List<JCStatement> statements = createJavaUtilSimpleCreationAndFillStatements(maker, data, builderType, mapMode, false, true, emptyCollectionMethod == null, targetType, source);
JCCase defaultCase = maker.Case(null, statements);
cases.append(defaultCase);
}
JCStatement switchStat = maker.Switch(getSize(maker, builderType, mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName(), true, false), cases.toList());
JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs(), source);
JCStatement varDefStat = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, null);
return List.of(varDefStat, switchStat);
}
use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class JavacJavaUtilSingularizer method createConstructBuilderVarIfNeeded.
protected JCStatement createConstructBuilderVarIfNeeded(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, JCTree source) {
List<JCExpression> jceBlank = List.nil();
Name v1Name = mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName();
Name v2Name = mapMode ? builderType.toName(data.getPluralName() + "$value") : null;
JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), v1Name);
JCExpression cond = maker.Binary(CTC_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
thisDotField = maker.Select(maker.Ident(builderType.toName("this")), v1Name);
JCExpression v1Type = chainDots(builderType, "java", "util", "ArrayList");
v1Type = addTypeArgs(1, false, builderType, v1Type, data.getTypeArgs(), source);
JCExpression constructArrayList = maker.NewClass(null, jceBlank, v1Type, jceBlank, null);
JCStatement initV1 = maker.Exec(maker.Assign(thisDotField, constructArrayList));
JCStatement thenPart;
if (mapMode) {
thisDotField = maker.Select(maker.Ident(builderType.toName("this")), v2Name);
JCExpression v2Type = chainDots(builderType, "java", "util", "ArrayList");
List<JCExpression> tArgs = data.getTypeArgs();
if (tArgs != null && tArgs.tail != null)
tArgs = tArgs.tail;
else
tArgs = List.nil();
v2Type = addTypeArgs(1, false, builderType, v2Type, tArgs, source);
constructArrayList = maker.NewClass(null, jceBlank, v2Type, jceBlank, null);
JCStatement initV2 = maker.Exec(maker.Assign(thisDotField, constructArrayList));
thenPart = maker.Block(0, List.of(initV1, initV2));
} else {
thenPart = initV1;
}
return maker.If(cond, thenPart, null);
}
use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class PrettyPrinter method visitTry.
@Override
public void visitTry(JCTry tree) {
aPrint("try ");
List<?> resources = readObject(tree, "resources", List.nil());
int len = resources.length();
switch(len) {
case 0:
break;
case 1:
print("(");
JCVariableDecl decl = (JCVariableDecl) resources.get(0);
flagMod = -1L & ~FINAL;
printVarDefInline(decl);
print(") ");
break;
default:
println("(");
indent++;
int c = 0;
for (Object i : resources) {
align();
flagMod = -1L & ~FINAL;
printVarDefInline((JCVariableDecl) i);
if (++c == len) {
print(") ");
} else {
println(";", (JCTree) i);
}
}
indent--;
}
println("{");
indent++;
for (JCStatement stat : tree.body.stats) print(stat);
indent--;
aPrint("}");
for (JCCatch catchBlock : tree.catchers) {
printCatch(catchBlock);
}
if (tree.finalizer != null) {
println(" finally {");
indent++;
for (JCStatement stat : tree.finalizer.stats) print(stat);
indent--;
aPrint("}");
}
println(tree);
}
Aggregations