use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class JavacJavaUtilMapSingularizer method generateSingularMethod.
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, true, source));
Name keyName = builderType.toName(data.getSingularName().toString() + "Key");
Name valueName = builderType.toName(data.getSingularName().toString() + "Value");
/* this.pluralname$key.add(singularnameKey); */
{
JCExpression thisDotKeyFieldDotAdd = chainDots(builderType, "this", data.getPluralName() + "$key", "add");
JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotKeyFieldDotAdd, List.<JCExpression>of(maker.Ident(keyName)));
statements.append(maker.Exec(invokeAdd));
}
/* this.pluralname$value.add(singularnameValue); */
{
JCExpression thisDotValueFieldDotAdd = chainDots(builderType, "this", data.getPluralName() + "$value", "add");
JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotValueFieldDotAdd, List.<JCExpression>of(maker.Ident(valueName)));
statements.append(maker.Exec(invokeAdd));
}
if (returnStatement != null)
statements.append(returnStatement);
JCBlock body = maker.Block(0, statements.toList());
long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
Name name = data.getSingularName();
if (!fluent)
name = builderType.toName(HandlerUtil.buildAccessorName("put", name.toString()));
JCExpression paramTypeKey = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
JCExpression paramTypeValue = cloneParamType(1, maker, data.getTypeArgs(), builderType, source);
JCVariableDecl paramKey = maker.VarDef(maker.Modifiers(paramFlags), keyName, paramTypeKey, null);
JCVariableDecl paramValue = maker.VarDef(maker.Modifiers(paramFlags), valueName, paramTypeValue, null);
JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(paramKey, paramValue), thrown, body, null);
injectMethod(builderType, method);
}
use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class JavacJavaUtilSingularizer method createJavaUtilSimpleCreationAndFillStatements.
protected List<JCStatement> createJavaUtilSimpleCreationAndFillStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType, boolean mapMode, boolean defineVar, boolean addInitialCapacityArg, boolean nullGuard, String targetType, JCTree source) {
List<JCExpression> jceBlank = List.nil();
Name thisName = builderType.toName("this");
JCStatement createStat;
{
// pluralName = new java.util.TargetType(initialCap);
List<JCExpression> constructorArgs = List.nil();
if (addInitialCapacityArg) {
Name varName = mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName();
// this.varName.size() < MAX_POWER_OF_2 ? 1 + this.varName.size() + (this.varName.size() - 3) / 3 : Integer.MAX_VALUE;
// lessThanCutOff = this.varName.size() < MAX_POWER_OF_2
JCExpression lessThanCutoff = maker.Binary(CTC_LESS_THAN, getSize(maker, builderType, varName, nullGuard, true), maker.Literal(CTC_INT, 0x40000000));
JCExpression integerMaxValue = genJavaLangTypeRef(builderType, "Integer", "MAX_VALUE");
JCExpression sizeFormulaLeft = maker.Binary(CTC_PLUS, maker.Literal(CTC_INT, 1), getSize(maker, builderType, varName, nullGuard, true));
JCExpression sizeFormulaRightLeft = maker.Parens(maker.Binary(CTC_MINUS, getSize(maker, builderType, varName, nullGuard, true), maker.Literal(CTC_INT, 3)));
JCExpression sizeFormulaRight = maker.Binary(CTC_DIV, sizeFormulaRightLeft, maker.Literal(CTC_INT, 3));
JCExpression sizeFormula = maker.Binary(CTC_PLUS, sizeFormulaLeft, sizeFormulaRight);
constructorArgs = List.<JCExpression>of(maker.Conditional(lessThanCutoff, sizeFormula, integerMaxValue));
}
JCExpression targetTypeExpr = chainDots(builderType, "java", "util", targetType);
targetTypeExpr = addTypeArgs(mapMode ? 2 : 1, false, builderType, targetTypeExpr, data.getTypeArgs(), source);
JCExpression constructorCall = maker.NewClass(null, jceBlank, targetTypeExpr, constructorArgs, null);
if (defineVar) {
JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs(), source);
createStat = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, constructorCall);
} else {
createStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), constructorCall));
}
}
JCStatement fillStat;
{
if (mapMode) {
// for (int $i = 0; $i < this.pluralname$key.size(); i++) pluralname.put(this.pluralname$key.get($i), this.pluralname$value.get($i));
Name ivar = builderType.toName("$i");
Name keyVarName = builderType.toName(data.getPluralName() + "$key");
JCExpression pluralnameDotPut = maker.Select(maker.Ident(data.getPluralName()), builderType.toName("put"));
JCExpression arg1 = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + "$key", "get"), List.<JCExpression>of(maker.Ident(ivar)));
JCExpression arg2 = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName() + "$value", "get"), List.<JCExpression>of(maker.Ident(ivar)));
JCStatement putStatement = maker.Exec(maker.Apply(jceBlank, pluralnameDotPut, List.of(arg1, arg2)));
JCStatement forInit = maker.VarDef(maker.Modifiers(0), ivar, maker.TypeIdent(CTC_INT), maker.Literal(CTC_INT, 0));
JCExpression checkExpr = maker.Binary(CTC_LESS_THAN, maker.Ident(ivar), getSize(maker, builderType, keyVarName, nullGuard, true));
JCExpression incrementExpr = maker.Unary(CTC_POSTINC, maker.Ident(ivar));
fillStat = maker.ForLoop(List.of(forInit), checkExpr, List.of(maker.Exec(incrementExpr)), putStatement);
} else {
// pluralname.addAll(this.pluralname);
JCExpression thisDotPluralName = maker.Select(maker.Ident(thisName), data.getPluralName());
fillStat = maker.Exec(maker.Apply(jceBlank, maker.Select(maker.Ident(data.getPluralName()), builderType.toName("addAll")), List.of(thisDotPluralName)));
}
if (nullGuard) {
JCExpression thisDotField = maker.Select(maker.Ident(thisName), mapMode ? builderType.toName(data.getPluralName() + "$key") : data.getPluralName());
JCExpression nullCheck = maker.Binary(CTC_NOT_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
fillStat = maker.If(nullCheck, fillStat, null);
}
}
JCStatement unmodifiableStat;
{
// pluralname = Collections.unmodifiableInterfaceType(pluralname);
JCExpression arg = maker.Ident(data.getPluralName());
JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "unmodifiable" + data.getTargetSimpleType()), List.of(arg));
unmodifiableStat = maker.Exec(maker.Assign(maker.Ident(data.getPluralName()), invoke));
}
return List.of(createStat, fillStat, unmodifiableStat);
}
use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class PrettyPrinter method printCatch.
private void printCatch(JCCatch catchBlock) {
print(" catch (");
// ExprType1 | ExprType2 handled via JCTypeUnion.
printVarDefInline(catchBlock.param);
println(") {");
indent++;
for (JCStatement stat : catchBlock.body.stats) print(stat);
indent--;
aPrint("}");
}
use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class PrettyPrinter method visitForLoop.
@Override
public void visitForLoop(JCForLoop tree) {
aPrint("for (");
if (tree.init.nonEmpty()) {
// ForInit is either a StatementExpressionList or a LocalVariableDeclaration
if (tree.init.head instanceof JCVariableDecl) {
boolean first = true;
int dims = 0;
for (JCStatement i : tree.init) {
JCVariableDecl vd = (JCVariableDecl) i;
if (first) {
printVarDefInline(vd);
dims = dims(vd.vartype);
} else {
print(", ");
print(vd.name);
int dimDiff = dims(vd.vartype) - dims;
for (int j = 0; j < dimDiff; j++) print("[]");
if (vd.init != null) {
print(" = ");
print(vd.init);
}
}
first = false;
}
} else {
boolean first = true;
for (JCStatement exprStatement : tree.init) {
if (!first)
print(", ");
first = false;
print(((JCExpressionStatement) exprStatement).expr);
}
}
}
print("; ");
if (tree.cond != null)
print(tree.cond);
print("; ");
boolean first = true;
for (JCExpressionStatement exprStatement : tree.step) {
if (!first)
print(", ");
first = false;
print(exprStatement.expr);
}
print(") ");
print(tree.body);
}
use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.
the class JavacHandlerUtil method generateNullCheck.
/**
* Generates a new statement that checks if the given variable is null, and if so, throws a specified exception with the
* variable name as message.
*
* @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}.
*/
public static JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable, JavacNode source) {
NullCheckExceptionType exceptionType = source.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
if (exceptionType == null)
exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION;
JCVariableDecl varDecl = (JCVariableDecl) variable.get();
if (isPrimitive(varDecl.vartype))
return null;
Name fieldName = varDecl.name;
JCExpression exType = genTypeRef(variable, exceptionType.getExceptionType());
JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), exType, List.<JCExpression>of(maker.Literal(exceptionType.toExceptionMessage(fieldName.toString()))), null);
JCStatement throwStatement = maker.Throw(exception);
JCBlock throwBlock = maker.Block(0, List.of(throwStatement));
return maker.If(maker.Binary(CTC_EQUAL, maker.Ident(fieldName), maker.Literal(CTC_BOT, null)), throwBlock, null);
}
Aggregations