Search in sources :

Example 1 with PropertyAnnotation

use of com.google.devtools.j2objc.ast.PropertyAnnotation 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 2 with PropertyAnnotation

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

the class TypeDeclarationGenerator method printProperties.

protected void printProperties() {
    Iterable<VariableDeclarationFragment> fields = getAllFields();
    for (VariableDeclarationFragment fragment : fields) {
        FieldDeclaration fieldDecl = (FieldDeclaration) fragment.getParent();
        VariableElement varElement = fragment.getVariableElement();
        PropertyAnnotation property = (PropertyAnnotation) TreeUtil.getAnnotation(Property.class, fieldDecl.getAnnotations());
        if (property != null) {
            print("@property ");
            TypeMirror varType = varElement.asType();
            String propertyName = nameTable.getVariableBaseName(varElement);
            // Add default getter/setter here, as each fragment needs its own attributes
            // to support its unique accessors.
            Set<String> attributes = property.getPropertyAttributes();
            TypeElement declaringClass = ElementUtil.getDeclaringClass(varElement);
            if (property.getGetter() == null) {
                ExecutableElement getter = findGetterMethod(propertyName, varType, declaringClass);
                if (getter != null) {
                    attributes.add("getter=" + NameTable.getMethodName(getter));
                    if (!ElementUtil.isSynchronized(getter)) {
                        attributes.add("nonatomic");
                    }
                }
            }
            if (property.getSetter() == null) {
                ExecutableElement setter = findSetterMethod(propertyName, declaringClass);
                if (setter != null) {
                    attributes.add("setter=" + NameTable.getMethodName(setter));
                    if (!ElementUtil.isSynchronized(setter)) {
                        attributes.add("nonatomic");
                    }
                }
            }
            if (ElementUtil.isStatic(varElement)) {
                attributes.add("class");
            } else if (attributes.contains("class")) {
                ErrorUtil.error(fragment, "Only static fields can be translated to class properties");
            }
            if (attributes.contains("class") && !options.staticAccessorMethods()) {
                // Class property accessors must be present, as they are not synthesized by runtime.
                ErrorUtil.error(fragment, "Class properties require either a --swift-friendly or" + " --static-accessor-methods flag");
            }
            if (options.nullability()) {
                if (ElementUtil.hasNullableAnnotation(varElement)) {
                    attributes.add("nullable");
                } else if (ElementUtil.isNonnull(varElement, parametersNonnullByDefault)) {
                    attributes.add("nonnull");
                } else if (!attributes.contains("null_unspecified")) {
                    attributes.add("null_resettable");
                }
            }
            if (!attributes.isEmpty()) {
                print('(');
                print(PropertyAnnotation.toAttributeString(attributes));
                print(") ");
            }
            String objcType = nameTable.getObjCType(varType);
            print(objcType);
            if (!objcType.endsWith("*")) {
                print(' ');
            }
            println(propertyName + ";");
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Property(com.google.j2objc.annotations.Property) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration) PropertyAnnotation(com.google.devtools.j2objc.ast.PropertyAnnotation)

Aggregations

PropertyAnnotation (com.google.devtools.j2objc.ast.PropertyAnnotation)2 Annotation (com.google.devtools.j2objc.ast.Annotation)1 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)1 Expression (com.google.devtools.j2objc.ast.Expression)1 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)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 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)1 MarkerAnnotation (com.google.devtools.j2objc.ast.MarkerAnnotation)1 MemberValuePair (com.google.devtools.j2objc.ast.MemberValuePair)1 NormalAnnotation (com.google.devtools.j2objc.ast.NormalAnnotation)1 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)1 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)1 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)1 SingleMemberAnnotation (com.google.devtools.j2objc.ast.SingleMemberAnnotation)1 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)1 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)1 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)1