Search in sources :

Example 41 with JCAnnotation

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

the class Attr method attribAnnotationTypes.

/**
 * Attribute the type references in a list of annotations.
 */
void attribAnnotationTypes(List<JCAnnotation> annotations, Env<AttrContext> env) {
    for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
        JCAnnotation a = al.head;
        attribType(a.annotationType, env);
    }
}
Also used : JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 42 with JCAnnotation

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

the class ClassTransformer method transformMethod.

private List<MethodDefinitionBuilder> transformMethod(final Function methodModel, Tree.TypeParameterList typeParameterList, Tree.AnyMethod node, java.util.List<Tree.ParameterList> parameterLists, Tree.Declaration annotated, boolean transformMethod, boolean actual, boolean includeAnnotations, List<JCStatement> body, DaoBody daoTransformation, boolean defaultValuesBody) {
    ListBuffer<MethodDefinitionBuilder> lb = ListBuffer.<MethodDefinitionBuilder>lb();
    Declaration refinedDeclaration = methodModel.getRefinedDeclaration();
    final MethodDefinitionBuilder methodBuilder = MethodDefinitionBuilder.method(this, methodModel);
    // do the reified type param arguments
    if (gen().supportsReified(methodModel)) {
        methodBuilder.reifiedTypeParameters(methodModel.getTypeParameters());
    }
    if (methodModel.getParameterLists().size() > 1) {
        methodBuilder.mpl(methodModel.getParameterLists());
    }
    boolean hasOverloads = false;
    Tree.ParameterList parameterList = parameterLists.get(0);
    int flags = 0;
    if (rawParameters(methodModel)) {
        flags |= JT_RAW;
    }
    for (final Tree.Parameter parameter : parameterList.getParameters()) {
        Parameter parameterModel = parameter.getParameterModel();
        List<JCAnnotation> annotations = null;
        if (includeAnnotations) {
            Tree.TypedDeclaration typedDeclaration = Decl.getMemberDeclaration(annotated, parameter);
            // f = function(Integer param) => 2;
            if (typedDeclaration != null)
                annotations = expressionGen().transformAnnotations(OutputElement.PARAMETER, typedDeclaration);
        }
        methodBuilder.parameter(parameterModel, annotations, flags, WideningRules.CAN_WIDEN);
        if (Strategy.hasDefaultParameterValueMethod(parameterModel) || Strategy.hasDefaultParameterOverload(parameterModel)) {
            if (Decl.equal(refinedDeclaration, methodModel) || (!Decl.withinInterface(methodModel) && body != null) || Decl.withinInterface(methodModel) && daoTransformation instanceof DaoCompanion == false) {
                if (daoTransformation != null && (daoTransformation instanceof DaoCompanion == false || body != null)) {
                    DaoBody daoTrans = (body == null) ? daoAbstract : new DaoThis(node, parameterList);
                    MethodDefinitionBuilder overloadedMethod = new DefaultedArgumentMethod(daoTrans, MethodDefinitionBuilder.method(this, methodModel), methodModel).makeOverload(parameterList.getModel(), parameter.getParameterModel(), methodModel.getTypeParameters());
                    overloadedMethod.location(null);
                    lb.append(overloadedMethod);
                }
                if (Decl.equal(refinedDeclaration, methodModel) && Strategy.hasDefaultParameterValueMethod(parameterModel)) {
                    lb.append(makeParamDefaultValueMethod(defaultValuesBody, methodModel, parameterList, parameter));
                }
            }
            hasOverloads = true;
        }
    }
    // Determine if we need to generate a "canonical" method
    boolean createCanonical = hasOverloads && Decl.withinClassOrInterface(methodModel) && body != null;
    if (createCanonical) {
        // Creates the private "canonical" method containing the actual body
        MethodDefinitionBuilder canonicalMethod = new CanonicalMethod(daoTransformation, methodModel, body).makeOverload(parameterList.getModel(), null, methodModel.getTypeParameters());
        lb.append(canonicalMethod);
    }
    if (transformMethod) {
        methodBuilder.modifiers(transformMethodDeclFlags(methodModel));
        if (actual) {
            methodBuilder.isOverride(methodModel.isActual());
        }
        if (includeAnnotations) {
            methodBuilder.userAnnotations(expressionGen().transformAnnotations(OutputElement.METHOD, annotated));
            methodBuilder.modelAnnotations(methodModel.getAnnotations());
        } else {
            methodBuilder.ignoreModelAnnotations();
        }
        methodBuilder.resultType(methodModel, 0);
        copyTypeParameters(methodModel, methodBuilder);
        if (createCanonical) {
            // Creates method that redirects to the "canonical" method containing the actual body
            MethodDefinitionBuilder overloadedMethod = new CanonicalMethod(new DaoThis(node, parameterList), methodBuilder, methodModel).makeOverload(parameterList.getModel(), null, methodModel.getTypeParameters());
            lb.append(overloadedMethod);
        } else {
            if (body != null) {
                // Construct the outermost method using the body we've built so far
                methodBuilder.body(body);
            } else {
                methodBuilder.noBody();
            }
            lb.append(methodBuilder);
        }
    }
    return lb.toList();
}
Also used : JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) 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) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 43 with JCAnnotation

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

the class CeylonTransformer method transformModuleDescriptor.

/**
 * Creates a module class in the package, with the Module annotation required by the runtime.
 */
