Search in sources :

Example 16 with JCAnnotation

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

the class CeylonTransformer method transformAttribute.

public List<JCTree> transformAttribute(TypedDeclaration declarationModel, String attrName, String attrClassName, final Tree.Declaration annotated, final Tree.Block block, final Tree.SpecifierOrInitializerExpression expression, final Tree.TypedDeclaration decl, final Tree.AttributeSetterDefinition setterDecl) {
    // For everything else generate a getter/setter method
    AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.wrapped(this, attrClassName, null, attrName, declarationModel, declarationModel.isToplevel()).is(Flags.PUBLIC, declarationModel.isShared());
    final JCExpression initialValue;
    final HasErrorException expressionError;
    if (expression != null) {
        expressionError = errors().getFirstExpressionErrorAndMarkBrokenness(expression.getExpression());
        if (expressionError != null) {
            initialValue = make().Erroneous();
        } else {
            initialValue = transformValueInit(declarationModel, attrName, expression);
        }
    } else {
        expressionError = null;
        initialValue = transformValueInit(declarationModel, attrName, expression);
    }
    // For captured local variable Values, use a VariableBox
    if (Decl.isBoxedVariable(declarationModel)) {
        if (expressionError != null) {
            return List.<JCTree>of(this.makeThrowUnresolvedCompilationError(expressionError));
        } else {
            return List.<JCTree>of(makeVariableBoxDecl(initialValue, declarationModel));
        }
    }
    // For late-bound getters we only generate a declaration
    if (block == null && expression == null && !Decl.isToplevel(declarationModel)) {
        JCExpression typeExpr = makeJavaType(getGetterInterfaceType(declarationModel));
        JCTree.JCVariableDecl var = makeVar(attrClassName, typeExpr, null);
        return List.<JCTree>of(var);
    }
    // Set the local declarations annotation
    if (decl != null) {
        List<JCAnnotation> scopeAnnotations;
        if (Decl.isToplevel(declarationModel) && setterDecl != null) {
            scopeAnnotations = makeAtLocalDeclarations(decl, setterDecl);
        } else {
            scopeAnnotations = makeAtLocalDeclarations(decl);
        }
        builder.classAnnotations(scopeAnnotations);
    } else if (block != null) {
        List<JCAnnotation> scopeAnnotations = makeAtLocalDeclarations(block);
        builder.classAnnotations(scopeAnnotations);
    }
    // Remember the setter class if we generate a getter
    if (Decl.isGetter(declarationModel) && declarationModel.isVariable() && Decl.isLocal(declarationModel)) {
        // we must have a setter class
        Setter setter = ((Value) declarationModel).getSetter();
        if (setter != null) {
            String setterClassName = Naming.getAttrClassName(setter, 0);
            JCExpression setterClassNameExpr = naming.makeUnquotedIdent(setterClassName);
            builder.setterClass(makeSelect(setterClassNameExpr, "class"));
        }
    }
    if (declarationModel instanceof Setter || (declarationModel instanceof FunctionOrValue && ((FunctionOrValue) declarationModel).isParameter())) {
        // For local setters
        JCBlock setterBlock = makeSetterBlock(declarationModel, block, expression);
        builder.setterBlock(setterBlock);
        builder.skipGetter();
        if (Decl.isLocal(decl)) {
            // we need to find back the Setter model for local setters, because
            // in transformAttribute(Tree.TypedDeclaration decl, Tree.AttributeSetterDefinition setterDecl)
            // we turn the declaration model from the Setter to its single parameter
            Setter setter = (Setter) declarationModel.getContainer();
            String getterClassName = Naming.getAttrClassName(setter.getGetter(), 0);
            JCExpression getterClassNameExpr = naming.makeUnquotedIdent(getterClassName);
            builder.isSetter(makeSelect(getterClassNameExpr, "class"));
        }
    } else {
        if (Decl.isValue(declarationModel)) {
            // For local and toplevel value attributes
            if (!declarationModel.isVariable() && !declarationModel.isLate()) {
                builder.immutable();
            }
        } else {
            // For local and toplevel getters
            boolean prevSyntheticClassBody;
            if (Decl.isLocal(declarationModel)) {
                prevSyntheticClassBody = expressionGen().withinSyntheticClassBody(true);
            } else {
                prevSyntheticClassBody = expressionGen().isWithinSyntheticClassBody();
            }
            JCBlock getterBlock = makeGetterBlock(declarationModel, block, expression);
            prevSyntheticClassBody = expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
            builder.getterBlock(getterBlock);
            if (Decl.isLocal(declarationModel)) {
                // For local getters
                builder.immutable();
            } else {
                // For toplevel getters
                if (setterDecl != null) {
                    JCBlock setterBlock = makeSetterBlock(setterDecl.getDeclarationModel(), setterDecl.getBlock(), setterDecl.getSpecifierExpression());
                    builder.setterBlock(setterBlock);
                    // builder.userAnnotationsSetter(expressionGen().transformAnnotations(true, OutputElement.METHOD, setterDecl));
                    builder.userAnnotationsSetter(expressionGen().transformAnnotations(OutputElement.SETTER, setterDecl));
                } else {
                    builder.immutable();
                }
            }
        }
    }
    if (annotated != null) {
        builder.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, annotated));
    }
    if (Decl.isLocal(declarationModel)) {
        if (expressionError != null) {
            return List.<JCTree>of(this.makeThrowUnresolvedCompilationError(expressionError));
        }
        builder.classAnnotations(makeAtLocalDeclaration(declarationModel.getQualifier(), false));
        if (initialValue != null)
            builder.valueConstructor();
        JCExpression typeExpr;
        if (declarationModel instanceof Setter || (declarationModel instanceof FunctionOrValue && ((FunctionOrValue) declarationModel).isParameter())) {
            typeExpr = makeQuotedIdent(attrClassName);
        } else {
            typeExpr = makeJavaType(getGetterInterfaceType(declarationModel));
        }
        return builder.build().append(makeLocalIdentityInstance(typeExpr, attrClassName, attrClassName, declarationModel.isShared(), initialValue));
    } else {
        if (expressionError != null) {
            builder.initialValueError(expressionError);
        } else if (initialValue != null) {
            builder.initialValue(initialValue);
        }
        builder.is(Flags.STATIC, true);
        return builder.build();
    }
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCTree(com.sun.tools.javac.tree.JCTree) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Setter(com.redhat.ceylon.model.typechecker.model.Setter) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) List(com.sun.tools.javac.util.List) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Example 17 with JCAnnotation

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

