use of com.sun.tools.javac.tree.JCTree.JCAnnotation in project lombok by rzwitserloot.
the class HandleSuperBuilder method generateAbstractSelfMethod.
private JCMethodDecl generateAbstractSelfMethod(SuperBuilderJob job, boolean override, String builderGenericName) {
JavacTreeMaker maker = job.getTreeMaker();
List<JCAnnotation> annotations = List.nil();
JCAnnotation overrideAnnotation = override ? maker.Annotation(genJavaLangTypeRef(job.builderType, "Override"), List.<JCExpression>nil()) : null;
JCAnnotation sefAnnotation = job.checkerFramework.generatePure() ? maker.Annotation(genTypeRef(job.builderType, CheckerFrameworkVersion.NAME__PURE), List.<JCExpression>nil()) : null;
if (sefAnnotation != null)
annotations = annotations.prepend(sefAnnotation);
if (overrideAnnotation != null)
annotations = annotations.prepend(overrideAnnotation);
JCModifiers modifiers = maker.Modifiers(Flags.PROTECTED | Flags.ABSTRACT, annotations);
Name name = job.toName(SELF_METHOD);
JCExpression returnType = maker.Ident(job.toName(builderGenericName));
returnType = addCheckerFrameworkReturnsReceiver(returnType, maker, job.builderType, job.checkerFramework);
return maker.MethodDef(modifiers, name, returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null);
}
use of com.sun.tools.javac.tree.JCTree.JCAnnotation in project lombok by rzwitserloot.
the class HandleWithBy method handle.
@Override
public void handle(AnnotationValues<WithBy> annotation, JCAnnotation ast, JavacNode annotationNode) {
handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.WITHBY_FLAG_USAGE, "@WithBy");
deleteAnnotationIfNeccessary(annotationNode, WithBy.class);
deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
JavacNode node = annotationNode.up();
AccessLevel level = annotation.getInstance().value();
if (level == AccessLevel.NONE || node == null)
return;
List<JCAnnotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@WithBy(onMethod", annotationNode);
switch(node.getKind()) {
case FIELD:
createWithByForFields(level, annotationNode.upFromAnnotationToFields(), annotationNode, true, onMethod);
break;
case TYPE:
if (!onMethod.isEmpty())
annotationNode.addError("'onMethod' is not supported for @WithBy on a type.");
generateWithByForType(node, annotationNode, level, false);
break;
}
}
use of com.sun.tools.javac.tree.JCTree.JCAnnotation 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.JCAnnotation in project lombok by rzwitserloot.
the class JavacHandlerUtil method unboxAndRemoveAnnotationParameter.
static List<JCAnnotation> unboxAndRemoveAnnotationParameter(JCAnnotation ast, String parameterName, String errorName, JavacNode annotationNode) {
ListBuffer<JCExpression> params = new ListBuffer<JCExpression>();
ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>();
outer: for (JCExpression param : ast.args) {
boolean allowRaw;
String nameOfParam = "value";
JCExpression valueOfParam = null;
if (param instanceof JCAssign) {
JCAssign assign = (JCAssign) param;
if (assign.lhs instanceof JCIdent) {
JCIdent ident = (JCIdent) assign.lhs;
nameOfParam = ident.name.toString();
}
valueOfParam = assign.rhs;
}
/* strip trailing underscores */
{
int lastIdx;
for (lastIdx = nameOfParam.length(); lastIdx > 0; lastIdx--) {
if (nameOfParam.charAt(lastIdx - 1) != '_')
break;
}
allowRaw = lastIdx < nameOfParam.length();
nameOfParam = nameOfParam.substring(0, lastIdx);
}
if (!parameterName.equals(nameOfParam)) {
params.append(param);
continue outer;
}
int endPos = Javac.getEndPosition(param.pos(), (JCCompilationUnit) annotationNode.top().get());
annotationNode.getAst().removeFromDeferredDiagnostics(param.pos, endPos);
if (valueOfParam instanceof JCAnnotation) {
String dummyAnnotationName = ((JCAnnotation) valueOfParam).annotationType.toString();
dummyAnnotationName = dummyAnnotationName.replace("_", "").replace("$", "").replace("x", "").replace("X", "");
if (dummyAnnotationName.length() > 0) {
if (allowRaw) {
result.append((JCAnnotation) valueOfParam);
} else {
addError(errorName, annotationNode);
continue outer;
}
} else {
for (JCExpression expr : ((JCAnnotation) valueOfParam).args) {
if (expr instanceof JCAssign && ((JCAssign) expr).lhs instanceof JCIdent) {
JCIdent id = (JCIdent) ((JCAssign) expr).lhs;
if ("value".equals(id.name.toString())) {
expr = ((JCAssign) expr).rhs;
} else {
addError(errorName, annotationNode);
}
}
if (expr instanceof JCAnnotation) {
result.append((JCAnnotation) expr);
} else if (expr instanceof JCNewArray) {
for (JCExpression expr2 : ((JCNewArray) expr).elems) {
if (expr2 instanceof JCAnnotation) {
result.append((JCAnnotation) expr2);
} else {
addError(errorName, annotationNode);
continue outer;
}
}
} else {
addError(errorName, annotationNode);
continue outer;
}
}
}
} else if (valueOfParam instanceof JCNewArray) {
JCNewArray arr = (JCNewArray) valueOfParam;
if (arr.elems.isEmpty()) {
// Just remove it, this is always fine.
} else if (allowRaw) {
for (JCExpression jce : arr.elems) {
if (jce instanceof JCAnnotation)
result.append((JCAnnotation) jce);
else
addError(errorName, annotationNode);
}
} else {
addError(errorName, annotationNode);
}
} else {
addError(errorName, annotationNode);
}
}
for (JCAnnotation annotation : result) {
clearTypes(annotation);
}
ast.args = params.toList();
return result.toList();
}
use of com.sun.tools.javac.tree.JCTree.JCAnnotation 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);
}
}
Aggregations