Search in sources :

Example 21 with MethodDeclaration

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

the class TypeDeclarationGeneratorTest method testSortMethods.

public void testSortMethods() throws IOException {
    String source = "class A {" + "void zebra() {}" + "void gnu(String s, int i, Runnable r) {}" + "A(int i) {}" + "void gnu() {}" + "void gnu(int i, Runnable r) {}" + "void yak() {}" + "A(String s) {}" + "A() {}" + "A(int i, Runnable r) {}" + "void gnu(String s, int i) {}}";
    CompilationUnit unit = translateType("A", source);
    final ArrayList<MethodDeclaration> methods = new ArrayList<>();
    unit.accept(new TreeVisitor() {

        @Override
        public void endVisit(MethodDeclaration node) {
            if (!ElementUtil.isSynthetic(node.getExecutableElement())) {
                methods.add(node);
            }
        }
    });
    Collections.sort(methods, TypeDeclarationGenerator.METHOD_DECL_ORDER);
    assertTrue(methods.get(0).toString().startsWith("<init>()"));
    assertTrue(methods.get(1).toString().startsWith("<init>(int i)"));
    assertTrue(methods.get(2).toString().startsWith("<init>(int i,java.lang.Runnable r)"));
    assertTrue(methods.get(3).toString().startsWith("<init>(java.lang.String s)"));
    assertTrue(methods.get(4).toString().startsWith("void gnu()"));
    assertTrue(methods.get(5).toString().startsWith("void gnu(int i,java.lang.Runnable r)"));
    assertTrue(methods.get(6).toString().startsWith("void gnu(java.lang.String s,int i)"));
    assertTrue(methods.get(7).toString().startsWith("void gnu(java.lang.String s,int i,java.lang.Runnable r)"));
    assertTrue(methods.get(8).toString().startsWith("void yak()"));
    assertTrue(methods.get(9).toString().startsWith("void zebra()"));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ArrayList(java.util.ArrayList)

Example 22 with MethodDeclaration

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

Example 23 with MethodDeclaration

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

the class Functionizer method addDisallowedConstructors.

/**
   * Declare any inherited constructors that aren't allowed to be accessed in Java
   * with a NS_UNAVAILABLE macro, so that clang will flag such access from native
   * code as an error.
   */
private void addDisallowedConstructors(TypeDeclaration node) {
    TypeElement typeElement = node.getTypeElement();
    TypeElement superClass = ElementUtil.getSuperclass(typeElement);
    if (ElementUtil.isPrivateInnerType(typeElement) || ElementUtil.isAbstract(typeElement) || superClass == null) {
        return;
    }
    Set<String> constructors = new HashSet<>();
    for (ExecutableElement constructor : ElementUtil.getConstructors(typeElement)) {
        constructors.add(nameTable.getMethodSelector(constructor));
    }
    Map<String, ExecutableElement> inheritedConstructors = new HashMap<>();
    // Add super constructors that have unique parameter lists.
    for (ExecutableElement superC : ElementUtil.getConstructors(superClass)) {
        if (ElementUtil.isPrivate(superC)) {
            // Skip private super constructors since they're already unavailable.
            continue;
        }
        String selector = nameTable.getMethodSelector(superC);
        if (!constructors.contains(selector)) {
            inheritedConstructors.put(selector, superC);
        }
    }
    for (Map.Entry<String, ExecutableElement> entry : inheritedConstructors.entrySet()) {
        ExecutableElement oldConstructor = entry.getValue();
        GeneratedExecutableElement newConstructor = GeneratedExecutableElement.newConstructorWithSelector(entry.getKey(), typeElement, typeUtil);
        MethodDeclaration decl = new MethodDeclaration(newConstructor).setUnavailable(true);
        decl.addModifiers(Modifier.ABSTRACT);
        int count = 0;
        for (VariableElement param : oldConstructor.getParameters()) {
            VariableElement newParam = GeneratedVariableElement.newParameter("arg" + count++, param.asType(), newConstructor);
            newConstructor.addParameter(newParam);
            decl.addParameter(new SingleVariableDeclaration(newParam));
        }
        addImplicitParameters(decl, ElementUtil.getDeclaringClass(oldConstructor));
        node.addBodyDeclaration(decl);
    }
}
Also used : HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) 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) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 24 with MethodDeclaration

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

the class MetadataWriter method visitType.

private void visitType(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    if (!translationUtil.needsReflection(type)) {
        return;
    }
    ExecutableElement metadataElement = GeneratedExecutableElement.newMethodWithSelector("__metadata", CLASS_INFO_TYPE, type).addModifiers(Modifier.STATIC, Modifier.PRIVATE);
    MethodDeclaration metadataDecl = new MethodDeclaration(metadataElement);
    metadataDecl.setHasDeclaration(false);
    Block body = new Block();
    metadataDecl.setBody(body);
    new MetadataGenerator(node, body.getStatements()).generateClassMetadata();
    node.addBodyDeclaration(metadataDecl);
}
Also used : GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) 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) Block(com.google.devtools.j2objc.ast.Block)

Example 25 with MethodDeclaration

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

the class DeadCodeEliminator method removeDeadMethods.

/**
   * Remove dead methods from a type's body declarations.
   */
private void removeDeadMethods(String clazz, List<BodyDeclaration> declarations) {
    Iterator<BodyDeclaration> declarationsIter = declarations.iterator();
    while (declarationsIter.hasNext()) {
        BodyDeclaration declaration = declarationsIter.next();
        if (declaration instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) declaration;
            // TODO(kstanger): Remove the method and its OCNI comment.
            if (Modifier.isNative(method.getModifiers())) {
                continue;
            }
            ExecutableElement elem = method.getExecutableElement();
            String name = typeUtil.getReferenceName(elem);
            String signature = typeUtil.getReferenceSignature(elem);
            if (deadCodeMap.containsMethod(clazz, name, signature)) {
                if (method.isConstructor()) {
                    deadCodeMap.addConstructorRemovedClass(clazz);
                }
                declarationsIter.remove();
            }
        }
    }
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration)

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