Search in sources :

Example 1 with Annotation

use of com.google.devtools.j2objc.ast.Annotation in project j2objc by google.

the class ElementUtilTest method testIsRuntimeAnnotation.

public void testIsRuntimeAnnotation() throws IOException {
    // SuppressWarnings is a source-level annotation.
    CompilationUnit unit = translateType("Example", "@SuppressWarnings(\"test\") class Example {}");
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    Annotation annotation = decl.getAnnotations().get(0);
    assertFalse(ElementUtil.isRuntimeAnnotation(annotation.getAnnotationMirror()));
    // Deprecated is a runtime annotation..
    unit = translateType("Example", "@Deprecated class Example {}");
    decl = unit.getTypes().get(0);
    annotation = decl.getAnnotations().get(0);
    assertTrue(ElementUtil.isRuntimeAnnotation(annotation.getAnnotationMirror()));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) Annotation(com.google.devtools.j2objc.ast.Annotation) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 2 with Annotation

use of com.google.devtools.j2objc.ast.Annotation in project j2objc by google.

the class TreeConverter method convertAnnotation.

private TreeNode convertAnnotation(JCTree.JCAnnotation node) {
    List<JCTree.JCExpression> args = node.getArguments();
    String annotationName = node.getAnnotationType().toString();
    boolean isPropertyAnnotation = annotationName.equals(Property.class.getSimpleName()) || annotationName.equals(Property.class.getName());
    Annotation newNode;
    if (isPropertyAnnotation) {
        newNode = new PropertyAnnotation().setAnnotationMirror(node.attribute);
        if (!args.isEmpty()) {
            for (String attr : ElementUtil.parsePropertyAttribute(node.attribute)) {
                ((PropertyAnnotation) newNode).addAttribute(attr);
            }
        }
    } else if (args.isEmpty()) {
        newNode = new MarkerAnnotation().setAnnotationMirror(node.attribute);
    } else if (args.size() == 1) {
        JCTree.JCAssign assign = (JCTree.JCAssign) args.get(0);
        newNode = new SingleMemberAnnotation().setValue((Expression) convert(assign.rhs));
    } else {
        NormalAnnotation normalAnn = new NormalAnnotation();
        for (JCTree.JCExpression obj : node.getArguments()) {
            JCTree.JCAssign assign = (JCTree.JCAssign) obj;
            Symbol sym = ((JCTree.JCIdent) assign.lhs).sym;
            MemberValuePair memberPair = new MemberValuePair().setName(convertSimpleName(sym, sym.asType(), getPosition(assign.lhs))).setValue((Expression) convert(assign.rhs));
            normalAnn.addValue(memberPair);
        }
        newNode = normalAnn;
    }
    return newNode.setAnnotationMirror(node.attribute).setTypeName((Name) convert(node.getAnnotationType()));
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) SingleMemberAnnotation(com.google.devtools.j2objc.ast.SingleMemberAnnotation) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) JCTree(com.sun.tools.javac.tree.JCTree) NormalAnnotation(com.google.devtools.j2objc.ast.NormalAnnotation) PropertyAnnotation(com.google.devtools.j2objc.ast.PropertyAnnotation) Annotation(com.google.devtools.j2objc.ast.Annotation) SingleMemberAnnotation(com.google.devtools.j2objc.ast.SingleMemberAnnotation) MarkerAnnotation(com.google.devtools.j2objc.ast.MarkerAnnotation) PropertyAnnotation(com.google.devtools.j2objc.ast.PropertyAnnotation) MarkerAnnotation(com.google.devtools.j2objc.ast.MarkerAnnotation) MemberValuePair(com.google.devtools.j2objc.ast.MemberValuePair) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) NormalAnnotation(com.google.devtools.j2objc.ast.NormalAnnotation)

Example 3 with Annotation

use of com.google.devtools.j2objc.ast.Annotation in project j2objc by google.

the class TypeDeclarationGenerator method hasDeprecated.

private boolean hasDeprecated(List<Annotation> annotations) {
    for (Annotation annotation : annotations) {
        Name annotationTypeName = annotation.getTypeName();
        String expectedTypeName = annotationTypeName.isQualifiedName() ? "java.lang.Deprecated" : "Deprecated";
        if (expectedTypeName.equals(annotationTypeName.getFullyQualifiedName())) {
            return true;
        }
    }
    return false;
}
Also used : PropertyAnnotation(com.google.devtools.j2objc.ast.PropertyAnnotation) Annotation(com.google.devtools.j2objc.ast.Annotation) Name(com.google.devtools.j2objc.ast.Name)

