Search in sources :

Example 6 with ExecutablePair

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

the class JavaToIOSMethodTranslator method visit.

@Override
public boolean visit(ClassInstanceCreation node) {
    // translate any embedded method invocations
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
    }
    for (Expression e : node.getArguments()) {
        e.accept(this);
    }
    if (node.getAnonymousClassDeclaration() != null) {
        node.getAnonymousClassDeclaration().accept(this);
    }
    ExecutableElement method = node.getExecutableElement();
    String key = Mappings.getMethodKey(method, typeUtil);
    String selector = Mappings.STRING_CONSTRUCTOR_TO_METHOD_MAPPINGS.get(key);
    if (selector != null) {
        assert !node.hasRetainedResult();
        if (key.equals("java.lang.String.<init>(Ljava/lang/String;)V")) {
            // Special case: replace new String(constant) to constant (avoid clang warning).
            Expression arg = node.getArgument(0);
            if (arg instanceof StringLiteral) {
                node.replaceWith(arg.copy());
                return false;
            }
        }
        ExecutableElement newElement = GeneratedExecutableElement.newMappedMethod(selector, method);
        MethodInvocation newInvocation = new MethodInvocation(new ExecutablePair(newElement), new SimpleName(ElementUtil.getDeclaringClass(method)));
        TreeUtil.copyList(node.getArguments(), newInvocation.getArguments());
        node.replaceWith(newInvocation);
    }
    return true;
}
Also used : StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) Expression(com.google.devtools.j2objc.ast.Expression) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 7 with ExecutablePair

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

the class AbstractMethodRewriter method addReturnTypeNarrowingDeclarations.

// Adds declarations for any methods where the known return type is more
// specific than what is already declared in inherited types.
private void addReturnTypeNarrowingDeclarations(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    // No need to run this if the entire class is dead.
    if (deadCodeMap != null && deadCodeMap.containsClass(type, elementUtil)) {
        return;
    }
    Map<String, ExecutablePair> newDeclarations = new HashMap<>();
    Map<String, TypeMirror> resolvedReturnTypes = new HashMap<>();
    for (DeclaredType inheritedType : typeUtil.getObjcOrderedInheritedTypes(type.asType())) {
        TypeElement inheritedElem = (TypeElement) inheritedType.asElement();
        for (ExecutableElement methodElem : ElementUtil.getMethods(inheritedElem)) {
            if (ElementUtil.isPrivate(methodElem)) {
                continue;
            }
            TypeMirror declaredReturnType = typeUtil.erasure(methodElem.getReturnType());
            if (!TypeUtil.isReferenceType(declaredReturnType)) {
                // Short circuit
                continue;
            }
            String selector = nameTable.getMethodSelector(methodElem);
            ExecutableType methodType = typeUtil.asMemberOf(inheritedType, methodElem);
            TypeMirror returnType = typeUtil.erasure(methodType.getReturnType());
            TypeMirror resolvedReturnType = resolvedReturnTypes.get(selector);
            if (resolvedReturnType == null) {
                resolvedReturnType = declaredReturnType;
                resolvedReturnTypes.put(selector, resolvedReturnType);
            } else if (!typeUtil.isSubtype(returnType, resolvedReturnType)) {
                continue;
            }
            if (resolvedReturnType != returnType && !nameTable.getObjCType(resolvedReturnType).equals(nameTable.getObjCType(returnType))) {
                newDeclarations.put(selector, new ExecutablePair(methodElem, methodType));
                resolvedReturnTypes.put(selector, returnType);
            }
        }
    }
    for (Map.Entry<String, ExecutablePair> newDecl : newDeclarations.entrySet()) {
        if (deadCodeMap != null && deadCodeMap.containsMethod(newDecl.getValue().element(), typeUtil)) {
            continue;
        }
        node.addBodyDeclaration(newReturnTypeNarrowingDeclaration(newDecl.getKey(), newDecl.getValue(), type));
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) HashMap(java.util.HashMap) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) 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) HashMap(java.util.HashMap) Map(java.util.Map) CodeReferenceMap(com.google.devtools.j2objc.util.CodeReferenceMap) DeclaredType(javax.lang.model.type.DeclaredType)

