Search in sources :

Example 6 with GeneratedExecutableElement

use of com.google.devtools.j2objc.types.GeneratedExecutableElement in project j2objc by google.

the class CastResolver method getDeclaredReturnType.

private TypeMirror getDeclaredReturnType(ExecutableElement method, TypeMirror receiverType) {
    // Check if the method is declared on the receiver type.
    if (ElementUtil.getDeclaringClass(method).equals(TypeUtil.asTypeElement(receiverType))) {
        return method.getReturnType();
    }
    // Search all inherited types for matching method declarations. Choose the
    // most narrow return type, because AbstractMethodRewriter will ensure that
    // a declaration exists with the most narrow return type.
    ExecutableType methodType = (ExecutableType) method.asType();
    String selector = nameTable.getMethodSelector(method);
    for (TypeMirror typeBound : typeUtil.getUpperBounds(receiverType)) {
        if (TypeUtil.isDeclaredType(typeBound)) {
            // Normalize any parameterized types before searching for method declarations.
            typeBound = ((DeclaredType) typeBound).asElement().asType();
        }
        TypeMirror returnType = null;
        for (DeclaredType inheritedType : typeUtil.getObjcOrderedInheritedTypes(typeBound)) {
            TypeElement inheritedElem = (TypeElement) inheritedType.asElement();
            for (ExecutableElement currentMethod : ElementUtil.getMethods(inheritedElem)) {
                ExecutableType currentMethodType = typeUtil.asMemberOf(inheritedType, currentMethod);
                if (typeUtil.isSubsignature(methodType, currentMethodType) && nameTable.getMethodSelector(currentMethod).equals(selector)) {
                    TypeMirror newReturnType = typeUtil.erasure(currentMethodType.getReturnType());
                    if (returnType == null || typeUtil.isSubtype(newReturnType, returnType)) {
                        returnType = newReturnType;
                    }
                }
            }
        }
        if (returnType != null) {
            return returnType;
        }
    }
    // Last resort. Might be a GeneratedExecutableElement.
    return method.getReturnType();
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 7 with GeneratedExecutableElement

use of com.google.devtools.j2objc.types.GeneratedExecutableElement in project j2objc by google.

the class Functionizer method addDisallowedConstructors.

/**
   * Declare any inherited constructors that aren't allowed to be accessed in Java
   * with a NS_UNAVAILABLE macro, so that clang will flag such access from native
   * code as an error.
   */
private void addDisallowedConstructors(TypeDeclaration node) {
    TypeElement typeElement = node.getTypeElement();
    TypeElement superClass = ElementUtil.getSuperclass(typeElement);
    if (ElementUtil.isPrivateInnerType(typeElement) || ElementUtil.isAbstract(typeElement) || superClass == null) {
        return;
    }
    Set<String> constructors = new HashSet<>();
    for (ExecutableElement constructor : ElementUtil.getConstructors(typeElement)) {
        constructors.add(nameTable.getMethodSelector(constructor));
    }
    Map<String, ExecutableElement> inheritedConstructors = new HashMap<>();
    // Add super constructors that have unique parameter lists.
    for (ExecutableElement superC : ElementUtil.getConstructors(superClass)) {
        if (ElementUtil.isPrivate(superC)) {
            // Skip private super constructors since they're already unavailable.
            continue;
        }
        String selector = nameTable.getMethodSelector(superC);
        if (!constructors.contains(selector)) {
            inheritedConstructors.put(selector, superC);
        }
    }
    for (Map.Entry<String, ExecutableElement> entry : inheritedConstructors.entrySet()) {
        ExecutableElement oldConstructor = entry.getValue();
        GeneratedExecutableElement newConstructor = GeneratedExecutableElement.newConstructorWithSelector(entry.getKey(), typeElement, typeUtil);
        MethodDeclaration decl = new MethodDeclaration(newConstructor).setUnavailable(true);
        decl.addModifiers(Modifier.ABSTRACT);
        int count = 0;
        for (VariableElement param : oldConstructor.getParameters()) {
            VariableElement newParam = GeneratedVariableElement.newParameter("arg" + count++, param.asType(), newConstructor);
            newConstructor.addParameter(newParam);
            decl.addParameter(new SingleVariableDeclaration(newParam));
        }
        addImplicitParameters(decl, ElementUtil.getDeclaringClass(oldConstructor));
        node.addBodyDeclaration(decl);
    }
}
Also used : HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 8 with GeneratedExecutableElement

use of com.google.devtools.j2objc.types.GeneratedExecutableElement in project j2objc by google.

the class ArrayRewriter method endVisit.

@Override
public void endVisit(InstanceofExpression node) {
    TypeMirror type = node.getRightOperand().getTypeMirror();
    if (!TypeUtil.isArray(type) || ((ArrayType) type).getComponentType().getKind().isPrimitive()) {
        return;
    }
    GeneratedExecutableElement element = GeneratedExecutableElement.newMethodWithSelector("isInstance", typeUtil.getBoolean(), TypeUtil.IOS_CLASS);
    element.addParameter(GeneratedVariableElement.newParameter("object", TypeUtil.ID_TYPE, element));
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(element), new TypeLiteral(type, typeUtil));
    invocation.addArgument(TreeUtil.remove(node.getLeftOperand()));
    node.replaceWith(invocation);
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 9 with GeneratedExecutableElement

use of com.google.devtools.j2objc.types.GeneratedExecutableElement in project j2objc by google.

the class ArrayRewriter method newInitializedArrayInvocation.

private MethodInvocation newInitializedArrayInvocation(ArrayType arrayType, List<Expression> elements, boolean retainedResult) {
    TypeMirror componentType = arrayType.getComponentType();
    TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
    GeneratedExecutableElement methodElement = GeneratedExecutableElement.newMethodWithSelector(getInitializeSelector(componentType, retainedResult), iosArrayElement.asType(), iosArrayElement).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
    methodElement.addParameter(GeneratedVariableElement.newParameter("values", new PointerType(componentType), methodElement));
    methodElement.addParameter(GeneratedVariableElement.newParameter("count", typeUtil.getInt(), methodElement));
    if (!componentType.getKind().isPrimitive()) {
        methodElement.addParameter(GeneratedVariableElement.newParameter("type", TypeUtil.IOS_CLASS.asType(), methodElement));
    }
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));
    // Create the array initializer and add it as the first parameter.
    ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
    for (Expression element : elements) {
        arrayInit.addExpression(element.copy());
    }
    invocation.addArgument(arrayInit);
    // Add the array size parameter.
    invocation.addArgument(NumberLiteral.newIntLiteral(arrayInit.getExpressions().size(), typeUtil));
    // Add the type argument for object arrays.
    if (!componentType.getKind().isPrimitive()) {
        invocation.addArgument(new TypeLiteral(componentType, typeUtil));
    }
    return invocation;
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) TypeElement(javax.lang.model.element.TypeElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PointerType(com.google.devtools.j2objc.types.PointerType) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Aggregations

GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)9 TypeMirror (javax.lang.model.type.TypeMirror)6 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)4 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)4 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)4 TypeElement (javax.lang.model.element.TypeElement)4 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)3 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)3 TypeLiteral (com.google.devtools.j2objc.ast.TypeLiteral)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 VariableElement (javax.lang.model.element.VariableElement)3 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 PointerType (com.google.devtools.j2objc.types.PointerType)2 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)1 Block (com.google.devtools.j2objc.ast.Block)1 Expression (com.google.devtools.j2objc.ast.Expression)1 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)1 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)1 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)1 GeneratedPackageElement (com.google.devtools.j2objc.types.GeneratedPackageElement)1