Search in sources :

Example 1 with ReturnStatement

use of com.google.devtools.j2objc.ast.ReturnStatement 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);
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) 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) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 2 with ReturnStatement

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

the class AnnotationRewriter method addDefaultAccessors.

// Create accessors for properties that have default values.
private void addDefaultAccessors(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members) {
    TypeElement type = node.getTypeElement();
    for (AnnotationTypeMemberDeclaration member : members) {
        ExecutableElement memberElement = member.getExecutableElement();
        AnnotationValue defaultValue = memberElement.getDefaultValue();
        if (defaultValue == null || defaultValue.getValue() == null) {
            continue;
        }
        TypeMirror memberType = memberElement.getReturnType();
        String propName = NameTable.getAnnotationPropertyName(memberElement);
        ExecutableElement defaultGetterElement = GeneratedExecutableElement.newMethodWithSelector(propName + "Default", memberType, type).addModifiers(Modifier.STATIC);
        MethodDeclaration defaultGetter = new MethodDeclaration(defaultGetterElement);
        defaultGetter.setHasDeclaration(false);
        Block defaultGetterBody = new Block();
        defaultGetter.setBody(defaultGetterBody);
        defaultGetterBody.addStatement(new ReturnStatement(translationUtil.createAnnotationValue(memberType, defaultValue)));
        node.addBodyDeclaration(defaultGetter);
    }
}
Also used : AnnotationTypeMemberDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) 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) AnnotationValue(javax.lang.model.element.AnnotationValue) Block(com.google.devtools.j2objc.ast.Block)

Example 3 with ReturnStatement

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

the class CompoundTypeTest method testIsCompound.

// Test TypeUtil.isIntersection(TypeMirror).
public void testIsCompound() throws Exception {
    String source = "interface Test<T> extends java.util.Comparator<T> {" + "  default Test<T> thenTesting(Test<? super T> other) { " + "    return (Test<T> & java.io.Serializable) (c1, c2) -> { " + "    int res = compare(c1, c2); " + "    return (res != 0) ? res : other.compare(c1, c2); }; }}";
    CompilationUnit unit = compileType("Test", source);
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    int methodsFound = 0;
    for (BodyDeclaration body : decl.getBodyDeclarations()) {
        if (body instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) body;
            if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
                // Verify a normal type isn't marked as compound.
                TypeMirror returnType = method.getReturnTypeMirror();
                assertFalse(TypeUtil.isIntersection(returnType));
                // The method's return type isn't compound, but the cast expression in
                // its return statement is.
                ReturnStatement stmt = (ReturnStatement) method.getBody().getStatements().get(0);
                assertTrue(TypeUtil.isIntersection(stmt.getExpression().getTypeMirror()));
                methodsFound++;
            }
        }
    }
    assertEquals(1, methodsFound);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TypeMirror(javax.lang.model.type.TypeMirror) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 4 with ReturnStatement

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

the class CompoundTypeTest method testCompoundTypeFullName.

// Test NameTable.getObjCType(TypeMirror).
public void testCompoundTypeFullName() throws IOException {
    String source = "package foo.bar; interface Test<T> extends java.util.Comparator<T> {" + "  default Test<T> thenTesting(Test<? super T> other) { " + "    return (Test<T> & java.io.Serializable) (c1, c2) -> { " + "    int res = compare(c1, c2); " + "    return (res != 0) ? res : other.compare(c1, c2); }; }}";
    CompilationUnit unit = compileType("Test", source);
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    for (BodyDeclaration body : decl.getBodyDeclarations()) {
        if (body instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) body;
            if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
                // The method's return type isn't compound, but the cast expression in
                // its return statement is.
                ReturnStatement stmt = (ReturnStatement) method.getBody().getStatements().get(0);
                TypeMirror mirror = stmt.getExpression().getTypeMirror();
                String typeName = unit.getEnv().nameTable().getObjCType(mirror);
                assertEquals("id<FooBarTest, JavaIoSerializable>", typeName);
                return;
            }
        }
    }
    fail("thenTesting method not found");
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TypeMirror(javax.lang.model.type.TypeMirror) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 5 with ReturnStatement

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

the class PackageInfoRewriter method createPrefixMethod.

private MethodDeclaration createPrefixMethod(String prefix, TypeElement type) {
    ExecutableElement element = GeneratedExecutableElement.newMethodWithSelector("__prefix", typeUtil.getJavaString().asType(), type).addModifiers(Modifier.STATIC);
    MethodDeclaration method = new MethodDeclaration(element);
    method.setHasDeclaration(false);
    Block body = new Block();
    method.setBody(body);
    body.addStatement(new ReturnStatement(new StringLiteral(prefix, typeUtil)));
    return method;
}
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

ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)8 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)7 Block (com.google.devtools.j2objc.ast.Block)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)5 ExecutableElement (javax.lang.model.element.ExecutableElement)5 TypeMirror (javax.lang.model.type.TypeMirror)4 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)2 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)2 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)2 StringLiteral (com.google.devtools.j2objc.ast.StringLiteral)2 TypeElement (javax.lang.model.element.TypeElement)2 AnnotationTypeMemberDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration)1 Expression (com.google.devtools.j2objc.ast.Expression)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)1 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)1 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)1 SimpleName (com.google.devtools.j2objc.ast.SimpleName)1 Statement (com.google.devtools.j2objc.ast.Statement)1