Search in sources :

Example 11 with MethodDeclaration

use of com.google.devtools.j2objc.ast.MethodDeclaration 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);
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) 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) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 12 with MethodDeclaration

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

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

Example 14 with MethodDeclaration

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

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

the class TreeConverter method convertAbstractTypeDeclaration.

private static TreeNode convertAbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration node, AbstractTypeDeclaration newNode) {
    convertBodyDeclaration(node, newNode);
    ITypeBinding typeBinding = node.resolveBinding();
    Set<IMethodBinding> declaredInAst = new HashSet<>();
    for (Object bodyDecl : node.bodyDeclarations()) {
        if (bodyDecl instanceof org.eclipse.jdt.core.dom.MethodDeclaration) {
            declaredInAst.add(((org.eclipse.jdt.core.dom.MethodDeclaration) bodyDecl).resolveBinding());
        }
        newNode.addBodyDeclaration((BodyDeclaration) convert(bodyDecl));
    }
    for (IMethodBinding method : typeBinding.getDeclaredMethods()) {
        if (method.isConstructor() && method.getParameterTypes().length == 0 && !declaredInAst.contains(method)) {
            MethodDeclaration defaultConstructor = new MethodDeclaration(BindingConverter.getExecutableElement(method)).setBody(new Block());
            addImplicitSuperCall(defaultConstructor);
            newNode.addBodyDeclaration(0, defaultConstructor);
            break;
        }
    }
    return newNode.setName((SimpleName) convert(node.getName())).setTypeElement(BindingConverter.getTypeElement(typeBinding));
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Block(com.google.devtools.j2objc.ast.Block) HashSet(java.util.HashSet)

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