use of com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration in project j2objc by google.
the class AnnotationRewriter method endVisit.
@Override
public void endVisit(AnnotationTypeDeclaration node) {
TypeElement type = node.getTypeElement();
if (!ElementUtil.isRuntimeAnnotation(type)) {
return;
}
List<AnnotationTypeMemberDeclaration> members = TreeUtil.getAnnotationMembers(node);
List<BodyDeclaration> bodyDecls = node.getBodyDeclarations();
Map<ExecutableElement, VariableElement> fieldElements = createMemberFields(node, members);
addMemberProperties(node, members, fieldElements);
addDefaultAccessors(node, members);
bodyDecls.add(createAnnotationTypeMethod(type));
bodyDecls.add(createDescriptionMethod(type));
addConstructor(node, fieldElements);
}
use of com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration in project j2objc by google.
the class AnnotationRewriter method addDefaultAccessors.
// Create accessors for properties that have default values.
private void addDefaultAccessors(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members) {
TypeElement type = node.getTypeElement();
for (AnnotationTypeMemberDeclaration member : members) {
ExecutableElement memberElement = member.getExecutableElement();
AnnotationValue defaultValue = memberElement.getDefaultValue();
if (defaultValue == null || defaultValue.getValue() == null) {
continue;
}
TypeMirror memberType = memberElement.getReturnType();
String propName = NameTable.getAnnotationPropertyName(memberElement);
ExecutableElement defaultGetterElement = GeneratedExecutableElement.newMethodWithSelector(propName + "Default", memberType, type).addModifiers(Modifier.STATIC);
MethodDeclaration defaultGetter = new MethodDeclaration(defaultGetterElement);
defaultGetter.setHasDeclaration(false);
Block defaultGetterBody = new Block();
defaultGetter.setBody(defaultGetterBody);
defaultGetterBody.addStatement(new ReturnStatement(translationUtil.createAnnotationValue(memberType, defaultValue)));
node.addBodyDeclaration(defaultGetter);
}
}
use of com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration in project j2objc by google.
the class AnnotationRewriter method addMemberProperties.
// Generate the property declarations and synthesize statements.
private void addMemberProperties(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members, Map<ExecutableElement, VariableElement> fieldElements) {
if (members.isEmpty()) {
return;
}
StringBuilder propertyDecls = new StringBuilder();
StringBuilder propertyImpls = new StringBuilder();
for (AnnotationTypeMemberDeclaration member : members) {
ExecutableElement memberElement = member.getExecutableElement();
String propName = NameTable.getAnnotationPropertyName(memberElement);
String memberTypeStr = nameTable.getObjCType(memberElement.getReturnType());
String fieldName = nameTable.getVariableShortName(fieldElements.get(memberElement));
propertyDecls.append(UnicodeUtils.format("@property (readonly) %s%s%s;\n", memberTypeStr, memberTypeStr.endsWith("*") ? "" : " ", propName));
if (NameTable.needsObjcMethodFamilyNoneAttribute(propName)) {
propertyDecls.append(UnicodeUtils.format("- (%s)%s OBJC_METHOD_FAMILY_NONE;\n", memberTypeStr, propName));
}
propertyImpls.append(UnicodeUtils.format("@synthesize %s = %s;\n", propName, fieldName));
}
node.addBodyDeclaration(NativeDeclaration.newInnerDeclaration(propertyDecls.toString(), propertyImpls.toString()));
}
use of com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration in project j2objc by google.
the class AnnotationRewriter method createMemberFields.
// Create an instance field for each member.
private Map<ExecutableElement, VariableElement> createMemberFields(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members) {
TypeElement type = node.getTypeElement();
Map<ExecutableElement, VariableElement> fieldElements = new HashMap<>();
for (AnnotationTypeMemberDeclaration member : members) {
ExecutableElement memberElement = member.getExecutableElement();
String propName = NameTable.getAnnotationPropertyName(memberElement);
VariableElement field = GeneratedVariableElement.newField(propName, memberElement.getReturnType(), type);
node.addBodyDeclaration(new FieldDeclaration(field, null));
fieldElements.put(memberElement, field);
}
return fieldElements;
}
use of com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration in project j2objc by google.
the class TreeConverter method convertAnnotationTypeMemberDeclaration.
private static TreeNode convertAnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration node) {
AnnotationTypeMemberDeclaration newNode = new AnnotationTypeMemberDeclaration();
convertBodyDeclaration(node, newNode);
return newNode.setExecutableElement(BindingConverter.getExecutableElement(node.resolveBinding())).setDefault((Expression) convert(node.getDefault()));
}
Aggregations