Search in sources :

Example 11 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

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.getName() != null) {
            // TODO: is this really supposed to just leave : and - untouched?!
            quotedName = imported.getName().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(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCModifiers) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) Name(org.eclipse.ceylon.langtools.tools.javac.util.Name)

Example 12 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

the class CeylonTransformer method transformPackageInfo.

public List<JCCompilationUnit> transformPackageInfo(CeylonCompilationUnit ccu) {
    final CeylonFileObject fo = (CeylonFileObject) ((CeylonPhasedUnit) ccu.phasedUnit).getFileObject();
    ListBuffer<JCCompilationUnit> packageInfos = new ListBuffer<JCCompilationUnit>();
    for (Tree.PackageDescriptor pack : ccu.ceylonTree.getPackageDescriptors()) {
        List<JCAnnotation> packageAnnotations = expressionGen().transformAnnotations(OutputElement.PACKAGE, pack);
        if (packageAnnotations.isEmpty()) {
            continue;
        }
        JCCompilationUnit packageInfo = make().TopLevel(packageAnnotations, naming.makeQuotedFQIdent(pack.getScope().getQualifiedNameString()), List.<JCTree>nil());
        // Enter.visitTopLevel(JCCompilationUnit) uses the tree.sourceFile
        // to decide whether it's seeing a package-info.java
        // So set up a fake one...
        packageInfo.sourcefile = new JavaFileObject() {

            @Override
            public boolean isNameCompatible(String simpleName, Kind kind) {
                return "package-info".equals(simpleName) && JavaFileObject.Kind.SOURCE == kind;
            }

            @Override
            public URI toUri() {
                return fo.toUri();
            }

            @Override
            public String getName() {
                return fo.getName();
            }

            @Override
            public InputStream openInputStream() throws IOException {
                return fo.openInputStream();
            }

            @Override
            public OutputStream openOutputStream() throws IOException {
                return fo.openOutputStream();
            }

            @Override
            public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
                return fo.openReader(ignoreEncodingErrors);
            }

            @Override
            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
                return fo.getCharContent(ignoreEncodingErrors);
            }

            @Override
            public Writer openWriter() throws IOException {
                return fo.openWriter();
            }

            @Override
            public long getLastModified() {
                return fo.getLastModified();
            }

            @Override
            public boolean delete() {
                return fo.delete();
            }

            @Override
            public Kind getKind() {
                return fo.getKind();
            }

            @Override
            public NestingKind getNestingKind() {
                return fo.getNestingKind();
            }

            @Override
            public Modifier getAccessLevel() {
                return fo.getAccessLevel();
            }
        };
        packageInfos.add(packageInfo);
    }
    return packageInfos.toList();
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) InputStream(java.io.InputStream) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) OutputStream(java.io.OutputStream) Reader(java.io.Reader) IOException(java.io.IOException) URI(java.net.URI) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) NestingKind(org.eclipse.ceylon.javax.lang.model.element.NestingKind) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) PackageDescriptor(org.eclipse.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor) Modifier(org.eclipse.ceylon.javax.lang.model.element.Modifier) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) Writer(java.io.Writer) NestingKind(org.eclipse.ceylon.javax.lang.model.element.NestingKind)

Example 13 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

the class NamedArgumentInvocation method bindAttributeArgument.

private void bindAttributeArgument(Tree.AttributeArgument attrArg, Parameter declaredParam, Naming.SyntheticName argName) {
    ListBuffer<JCStatement> statements;
    final Value model = attrArg.getDeclarationModel();
    final String name = model.getName();
    String className = Naming.getAttrClassName(model, 0);
    final List<JCTree> attrClass = gen.gen().transformAttribute(model, name, className, null, attrArg.getBlock(), attrArg.getSpecifierExpression(), null, null);
    TypedReference typedRef = gen.getTypedReference(model);
    TypedReference nonWideningTypedRef = gen.nonWideningTypeDecl(typedRef);
    Type nonWideningType = gen.nonWideningType(typedRef, nonWideningTypedRef);
    Type type = parameterType(declaredParam, model.getType(), 0);
    final BoxingStrategy boxType = getNamedParameterBoxingStrategy(declaredParam);
    JCExpression initValue = gen.make().Apply(null, gen.makeSelect(gen.makeUnquotedIdent(className), Naming.getGetterName(model)), List.<JCExpression>nil());
    initValue = gen.expressionGen().applyErasureAndBoxing(initValue, nonWideningType, !CodegenUtil.isUnBoxed(nonWideningTypedRef.getDeclaration()), boxType, type);
    JCTree.JCVariableDecl var = gen.make().VarDef(gen.make().Modifiers(FINAL, List.<JCAnnotation>nil()), argName.asName(), gen.makeJavaType(type, boxType == BoxingStrategy.BOXED ? JT_NO_PRIMITIVES : 0), initValue);
    statements = toStmts(attrArg, attrClass).append(var);
    bind(declaredParam, argName, gen.makeJavaType(type, boxType == BoxingStrategy.BOXED ? JT_NO_PRIMITIVES : 0), statements.toList());
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) Value(org.eclipse.ceylon.model.typechecker.model.Value) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) BoxingStrategy(org.eclipse.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)

Example 14 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

the class ParameterDefinitionBuilder method build.

public JCVariableDecl build() {
    if (built) {
        throw new BugException("already built");
    }
    built = true;
    ListBuffer<JCAnnotation> annots = new ListBuffer<JCAnnotation>();
    if (Annotations.includeModel(annotationFlags)) {
        annots.appendList(gen.makeAtName(name));
        if (functionalParameterName != null) {
            annots.appendList(gen.makeAtFunctionalParameter(functionalParameterName));
        }
        if (sequenced) {
            annots.appendList(gen.makeAtSequenced());
        }
        if (defaulted) {
            annots.appendList(gen.makeAtDefaulted());
        }
        if (type != null) {
            annots.appendList(type.getTypeAnnotations());
        }
    }
    if (Annotations.includeUser(annotationFlags) && userAnnotations != null) {
        annots.appendList(userAnnotations.toList());
    }
    if (Annotations.includeModel(annotationFlags)) {
        if (modelAnnotations != null) {
            annots.appendList(modelAnnotations.toList());
        }
    }
    if (Annotations.includeIgnore(annotationFlags)) {
        annots = annots.appendList(gen.makeAtIgnore());
    }
    Name name = gen.names().fromString(Naming.quoteParameterName(getJavaParameterName()));
    return gen.make().VarDef(gen.make().Modifiers(modifiers | Flags.PARAMETER, annots.toList()), name, type.getTypeExpression(), null);
}
Also used : ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) Name(org.eclipse.ceylon.langtools.tools.javac.util.Name)

Example 15 with JCAnnotation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.

the class ExpressionTransformer method transformAnnotation.

void transformAnnotation(Tree.Annotation invocation, Map<Class, ListBuffer<JCAnnotation>> annotationSet) {
    at(invocation);
    HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(invocation);
    if (error == null) {
        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 : HasErrorException(org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

JCAnnotation (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)23 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)14 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)12 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)12 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)7 Type (org.eclipse.ceylon.model.typechecker.model.Type)6 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)6 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)5 HasErrorException (org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException)4 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)4 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)4 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)4 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)4 MethodDeclaration (org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)3 JCPrimitiveTypeTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree)3 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)3 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)3 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)3 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2