Search in sources :

Example 6 with MethodDeclaration

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

the class NameTableTest method testTypeVariableWithTypeVariableBounds.

public void testTypeVariableWithTypeVariableBounds() {
    String source = "class A<T> { <E extends T> void foo(E e) {} }";
    CompilationUnit unit = translateType("A", source);
    NameTable nameTable = unit.getEnv().nameTable();
    final ExecutableElement[] methodElement = new ExecutableElement[1];
    unit.accept(new TreeVisitor() {

        @Override
        public void endVisit(MethodDeclaration node) {
            ExecutableElement element = node.getExecutableElement();
            if (ElementUtil.getName(element).equals("foo")) {
                methodElement[0] = element;
            }
        }
    });
    assertNotNull(methodElement[0]);
    TypeMirror paramType = methodElement[0].getParameters().get(0).asType();
    assertEquals("id", nameTable.getObjCType(paramType));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) TypeMirror(javax.lang.model.type.TypeMirror) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 7 with MethodDeclaration

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

the class EnumRewriter method addValueOfMethod.

private void addValueOfMethod(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    ExecutableElement method = ElementUtil.findMethod(type, "valueOf", "java.lang.String");
    assert method != null : "Can't find valueOf method on enum type.";
    String typeName = nameTable.getFullName(type);
    int numConstants = node.getEnumConstants().size();
    VariableElement nameParam = GeneratedVariableElement.newParameter("name", method.getParameters().get(0).asType(), method);
    MethodDeclaration methodDecl = new MethodDeclaration(method);
    methodDecl.addParameter(new SingleVariableDeclaration(nameParam));
    Block body = new Block();
    methodDecl.setBody(body);
    StringBuilder impl = new StringBuilder();
    if (numConstants > 0) {
        impl.append(UnicodeUtils.format("  for (int i = 0; i < %s; i++) {\n" + "    %s *e = %s_values_[i];\n" + "    if ([name isEqual:[e name]]) {\n" + "      return e;\n" + "    }\n" + "  }\n", numConstants, typeName, typeName));
    }
    impl.append("  @throw create_JavaLangIllegalArgumentException_initWithNSString_(name);\n" + "  return nil;");
    body.addStatement(new NativeStatement(impl.toString()));
    node.addBodyDeclaration(methodDecl);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) 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 8 with MethodDeclaration

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

the class EnumRewriter method addValuesMethod.

private void addValuesMethod(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    ExecutableElement method = ElementUtil.findMethod(type, "values");
    assert method != null : "Can't find values method on enum type.";
    String typeName = nameTable.getFullName(type);
    MethodDeclaration methodDecl = new MethodDeclaration(method);
    Block body = new Block();
    methodDecl.setBody(body);
    body.addStatement(new NativeStatement(UnicodeUtils.format("  return [IOSObjectArray arrayWithObjects:%s_values_ count:%s type:%s_class_()];", typeName, node.getEnumConstants().size(), typeName)));
    node.addBodyDeclaration(methodDecl);
}
Also used : NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block)

Example 9 with MethodDeclaration

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

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

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