Search in sources :

Example 1 with EnumDeclaration

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

the class TreeConverter method convertEnum.

private TreeNode convertEnum(JCTree.JCClassDecl node) {
    if (node.sym.isAnonymous()) {
        return convertClassDeclaration(node).setPosition(getPosition(node));
    }
    EnumDeclaration newNode = (EnumDeclaration) new EnumDeclaration();
    convertBodyDeclaration(node, node.getModifiers(), newNode, node.sym);
    newNode.setName(convertSimpleName(node.sym, node.type, getNamePosition(node))).setTypeElement(node.sym);
    for (JCTree bodyDecl : node.getMembers()) {
        if (bodyDecl.getKind() == Kind.VARIABLE) {
            TreeNode var = convertVariableDeclaration((JCTree.JCVariableDecl) bodyDecl);
            if (var.getKind() == TreeNode.Kind.ENUM_CONSTANT_DECLARATION) {
                newNode.addEnumConstant((EnumConstantDeclaration) var);
            } else {
                newNode.addBodyDeclaration((BodyDeclaration) var);
            }
        } else if (bodyDecl.getKind() == Kind.BLOCK) {
            JCTree.JCBlock javacBlock = (JCTree.JCBlock) bodyDecl;
            Block block = (Block) convert(javacBlock);
            newNode.addBodyDeclaration(new Initializer(block, javacBlock.isStatic()));
        } else {
            newNode.addBodyDeclaration((BodyDeclaration) convert(bodyDecl));
        }
    }
    return newNode;
}
Also used : Initializer(com.google.devtools.j2objc.ast.Initializer) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer) TreeNode(com.google.devtools.j2objc.ast.TreeNode) JCTree(com.sun.tools.javac.tree.JCTree) Block(com.google.devtools.j2objc.ast.Block) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 2 with EnumDeclaration

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

the class TypeDeclarationGenerator method printNativeEnum.

private void printNativeEnum() {
    if (!(typeNode instanceof EnumDeclaration)) {
        return;
    }
    List<EnumConstantDeclaration> constants = ((EnumDeclaration) typeNode).getEnumConstants();
    String nativeName = NameTable.getNativeEnumName(typeName);
    // C enum declaration and generate the type declaration.
    if (!constants.isEmpty()) {
        newline();
        printf("typedef NS_ENUM(NSUInteger, %s) {\n", nativeName);
        // Print C enum typedef.
        indent();
        int ordinal = 0;
        for (EnumConstantDeclaration constant : constants) {
            printIndent();
            printf("%s_%s = %d,\n", nativeName, nameTable.getVariableBaseName(constant.getVariableElement()), ordinal++);
        }
        unindent();
        print("};\n");
    }
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 3 with EnumDeclaration

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

the class TypeDeclarationGenerator method printProperties.

protected void printProperties() {
    Iterable<VariableDeclarationFragment> fields = getAllFields();
    for (VariableDeclarationFragment fragment : fields) {
        PropertyGenerator.generate(fragment, options, nameTable, typeUtil, parametersNonnullByDefault).ifPresent(this::println);
    }
    if (options.classProperties() && typeNode instanceof EnumDeclaration) {
        for (EnumConstantDeclaration constant : ((EnumDeclaration) typeNode).getEnumConstants()) {
            String accessorName = nameTable.getStaticAccessorName(constant.getVariableElement());
            print("\n@property (readonly, class");
            if (options.nullability()) {
                print(", nonnull");
            }
            // TODO(antoniocortes): use nameTable.getSwiftName() when it is implemented.
            printf(") %s *%s NS_SWIFT_NAME(%s);", typeName, accessorName, accessorName);
        }
    }
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 4 with EnumDeclaration

use of com.google.devtools.j2objc.ast.EnumDeclaration 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() && !options.classProperties()) {
        for (VariableDeclarationFragment fragment : getStaticFields()) {
            VariableElement var = fragment.getVariableElement();
            TypeMirror type = var.asType();
            String accessorName = nameTable.getStaticAccessorName(var);
            String objcType = nameTable.getObjCType(type);
            TypeElement declaringClass = ElementUtil.getDeclaringClass(var);
            String baseName = nameTable.getVariableBaseName(var);
            ExecutableElement getter = ElementUtil.findGetterMethod(baseName, type, declaringClass, /* isStatic = */
            true);
            if (getter == null) {
                printf("\n+ (%s)%s;\n", objcType, accessorName);
            }
            ExecutableElement setter = ElementUtil.findSetterMethod(baseName, type, declaringClass, /* isStatic = */
            true);
            if (setter == null && !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) 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) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Example 5 with EnumDeclaration

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

the class TypeDeclarationGenerator method printEnumConstants.

private void printEnumConstants() {
    if (typeNode instanceof EnumDeclaration) {
        newline();
        println("/*! INTERNAL ONLY - Use enum accessors declared below. */");
        printf("FOUNDATION_EXPORT %s *%s_values_[];\n", typeName, typeName);
        for (EnumConstantDeclaration constant : ((EnumDeclaration) typeNode).getEnumConstants()) {
            String varName = nameTable.getVariableBaseName(constant.getVariableElement());
            newline();
            JavadocGenerator.printDocComment(getBuilder(), constant.getJavadoc());
            printf("inline %s *%s_get_%s(void);\n", typeName, typeName, varName);
            printf("J2OBJC_ENUM_CONSTANT(%s, %s)\n", typeName, varName);
        }
    }
}
Also used : EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) EnumDeclaration(com.google.devtools.j2objc.ast.EnumDeclaration)

Aggregations

EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)9 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)5 TypeElement (javax.lang.model.element.TypeElement)5 TreeNode (com.google.devtools.j2objc.ast.TreeNode)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 VariableElement (javax.lang.model.element.VariableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)2 Block (com.google.devtools.j2objc.ast.Block)2 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 Initializer (com.google.devtools.j2objc.ast.Initializer)2 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)1 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)1 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)1 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)1 AnnotatedTypeTree (com.sun.source.tree.AnnotatedTypeTree)1 AnnotationTree (com.sun.source.tree.AnnotationTree)1 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)1 ArrayTypeTree (com.sun.source.tree.ArrayTypeTree)1