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;
}
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));
}
}
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);
}
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;
}
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;
}
Aggregations