Search in sources :

Example 11 with VariableDeclarationFragment

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

the class TypeImplementationGenerator method printProperties.

private void printProperties() {
    Iterable<VariableDeclarationFragment> fields = Iterables.filter(getInstanceFields(), PROPERTIES);
    if (Iterables.isEmpty(fields)) {
        return;
    }
    newline();
    for (VariableDeclarationFragment fragment : fields) {
        VariableElement varElement = fragment.getVariableElement();
        String propertyName = nameTable.getVariableBaseName(varElement);
        String varName = nameTable.getVariableShortName(varElement);
        println("@synthesize " + propertyName + " = " + varName + ";");
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement)

Example 12 with VariableDeclarationFragment

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

the class TypeImplementationGenerator method printStaticAccessors.

/**
   * Prints the list of static variable and/or enum constant accessor methods.
   */
protected void printStaticAccessors() {
    if (!options.staticAccessorMethods()) {
        return;
    }
    for (VariableDeclarationFragment fragment : getStaticFields()) {
        if (!((FieldDeclaration) fragment.getParent()).hasPrivateDeclaration()) {
            VariableElement varElement = fragment.getVariableElement();
            TypeMirror type = varElement.asType();
            boolean isVolatile = ElementUtil.isVolatile(varElement);
            boolean isPrimitive = type.getKind().isPrimitive();
            String accessorName = nameTable.getStaticAccessorName(varElement);
            String varName = nameTable.getVariableQualifiedName(varElement);
            String objcType = nameTable.getObjCType(type);
            String typeSuffix = isPrimitive ? NameTable.capitalize(TypeUtil.getName(type)) : "Id";
            if (isVolatile) {
                printf("\n+ (%s)%s {\n  return JreLoadVolatile%s(&%s);\n}\n", objcType, accessorName, typeSuffix, varName);
            } else {
                printf("\n+ (%s)%s {\n  return %s;\n}\n", objcType, accessorName, varName);
            }
            if (!ElementUtil.isFinal(varElement)) {
                String setterFunc = isVolatile ? (isPrimitive ? "JreAssignVolatile" + typeSuffix : "JreVolatileStrongAssign") : (isPrimitive | options.useARC() ? null : "JreStrongAssign");
                if (setterFunc == null) {
                    printf("\n+ (void)set%s:(%s)value {\n  %s = value;\n}\n", NameTable.capitalize(accessorName), objcType, varName);
                } else {
                    printf("\n+ (void)set%s:(%s)value {\n  %s(&%s, value);\n}\n", NameTable.capitalize(accessorName), objcType, setterFunc, varName);
                }
            }
        }
    }
    if (typeNode instanceof EnumDeclaration) {
        for (EnumConstantDeclaration constant : ((EnumDeclaration) typeNode).getEnumConstants()) {
            VariableElement varElement = constant.getVariableElement();
            printf("\n+ (%s *)%s {\n  return %s;\n}\n", typeName, nameTable.getStaticAccessorName(varElement), nameTable.getVariableQualifiedName(varElement));
        }
    }
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) TypeMirror(javax.lang.model.type.TypeMirror) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 13 with VariableDeclarationFragment

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

the class TypeDeclarationGenerator method printInstanceVariables.

/**
   * Prints the list of instance variables in a type.
   */
protected void printInstanceVariables() {
    Iterable<VariableDeclarationFragment> fields = getInstanceFields();
    if (Iterables.isEmpty(fields)) {
        newline();
        return;
    }
    // Need direct access to fields possibly from inner classes that are
    // promoted to top level classes, so must make all visible fields public.
    println(" {");
    println(" @public");
    indent();
    FieldDeclaration lastDeclaration = null;
    boolean needsAsterisk = false;
    for (VariableDeclarationFragment fragment : fields) {
        VariableElement varElement = fragment.getVariableElement();
        FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
        if (declaration != lastDeclaration) {
            if (lastDeclaration != null) {
                println(";");
            }
            lastDeclaration = declaration;
            JavadocGenerator.printDocComment(getBuilder(), declaration.getJavadoc());
            printIndent();
            if (ElementUtil.isWeakReference(varElement) && !ElementUtil.isVolatile(varElement)) {
                // We must add this even without -use-arc because the header may be
                // included by a file compiled with ARC.
                print("__unsafe_unretained ");
            }
            String objcType = getDeclarationType(varElement);
            needsAsterisk = objcType.endsWith("*");
            if (needsAsterisk) {
                // Strip pointer from type, as it will be added when appending fragment.
                // This is necessary to create "Foo *one, *two;" declarations.
                objcType = objcType.substring(0, objcType.length() - 2);
            }
            print(objcType);
            print(' ');
        } else {
            print(", ");
        }
        if (needsAsterisk) {
            print('*');
        }
        print(nameTable.getVariableShortName(varElement));
    }
    println(";");
    unindent();
    println("}");
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 14 with VariableDeclarationFragment

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

the class TypeDeclarationGenerator method printStaticAccessors.

/**
   * Prints the list of static variable and/or enum constant accessor methods.
   */
protected void printStaticAccessors() {
    if (options.staticAccessorMethods()) {
        for (VariableDeclarationFragment fragment : getStaticFields()) {
            VariableElement var = fragment.getVariableElement();
            String accessorName = nameTable.getStaticAccessorName(var);
            String objcType = nameTable.getObjCType(var.asType());
            printf("\n+ (%s)%s;\n", objcType, accessorName);
            if (!ElementUtil.isFinal(var)) {
                printf("\n+ (void)set%s:(%s)value;\n", NameTable.capitalize(accessorName), objcType);
            }
        }
        if (typeNode instanceof EnumDeclaration) {
            for (EnumConstantDeclaration constant : ((EnumDeclaration) typeNode).getEnumConstants()) {
                String accessorName = nameTable.getStaticAccessorName(constant.getVariableElement());
                if (options.nullability()) {
                    printf("\n+ (%s * __nonnull)%s;\n", typeName, accessorName);
                } else {
                    printf("\n+ (%s *)%s;\n", typeName, accessorName);
                }
            }
        }
    }
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 15 with VariableDeclarationFragment

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

VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)24 VariableElement (javax.lang.model.element.VariableElement)11 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)6 Statement (com.google.devtools.j2objc.ast.Statement)5 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 TypeMirror (javax.lang.model.type.TypeMirror)5 Block (com.google.devtools.j2objc.ast.Block)4 Expression (com.google.devtools.j2objc.ast.Expression)4 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)4 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)3 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)3 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 ForStatement (com.google.devtools.j2objc.ast.ForStatement)3 NameTable (com.google.devtools.j2objc.util.NameTable)3 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2