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