Search in sources :

Example 16 with JCBlock

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

the class JavacJavaUtilListSetSingularizer method generateClearMethod.

private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) {
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    List<JCVariableDecl> params = List.nil();
    List<JCExpression> jceBlank = List.nil();
    JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName());
    JCExpression thisDotFieldDotClear = maker.Select(maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName()), builderType.toName("clear"));
    JCStatement clearCall = maker.Exec(maker.Apply(jceBlank, thisDotFieldDotClear, jceBlank));
    JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotField, maker.Literal(CTC_BOT, null));
    JCStatement ifSetCallClear = maker.If(cond, clearCall, 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 17 with JCBlock

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

the class JavacJavaUtilListSetSingularizer method generateSingularMethod.

void generateSingularMethod(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 = maker.Modifiers(Flags.PUBLIC);
    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)

Example 18 with JCBlock

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

the class JavacJavaUtilMapSingularizer method generateClearMethod.

private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) {
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    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 19 with JCBlock

use of com.sun.tools.javac.tree.JCTree.JCBlock in project error-prone by google.

the class BlockTemplate method match.

/**
   * If the tree is a {@link JCBlock}, returns a list of disjoint matches corresponding to the exact
   * list of template statements found consecutively; otherwise, returns an empty list.
   */
@Override
public Iterable<BlockTemplateMatch> match(JCTree tree, Context context) {
    // TODO(lowasser): consider nonconsecutive matches?
    if (tree instanceof JCBlock) {
        JCBlock block = (JCBlock) tree;
        ImmutableList<JCStatement> targetStatements = ImmutableList.copyOf(block.getStatements());
        return matchesStartingAnywhere(block, 0, targetStatements, context).first().or(List.<BlockTemplateMatch>nil());
    }
    return ImmutableList.of();
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 20 with JCBlock

use of com.sun.tools.javac.tree.JCTree.JCBlock in project ceylon-compiler by ceylon.

the class Attr method visitMethodDef.

public void visitMethodDef(JCMethodDecl tree) {
    MethodSymbol m = tree.sym;
    Lint lint = env.info.lint.augment(m.attributes_field, m.flags());
    Lint prevLint = chk.setLint(lint);
    MethodSymbol prevMethod = chk.setMethod(m);
    try {
        deferredLintHandler.flush(tree.pos());
        chk.checkDeprecatedAnnotation(tree.pos(), m);
        attribBounds(tree.typarams);
        // JLS ???
        if (m.isStatic()) {
            chk.checkHideClashes(tree.pos(), env.enclClass.type, m);
        } else {
            chk.checkOverrideClashes(tree.pos(), env.enclClass.type, m);
        }
        chk.checkOverride(tree, m);
        // Create a new environment with local scope
        // for attributing the method.
        Env<AttrContext> localEnv = memberEnter.methodEnv(tree, env);
        localEnv.info.lint = lint;
        // Enter all type parameters into the local method scope.
        for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) localEnv.info.scope.enterIfAbsent(l.head.type.tsym);
        ClassSymbol owner = env.enclClass.sym;
        if ((owner.flags() & ANNOTATION) != 0 && tree.params.nonEmpty())
            log.error(tree.params.head.pos(), "intf.annotation.members.cant.have.params");
        // Attribute all value parameters.
        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
            attribStat(l.head, localEnv);
        }
        chk.checkVarargsMethodDecl(localEnv, tree);
        // Check that type parameters are well-formed.
        chk.validate(tree.typarams, localEnv);
        // Check that result type is well-formed.
        chk.validate(tree.restype, localEnv);
        // annotation method checks
        if ((owner.flags() & ANNOTATION) != 0) {
            // annotation method cannot have throws clause
            if (tree.thrown.nonEmpty()) {
                log.error(tree.thrown.head.pos(), "throws.not.allowed.in.intf.annotation");
            }
            // annotation method cannot declare type-parameters
            if (tree.typarams.nonEmpty()) {
                log.error(tree.typarams.head.pos(), "intf.annotation.members.cant.have.type.params");
            }
            // validate annotation method's return type (could be an annotation type)
            chk.validateAnnotationType(tree.restype);
            // ensure that annotation method does not clash with members of Object/Annotation
            chk.validateAnnotationMethod(tree.pos(), m);
            if (tree.defaultValue != null) {
                // if default value is an annotation, check it is a well-formed
                // annotation value (e.g. no duplicate values, no missing values, etc.)
                chk.validateAnnotationTree(tree.defaultValue);
            }
        }
        for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail) chk.checkType(l.head.pos(), l.head.type, syms.throwableType);
        if (tree.body == null) {
            // in a retrofit signature class.
            if ((owner.flags() & INTERFACE) == 0 && (tree.mods.flags & (ABSTRACT | NATIVE)) == 0 && !relax)
                log.error(tree.pos(), "missing.meth.body.or.decl.abstract");
            if (tree.defaultValue != null) {
                if ((owner.flags() & ANNOTATION) == 0)
                    log.error(tree.pos(), "default.allowed.in.intf.annotation.member");
            }
        } else if ((owner.flags() & INTERFACE) != 0) {
            log.error(tree.body.pos(), "intf.meth.cant.have.body");
        } else if ((tree.mods.flags & ABSTRACT) != 0) {
            log.error(tree.pos(), "abstract.meth.cant.have.body");
        } else if ((tree.mods.flags & NATIVE) != 0) {
            log.error(tree.pos(), "native.meth.cant.have.body");
        } else {
            // or we are compiling class java.lang.Object.
            if (tree.name == names.init && owner.type != syms.objectType) {
                JCBlock body = tree.body;
                if (body.stats.isEmpty() || !TreeInfo.isSelfCall(names, body.stats.head)) {
                    body.stats = body.stats.prepend(memberEnter.SuperCall(make.at(body.pos), List.<Type>nil(), List.<JCVariableDecl>nil(), false));
                } else if ((env.enclClass.sym.flags() & ENUM) != 0 && (tree.mods.flags & GENERATEDCONSTR) == 0 && TreeInfo.isSuperCall(names, body.stats.head)) {
                    // enum constructors are not allowed to call super
                    // directly, so make sure there aren't any super calls
                    // in enum constructors, except in the compiler
                    // generated one.
                    log.error(tree.body.stats.head.pos(), "call.to.super.not.allowed.in.enum.ctor", env.enclClass.sym);
                }
            }
            // Attribute method body.
            attribStat(tree.body, localEnv);
        }
        localEnv.info.scope.leave();
        result = tree.type = m.type;
        chk.validateAnnotations(tree.mods.annotations, m);
    } finally {
        chk.setLint(prevLint);
        chk.setMethod(prevMethod);
    }
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Lint(com.sun.tools.javac.code.Lint) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol)

Aggregations

JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)44 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)32 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)31 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)28 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)23 Name (com.sun.tools.javac.util.Name)22 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)17 ListBuffer (com.sun.tools.javac.util.ListBuffer)17 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)16 JavacTreeMaker (lombok.javac.JavacTreeMaker)13 JCTree (com.sun.tools.javac.tree.JCTree)11 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)10 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)6 JavacNode (lombok.javac.JavacNode)6 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)5 HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)4 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)4 Type (com.redhat.ceylon.model.typechecker.model.Type)4 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)4 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)3