Search in sources :

Example 16 with MethodDeclaration

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;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Block(com.google.devtools.j2objc.ast.Block) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 17 with MethodDeclaration

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;
}
Also used : TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 18 with MethodDeclaration

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;
}
Also used : StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 19 with MethodDeclaration

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;
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration)

Example 20 with MethodDeclaration

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];
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration)

Aggregations

MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)30 ExecutableElement (javax.lang.model.element.ExecutableElement)18 Block (com.google.devtools.j2objc.ast.Block)13 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)11 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)7 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)7 TypeElement (javax.lang.model.element.TypeElement)7 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)6 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)6 VariableElement (javax.lang.model.element.VariableElement)6 TypeMirror (javax.lang.model.type.TypeMirror)6 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)5 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)4 Statement (com.google.devtools.j2objc.ast.Statement)4 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)4 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)3 SimpleName (com.google.devtools.j2objc.ast.SimpleName)3 SuperMethodInvocation (com.google.devtools.j2objc.ast.SuperMethodInvocation)3 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)3 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)2