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;
}
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");
}
}
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);
}
}
}
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);
}
}
}
}
}
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);
}
}
}
Aggregations