Search in sources :

Example 86 with ExecutableElement

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

the class Functionizer method makeFunction.

/**
   * Create an equivalent function declaration for a given method.
   */
private FunctionDeclaration makeFunction(MethodDeclaration method) {
    ExecutableElement elem = method.getExecutableElement();
    TypeElement declaringClass = ElementUtil.getDeclaringClass(elem);
    boolean isInstanceMethod = !ElementUtil.isStatic(elem) && !ElementUtil.isConstructor(elem);
    FunctionDeclaration function = new FunctionDeclaration(nameTable.getFullFunctionName(elem), elem.getReturnType());
    function.setJniSignature(signatureGenerator.createJniFunctionSignature(elem));
    function.setLineNumber(method.getLineNumber());
    if (!ElementUtil.isStatic(elem)) {
        VariableElement var = GeneratedVariableElement.newParameter(NameTable.SELF_NAME, declaringClass.asType(), null);
        function.addParameter(new SingleVariableDeclaration(var));
    }
    TreeUtil.copyList(method.getParameters(), function.getParameters());
    function.setModifiers(method.getModifiers() & Modifier.STATIC);
    if (ElementUtil.isPrivate(elem) || (isInstanceMethod && !ElementUtil.isDefault(elem))) {
        function.addModifiers(Modifier.PRIVATE);
    } else {
        function.addModifiers(Modifier.PUBLIC);
    }
    if (Modifier.isNative(method.getModifiers())) {
        function.addModifiers(Modifier.NATIVE);
        return function;
    }
    function.setBody(TreeUtil.remove(method.getBody()));
    if (ElementUtil.isStatic(elem)) {
        // Add class initialization invocation, since this may be the first use of this class.
        String initName = UnicodeUtils.format("%s_initialize", nameTable.getFullName(declaringClass));
        TypeMirror voidType = typeUtil.getVoid();
        FunctionElement initElement = new FunctionElement(initName, voidType, declaringClass);
        FunctionInvocation initCall = new FunctionInvocation(initElement, voidType);
        function.getBody().addStatement(0, new ExpressionStatement(initCall));
    } else {
        FunctionConverter.convert(function);
    }
    return function;
}
Also used : FunctionDeclaration(com.google.devtools.j2objc.ast.FunctionDeclaration) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 87 with ExecutableElement

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

the class Functionizer method endVisit.

@Override
public void endVisit(ClassInstanceCreation node) {
    ExecutableElement element = node.getExecutableElement();
    TypeElement type = ElementUtil.getDeclaringClass(element);
    FunctionElement funcElement = newAllocatingConstructorElement(element);
    FunctionInvocation invocation = new FunctionInvocation(funcElement, node.getTypeMirror());
    invocation.setHasRetainedResult(node.hasRetainedResult() || options.useARC());
    List<Expression> args = invocation.getArguments();
    Expression outerExpr = node.getExpression();
    if (outerExpr != null) {
        args.add(TreeUtil.remove(outerExpr));
    } else if (captureInfo.needsOuterParam(type)) {
        args.add(new ThisExpression(ElementUtil.getDeclaringClass(type).asType()));
    }
    Expression superOuterArg = node.getSuperOuterArg();
    if (superOuterArg != null) {
        args.add(TreeUtil.remove(superOuterArg));
    }
    TreeUtil.moveList(node.getCaptureArgs(), args);
    TreeUtil.moveList(node.getArguments(), args);
    node.replaceWith(invocation);
    assert funcElement.getParameterTypes().size() == args.size();
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 88 with ExecutableElement

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

the class Functionizer method determineFunctionizableMethods.

/**
   * Determines the set of methods to functionize. In addition to a method being
   * final we must also find an invocation for that method. Static methods, though,
   * are always functionized since there are no dynamic dispatch issues.
   */
private Set<ExecutableElement> determineFunctionizableMethods(final CompilationUnit unit) {
    final Set<ExecutableElement> functionizableDeclarations = Sets.newHashSet();
    final Set<ExecutableElement> invocations = Sets.newHashSet();
    unit.accept(new TreeVisitor() {

        @Override
        public void endVisit(MethodDeclaration node) {
            if (canFunctionize(node)) {
                functionizableDeclarations.add(node.getExecutableElement());
            }
        }

        @Override
        public void endVisit(MethodInvocation node) {
            invocations.add(node.getExecutableElement());
        }
    });
    return Sets.intersection(functionizableDeclarations, invocations);
}
Also used : TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) UnitTreeVisitor(com.google.devtools.j2objc.ast.UnitTreeVisitor) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation)

Example 89 with ExecutableElement

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

the class Functionizer method endVisit.

@Override
public void endVisit(SuperConstructorInvocation node) {
    ExecutableElement element = node.getExecutableElement();
    AbstractTypeDeclaration typeDecl = TreeUtil.getEnclosingType(node);
    TypeElement type = typeDecl.getTypeElement();
    FunctionElement funcElement = newFunctionElement(element);
    FunctionInvocation invocation = new FunctionInvocation(funcElement, typeUtil.getVoid());
    List<Expression> args = invocation.getArguments();
    args.add(new ThisExpression(ElementUtil.getDeclaringClass(element).asType()));
    if (typeDecl instanceof TypeDeclaration) {
        TypeDeclaration typeDeclaration = (TypeDeclaration) typeDecl;
        if (captureInfo.needsOuterParam(ElementUtil.getSuperclass(type))) {
            Expression outerArg = TreeUtil.remove(node.getExpression());
            args.add(outerArg != null ? outerArg : typeDeclaration.getSuperOuter().copy());
        }
        TreeUtil.moveList(typeDeclaration.getSuperCaptureArgs(), args);
    }
    TreeUtil.moveList(node.getArguments(), args);
    if (ElementUtil.isEnum(type)) {
        for (VariableElement param : captureInfo.getImplicitEnumParams()) {
            args.add(new SimpleName(param));
        }
    }
    node.replaceWith(new ExpressionStatement(invocation));
    assert funcElement.getParameterTypes().size() == args.size();
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) AnnotationTypeDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 90 with ExecutableElement

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

the class Functionizer method makeAllocatingConstructor.

/**
   * Create a wrapper for a constructor that does the object allocation.
   */
private FunctionDeclaration makeAllocatingConstructor(MethodDeclaration method, boolean releasing) {
    assert method.isConstructor();
    ExecutableElement element = method.getExecutableElement();
    TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
    String name = releasing ? nameTable.getReleasingConstructorName(element) : nameTable.getAllocatingConstructorName(element);
    FunctionDeclaration function = new FunctionDeclaration(name, declaringClass.asType());
    function.setLineNumber(method.getLineNumber());
    function.setModifiers(ElementUtil.isPrivate(element) ? Modifier.PRIVATE : Modifier.PUBLIC);
    function.setReturnsRetained(!releasing);
    TreeUtil.copyList(method.getParameters(), function.getParameters());
    Block body = new Block();
    function.setBody(body);
    StringBuilder sb = new StringBuilder(releasing ? "J2OBJC_CREATE_IMPL(" : "J2OBJC_NEW_IMPL(");
    sb.append(nameTable.getFullName(declaringClass));
    sb.append(", ").append(nameTable.getFunctionName(element));
    for (SingleVariableDeclaration param : function.getParameters()) {
        sb.append(", ").append(nameTable.getVariableQualifiedName(param.getVariableElement()));
    }
    sb.append(")");
    body.addStatement(new NativeStatement(sb.toString()));
    return function;
}
Also used : FunctionDeclaration(com.google.devtools.j2objc.ast.FunctionDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeElement(javax.lang.model.element.TypeElement) 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)

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