Search in sources :

Example 1 with AnnotationTypeMemberDeclaration

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);
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 2 with AnnotationTypeMemberDeclaration

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);
    }
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) AnnotationValue(javax.lang.model.element.AnnotationValue) Block(com.google.devtools.j2objc.ast.Block)

Example 3 with AnnotationTypeMemberDeclaration

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()));
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 4 with AnnotationTypeMemberDeclaration

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;
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 5 with AnnotationTypeMemberDeclaration

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()));
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration)

Aggregations

AnnotationTypeMemberDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)4 ExecutableElement (javax.lang.model.element.ExecutableElement)4 TypeElement (javax.lang.model.element.TypeElement)3 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 VariableElement (javax.lang.model.element.VariableElement)2 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)1 Block (com.google.devtools.j2objc.ast.Block)1 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)1 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)1 Expression (com.google.devtools.j2objc.ast.Expression)1 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)1 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)1 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)1 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)1 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)1 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)1 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)1 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)1