Search in sources :

Example 11 with BodyDeclaration

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

the class Rewriter method endVisit.

@Override
public void endVisit(FieldDeclaration node) {
    if (options.isJDT()) {
        ListMultimap<Integer, VariableDeclarationFragment> newDeclarations = rewriteExtraDimensions(node.getFragments());
        if (newDeclarations != null) {
            List<BodyDeclaration> bodyDecls = TreeUtil.asDeclarationSublist(node);
            for (Integer dimensions : newDeclarations.keySet()) {
                List<VariableDeclarationFragment> fragments = newDeclarations.get(dimensions);
                FieldDeclaration newDecl = new FieldDeclaration(fragments.get(0));
                newDecl.getFragments().addAll(fragments.subList(1, fragments.size()));
                bodyDecls.add(newDecl);
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 12 with BodyDeclaration

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

the class OcniExtractor method visitType.

private void visitType(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    Set<String> methodsPrinted = Sets.newHashSet();
    List<BodyDeclaration> bodyDeclarations = node.getBodyDeclarations();
    int minPos = 0;
    int declIdx = 0;
    for (Comment comment : blockComments.get(node)) {
        int commentPos = comment.getStartPosition();
        while (declIdx < bodyDeclarations.size()) {
            BodyDeclaration decl = bodyDeclarations.get(declIdx);
            if (decl.getStartPosition() > commentPos) {
                break;
            }
            minPos = Math.max(minPos, decl.getStartPosition() + decl.getLength());
            declIdx++;
        }
        if (commentPos > minPos) {
            NativeDeclaration nativeDecl = extractNativeDeclaration(comment);
            if (nativeDecl != null) {
                findMethodSignatures(nativeDecl.getImplementationCode(), methodsPrinted);
                bodyDeclarations.add(declIdx++, nativeDecl);
            }
        }
    }
    // methods are always live.
    if (typeUtil.findSupertype(type.asType(), "java.lang.Iterable") != null && !methodsPrinted.contains("countByEnumeratingWithState:objects:count:") && (deadCodeMap == null || !deadCodeMap.containsClass(type, elementUtil))) {
        bodyDeclarations.add(NativeDeclaration.newInnerDeclaration(null, "- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state " + "objects:(__unsafe_unretained id *)stackbuf count:(NSUInteger)len {\n" + "  return JreDefaultFastEnumeration(self, state, stackbuf, len);\n}\n"));
    }
}
Also used : Comment(com.google.devtools.j2objc.ast.Comment) NativeDeclaration(com.google.devtools.j2objc.ast.NativeDeclaration) TypeElement(javax.lang.model.element.TypeElement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration)

Example 13 with BodyDeclaration

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

BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)13 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)4 TypeElement (javax.lang.model.element.TypeElement)4 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)3 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)2 Block (com.google.devtools.j2objc.ast.Block)2 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)2 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)2 Initializer (com.google.devtools.j2objc.ast.Initializer)2 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)2 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)2 JCTree (com.sun.tools.javac.tree.JCTree)2 VariableElement (javax.lang.model.element.VariableElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 AnnotationTypeMemberDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeMemberDeclaration)1 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)1 Comment (com.google.devtools.j2objc.ast.Comment)1 ContinueStatement (com.google.devtools.j2objc.ast.ContinueStatement)1