Search in sources :

Example 51 with JCMethodDecl

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

the class JavacJavaUtilMapSingularizer method generatePluralMethod.

private 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> jceBlank = List.nil();
    JCModifiers mods = makeMods(maker, builderType, deprecate);
    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 52 with JCMethodDecl

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

the class JavacJavaUtilMapSingularizer method generateSingularMethod.

private 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, true, source));
    Name keyName = builderType.toName(data.getSingularName().toString() + "Key");
    Name valueName = builderType.toName(data.getSingularName().toString() + "Value");
    /* this.pluralname$key.add(singularnameKey); */
    {
        JCExpression thisDotKeyFieldDotAdd = chainDots(builderType, "this", data.getPluralName() + "$key", "add");
        JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotKeyFieldDotAdd, List.<JCExpression>of(maker.Ident(keyName)));
        statements.append(maker.Exec(invokeAdd));
    }
    /* this.pluralname$value.add(singularnameValue); */
    {
        JCExpression thisDotValueFieldDotAdd = chainDots(builderType, "this", data.getPluralName() + "$value", "add");
        JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotValueFieldDotAdd, List.<JCExpression>of(maker.Ident(valueName)));
        statements.append(maker.Exec(invokeAdd));
    }
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    Name name = data.getSingularName();
    if (!fluent)
        name = builderType.toName(HandlerUtil.buildAccessorName("put", name.toString()));
    JCExpression paramTypeKey = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
    JCExpression paramTypeValue = cloneParamType(1, maker, data.getTypeArgs(), builderType, source);
    JCVariableDecl paramKey = maker.VarDef(maker.Modifiers(paramFlags), keyName, paramTypeKey, null);
    JCVariableDecl paramValue = maker.VarDef(maker.Modifiers(paramFlags), valueName, paramTypeValue, null);
    JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(paramKey, paramValue), 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 53 with JCMethodDecl

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

the class JavacJavaUtilMapSingularizer method generateClearMethod.

private void generateClearMethod(boolean deprecate, JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) {
    JCModifiers mods = makeMods(maker, builderType, deprecate);
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    List<JCVariableDecl> params = List.nil();
    List<JCExpression> jceBlank = List.nil();
    JCExpression thisDotKeyField = chainDots(builderType, "this", data.getPluralName() + "$key");
    JCExpression thisDotKeyFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$key", "clear");
    JCExpression thisDotValueFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$value", "clear");
    JCStatement clearKeyCall = maker.Exec(maker.Apply(jceBlank, thisDotKeyFieldDotClear, jceBlank));
    JCStatement clearValueCall = maker.Exec(maker.Apply(jceBlank, thisDotValueFieldDotClear, jceBlank));
    JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotKeyField, maker.Literal(CTC_BOT, null));
    JCBlock clearCalls = maker.Block(0, List.of(clearKeyCall, clearValueCall));
    JCStatement ifSetCallClear = maker.If(cond, clearCalls, null);
    List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear);
    JCBlock body = maker.Block(0, statements);
    Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString()));
    JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 54 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project st-js by st-js.

the class InternalUtils method constructor.

/**
 * Determines the symbol for a constructor given an invocation via {@code new}. If the tree is a declaration of an
 * anonymous class, then method returns constructor that gets invoked in the extended class, rather than the
 * anonymous constructor implicitly added by the constructor (JLS 15.9.5.1)
 *
 * @param tree
 *            the constructor invocation
 * @return the {@link javax.lang.model.element.ExecutableElement} corresponding to the constructor call in
 *         {@code tree}
 */
public static ExecutableElement constructor(NewClassTree tree) {
    if (!(tree instanceof JCTree.JCNewClass)) {
        ErrorReporter.errorAbort("InternalUtils.constructor: not a javac internal tree");
        // dead code
        return null;
    }
    JCNewClass newClassTree = (JCNewClass) tree;
    if (RETURN_INVOKE_CONSTRUCTOR && tree.getClassBody() != null) {
        // anonymous constructor bodies should contain exactly one statement
        // in the form:
        // super(arg1, ...)
        // or
        // o.super(arg1, ...)
        // 
        // which is a method invocation (!) to the actual constructor
        // the method call is guaranteed to return nonnull
        JCMethodDecl anonConstructor = (JCMethodDecl) TreeInfo.declarationFor(newClassTree.constructor, newClassTree);
        assert anonConstructor != null;
        assert anonConstructor.body.stats.size() == 1;
        JCExpressionStatement stmt = (JCExpressionStatement) anonConstructor.body.stats.head;
        JCTree.JCMethodInvocation superInvok = (JCMethodInvocation) stmt.expr;
        return (ExecutableElement) TreeInfo.symbol(superInvok.meth);
    }
    Element e = newClassTree.constructor;
    assert e instanceof ExecutableElement;
    return (ExecutableElement) e;
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCTree(com.sun.tools.javac.tree.JCTree) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation)

Example 55 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project checker-framework by typetools.

the class TreeUtils method constructor.

/**
 * Determines the symbol for a constructor given an invocation via {@code new}.
 *
 * <p>If the tree is a declaration of an anonymous class, then method returns constructor that
 * gets invoked in the extended class, rather than the anonymous constructor implicitly added by
 * the constructor (JLS 15.9.5.1)
 *
 * @see #elementFromUse(NewClassTree)
 * @param tree the constructor invocation
 * @return the {@link ExecutableElement} corresponding to the constructor call in {@code tree}
 */
public static ExecutableElement constructor(NewClassTree tree) {
    if (!(tree instanceof JCTree.JCNewClass)) {
        throw new BugInCF("TreeUtils.constructor: not a javac internal tree");
    }
    JCNewClass newClassTree = (JCNewClass) tree;
    if (tree.getClassBody() != null) {
        // anonymous constructor bodies should contain exactly one statement
        // in the form:
        // super(arg1, ...)
        // or
        // o.super(arg1, ...)
        // 
        // which is a method invocation (!) to the actual constructor
        // the method call is guaranteed to return nonnull
        JCMethodDecl anonConstructor = (JCMethodDecl) TreeInfo.declarationFor(newClassTree.constructor, newClassTree);
        assert anonConstructor != null;
        assert anonConstructor.body.stats.size() == 1;
        JCExpressionStatement stmt = (JCExpressionStatement) anonConstructor.body.stats.head;
        JCTree.JCMethodInvocation superInvok = (JCMethodInvocation) stmt.expr;
        return (ExecutableElement) TreeInfo.symbol(superInvok.meth);
    } else {
        Element e = newClassTree.constructor;
        return (ExecutableElement) e;
    }
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCTree(com.sun.tools.javac.tree.JCTree) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation)

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