use of com.sun.tools.javac.util.List in project lombok by rzwitserloot.
the class JavacTransformer method transform.
public void transform(long priority, Context context, java.util.List<JCCompilationUnit> compilationUnitsRaw) {
List<JCCompilationUnit> compilationUnits;
if (compilationUnitsRaw instanceof List<?>) {
compilationUnits = (List<JCCompilationUnit>) compilationUnitsRaw;
} else {
compilationUnits = List.nil();
for (int i = compilationUnitsRaw.size() - 1; i >= 0; i--) {
compilationUnits = compilationUnits.prepend(compilationUnitsRaw.get(i));
}
}
java.util.List<JavacAST> asts = new ArrayList<JavacAST>();
for (JCCompilationUnit unit : compilationUnits) asts.add(new JavacAST(messager, context, unit));
for (JavacAST ast : asts) {
ast.traverse(new AnnotationVisitor(priority));
handlers.callASTVisitors(ast, priority);
}
for (JavacAST ast : asts) if (ast.isChanged())
LombokOptions.markChanged(context, (JCCompilationUnit) ast.top().get());
}
use of com.sun.tools.javac.util.List in project lombok by rzwitserloot.
the class HandleNonNull method handle.
@Override
public void handle(AnnotationValues<NonNull> annotation, JCAnnotation ast, JavacNode annotationNode) {
handleFlagUsage(annotationNode, ConfigurationKeys.NON_NULL_FLAG_USAGE, "@NonNull");
if (annotationNode.up().getKind() == Kind.FIELD) {
try {
if (isPrimitive(((JCVariableDecl) annotationNode.up().get()).vartype)) {
annotationNode.addWarning("@NonNull is meaningless on a primitive.");
}
} catch (Exception ignore) {
}
return;
}
if (annotationNode.up().getKind() != Kind.ARGUMENT)
return;
JCMethodDecl declaration;
try {
declaration = (JCMethodDecl) annotationNode.up().up().get();
} catch (Exception e) {
return;
}
if (declaration.body == null) {
// This used to be a warning, but as @NonNull also has a documentary purpose, better to not warn about this. Since 1.16.7
return;
}
// Possibly, if 'declaration instanceof ConstructorDeclaration', fetch declaration.constructorCall, search it for any references to our parameter,
// and if they exist, create a new method in the class: 'private static <T> T lombok$nullCheck(T expr, String msg) {if (expr == null) throw NPE; return expr;}' and
// wrap all references to it in the super/this to a call to this method.
JCStatement nullCheck = recursiveSetGeneratedBy(generateNullCheck(annotationNode.getTreeMaker(), annotationNode.up(), annotationNode), ast, annotationNode.getContext());
if (nullCheck == null) {
// @NonNull applied to a primitive. Kinda pointless. Let's generate a warning.
annotationNode.addWarning("@NonNull is meaningless on a primitive.");
return;
}
List<JCStatement> statements = declaration.body.stats;
String expectedName = annotationNode.up().getName();
/* Abort if the null check is already there, delving into try and synchronized statements */
{
List<JCStatement> stats = statements;
int idx = 0;
while (stats.size() > idx) {
JCStatement stat = stats.get(idx++);
if (JavacHandlerUtil.isConstructorCall(stat))
continue;
if (stat instanceof JCTry) {
stats = ((JCTry) stat).body.stats;
idx = 0;
continue;
}
if (stat instanceof JCSynchronized) {
stats = ((JCSynchronized) stat).body.stats;
idx = 0;
continue;
}
String varNameOfNullCheck = returnVarNameIfNullCheck(stat);
if (varNameOfNullCheck == null)
break;
if (varNameOfNullCheck.equals(expectedName))
return;
}
}
List<JCStatement> tail = statements;
List<JCStatement> head = List.nil();
for (JCStatement stat : statements) {
if (JavacHandlerUtil.isConstructorCall(stat) || (JavacHandlerUtil.isGenerated(stat) && isNullCheck(stat))) {
tail = tail.tail;
head = head.prepend(stat);
continue;
}
break;
}
List<JCStatement> newList = tail.prepend(nullCheck);
for (JCStatement stat : head) newList = newList.prepend(stat);
declaration.body.stats = newList;
annotationNode.getAst().setChanged();
}
use of com.sun.tools.javac.util.List in project lombok by rzwitserloot.
the class JavacJavaUtilListSingularizer method appendBuildCode.
@Override
public void appendBuildCode(SingularData data, JavacNode builderType, JCTree source, ListBuffer<JCStatement> statements, Name targetVariableName) {
if (useGuavaInstead(builderType)) {
guavaListSetSingularizer.appendBuildCode(data, builderType, source, statements, targetVariableName);
return;
}
JavacTreeMaker maker = builderType.getTreeMaker();
List<JCExpression> jceBlank = List.nil();
ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
/* case 0: (empty); break; */
{
JCStatement assignStat;
{
// pluralName = java.util.Collections.emptyList();
JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "emptyList"), 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);
}
/* case 1: (singletonList); break; */
{
JCStatement assignStat;
{
// pluralName = java.util.Collections.singletonList(this.pluralName.get(0));
JCExpression zeroLiteral = maker.Literal(CTC_INT, 0);
JCExpression arg = maker.Apply(jceBlank, chainDots(builderType, "this", data.getPluralName().toString(), "get"), List.of(zeroLiteral));
List<JCExpression> args = List.of(arg);
JCExpression invoke = maker.Apply(jceBlank, chainDots(builderType, "java", "util", "Collections", "singletonList"), 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: Create with right size, then addAll */
{
List<JCStatement> defStats = createListCopy(maker, data, builderType, source);
JCCase defaultCase = maker.Case(null, defStats);
cases.append(defaultCase);
}
JCStatement switchStat = maker.Switch(getSize(maker, builderType, data.getPluralName(), true, false), cases.toList());
JCExpression localShadowerType = chainDotsString(builderType, data.getTargetFqn());
localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs(), source);
JCStatement varDefStat = maker.VarDef(maker.Modifiers(0), data.getPluralName(), localShadowerType, null);
statements.append(varDefStat);
statements.append(switchStat);
}
use of com.sun.tools.javac.util.List 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.util.List in project lombok by rzwitserloot.
the class JavacJavaUtilMapSingularizer method generateFields.
@Override
public java.util.List<JavacNode> generateFields(SingularData data, JavacNode builderType, JCTree source) {
if (useGuavaInstead(builderType)) {
return guavaMapSingularizer.generateFields(data, builderType, source);
}
JavacTreeMaker maker = builderType.getTreeMaker();
JCVariableDecl buildKeyField;
{
JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
type = addTypeArgs(1, false, builderType, type, data.getTypeArgs(), source);
buildKeyField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$key"), type, null);
}
JCVariableDecl buildValueField;
{
JCExpression type = JavacHandlerUtil.chainDots(builderType, "java", "util", "ArrayList");
List<JCExpression> tArgs = data.getTypeArgs();
if (tArgs != null && tArgs.size() > 1)
tArgs = tArgs.tail;
else
tArgs = List.nil();
type = addTypeArgs(1, false, builderType, type, tArgs, source);
buildValueField = maker.VarDef(maker.Modifiers(Flags.PRIVATE), builderType.toName(data.getPluralName() + "$value"), type, null);
}
JavacNode valueFieldNode = injectFieldAndMarkGenerated(builderType, buildValueField);
JavacNode keyFieldNode = injectFieldAndMarkGenerated(builderType, buildKeyField);
return Arrays.asList(keyFieldNode, valueFieldNode);
}
Aggregations