Search in sources :

Example 91 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class Functionizer method endVisit.

@Override
public void endVisit(MethodDeclaration node) {
    ExecutableElement element = node.getExecutableElement();
    // they are added by the translator and need to remain in method form.
    if (!node.hasDeclaration()) {
        return;
    }
    boolean isConstructor = ElementUtil.isConstructor(element);
    boolean isInstanceMethod = !ElementUtil.isStatic(element) && !isConstructor;
    boolean isDefaultMethod = ElementUtil.isDefault(element);
    List<BodyDeclaration> declarationList = TreeUtil.asDeclarationSublist(node);
    if (!isInstanceMethod || isDefaultMethod || Modifier.isNative(node.getModifiers()) || functionizableMethods.contains(element)) {
        TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
        boolean isEnumConstructor = isConstructor && ElementUtil.isEnum(declaringClass);
        if (isConstructor) {
            addImplicitParameters(node, declaringClass);
        }
        FunctionDeclaration function = makeFunction(node);
        declarationList.add(function);
        if (isConstructor && !ElementUtil.isAbstract(declaringClass) && !isEnumConstructor) {
            declarationList.add(makeAllocatingConstructor(node, false));
            declarationList.add(makeAllocatingConstructor(node, true));
        } else if (isEnumConstructor && options.useARC()) {
            // Enums with ARC need the retaining constructor.
            declarationList.add(makeAllocatingConstructor(node, false));
        }
        // Instance methods must be kept in case they are invoked using "super".
        boolean keepMethod = isInstanceMethod || // Public methods must be kept for the public API.
        !(ElementUtil.isPrivateInnerType(declaringClass) || ElementUtil.isPrivate(element)) || // Methods must be kept for reflection if enabled.
        (translationUtil.needsReflection(declaringClass) && !isEnumConstructor);
        if (keepMethod) {
            if (isDefaultMethod) {
                // For default methods keep only the declaration. Implementing classes will add a shim.
                node.setBody(null);
                node.addModifiers(Modifier.ABSTRACT);
            } else {
                setFunctionCaller(node, element);
            }
        } else {
            node.remove();
        }
        ErrorUtil.functionizedMethod();
    }
}
Also used : FunctionDeclaration(com.google.devtools.j2objc.ast.FunctionDeclaration) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration)

Example 92 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class Functionizer method endVisit.

@Override
public void endVisit(SuperMethodInvocation node) {
    ExecutableElement element = node.getExecutableElement();
    // Yes, super method invocations can be static.
    if (!ElementUtil.isStatic(element)) {
        return;
    }
    FunctionInvocation functionInvocation = new FunctionInvocation(newFunctionElement(element), node.getTypeMirror());
    TreeUtil.moveList(node.getArguments(), functionInvocation.getArguments());
    node.replaceWith(functionInvocation);
}
Also used : FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 93 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project j2objc by google.

the class DestructorGenerator method addDeallocMethod.

private void addDeallocMethod(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    boolean hasFinalize = hasFinalizeMethod(type);
    List<Statement> releaseStatements = createReleaseStatements(node);
    if (releaseStatements.isEmpty() && !hasFinalize) {
        return;
    }
    ExecutableElement deallocElement = GeneratedExecutableElement.newMethodWithSelector(NameTable.DEALLOC_METHOD, typeUtil.getVoid(), type).addModifiers(Modifier.PUBLIC);
    MethodDeclaration deallocDecl = new MethodDeclaration(deallocElement);
    deallocDecl.setHasDeclaration(false);
    Block block = new Block();
    deallocDecl.setBody(block);
    List<Statement> stmts = block.getStatements();
    if (hasFinalize) {
        String clsName = nameTable.getFullName(type);
        stmts.add(new NativeStatement("JreCheckFinalize(self, [" + clsName + " class]);"));
    }
    stmts.addAll(releaseStatements);
    if (options.useReferenceCounting()) {
        stmts.add(new ExpressionStatement(new SuperMethodInvocation(new ExecutablePair(superDeallocElement))));
    }
    node.addBodyDeclaration(deallocDecl);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) 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) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation)

Example 94 with ExecutableElement

use of javax.lang.model.element.ExecutableElement 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 95 with ExecutableElement

use of javax.lang.model.element.ExecutableElement 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)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)345 TypeElement (javax.lang.model.element.TypeElement)158 TypeMirror (javax.lang.model.type.TypeMirror)97 VariableElement (javax.lang.model.element.VariableElement)85 Element (javax.lang.model.element.Element)72 Test (org.junit.Test)41 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)32 DeclaredType (javax.lang.model.type.DeclaredType)31 ArrayList (java.util.ArrayList)26 JBlock (com.helger.jcodemodel.JBlock)25 JInvocation (com.helger.jcodemodel.JInvocation)20 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)18 JVar (com.helger.jcodemodel.JVar)18 Map (java.util.Map)18 HashSet (java.util.HashSet)17 IJExpression (com.helger.jcodemodel.IJExpression)16 ElementValidation (org.androidannotations.ElementValidation)15 Block (com.google.devtools.j2objc.ast.Block)13 JMethod (com.helger.jcodemodel.JMethod)13 Elements (javax.lang.model.util.Elements)13