Example 8 with ExecutablePair

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

the class TreeConverter method addImplicitSuperCall.

private static void addImplicitSuperCall(MethodDeclaration node) {
    ExecutableElement method = node.getExecutableElement();
    DeclaredType superType = (DeclaredType) ElementUtil.getDeclaringClass(method).getSuperclass();
    TypeElement superClass = TypeUtil.asTypeElement(superType);
    ExecutableElement superConstructor = findDefaultConstructorElement(superClass);
    SuperConstructorInvocation invocation = new SuperConstructorInvocation(new ExecutablePair(superConstructor, (ExecutableType) JdtTypes.asMemberOfInternal(superType, superConstructor))).setVarargsType(getVarargsType(BindingConverter.unwrapExecutableElement(superConstructor), Collections.emptyList()));
    node.getBody().addStatement(0, invocation);
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) DeclaredType(javax.lang.model.type.DeclaredType)

Example 9 with ExecutablePair

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

the class TreeConverter method convertEnumConstantDeclaration.

private static TreeNode convertEnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration node) {
    EnumConstantDeclaration newNode = new EnumConstantDeclaration();
    convertBodyDeclaration(node, newNode);
    IMethodBinding methodBinding = node.resolveConstructorBinding();
    JdtExecutableElement element = (JdtExecutableElement) BindingConverter.getExecutableElement(methodBinding);
    newNode.setVariableElement(BindingConverter.getVariableElement(node.resolveVariable())).setExecutablePair(new ExecutablePair(element, BindingConverter.getType(methodBinding))).setVarargsType(getVarargsType(methodBinding, node.arguments()));
    for (Object argument : node.arguments()) {
        newNode.addArgument((Expression) convert(argument));
    }
    org.eclipse.jdt.core.dom.AnonymousClassDeclaration anonymousClassDecl = node.getAnonymousClassDeclaration();
    if (anonymousClassDecl != null) {
        newNode.setAnonymousClassDeclaration(convertAnonymousClassDeclaration(anonymousClassDecl, element, methodBinding));
    }
    return newNode;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair)

Example 10 with ExecutablePair

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

the class TreeConverter method convertConstructorInvocation.

private static TreeNode convertConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation node) {
    IMethodBinding binding = node.resolveConstructorBinding();
    ConstructorInvocation newNode = new ConstructorInvocation().setExecutablePair(new ExecutablePair(BindingConverter.getExecutableElement(binding), BindingConverter.getType(binding))).setVarargsType(getVarargsType(binding, node.arguments()));
    for (Object argument : node.arguments()) {
        newNode.addArgument((Expression) convert(argument));
    }
    return newNode;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ConstructorInvocation(com.google.devtools.j2objc.ast.ConstructorInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair)

Aggregations

ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)25 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)14 SimpleName (com.google.devtools.j2objc.ast.SimpleName)12 ExecutableElement (javax.lang.model.element.ExecutableElement)11 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)10 TypeMirror (javax.lang.model.type.TypeMirror)10 SuperMethodInvocation (com.google.devtools.j2objc.ast.SuperMethodInvocation)9 TypeElement (javax.lang.model.element.TypeElement)9 Expression (com.google.devtools.j2objc.ast.Expression)8 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)7 Block (com.google.devtools.j2objc.ast.Block)5 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)5 SuperConstructorInvocation (com.google.devtools.j2objc.ast.SuperConstructorInvocation)5 TypeLiteral (com.google.devtools.j2objc.ast.TypeLiteral)5 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)5 VariableElement (javax.lang.model.element.VariableElement)5 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)4 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)4 DeclaredType (javax.lang.model.type.DeclaredType)4 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)3