Search in sources :

Example 46 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project lombok by rzwitserloot.

the class JavacJavaUtilMapSingularizer method generatePluralMethod.

private void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> jceBlank = List.nil();
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, true, source));
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    long baseFlags = JavacHandlerUtil.addFinalIfNeeded(0, builderType.getContext());
    Name entryName = builderType.toName("$lombokEntry");
    JCExpression forEachType = chainDots(builderType, "java", "util", "Map", "Entry");
    forEachType = addTypeArgs(2, true, builderType, forEachType, data.getTypeArgs(), source);
    JCExpression keyArg = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(entryName), builderType.toName("getKey")), List.<JCExpression>nil());
    JCExpression valueArg = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(entryName), builderType.toName("getValue")), List.<JCExpression>nil());
    JCExpression addKey = maker.Apply(List.<JCExpression>nil(), chainDots(builderType, "this", data.getPluralName() + "$key", "add"), List.of(keyArg));
    JCExpression addValue = maker.Apply(List.<JCExpression>nil(), chainDots(builderType, "this", data.getPluralName() + "$value", "add"), List.of(valueArg));
    JCBlock forEachBody = maker.Block(0, List.<JCStatement>of(maker.Exec(addKey), maker.Exec(addValue)));
    JCExpression entrySetInvocation = maker.Apply(jceBlank, maker.Select(maker.Ident(data.getPluralName()), builderType.toName("entrySet")), jceBlank);
    JCStatement forEach = maker.ForeachLoop(maker.VarDef(maker.Modifiers(baseFlags), entryName, forEachType, null), entrySetInvocation, forEachBody);
    statements.append(forEach);
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    Name name = data.getPluralName();
    if (!fluent)
        name = builderType.toName(HandlerUtil.buildAccessorName("putAll", name.toString()));
    JCExpression paramType = chainDots(builderType, "java", "util", "Map");
    paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs(), source);
    JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null);
    JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), jceBlank, body, null);
    injectMethod(builderType, method);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Example 47 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project lombok by rzwitserloot.

the class JavacNode method traverse.

/**
 * Visits this node and all child nodes depth-first, calling the provided visitor's visit methods.
 */
