use of com.sun.tools.javac.tree.JCTree.JCBlock in project lombok by rzwitserloot.
the class HandleWithBy method createWithBy.
public JCMethodDecl createWithBy(long access, JavacNode field, JavacTreeMaker maker, JavacNode source, List<JCAnnotation> onMethod, boolean makeAbstract) {
String withByName = toWithByName(field);
if (withByName == null)
return null;
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
Name methodName = field.toName(withByName);
JCExpression returnType = cloneSelfType(field);
JCBlock methodBody = null;
long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext());
LombokImmutableList<String> functionalInterfaceName = null;
TypeTag requiredCast = null;
JCExpression parameterizer = null;
boolean superExtendsStyle = true;
String applyMethodName = "apply";
if (fieldDecl.vartype instanceof JCPrimitiveTypeTree) {
TypeKind kind = ((JCPrimitiveTypeTree) fieldDecl.vartype).getPrimitiveTypeKind();
if (kind == TypeKind.CHAR) {
requiredCast = Javac.CTC_CHAR;
functionalInterfaceName = NAME_JUF_INTOP;
} else if (kind == TypeKind.SHORT) {
requiredCast = Javac.CTC_SHORT;
functionalInterfaceName = NAME_JUF_INTOP;
} else if (kind == TypeKind.BYTE) {
requiredCast = Javac.CTC_BYTE;
functionalInterfaceName = NAME_JUF_INTOP;
} else if (kind == TypeKind.INT) {
functionalInterfaceName = NAME_JUF_INTOP;
} else if (kind == TypeKind.LONG) {
functionalInterfaceName = NAME_JUF_LONGOP;
} else if (kind == TypeKind.FLOAT) {
functionalInterfaceName = NAME_JUF_DOUBLEOP;
requiredCast = Javac.CTC_FLOAT;
} else if (kind == TypeKind.DOUBLE) {
functionalInterfaceName = NAME_JUF_DOUBLEOP;
} else if (kind == TypeKind.BOOLEAN) {
functionalInterfaceName = NAME_JUF_OP;
parameterizer = JavacHandlerUtil.genJavaLangTypeRef(field, "Boolean");
superExtendsStyle = false;
}
}
if (functionalInterfaceName == null) {
functionalInterfaceName = NAME_JUF_FUNCTION;
parameterizer = cloneType(maker, fieldDecl.vartype, source);
}
if (functionalInterfaceName == NAME_JUF_INTOP)
applyMethodName = "applyAsInt";
if (functionalInterfaceName == NAME_JUF_LONGOP)
applyMethodName = "applyAsLong";
if (functionalInterfaceName == NAME_JUF_DOUBLEOP)
applyMethodName = "applyAsDouble";
JCExpression varType = chainDots(field, functionalInterfaceName);
if (parameterizer != null && superExtendsStyle) {
JCExpression parameterizer1 = parameterizer;
JCExpression parameterizer2 = cloneType(maker, parameterizer, source);
// TODO: Apply copyable annotations to 'parameterizer' and 'parameterizer2'.
JCExpression arg1 = maker.Wildcard(maker.TypeBoundKind(BoundKind.SUPER), parameterizer1);
JCExpression arg2 = maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), parameterizer2);
varType = maker.TypeApply(varType, List.of(arg1, arg2));
}
if (parameterizer != null && !superExtendsStyle) {
varType = maker.TypeApply(varType, List.of(parameterizer));
}
Name paramName = field.toName("transformer");
JCVariableDecl param = maker.VarDef(maker.Modifiers(flags), paramName, varType, null);
if (!makeAbstract) {
ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
JCExpression selfType = cloneSelfType(field);
if (selfType == null)
return null;
ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
for (JavacNode child : field.up().down()) {
if (child.getKind() != Kind.FIELD)
continue;
JCVariableDecl childDecl = (JCVariableDecl) child.get();
// Skip fields that start with $
if (childDecl.name.toString().startsWith("$"))
continue;
long fieldFlags = childDecl.mods.flags;
// Skip static fields.
if ((fieldFlags & Flags.STATIC) != 0)
continue;
// Skip initialized final fields.
if (((fieldFlags & Flags.FINAL) != 0) && childDecl.init != null)
continue;
if (child.get() == field.get()) {
JCExpression invoke = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(paramName), field.toName(applyMethodName)), List.<JCExpression>of(createFieldAccessor(maker, child, FieldAccess.ALWAYS_FIELD)));
if (requiredCast != null)
invoke = maker.TypeCast(maker.TypeIdent(requiredCast), invoke);
args.append(invoke);
} else {
args.append(createFieldAccessor(maker, child, FieldAccess.ALWAYS_FIELD));
}
}
JCNewClass newClass = maker.NewClass(null, List.<JCExpression>nil(), selfType, args.toList(), null);
JCReturn returnStatement = maker.Return(newClass);
statements.append(returnStatement);
methodBody = maker.Block(0, statements.toList());
}
List<JCTypeParameter> methodGenericParams = List.nil();
List<JCVariableDecl> parameters = List.of(param);
List<JCExpression> throwsClauses = List.nil();
JCExpression annotationMethodDefaultValue = null;
List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod);
CheckerFrameworkVersion checkerFramework = getCheckerFrameworkVersion(source);
if (checkerFramework.generateSideEffectFree())
annsOnMethod = annsOnMethod.prepend(maker.Annotation(genTypeRef(source, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.<JCExpression>nil()));
if (isFieldDeprecated(field))
annsOnMethod = annsOnMethod.prepend(maker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil()));
if (makeAbstract)
access = access | Flags.ABSTRACT;
AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field);
boolean makeFinal = shouldMakeFinal(field, accessors);
if (makeFinal)
access |= Flags.FINAL;
createRelevantNonNullAnnotation(source, param);
JCMethodDecl decl = recursiveSetGeneratedBy(maker.MethodDef(maker.Modifiers(access, annsOnMethod), methodName, returnType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source);
copyJavadoc(field, decl, CopyJavadoc.WITH_BY);
createRelevantNonNullAnnotation(source, decl);
return decl;
}
use of com.sun.tools.javac.tree.JCTree.JCBlock in project lombok by rzwitserloot.
the class JavacHandlerUtil method generateNullCheck.
/**
* Generates a new statement that checks if the given local is null, and if so, throws a configured exception with the
* local variable name as message.
*/
public static JCStatement generateNullCheck(JavacTreeMaker maker, JCExpression typeNode, Name varName, JavacNode source, String customMessage) {
NullCheckExceptionType exceptionType = source.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
if (exceptionType == null)
exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION;
if (typeNode != null && isPrimitive(typeNode))
return null;
JCLiteral message = maker.Literal(exceptionType.toExceptionMessage(varName.toString(), customMessage));
LombokImmutableList<String> method = exceptionType.getMethod();
if (method != null) {
return maker.Exec(maker.Apply(List.<JCExpression>nil(), chainDots(source, method), List.of(maker.Ident(varName), message)));
}
if (exceptionType == NullCheckExceptionType.ASSERTION) {
return maker.Assert(maker.Binary(CTC_NOT_EQUAL, maker.Ident(varName), maker.Literal(CTC_BOT, null)), message);
}
JCExpression exType = genTypeRef(source, exceptionType.getExceptionType());
JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), exType, List.<JCExpression>of(message), null);
JCStatement throwStatement = maker.Throw(exception);
JCBlock throwBlock = maker.Block(0, List.of(throwStatement));
return maker.If(maker.Binary(CTC_EQUAL, maker.Ident(varName), maker.Literal(CTC_BOT, null)), throwBlock, null);
}
use of com.sun.tools.javac.tree.JCTree.JCBlock in project lombok by rzwitserloot.
the class PrettyPrinter method visitCase.
@Override
public void visitCase(JCCase tree) {
// Starting with JDK12, switches allow multiple expressions per case, and can take the form of an expression (preview feature).
// JDK 17+
List<JCTree> pats = readObject(tree, "labels", null);
if (pats == null) {
// JDK 12-17
pats = readObject(tree, "pats", null);
}
if (pats == null) {
// JDK -11
JCTree pat = readObject(tree, "pat", null);
pats = pat == null ? List.<JCTree>nil() : List.of(pat);
}
if (pats.isEmpty() || pats.size() == 1 && pats.head.getClass().getName().endsWith("$JCDefaultCaseLabel")) {
aPrint("default");
} else {
aPrint("case ");
print(pats, ", ");
}
// JDK 12+
Enum<?> caseKind = readObject(tree, "caseKind", null);
if (caseKind != null && caseKind.name().equalsIgnoreCase("RULE")) {
print(" -> ");
if (tree.stats.head instanceof JCBreak) {
JCBreak b = (JCBreak) tree.stats.head;
print((JCExpression) readObject(b, "value", null));
print(";");
needsNewLine = true;
needsAlign = true;
} else if (tree.stats.head.getClass().getName().endsWith("$JCYield")) {
print((JCExpression) readObject(tree.stats.head, "value", null));
print(";");
needsNewLine = true;
needsAlign = true;
} else {
print(tree.stats.head);
if (tree.stats.head instanceof JCBlock)
needsNewLine = false;
}
} else {
println(": ");
indent++;
print(tree.stats, "");
indent--;
}
}
use of com.sun.tools.javac.tree.JCTree.JCBlock in project lombok by rzwitserloot.
the class PrettyPrinter method printLambda0.
private void printLambda0(JCTree tree) {
List<JCVariableDecl> params = readObject(tree, "params", List.<JCVariableDecl>nil());
boolean explicit = true;
int paramLength = params.size();
try {
explicit = readObject(tree, "paramKind", new Object()).toString().equals("EXPLICIT");
} catch (Exception e) {
}
boolean useParens = paramLength != 1 || explicit;
if (useParens)
print("(");
if (explicit) {
boolean first = true;
for (JCVariableDecl vd : params) {
if (!first)
print(", ");
first = false;
printVarDefInline(vd);
}
} else {
String sep = "";
for (JCVariableDecl param : params) {
print(sep);
print(param.name);
sep = ", ";
}
}
if (useParens)
print(")");
print(" -> ");
JCTree body = readObject(tree, "body", (JCTree) null);
if (body instanceof JCBlock) {
println("{");
indent++;
print(((JCBlock) body).stats, "");
indent--;
aPrint("}");
} else {
print(body);
}
}
use of com.sun.tools.javac.tree.JCTree.JCBlock in project lombok by rzwitserloot.
the class JavacJavaUtilMapSingularizer method generateClearStatements.
@Override
protected JCStatement generateClearStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType) {
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));
return maker.If(cond, clearCalls, null);
}
Aggregations