use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(MethodDeclaration node) {
ExecutableElement element = node.getExecutableElement();
// they are added by the translator and need to remain in method form.
if (!node.hasDeclaration()) {
return;
}
boolean isConstructor = ElementUtil.isConstructor(element);
boolean isInstanceMethod = !ElementUtil.isStatic(element) && !isConstructor;
boolean isDefaultMethod = ElementUtil.isDefault(element);
List<BodyDeclaration> declarationList = TreeUtil.asDeclarationSublist(node);
if (!isInstanceMethod || isDefaultMethod || Modifier.isNative(node.getModifiers()) || functionizableMethods.contains(element)) {
TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
boolean isEnumConstructor = isConstructor && ElementUtil.isEnum(declaringClass);
if (isConstructor) {
addImplicitParameters(node, declaringClass);
}
FunctionDeclaration function = makeFunction(node);
declarationList.add(function);
if (isConstructor && !ElementUtil.isAbstract(declaringClass) && !isEnumConstructor) {
declarationList.add(makeAllocatingConstructor(node, false));
declarationList.add(makeAllocatingConstructor(node, true));
} else if (isEnumConstructor && options.useARC()) {
// Enums with ARC need the retaining constructor.
declarationList.add(makeAllocatingConstructor(node, false));
}
// Instance methods must be kept in case they are invoked using "super".
boolean keepMethod = isInstanceMethod || // Public methods must be kept for the public API.
!(ElementUtil.isPrivateInnerType(declaringClass) || ElementUtil.isPrivate(element)) || // Methods must be kept for reflection if enabled.
(translationUtil.needsReflection(declaringClass) && !isEnumConstructor);
if (keepMethod) {
if (isDefaultMethod) {
// For default methods keep only the declaration. Implementing classes will add a shim.
node.setBody(null);
node.addModifiers(Modifier.ABSTRACT);
} else {
setFunctionCaller(node, element);
}
} else {
node.remove();
}
ErrorUtil.functionizedMethod();
}
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(SuperMethodInvocation node) {
ExecutableElement element = node.getExecutableElement();
// Yes, super method invocations can be static.
if (!ElementUtil.isStatic(element)) {
return;
}
FunctionInvocation functionInvocation = new FunctionInvocation(newFunctionElement(element), node.getTypeMirror());
TreeUtil.moveList(node.getArguments(), functionInvocation.getArguments());
node.replaceWith(functionInvocation);
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class DestructorGenerator method addDeallocMethod.
private void addDeallocMethod(AbstractTypeDeclaration node) {
TypeElement type = node.getTypeElement();
boolean hasFinalize = hasFinalizeMethod(type);
List<Statement> releaseStatements = createReleaseStatements(node);
if (releaseStatements.isEmpty() && !hasFinalize) {
return;
}
ExecutableElement deallocElement = GeneratedExecutableElement.newMethodWithSelector(NameTable.DEALLOC_METHOD, typeUtil.getVoid(), type).addModifiers(Modifier.PUBLIC);
MethodDeclaration deallocDecl = new MethodDeclaration(deallocElement);
deallocDecl.setHasDeclaration(false);
Block block = new Block();
deallocDecl.setBody(block);
List<Statement> stmts = block.getStatements();
if (hasFinalize) {
String clsName = nameTable.getFullName(type);
stmts.add(new NativeStatement("JreCheckFinalize(self, [" + clsName + " class]);"));
}
stmts.addAll(releaseStatements);
if (options.useReferenceCounting()) {
stmts.add(new ExpressionStatement(new SuperMethodInvocation(new ExecutablePair(superDeallocElement))));
}
node.addBodyDeclaration(deallocDecl);
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class JavaCloneWriter method endVisit.
@Override
public void endVisit(TypeDeclaration node) {
TypeElement type = node.getTypeElement();
VariableElement originalVar = GeneratedVariableElement.newParameter("original", type.asType(), null);
List<Statement> adjustments = getFieldAdjustments(node, originalVar);
if (adjustments.isEmpty()) {
return;
}
TypeMirror voidType = typeUtil.getVoid();
ExecutableElement javaCloneElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, type).addParameter(originalVar);
MethodDeclaration declaration = new MethodDeclaration(javaCloneElement);
declaration.setHasDeclaration(false);
node.addBodyDeclaration(declaration);
declaration.addParameter(new SingleVariableDeclaration(originalVar));
Block body = new Block();
declaration.setBody(body);
List<Statement> statements = body.getStatements();
ExecutableElement javaCloneSuperElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, typeUtil.getJavaObject());
SuperMethodInvocation superCall = new SuperMethodInvocation(new ExecutablePair(javaCloneSuperElement));
superCall.addArgument(new SimpleName(originalVar));
statements.add(new ExpressionStatement(superCall));
statements.addAll(adjustments);
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class JavaToIOSMethodTranslator method addCopyWithZoneMethod.
private void addCopyWithZoneMethod(TypeDeclaration node) {
// Create copyWithZone: method.
GeneratedExecutableElement copyElement = GeneratedExecutableElement.newMethodWithSelector("copyWithZone:", TypeUtil.ID_TYPE, node.getTypeElement());
MethodDeclaration copyDecl = new MethodDeclaration(copyElement);
copyDecl.setHasDeclaration(false);
// Add NSZone *zone parameter.
VariableElement zoneParam = GeneratedVariableElement.newParameter("zone", NSZONE_TYPE, copyElement);
copyElement.addParameter(zoneParam);
copyDecl.addParameter(new SingleVariableDeclaration(zoneParam));
Block block = new Block();
copyDecl.setBody(block);
ExecutableElement cloneElement = ElementUtil.findMethod(typeUtil.getJavaObject(), "clone");
MethodInvocation invocation = new MethodInvocation(new ExecutablePair(cloneElement), null);
if (options.useReferenceCounting()) {
invocation = new MethodInvocation(new ExecutablePair(RETAIN_METHOD), invocation);
}
block.addStatement(new ReturnStatement(invocation));
node.addBodyDeclaration(copyDecl);
}
Aggregations