public void traverse(JavacASTVisitor visitor) {
    switch(this.getKind()) {
        case COMPILATION_UNIT:
            visitor.visitCompilationUnit(this, (JCCompilationUnit) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitCompilationUnit(this, (JCCompilationUnit) get());
            break;
        case TYPE:
            visitor.visitType(this, (JCClassDecl) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitType(this, (JCClassDecl) get());
            break;
        case FIELD:
            visitor.visitField(this, (JCVariableDecl) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitField(this, (JCVariableDecl) get());
            break;
        case METHOD:
            visitor.visitMethod(this, (JCMethodDecl) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitMethod(this, (JCMethodDecl) get());
            break;
        case INITIALIZER:
            visitor.visitInitializer(this, (JCBlock) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitInitializer(this, (JCBlock) get());
            break;
        case ARGUMENT:
            JCMethodDecl parentMethod = (JCMethodDecl) up().get();
            visitor.visitMethodArgument(this, (JCVariableDecl) get(), parentMethod);
            ast.traverseChildren(visitor, this);
            visitor.endVisitMethodArgument(this, (JCVariableDecl) get(), parentMethod);
            break;
        case LOCAL:
            visitor.visitLocal(this, (JCVariableDecl) get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitLocal(this, (JCVariableDecl) get());
            break;
        case STATEMENT:
            visitor.visitStatement(this, get());
            ast.traverseChildren(visitor, this);
            visitor.endVisitStatement(this, get());
            break;
        case ANNOTATION:
            switch(up().getKind()) {
                case TYPE:
                    visitor.visitAnnotationOnType((JCClassDecl) up().get(), this, (JCAnnotation) get());
                    break;
                case FIELD:
                    visitor.visitAnnotationOnField((JCVariableDecl) up().get(), this, (JCAnnotation) get());
                    break;
                case METHOD:
                    visitor.visitAnnotationOnMethod((JCMethodDecl) up().get(), this, (JCAnnotation) get());
                    break;
                case ARGUMENT:
                    JCVariableDecl argument = (JCVariableDecl) up().get();
                    JCMethodDecl method = (JCMethodDecl) up().up().get();
                    visitor.visitAnnotationOnMethodArgument(argument, method, this, (JCAnnotation) get());
                    break;
                case LOCAL:
                    visitor.visitAnnotationOnLocal((JCVariableDecl) up().get(), this, (JCAnnotation) get());
                    break;
                default:
                    throw new AssertionError("Annotion not expected as child of a " + up().getKind());
            }
            break;
        default:
            throw new AssertionError("Unexpected kind during node traversal: " + getKind());
    }
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 48 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project lombok by rzwitserloot.

the class JavacHandlerUtil method deleteAnnotationIfNeccessary0.

private static void deleteAnnotationIfNeccessary0(JavacNode annotation, String... annotationTypes) {
    if (inNetbeansEditor(annotation))
        return;
    if (!annotation.shouldDeleteLombokAnnotations())
        return;
    JavacNode parentNode = annotation.directUp();
    switch(parentNode.getKind()) {
        case FIELD:
        case ARGUMENT:
        case LOCAL:
            JCVariableDecl variable = (JCVariableDecl) parentNode.get();
            variable.mods.annotations = filterList(variable.mods.annotations, annotation.get());
            break;
        case METHOD:
            JCMethodDecl method = (JCMethodDecl) parentNode.get();
            method.mods.annotations = filterList(method.mods.annotations, annotation.get());
            break;
        case TYPE:
            try {
                JCClassDecl type = (JCClassDecl) parentNode.get();
                type.mods.annotations = filterList(type.mods.annotations, annotation.get());
            } catch (ClassCastException e) {
            // something rather odd has been annotated. Better to just break only delombok instead of everything.
            }
            break;
        default:
            // This really shouldn't happen, but if it does, better just break delombok instead of breaking everything.
            return;
    }
    parentNode.getAst().setChanged();
    for (String annotationType : annotationTypes) {
        deleteImportFromCompilationUnit(annotation, annotationType);
    }
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 49 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project lombok by rzwitserloot.

the class JavacJavaUtilListSetSingularizer method generatePluralMethod.

void generatePluralMethod(boolean deprecate, JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    JCModifiers mods = makeMods(maker, builderType, deprecate);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source));
    JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "addAll");
    JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getPluralName())));
    statements.append(maker.Exec(invokeAdd));
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    Name name = data.getPluralName();
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    if (!fluent)
        name = builderType.toName(HandlerUtil.buildAccessorName("addAll", name.toString()));
    JCExpression paramType = chainDots(builderType, "java", "util", "Collection");
    paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs(), source);
    JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null);
    JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Example 50 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project lombok by rzwitserloot.

the class JavacJavaUtilListSetSingularizer method generateSingularMethod.

void generateSingularMethod(boolean deprecate, JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    JCModifiers mods = makeMods(maker, builderType, deprecate);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source));
    JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "add");
    JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getSingularName())));
    statements.append(maker.Exec(invokeAdd));
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    Name name = data.getSingularName();
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    if (!fluent)
        name = builderType.toName(HandlerUtil.buildAccessorName("add", name.toString()));
    JCExpression paramType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
    JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getSingularName(), paramType, null);
    JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Aggregations

JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)60 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)43 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)30 Name (com.sun.tools.javac.util.Name)28 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)26 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)26 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)25 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)22 ListBuffer (com.sun.tools.javac.util.ListBuffer)21 JavacNode (lombok.javac.JavacNode)15 JCTree (com.sun.tools.javac.tree.JCTree)14 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)14 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)7 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)6 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 JCExpressionStatement (com.sun.tools.javac.tree.JCTree.JCExpressionStatement)5 JavacTreeMaker (lombok.javac.JavacTreeMaker)5 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)4 Type (com.sun.tools.javac.code.Type)4 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)4