use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.
the class HandleSuperBuilder method generateBuilderAbstractClass.
/**
* Creates and returns the abstract builder class and injects it into the annotated class.
*/
private JavacNode generateBuilderAbstractClass(SuperBuilderJob job, JCExpression superclassBuilderClass, List<JCExpression> superclassTypeParams, String classGenericName, String builderGenericName) {
JavacTreeMaker maker = job.parentType.getTreeMaker();
JCModifiers mods = maker.Modifiers(Flags.STATIC | Flags.ABSTRACT | Flags.PUBLIC);
// Keep any type params of the annotated class.
ListBuffer<JCTypeParameter> allTypeParams = new ListBuffer<JCTypeParameter>();
allTypeParams.appendList(copyTypeParams(job.sourceNode, job.typeParams));
// Add builder-specific type params required for inheritable builders.
// 1. The return type for the build() method, named "C", which extends the annotated class.
JCExpression annotatedClass = namePlusTypeParamsToTypeReference(maker, job.parentType, job.typeParams);
allTypeParams.append(maker.TypeParameter(job.toName(classGenericName), List.<JCExpression>of(annotatedClass)));
// 2. The return type for all setter methods, named "B", which extends this builder class.
Name builderClassName = job.toName(job.builderClassName);
ListBuffer<JCExpression> typeParamsForBuilder = getTypeParamExpressions(job.typeParams, maker, job.sourceNode);
typeParamsForBuilder.append(maker.Ident(job.toName(classGenericName)));
typeParamsForBuilder.append(maker.Ident(job.toName(builderGenericName)));
JCTypeApply typeApply = maker.TypeApply(namePlusTypeParamsToTypeReference(maker, job.parentType, builderClassName, false, List.<JCTypeParameter>nil()), typeParamsForBuilder.toList());
allTypeParams.append(maker.TypeParameter(job.toName(builderGenericName), List.<JCExpression>of(typeApply)));
JCExpression extending = null;
if (superclassBuilderClass != null) {
// If the annotated class extends another class, we want this builder to extend the builder of the superclass.
// 1. Add the type parameters of the superclass.
typeParamsForBuilder = getTypeParamExpressions(superclassTypeParams, maker, job.sourceNode);
// 2. Add the builder type params <C, B>.
typeParamsForBuilder.append(maker.Ident(job.toName(classGenericName)));
typeParamsForBuilder.append(maker.Ident(job.toName(builderGenericName)));
extending = maker.TypeApply(superclassBuilderClass, typeParamsForBuilder.toList());
}
JCClassDecl builder = maker.ClassDef(mods, builderClassName, allTypeParams.toList(), extending, List.<JCExpression>nil(), List.<JCTree>nil());
recursiveSetGeneratedBy(builder, job.sourceNode);
return injectType(job.parentType, builder);
}
use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.
the class JavacHandlerUtil method addSuppressWarningsAll.
public static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, JavacNode source, Context context) {
if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings())
return;
boolean addJLSuppress = !Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_SUPPRESSWARNINGS_ANNOTATIONS));
if (addJLSuppress) {
for (JCAnnotation ann : mods.annotations) {
JCTree type = ann.getAnnotationType();
Name n = null;
if (type instanceof JCIdent)
n = ((JCIdent) type).name;
else if (type instanceof JCFieldAccess)
n = ((JCFieldAccess) type).name;
if (n != null && n.contentEquals("SuppressWarnings")) {
addJLSuppress = false;
}
}
}
if (addJLSuppress)
addAnnotation(mods, node, source, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all"));
if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_FINDBUGS_SUPPRESSWARNINGS_ANNOTATIONS))) {
JavacTreeMaker maker = node.getTreeMaker();
JCExpression arg = maker.Assign(maker.Ident(node.toName("justification")), maker.Literal("generated code"));
addAnnotation(mods, node, source, "edu.umd.cs.findbugs.annotations.SuppressFBWarnings", arg);
}
}
use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.
the class JavacHandlerUtil method addAnnotation.
public static void addAnnotation(JCModifiers mods, JavacNode node, JavacNode source, String annotationTypeFqn, JCExpression arg) {
boolean isJavaLangBased;
String simpleName;
{
int idx = annotationTypeFqn.lastIndexOf('.');
simpleName = idx == -1 ? annotationTypeFqn : annotationTypeFqn.substring(idx + 1);
isJavaLangBased = idx == 9 && annotationTypeFqn.regionMatches(0, "java.lang.", 0, 10);
}
for (JCAnnotation ann : mods.annotations) {
JCTree annType = ann.getAnnotationType();
if (annType instanceof JCIdent) {
Name lastPart = ((JCIdent) annType).name;
if (lastPart.contentEquals(simpleName))
return;
}
if (annType instanceof JCFieldAccess) {
if (annType.toString().equals(annotationTypeFqn))
return;
}
}
JavacTreeMaker maker = node.getTreeMaker();
JCExpression annType = isJavaLangBased ? genJavaLangTypeRef(node, simpleName) : chainDotsString(node, annotationTypeFqn);
List<JCExpression> argList = arg != null ? List.of(arg) : List.<JCExpression>nil();
JCAnnotation annotation = recursiveSetGeneratedBy(maker.Annotation(annType, argList), source);
mods.annotations = mods.annotations.append(annotation);
}
use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.
the class JavacHandlerUtil method cloneSelfType.
public static JCExpression cloneSelfType(JavacNode childOfType) {
JavacNode typeNode = childOfType;
JavacTreeMaker maker = childOfType.getTreeMaker();
while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up();
return JavacHandlerUtil.namePlusTypeParamsToTypeReference(maker, typeNode, ((JCClassDecl) typeNode.get()).typarams);
}
use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.
the class JavacHandlerUtil method chainDots.
/**
* In javac, dotted access of any kind, from {@code java.lang.String} to {@code var.methodName}
* is represented by a fold-left of {@code Select} nodes with the leftmost string represented by
* a {@code Ident} node. This method generates such an expression.
* <p>
* The position of the generated node(s) will be equal to the {@code pos} parameter.
*
* For example, maker.Select(maker.Select(maker.Ident(NAME[java]), NAME[lang]), NAME[String]).
*
* @see com.sun.tools.javac.tree.JCTree.JCIdent
* @see com.sun.tools.javac.tree.JCTree.JCFieldAccess
*/
public static JCExpression chainDots(JavacNode node, int pos, String elem1, String elem2, String... elems) {
assert elems != null;
JavacTreeMaker maker = node.getTreeMaker();
if (pos != -1)
maker = maker.at(pos);
JCExpression e = null;
if (elem1 != null)
e = maker.Ident(node.toName(elem1));
if (elem2 != null)
e = e == null ? maker.Ident(node.toName(elem2)) : maker.Select(e, node.toName(elem2));
for (int i = 0; i < elems.length; i++) {
e = e == null ? maker.Ident(node.toName(elems[i])) : maker.Select(e, node.toName(elems[i]));
}
assert e != null;
return e;
}
Aggregations