Search in sources :

Example 1 with List

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());
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) ArrayList(java.util.ArrayList) List(com.sun.tools.javac.util.List) ArrayList(java.util.ArrayList)

Example 2 with List

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();
}
Also used : JCTry(com.sun.tools.javac.tree.JCTree.JCTry) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) List(com.sun.tools.javac.util.List) JCSynchronized(com.sun.tools.javac.tree.JCTree.JCSynchronized) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 3 with List

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);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) LombokImmutableList(lombok.core.LombokImmutableList) List(com.sun.tools.javac.util.List) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCCase(com.sun.tools.javac.tree.JCTree.JCCase)

Example 4 with List

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);
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) LombokImmutableList(lombok.core.LombokImmutableList) List(com.sun.tools.javac.util.List) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Name(com.sun.tools.javac.util.Name)

Example 5 with List

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);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) LombokImmutableList(lombok.core.LombokImmutableList) List(com.sun.tools.javac.util.List) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

List (com.sun.tools.javac.util.List)29 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)16 ArrayList (java.util.ArrayList)13 JCTree (com.sun.tools.javac.tree.JCTree)9 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)8 ListBuffer (com.sun.tools.javac.util.ListBuffer)7 ParameterList (com.redhat.ceylon.model.typechecker.model.ParameterList)5 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)5 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)5 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)4 Class (com.redhat.ceylon.model.typechecker.model.Class)4 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)4 Value (com.redhat.ceylon.model.typechecker.model.Value)4 Type (com.sun.tools.javac.code.Type)4 LombokImmutableList (lombok.core.LombokImmutableList)4 ImmutableList (com.google.common.collect.ImmutableList)3 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)3 Function (com.redhat.ceylon.model.typechecker.model.Function)3 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)3 Interface (com.redhat.ceylon.model.typechecker.model.Interface)3