Search in sources :

Example 21 with Block

use of com.google.devtools.j2objc.ast.Block 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 22 with Block

use of com.google.devtools.j2objc.ast.Block in project j2objc by google.

the class AbstractMethodRewriter method endVisit.

@Override
public void endVisit(MethodDeclaration node) {
    ExecutableElement methodElement = node.getExecutableElement();
    if (!ElementUtil.isAbstract(methodElement)) {
        return;
    }
    // JDT only adds the abstract bit to a MethodDeclaration node's modifiers if the abstract
    // method is from a class. Since we want our code generator to go over an interface's
    // method nodes for default method support and skip abstract methods, we add the bit if the
    // method is from an interface.
    TypeElement declaringClass = ElementUtil.getDeclaringClass(methodElement);
    if (declaringClass.getKind().isInterface()) {
        node.addModifiers(java.lang.reflect.Modifier.ABSTRACT);
        return;
    }
    // we skip the stubbing out.
    if (!translationUtil.needsReflection(declaringClass)) {
        unit.setHasIncompleteProtocol();
        unit.setHasIncompleteImplementation();
        return;
    }
    Block body = new Block();
    // Generate a body which throws a NSInvalidArgumentException.
    String bodyCode = "// can't call an abstract method\n" + "[self doesNotRecognizeSelector:_cmd];";
    if (!TypeUtil.isVoid(node.getReturnTypeMirror())) {
        // Never executes, but avoids a gcc warning.
        bodyCode += "\nreturn 0;";
    }
    body.addStatement(new NativeStatement(bodyCode));
    node.setBody(body);
    node.removeModifiers(java.lang.reflect.Modifier.ABSTRACT);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block)

Example 23 with Block

use of com.google.devtools.j2objc.ast.Block in project j2objc by google.

the class AnnotationRewriter method addConstructor.

private void addConstructor(AnnotationTypeDeclaration node, Map<ExecutableElement, VariableElement> fieldElements) {
    TypeElement type = node.getTypeElement();
    String typeName = nameTable.getFullName(type);
    FunctionDeclaration constructorDecl = new FunctionDeclaration("create_" + typeName, type.asType());
    Block constructorBody = new Block();
    constructorDecl.setBody(constructorBody);
    List<Statement> stmts = constructorBody.getStatements();
    stmts.add(new NativeStatement(UnicodeUtils.format("%s *self = AUTORELEASE([[%s alloc] init]);", typeName, typeName)));
    for (ExecutableElement memberElement : ElementUtil.getSortedAnnotationMembers(type)) {
        TypeMirror memberType = memberElement.getReturnType();
        String propName = NameTable.getAnnotationPropertyName(memberElement);
        String fieldName = nameTable.getVariableShortName(fieldElements.get(memberElement));
        VariableElement param = GeneratedVariableElement.newParameter(propName, memberType, null);
        constructorDecl.addParameter(new SingleVariableDeclaration(param));
        String rhs = TypeUtil.isReferenceType(memberType) ? "RETAIN_(" + propName + ")" : propName;
        stmts.add(new NativeStatement("self->" + fieldName + " = " + rhs + ";"));
    }
    stmts.add(new NativeStatement("return self;"));
    node.addBodyDeclaration(constructorDecl);
}
Also used : FunctionDeclaration(com.google.devtools.j2objc.ast.FunctionDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 24 with Block

use of com.google.devtools.j2objc.ast.Block 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 25 with Block

use of com.google.devtools.j2objc.ast.Block 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)

Aggregations

Block (com.google.devtools.j2objc.ast.Block)38 Statement (com.google.devtools.j2objc.ast.Statement)18 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)13 ExecutableElement (javax.lang.model.element.ExecutableElement)13 TypeElement (javax.lang.model.element.TypeElement)12 SimpleName (com.google.devtools.j2objc.ast.SimpleName)11 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)11 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)10 VariableElement (javax.lang.model.element.VariableElement)10 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)9 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)9 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)8 ForStatement (com.google.devtools.j2objc.ast.ForStatement)8 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)8 TypeMirror (javax.lang.model.type.TypeMirror)8 Expression (com.google.devtools.j2objc.ast.Expression)7 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)7 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)6 IfStatement (com.google.devtools.j2objc.ast.IfStatement)6 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)6