Search in sources :

Example 1 with Weak

use of com.google.j2objc.annotations.Weak in project j2objc by google.

the class Rewriter method endVisit.

/**
   * Verify, update property attributes. Accessor methods are not checked since a
   * property annotation may apply to separate variables in a field declaration, so
   * each variable needs to be checked separately during generation.
   */
@Override
public void endVisit(PropertyAnnotation node) {
    FieldDeclaration field = (FieldDeclaration) node.getParent();
    TypeMirror fieldType = field.getTypeMirror();
    VariableDeclarationFragment firstVarNode = field.getFragment(0);
    if (typeUtil.isString(fieldType)) {
        node.addAttribute("copy");
    } else if (ElementUtil.hasAnnotation(firstVarNode.getVariableElement(), Weak.class)) {
        if (node.hasAttribute("strong")) {
            ErrorUtil.error(field, "Weak field annotation conflicts with strong Property attribute");
            return;
        }
        node.addAttribute("weak");
    }
    node.removeAttribute("readwrite");
    node.removeAttribute("strong");
    node.removeAttribute("atomic");
    // Make sure attempt isn't made to specify an accessor method for fields with multiple
    // fragments, since each variable needs unique accessors.
    String getter = node.getGetter();
    String setter = node.getSetter();
    if (field.getFragments().size() > 1) {
        if (getter != null) {
            ErrorUtil.error(field, "@Property getter declared for multiple fields");
            return;
        }
        if (setter != null) {
            ErrorUtil.error(field, "@Property setter declared for multiple fields");
            return;
        }
    } else {
        // Check that specified accessors exist.
        TypeElement enclosingType = TreeUtil.getEnclosingTypeElement(node);
        if (getter != null) {
            if (ElementUtil.findMethod(enclosingType, getter) == null) {
                ErrorUtil.error(field, "Non-existent getter specified: " + getter);
            }
        }
        if (setter != null) {
            if (ElementUtil.findMethod(enclosingType, setter, TypeUtil.getQualifiedName(fieldType)) == null) {
                ErrorUtil.error(field, "Non-existent setter specified: " + setter);
            }
        }
    }
}
Also used : Weak(com.google.j2objc.annotations.Weak) TypeMirror(javax.lang.model.type.TypeMirror) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) TypeElement(javax.lang.model.element.TypeElement) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Aggregations

FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)1 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)1 Weak (com.google.j2objc.annotations.Weak)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1