Search in sources :

Example 66 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class TypeDeclarationGenerator method printProperties.

protected void printProperties() {
    Iterable<VariableDeclarationFragment> fields = getAllFields();
    for (VariableDeclarationFragment fragment : fields) {
        FieldDeclaration fieldDecl = (FieldDeclaration) fragment.getParent();
        VariableElement varElement = fragment.getVariableElement();
        PropertyAnnotation property = (PropertyAnnotation) TreeUtil.getAnnotation(Property.class, fieldDecl.getAnnotations());
        if (property != null) {
            print("@property ");
            TypeMirror varType = varElement.asType();
            String propertyName = nameTable.getVariableBaseName(varElement);
            // Add default getter/setter here, as each fragment needs its own attributes
            // to support its unique accessors.
            Set<String> attributes = property.getPropertyAttributes();
            TypeElement declaringClass = ElementUtil.getDeclaringClass(varElement);
            if (property.getGetter() == null) {
                ExecutableElement getter = findGetterMethod(propertyName, varType, declaringClass);
                if (getter != null) {
                    attributes.add("getter=" + NameTable.getMethodName(getter));
                    if (!ElementUtil.isSynchronized(getter)) {
                        attributes.add("nonatomic");
                    }
                }
            }
            if (property.getSetter() == null) {
                ExecutableElement setter = findSetterMethod(propertyName, declaringClass);
                if (setter != null) {
                    attributes.add("setter=" + NameTable.getMethodName(setter));
                    if (!ElementUtil.isSynchronized(setter)) {
                        attributes.add("nonatomic");
                    }
                }
            }
            if (ElementUtil.isStatic(varElement)) {
                attributes.add("class");
            } else if (attributes.contains("class")) {
                ErrorUtil.error(fragment, "Only static fields can be translated to class properties");
            }
            if (attributes.contains("class") && !options.staticAccessorMethods()) {
                // Class property accessors must be present, as they are not synthesized by runtime.
                ErrorUtil.error(fragment, "Class properties require either a --swift-friendly or" + " --static-accessor-methods flag");
            }
            if (options.nullability()) {
                if (ElementUtil.hasNullableAnnotation(varElement)) {
                    attributes.add("nullable");
                } else if (ElementUtil.isNonnull(varElement, parametersNonnullByDefault)) {
                    attributes.add("nonnull");
                } else if (!attributes.contains("null_unspecified")) {
                    attributes.add("null_resettable");
                }
            }
            if (!attributes.isEmpty()) {
                print('(');
                print(PropertyAnnotation.toAttributeString(attributes));
                print(") ");
            }
            String objcType = nameTable.getObjCType(varType);
            print(objcType);
            if (!objcType.endsWith("*")) {
                print(' ');
            }
            println(propertyName + ";");
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Property(com.google.j2objc.annotations.Property) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration) PropertyAnnotation(com.google.devtools.j2objc.ast.PropertyAnnotation)

Example 67 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class TypeDeclarationGenerator method findGetterMethod.

/**
   * Locate method which matches either Java or Objective C getter name patterns.
   */
public static ExecutableElement findGetterMethod(String propertyName, TypeMirror propertyType, TypeElement declaringClass) {
    // Try Objective-C getter naming convention.
    ExecutableElement getter = ElementUtil.findMethod(declaringClass, propertyName);
    if (getter == null) {
        // Try Java getter naming conventions.
        String prefix = TypeUtil.isBoolean(propertyType) ? "is" : "get";
        getter = ElementUtil.findMethod(declaringClass, prefix + NameTable.capitalize(propertyName));
    }
    return getter;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 68 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class StatementGenerator method visit.

@Override
public boolean visit(MethodInvocation node) {
    ExecutableElement element = node.getExecutableElement();
    assert element != null;
    // Object receiving the message, or null if it's a method in this class.
    Expression receiver = node.getExpression();
    buffer.append('[');
    if (ElementUtil.isStatic(element)) {
        buffer.append(nameTable.getFullName(ElementUtil.getDeclaringClass(element)));
    } else if (receiver != null) {
        receiver.accept(this);
    } else {
        buffer.append("self");
    }
    printMethodInvocationNameAndArgs(nameTable.getMethodSelector(element), node.getArguments());
    buffer.append(']');
    return false;
}
Also used : LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 69 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class SignatureGenerator method createJniFunctionSignature.

public String createJniFunctionSignature(ExecutableElement method) {
    // Mangle function name as described in JNI specification.
    // http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp615
    StringBuilder sb = new StringBuilder();
    sb.append("Java_");
    String methodName = ElementUtil.getName(method);
    TypeElement declaringClass = ElementUtil.getDeclaringClass(method);
    PackageElement pkg = ElementUtil.getPackage(declaringClass);
    if (pkg != null && !pkg.isUnnamed()) {
        String pkgName = pkg.getQualifiedName().toString();
        for (String part : pkgName.split("\\.")) {
            sb.append(part);
            sb.append('_');
        }
    }
    jniMangleClass(declaringClass, sb);
    sb.append('_');
    sb.append(jniMangle(methodName));
    // Check whether the method is overloaded.
    int nameCount = 0;
    for (ExecutableElement m : ElementUtil.getExecutables(declaringClass)) {
        if (methodName.equals(ElementUtil.getName(m)) && ElementUtil.isNative(m)) {
            nameCount++;
        }
    }
    if (nameCount >= 2) {
        // Overloaded native methods, append JNI-mangled parameter types.
        sb.append("__");
        for (VariableElement param : method.getParameters()) {
            String type = createTypeSignature(typeUtil.erasure(param.asType()));
            sb.append(jniMangle(type));
        }
    }
    return sb.toString();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement)

Example 70 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project tiger by google.

the class TigerDaggerGeneratorProcessor method getMembersInjectorScope.

/**
   * Returns the {@link DeclaredType} of the scope class of the
   * {@link MembersInjector} specified.
   */
private DeclaredType getMembersInjectorScope(DeclaredType membersInjectorType) {
    ExecutableElement scopeElement = null;
    TypeElement membersInjectorTypeElement = elementUtils.getTypeElement(MembersInjector.class.getCanonicalName());
    for (Element element : membersInjectorTypeElement.getEnclosedElements()) {
        if (element.getSimpleName().contentEquals("scope")) {
            scopeElement = (ExecutableElement) element;
        }
    }
    Preconditions.checkNotNull(scopeElement);
    for (AnnotationMirror annotationMirror : membersInjectorType.asElement().getAnnotationMirrors()) {
        if (annotationMirror.getAnnotationType().asElement().equals(elementUtils.getTypeElement(MembersInjector.class.getCanonicalName()))) {
            return (DeclaredType) annotationMirror.getElementValues().get(scopeElement).getValue();
        }
    }
    throw new RuntimeException(String.format("Scope not found for MembersInjector: %s", membersInjectorType));
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)345 TypeElement (javax.lang.model.element.TypeElement)158 TypeMirror (javax.lang.model.type.TypeMirror)97 VariableElement (javax.lang.model.element.VariableElement)85 Element (javax.lang.model.element.Element)72 Test (org.junit.Test)41 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)32 DeclaredType (javax.lang.model.type.DeclaredType)31 ArrayList (java.util.ArrayList)26 JBlock (com.helger.jcodemodel.JBlock)25 JInvocation (com.helger.jcodemodel.JInvocation)20 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)18 JVar (com.helger.jcodemodel.JVar)18 Map (java.util.Map)18 HashSet (java.util.HashSet)17 IJExpression (com.helger.jcodemodel.IJExpression)16 ElementValidation (org.androidannotations.ElementValidation)15 Block (com.google.devtools.j2objc.ast.Block)13 JMethod (com.helger.jcodemodel.JMethod)13 Elements (javax.lang.model.util.Elements)13