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