Search in sources :

Example 6 with FieldDeclaration

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

the class TreeConverter method convertFieldDeclaration.

private static TreeNode convertFieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration node) {
    FieldDeclaration newNode = new FieldDeclaration();
    convertBodyDeclaration(node, newNode);
    for (Object fragment : node.fragments()) {
        newNode.addFragment((VariableDeclarationFragment) convert(fragment));
    }
    return newNode;
}
Also used : FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 7 with FieldDeclaration

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

Example 8 with FieldDeclaration

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

the class Rewriter method endVisit.

@Override
public void endVisit(FieldDeclaration node) {
    if (options.isJDT()) {
        ListMultimap<Integer, VariableDeclarationFragment> newDeclarations = rewriteExtraDimensions(node.getFragments());
        if (newDeclarations != null) {
            List<BodyDeclaration> bodyDecls = TreeUtil.asDeclarationSublist(node);
            for (Integer dimensions : newDeclarations.keySet()) {
                List<VariableDeclarationFragment> fragments = newDeclarations.get(dimensions);
                FieldDeclaration newDecl = new FieldDeclaration(fragments.get(0));
                newDecl.getFragments().addAll(fragments.subList(1, fragments.size()));
                bodyDecls.add(newDecl);
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 9 with FieldDeclaration

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

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

the class TypeDeclarationGenerator method printStaticFieldFullDeclaration.

private void printStaticFieldFullDeclaration(VariableDeclarationFragment fragment) {
    VariableElement var = fragment.getVariableElement();
    boolean isVolatile = ElementUtil.isVolatile(var);
    String objcType = nameTable.getObjCType(var.asType());
    String objcTypePadded = objcType + (objcType.endsWith("*") ? "" : " ");
    String declType = getDeclarationType(var);
    declType += (declType.endsWith("*") ? "" : " ");
    String name = nameTable.getVariableShortName(var);
    boolean isFinal = ElementUtil.isFinal(var);
    boolean isPrimitive = var.asType().getKind().isPrimitive();
    boolean isConstant = ElementUtil.isPrimitiveConstant(var);
    String qualifiers = isConstant ? "_CONSTANT" : (isPrimitive ? "_PRIMITIVE" : "_OBJ") + (isVolatile ? "_VOLATILE" : "") + (isFinal ? "_FINAL" : "");
    newline();
    FieldDeclaration decl = (FieldDeclaration) fragment.getParent();
    JavadocGenerator.printDocComment(getBuilder(), decl.getJavadoc());
    printf("inline %s%s_get_%s();\n", objcTypePadded, typeName, name);
    if (!isFinal) {
        printf("inline %s%s_set_%s(%svalue);\n", objcTypePadded, typeName, name, objcTypePadded);
        if (isPrimitive && !isVolatile) {
            printf("inline %s *%s_getRef_%s();\n", objcType, typeName, name);
        }
    }
    if (isConstant) {
        Object value = var.getConstantValue();
        assert value != null;
        printf("#define %s_%s %s\n", typeName, name, LiteralGenerator.generate(value));
    } else {
        printStaticFieldDeclaration(fragment, UnicodeUtils.format("%s%s_%s", declType, typeName, name));
    }
    printf("J2OBJC_STATIC_FIELD%s(%s, %s, %s)\n", qualifiers, typeName, name, objcType);
}
Also used : VariableElement(javax.lang.model.element.VariableElement) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Aggregations

FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)10 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)6 VariableElement (javax.lang.model.element.VariableElement)5 TypeElement (javax.lang.model.element.TypeElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 AnnotationTypeMemberDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration)1 ClassInstanceCreation (com.google.devtools.j2objc.ast.ClassInstanceCreation)1 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)1 PropertyAnnotation (com.google.devtools.j2objc.ast.PropertyAnnotation)1 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)1 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)1 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)1 Property (com.google.j2objc.annotations.Property)1 Weak (com.google.j2objc.annotations.Weak)1 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)1 HashMap (java.util.HashMap)1