Example 4 with Annotation

use of com.google.devtools.j2objc.ast.Annotation in project j2objc by google.

the class PackageInfoRewriter method run.

private void run() {
    PackageDeclaration pkg = unit.getPackage();
    List<Annotation> annotations = pkg.getAnnotations();
    List<Annotation> runtimeAnnotations = TreeUtil.getRuntimeAnnotationsList(annotations);
    String prefix = getPackagePrefix(pkg);
    boolean needsReflection = translationUtil.needsReflection(pkg);
    // Remove compile time annotations.
    annotations.retainAll(runtimeAnnotations);
    if ((annotations.isEmpty() && prefix == null) || !needsReflection) {
        return;
    }
    TypeElement typeElement = GeneratedTypeElement.newPackageInfoClass(pkg.getPackageElement(), typeUtil);
    TypeDeclaration typeDecl = new TypeDeclaration(typeElement);
    TreeUtil.moveList(pkg.getAnnotations(), typeDecl.getAnnotations());
    if (prefix != null) {
        typeDecl.addBodyDeclaration(createPrefixMethod(prefix, typeElement));
    }
    unit.addType(0, typeDecl);
}
Also used : GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) PackageDeclaration(com.google.devtools.j2objc.ast.PackageDeclaration) Annotation(com.google.devtools.j2objc.ast.Annotation)

Example 5 with Annotation

use of com.google.devtools.j2objc.ast.Annotation in project j2objc by google.

the class InputFilePreprocessor method extractPackagePrefix.

private void extractPackagePrefix(InputFile file, CompilationUnit unit) {
    // We should only reach here if it's a package-info.java file.
    assert file.getUnitName().endsWith("package-info.java");
    List<Annotation> annotations = (List<Annotation>) unit.getPackage().getAnnotations();
    for (Annotation annotation : annotations) {
        // getFullyQualifiedName() might not actually return a fully qualified name.
        String name = annotation.getTypeName().getFullyQualifiedName();
        if (name.endsWith("ObjectiveCName")) {
            // Per Eclipse docs, binding resolution can be a resource hog.
            if (TypeUtil.getQualifiedName(annotation.getAnnotationMirror().getAnnotationType()).equals(ObjectiveCName.class.getCanonicalName())) {
                String key = unit.getPackage().getName().getFullyQualifiedName();
                String val = (String) ((SingleMemberAnnotation) annotation).getValue().getConstantValue();
                options.getPackagePrefixes().addPrefix(key, val);
            }
        }
    }
}
Also used : ObjectiveCName(com.google.j2objc.annotations.ObjectiveCName) SingleMemberAnnotation(com.google.devtools.j2objc.ast.SingleMemberAnnotation) List(java.util.List) Annotation(com.google.devtools.j2objc.ast.Annotation) SingleMemberAnnotation(com.google.devtools.j2objc.ast.SingleMemberAnnotation)

Aggregations

Annotation (com.google.devtools.j2objc.ast.Annotation)6 PropertyAnnotation (com.google.devtools.j2objc.ast.PropertyAnnotation)3 SingleMemberAnnotation (com.google.devtools.j2objc.ast.SingleMemberAnnotation)3 MarkerAnnotation (com.google.devtools.j2objc.ast.MarkerAnnotation)2 NormalAnnotation (com.google.devtools.j2objc.ast.NormalAnnotation)2 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)1 AnnotatableType (com.google.devtools.j2objc.ast.AnnotatableType)1 ArrayType (com.google.devtools.j2objc.ast.ArrayType)1 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)1 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)1 Expression (com.google.devtools.j2objc.ast.Expression)1 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)1 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)1 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)1 IntersectionType (com.google.devtools.j2objc.ast.IntersectionType)1 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)1 MemberValuePair (com.google.devtools.j2objc.ast.MemberValuePair)1 Name (com.google.devtools.j2objc.ast.Name)1 NameQualifiedType (com.google.devtools.j2objc.ast.NameQualifiedType)1