the class ClassTransformer method addAtContainer.

private void addAtContainer(ClassDefinitionBuilder classBuilder, TypeDeclaration model) {
    Scope scope = Decl.getNonSkippedContainer((Scope) model);
    Scope declarationScope = Decl.getFirstDeclarationContainer((Scope) model);
    boolean inlineObjectInToplevelAttr = Decl.isTopLevelObjectExpressionType(model);
    if (scope == null || (scope instanceof Package && !inlineObjectInToplevelAttr) && scope == declarationScope)
        return;
    if (scope instanceof ClassOrInterface && scope == declarationScope && !inlineObjectInToplevelAttr && // we do not check for types inside initialiser section which are private and not captured because we treat them as members
    !(model instanceof Interface && Decl.hasLocalNotInitializerAncestor(model))) {
        ClassOrInterface container = (ClassOrInterface) scope;
        List<JCAnnotation> atContainer = makeAtContainer(container.getType());
        classBuilder.annotations(atContainer);
    } else {
        if (model instanceof Interface)
            classBuilder.annotations(makeLocalContainerPath((Interface) model));
        Declaration declarationContainer = getDeclarationContainer(model);
        classBuilder.annotations(makeAtLocalDeclaration(model.getQualifier(), declarationContainer == null));
    }
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Package(com.redhat.ceylon.model.typechecker.model.Package) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(com.redhat.ceylon.model.loader.model.LazyInterface) Interface(com.redhat.ceylon.model.typechecker.model.Interface) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 18 with JCAnnotation

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

the class ClassTransformer method addAtMembers.

private void addAtMembers(ClassDefinitionBuilder classBuilder, ClassOrInterface model, Tree.ClassOrInterface def) {
    List<JCExpression> members = List.nil();
    for (Declaration member : model.getMembers()) {
        if (member instanceof ClassOrInterface == false && member instanceof TypeAlias == false) {
            continue;
        }
        TypeDeclaration innerType = (TypeDeclaration) member;
        Tree.Declaration innerTypeTree = findInnerType(def, innerType.getName());
        if (innerTypeTree != null) {
            TransformationPlan plan = errors().hasDeclarationAndMarkBrokenness(innerTypeTree);
            if (plan instanceof Drop) {
                continue;
            }
        }
        if (innerType.isAlias() && innerTypeTree != null && Decl.isAncestorLocal(innerTypeTree))
            // for the same reason we do not generate aliases in transform(ClassOrInterface def) let's not list them
            continue;
        JCAnnotation atMember;
        // interfaces are moved to toplevel so they can lose visibility of member types if they are local
        if (Decl.isLocal(model) && model instanceof Interface)
            atMember = makeAtMember(innerType.getName());
        else
            atMember = makeAtMember(innerType.getType());
        members = members.prepend(atMember);
    }
    classBuilder.annotations(makeAtMembers(members));
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) TransformationPlan(com.redhat.ceylon.compiler.java.codegen.recovery.TransformationPlan) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(com.redhat.ceylon.model.loader.model.LazyInterface) Interface(com.redhat.ceylon.model.typechecker.model.Interface) Drop(com.redhat.ceylon.compiler.java.codegen.recovery.Drop)

Example 19 with JCAnnotation

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

the class Template method pretty.

protected static Pretty pretty(Context context, final Writer writer) {
    final JCCompilationUnit unit = context.get(JCCompilationUnit.class);
    try {
        final String unitContents = unit.getSourceFile().getCharContent(false).toString();
        return new Pretty(writer, true) {

            {
                // Work-around for b/22196513
                width = 0;
            }

            @Override
            public void visitAnnotation(JCAnnotation anno) {
                if (anno.getArguments().isEmpty()) {
                    try {
                        print("@");
                        printExpr(anno.annotationType);
                    } catch (IOException e) {
                        // the supertype swallows exceptions too
                        throw new RuntimeException(e);
                    }
                } else {
                    super.visitAnnotation(anno);
                }
            }

            @Override
            public void printExpr(JCTree tree, int prec) throws IOException {
                EndPosTable endPositions = unit.endPositions;
                /*
           * Modifiers, and specifically flags like final, appear to just need weird special
           * handling.
           *
           * Note: we can't use {@code TreeInfo.getEndPos()} or {@code JCTree.getEndPosition()}
           * here, because they will return the end position of an enclosing AST node for trees
           * whose real end positions aren't stored.
           */
                int endPos = endPositions.getEndPos(tree);
                boolean hasRealEndPosition = endPos != Position.NOPOS;
                if (tree.getKind() != Kind.MODIFIERS && hasRealEndPosition) {
                    writer.append(unitContents.substring(tree.getStartPosition(), endPos));
                } else {
                    super.printExpr(tree, prec);
                }
            }

            @Override
            public void visitApply(JCMethodInvocation tree) {
                JCExpression select = tree.getMethodSelect();
                if (select != null && select.toString().equals("Refaster.emitCommentBefore")) {
                    String commentLiteral = (String) ((JCLiteral) tree.getArguments().get(0)).getValue();
                    JCExpression expr = tree.getArguments().get(1);
                    try {
                        print("/* " + commentLiteral + " */ ");
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    expr.accept(this);
                } else {
                    super.visitApply(tree);
                }
            }

            @Override
            public void printStat(JCTree tree) throws IOException {
                if (tree instanceof JCExpressionStatement && ((JCExpressionStatement) tree).getExpression() instanceof JCMethodInvocation) {
                    JCMethodInvocation invocation = (JCMethodInvocation) ((JCExpressionStatement) tree).getExpression();
                    JCExpression select = invocation.getMethodSelect();
                    if (select != null && select.toString().equals("Refaster.emitComment")) {
                        String commentLiteral = (String) ((JCLiteral) invocation.getArguments().get(0)).getValue();
                        print("// " + commentLiteral);
                        return;
                    }
                }
                super.printStat(tree);
            }

            @Override
            public void visitTry(JCTry tree) {
                if (tree.getResources().isEmpty()) {
                    super.visitTry(tree);
                    return;
                }
                try {
                    print("try (");
                    boolean first = true;
                    for (JCTree resource : tree.getResources()) {
                        if (!first) {
                            print(";");
                            println();
                        }
                        printExpr(resource);
                        first = false;
                    }
                    print(")");
                    printStat(tree.body);
                    for (JCCatch catchStmt : tree.getCatches()) {
                        printStat(catchStmt);
                    }
                    if (tree.getFinallyBlock() != null) {
                        print(" finally ");
                        printStat(tree.getFinallyBlock());
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCTree(com.sun.tools.javac.tree.JCTree) IOException(java.io.IOException) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) Pretty(com.sun.tools.javac.tree.Pretty) JCTry(com.sun.tools.javac.tree.JCTree.JCTry) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) EndPosTable(com.sun.tools.javac.tree.EndPosTable)

Example 20 with JCAnnotation

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

the class PrettyPrinter method printAnnotations.

private void printAnnotations(List<JCAnnotation> annotations, boolean newlines) {
    for (JCAnnotation ann : annotations) {
        print(ann);
        if (newlines) {
            println();
            align();
        } else
            print(" ");
    }
}
Also used : JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)53 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)32 ListBuffer (com.sun.tools.javac.util.ListBuffer)17 Name (com.sun.tools.javac.util.Name)16 JCTree (com.sun.tools.javac.tree.JCTree)15 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)15 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)13 JavacNode (lombok.javac.JavacNode)13 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)12 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)11 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)10 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)8 JavacTreeMaker (lombok.javac.JavacTreeMaker)8 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)6 Type (com.redhat.ceylon.model.typechecker.model.Type)5 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)5 JCNewArray (com.sun.tools.javac.tree.JCTree.JCNewArray)5 ArrayList (java.util.ArrayList)5 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)4 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)4