use of com.google.devtools.j2objc.ast.SingleVariableDeclaration in project j2objc by google.
the class Functionizer method makeAllocatingConstructor.
/**
* Create a wrapper for a constructor that does the object allocation.
*/
private FunctionDeclaration makeAllocatingConstructor(MethodDeclaration method, boolean releasing) {
assert method.isConstructor();
ExecutableElement element = method.getExecutableElement();
TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
String name = releasing ? nameTable.getReleasingConstructorName(element) : nameTable.getAllocatingConstructorName(element);
FunctionDeclaration function = new FunctionDeclaration(name, declaringClass.asType());
function.setLineNumber(method.getLineNumber());
function.setModifiers(ElementUtil.isPrivate(element) ? Modifier.PRIVATE : Modifier.PUBLIC);
function.setReturnsRetained(!releasing);
TreeUtil.copyList(method.getParameters(), function.getParameters());
Block body = new Block();
function.setBody(body);
StringBuilder sb = new StringBuilder(releasing ? "J2OBJC_CREATE_IMPL(" : "J2OBJC_NEW_IMPL(");
sb.append(nameTable.getFullName(declaringClass));
sb.append(", ").append(nameTable.getFunctionName(element));
for (SingleVariableDeclaration param : function.getParameters()) {
sb.append(", ").append(nameTable.getVariableQualifiedName(param.getVariableElement()));
}
sb.append(")");
body.addStatement(new NativeStatement(sb.toString()));
return function;
}
use of com.google.devtools.j2objc.ast.SingleVariableDeclaration 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 com.google.devtools.j2objc.ast.SingleVariableDeclaration 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);
}
use of com.google.devtools.j2objc.ast.SingleVariableDeclaration in project j2objc by google.
the class AbstractMethodRewriter method newReturnTypeNarrowingDeclaration.
private MethodDeclaration newReturnTypeNarrowingDeclaration(String selector, ExecutablePair method, TypeElement declaringClass) {
GeneratedExecutableElement element = GeneratedExecutableElement.newMethodWithSelector(selector, method.type().getReturnType(), declaringClass).addModifiers(ElementUtil.getVisibilityModifiers(method.element())).addModifiers(Modifier.ABSTRACT);
MethodDeclaration decl = new MethodDeclaration(element);
if (!declaringClass.getKind().isInterface()) {
unit.setHasIncompleteImplementation();
}
int argCount = 0;
for (TypeMirror paramType : method.type().getParameterTypes()) {
VariableElement param = GeneratedVariableElement.newParameter("arg" + argCount++, paramType, element);
element.addParameter(param);
decl.addParameter(new SingleVariableDeclaration(param));
}
return decl;
}
use of com.google.devtools.j2objc.ast.SingleVariableDeclaration in project j2objc by google.
the class TreeConverter method createAnonymousConstructor.
private static MethodDeclaration createAnonymousConstructor(JdtExecutableElement constructorElem, IMethodBinding constructorBinding) {
MethodDeclaration constructor = new MethodDeclaration(constructorElem);
Block body = new Block();
constructor.setBody(body);
IMethodBinding superConstructorBinding = findSuperConstructor(constructorBinding);
ExecutablePair superConstructor = new ExecutablePair(BindingConverter.getExecutableElement(superConstructorBinding), BindingConverter.getType(superConstructorBinding));
SuperConstructorInvocation superCall = new SuperConstructorInvocation(superConstructor);
body.addStatement(superCall);
Iterator<? extends VariableElement> params = constructorElem.getParameters().iterator();
if (constructorElem.hasSuperOuter()) {
VariableElement param = params.next();
constructor.addParameter(new SingleVariableDeclaration(param));
superCall.setExpression(new SimpleName(param));
}
while (params.hasNext()) {
VariableElement param = params.next();
constructor.addParameter(new SingleVariableDeclaration(param));
superCall.addArgument(new SimpleName(param));
}
assert constructor.getParameters().size() == constructorElem.getParameters().size();
return constructor;
}
Aggregations