use of com.google.devtools.j2objc.ast.MethodDeclaration 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;
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class AnnotationRewriter method createAnnotationTypeMethod.
private MethodDeclaration createAnnotationTypeMethod(TypeElement type) {
ExecutableElement annotationTypeElement = GeneratedExecutableElement.newMethodWithSelector("annotationType", typeUtil.getJavaClass().asType(), type);
MethodDeclaration annotationTypeMethod = new MethodDeclaration(annotationTypeElement);
annotationTypeMethod.setHasDeclaration(false);
Block annotationTypeBody = new Block();
annotationTypeMethod.setBody(annotationTypeBody);
annotationTypeBody.addStatement(new ReturnStatement(new TypeLiteral(type.asType(), typeUtil)));
return annotationTypeMethod;
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class AnnotationRewriter method createDescriptionMethod.
private MethodDeclaration createDescriptionMethod(TypeElement type) {
ExecutableElement descriptionElement = GeneratedExecutableElement.newMethodWithSelector("description", typeUtil.getJavaString().asType(), type);
MethodDeclaration descriptionMethod = new MethodDeclaration(descriptionElement);
descriptionMethod.setHasDeclaration(false);
Block descriptionBody = new Block();
descriptionMethod.setBody(descriptionBody);
descriptionBody.addStatement(new ReturnStatement(new StringLiteral("@" + elementUtil.getBinaryName(type) + "()", typeUtil)));
return descriptionMethod;
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class TreeConverter method convertMethodDeclaration.
private static TreeNode convertMethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration node) {
MethodDeclaration newNode = new MethodDeclaration();
convertBodyDeclaration(node, newNode);
for (Object param : node.parameters()) {
newNode.addParameter((SingleVariableDeclaration) TreeConverter.convert(param));
}
newNode.setIsConstructor(node.isConstructor()).setExecutableElement(BindingConverter.getExecutableElement(node.resolveBinding())).setBody((Block) TreeConverter.convert(node.getBody()));
maybeAddImplicitSuperCall(newNode);
return newNode;
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class GenerationTest method translateMethod.
/**
* Translate a Java method into a JDT DOM MethodDeclaration. Although JDT
* has support for parsing methods, it doesn't resolve them. The statements
* are therefore wrapped in a type declaration so they having bindings.
*/
protected MethodDeclaration translateMethod(String method) {
// Wrap statements in test class, so type resolution works.
String source = "public class Test { " + method + " }";
CompilationUnit unit = translateType("Test", source);
final MethodDeclaration[] result = new MethodDeclaration[1];
unit.accept(new TreeVisitor() {
@Override
public boolean visit(MethodDeclaration node) {
String name = ElementUtil.getName(node.getExecutableElement());
if (name.equals(NameTable.INIT_NAME) || name.equals(NameTable.FINALIZE_METHOD) || name.equals(NameTable.DEALLOC_METHOD)) {
return false;
}
assert result[0] == null;
result[0] = node;
return false;
}
});
return result[0];
}
Aggregations