public List<JCTree> transformModuleDescriptor(Tree.ModuleDescriptor module) {
    at(null);
    ClassDefinitionBuilder builder = ClassDefinitionBuilder.klass(this, Naming.MODULE_DESCRIPTOR_CLASS_NAME, null, false);
    builder.modifiers(Flags.FINAL).annotations(makeAtModule(module));
    builder.getInitBuilder().modifiers(Flags.PRIVATE);
    builder.annotations(expressionGen().transformAnnotations(OutputElement.TYPE, module));
    for (Tree.ImportModule imported : module.getImportModuleList().getImportModules()) {
        if (!isForBackend(imported.getAnnotationList(), Backend.Java, imported.getUnit())) {
            continue;
        }
        String quotedName;
        if (imported.getImportPath() != null) {
            StringBuilder sb = new StringBuilder();
            for (Tree.Identifier part : imported.getImportPath().getIdentifiers()) {
                sb.append(part.getText()).append('$');
            }
            quotedName = sb.substring(0, sb.length() - 1);
        } else if (imported.getQuotedLiteral() != null) {
            quotedName = imported.getQuotedLiteral().getText();
            quotedName = quotedName.substring(1, quotedName.length() - 1);
            quotedName = quotedName.replace('.', '$');
        } else {
            throw new BugException(imported, "unhandled module import");
        }
        List<JCAnnotation> importAnnotations = expressionGen().transformAnnotations(OutputElement.FIELD, imported);
        JCModifiers mods = make().Modifiers(Flags.PUBLIC | Flags.STATIC | Flags.FINAL, importAnnotations);
        Name fieldName = names().fromString(quotedName);
        builder.defs(List.<JCTree>of(make().VarDef(mods, fieldName, make().Type(syms().stringType), makeNull())));
    }
    return builder.build();
}
Also used : JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) OptionName(com.sun.tools.javac.main.OptionName) Name(com.sun.tools.javac.util.Name)

Example 44 with JCAnnotation

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

the class ExpressionTransformer method transform.

private List<JCAnnotation> transform(Object useSite, OutputElement target, Tree.AnnotationList annotationList, EnumSet<OutputElement> outputs) {
    if (annotationList == null) {
        return List.nil();
    }
    if ((gen().disableAnnotations & CeylonTransformer.DISABLE_USER_ANNOS) != 0) {
        return List.nil();
    }
    LinkedHashMap<Class, ListBuffer<JCAnnotation>> annotationSet = new LinkedHashMap<>();
    if (annotationList != null) {
        if (annotationList.getAnonymousAnnotation() != null && isNaturalTarget((Function) typeFact().getLanguageModuleDeclaration("doc"), useSite, target)) {
            transformAnonymousAnnotation(annotationList.getAnonymousAnnotation(), annotationSet);
        }
        if (annotationList.getAnnotations() != null) {
            for (Tree.Annotation annotation : annotationList.getAnnotations()) {
                Function annoCtorDecl = ((Function) ((Tree.BaseMemberExpression) annotation.getPrimary()).getDeclaration());
                EnumSet<OutputElement> possibleTargets = AnnotationUtil.interopAnnotationTargeting(outputs, annotation, false);
                if ((isNaturalTarget(annoCtorDecl, useSite, target) && possibleTargets == null) || (possibleTargets != null && possibleTargets.equals(EnumSet.of(target)))) {
                    transformAnnotation(annotation, annotationSet);
                }
            }
        }
    }
    ListBuffer<JCAnnotation> result = ListBuffer.lb();
    for (Class annotationClass : annotationSet.keySet()) {
        ListBuffer<JCAnnotation> annotations = annotationSet.get(annotationClass);
        if (isSequencedAnnotation(annotationClass)) {
            JCAnnotation wrapperAnnotation = make().Annotation(makeJavaType(annotationClass.getType(), JT_ANNOTATIONS), List.<JCExpression>of(make().NewArray(null, null, (List) annotations.toList())));
            result.append(wrapperAnnotation);
        } else {
            if (annotations.size() > 1) {
                makeErroneous(annotationList, "compiler bug: multiple occurances of non-sequenced annotation class " + annotationClass.getQualifiedNameString());
            }
            result.appendList(annotations);
        }
    }
    // Special case: Generate a @java.lang.Deprecated() if Ceylon deprecated
    if (annotationList != null) {
        for (Tree.Annotation annotation : annotationList.getAnnotations()) {
            if (isNaturalTarget((Function) typeFact().getLanguageModuleDeclaration("deprecated"), useSite, target) && isDeprecatedAnnotation(annotation.getPrimary())) {
                result.append(make().Annotation(make().Type(syms().deprecatedType), List.<JCExpression>nil()));
            }
        }
    }
    return result.toList();
}
Also used : ListBuffer(com.sun.tools.javac.util.ListBuffer) LinkedHashMap(java.util.LinkedHashMap) Function(com.redhat.ceylon.model.typechecker.model.Function) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) OutputElement(com.redhat.ceylon.model.loader.model.OutputElement) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 45 with JCAnnotation

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

the class ExpressionTransformer method transformAnnotation.

void transformAnnotation(Tree.Annotation invocation, Map<Class, ListBuffer<JCAnnotation>> annotationSet) {
    at(invocation);
    try {
        JCAnnotation annotation = AnnotationInvocationVisitor.transformConstructor(this, invocation);
        if (annotation != null) {
            Class annotationClass = AnnotationInvocationVisitor.annoClass(invocation);
            putAnnotation(annotationSet, annotation, annotationClass);
        }
    } catch (BugException e) {
        e.addError(invocation);
    }
}
Also used : Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) 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