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