Example usage for org.eclipse.jdt.internal.compiler.ast Initializer traverse

List of usage examples for org.eclipse.jdt.internal.compiler.ast Initializer traverse

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast Initializer traverse.

Prototype

@Override
    public void traverse(ASTVisitor visitor, MethodScope scope) 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.util.ASTNodeFinder.java

License:Open Source License

public TypeDeclaration findType(IType typeHandle) {
    IJavaElement parent = typeHandle.getParent();
    final char[] typeName = typeHandle.getElementName().toCharArray();
    final int occurenceCount = ((SourceType) typeHandle).occurrenceCount;
    final boolean findAnonymous = typeName.length == 0;
    class Visitor extends ASTVisitor {
        TypeDeclaration result;//from  w  ww . j a  v  a  2 s.c om
        int count = 0;

        public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) {
            if (this.result != null)
                return false;
            if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
                if (findAnonymous && ++this.count == occurenceCount) {
                    this.result = typeDeclaration;
                }
            } else {
                if (!findAnonymous && CharOperation.equals(typeName, typeDeclaration.name)) {
                    this.result = typeDeclaration;
                }
            }
            return false; // visit only one level
        }
    }
    switch (parent.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        TypeDeclaration[] types = this.unit.types;
        if (types != null) {
            for (int i = 0, length = types.length; i < length; i++) {
                TypeDeclaration type = types[i];
                if (CharOperation.equals(typeName, type.name)) {
                    return type;
                }
            }
        }
        break;
    case IJavaElement.TYPE:
        TypeDeclaration parentDecl = findType((IType) parent);
        if (parentDecl == null)
            return null;
        types = parentDecl.memberTypes;
        if (types != null) {
            for (int i = 0, length = types.length; i < length; i++) {
                TypeDeclaration type = types[i];
                if (CharOperation.equals(typeName, type.name)) {
                    return type;
                }
            }
        }
        break;
    case IJavaElement.FIELD:
        FieldDeclaration fieldDecl = findField((IField) parent);
        if (fieldDecl == null)
            return null;
        Visitor visitor = new Visitor();
        fieldDecl.traverse(visitor, null);
        return visitor.result;
    case IJavaElement.INITIALIZER:
        Initializer initializer = findInitializer((IInitializer) parent);
        if (initializer == null)
            return null;
        visitor = new Visitor();
        initializer.traverse(visitor, null);
        return visitor.result;
    case IJavaElement.METHOD:
        AbstractMethodDeclaration methodDecl = findMethod((IMethod) parent);
        if (methodDecl == null)
            return null;
        visitor = new Visitor();
        methodDecl.traverse(visitor, (ClassScope) null);
        return visitor.result;
    }
    return null;
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void recoverStatements() {
    class MethodVisitor extends ASTVisitor {
        public ASTVisitor typeVisitor;

        TypeDeclaration enclosingType; // used only for initializer

        TypeDeclaration[] types = new TypeDeclaration[0];
        int typePtr = -1;

        public void endVisit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
            endVisitMethod(constructorDeclaration, scope);
        }// ww w . jav  a  2 s  .c  o  m

        public void endVisit(Initializer initializer, MethodScope scope) {
            if (initializer.block == null)
                return;
            TypeDeclaration[] foundTypes = null;
            int length = 0;
            if (this.typePtr > -1) {
                length = this.typePtr + 1;
                foundTypes = new TypeDeclaration[length];
                System.arraycopy(this.types, 0, foundTypes, 0, length);
            }
            ReferenceContext oldContext = Parser.this.referenceContext;
            Parser.this.recoveryScanner.resetTo(initializer.bodyStart, initializer.bodyEnd);
            Scanner oldScanner = Parser.this.scanner;
            Parser.this.scanner = Parser.this.recoveryScanner;
            parseStatements(this.enclosingType, initializer.bodyStart, initializer.bodyEnd, foundTypes,
                    Parser.this.compilationUnit);
            Parser.this.scanner = oldScanner;
            Parser.this.referenceContext = oldContext;

            for (int i = 0; i < length; i++) {
                foundTypes[i].traverse(this.typeVisitor, scope);
            }
        }

        public void endVisit(MethodDeclaration methodDeclaration, ClassScope scope) {
            endVisitMethod(methodDeclaration, scope);
        }

        private void endVisitMethod(AbstractMethodDeclaration methodDeclaration, ClassScope scope) {
            TypeDeclaration[] foundTypes = null;
            int length = 0;
            if (this.typePtr > -1) {
                length = this.typePtr + 1;
                foundTypes = new TypeDeclaration[length];
                System.arraycopy(this.types, 0, foundTypes, 0, length);
            }
            ReferenceContext oldContext = Parser.this.referenceContext;
            Parser.this.recoveryScanner.resetTo(methodDeclaration.bodyStart, methodDeclaration.bodyEnd);
            Scanner oldScanner = Parser.this.scanner;
            Parser.this.scanner = Parser.this.recoveryScanner;
            parseStatements(methodDeclaration, methodDeclaration.bodyStart, methodDeclaration.bodyEnd,
                    foundTypes, Parser.this.compilationUnit);
            Parser.this.scanner = oldScanner;
            Parser.this.referenceContext = oldContext;

            for (int i = 0; i < length; i++) {
                foundTypes[i].traverse(this.typeVisitor, scope);
            }
        }

        public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
            this.typePtr = -1;
            return true;
        }

        public boolean visit(Initializer initializer, MethodScope scope) {
            this.typePtr = -1;
            if (initializer.block == null)
                return false;
            return true;
        }

        public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
            this.typePtr = -1;
            return true;
        }

        private boolean visit(TypeDeclaration typeDeclaration) {
            if (this.types.length <= ++this.typePtr) {
                int length = this.typePtr;
                System.arraycopy(this.types, 0, this.types = new TypeDeclaration[length * 2 + 1], 0, length);
            }
            this.types[this.typePtr] = typeDeclaration;
            return false;
        }

        public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) {
            return this.visit(typeDeclaration);
        }

        public boolean visit(TypeDeclaration typeDeclaration, ClassScope scope) {
            return this.visit(typeDeclaration);
        }
    }
    class TypeVisitor extends ASTVisitor {
        public MethodVisitor methodVisitor;

        TypeDeclaration[] types = new TypeDeclaration[0];
        int typePtr = -1;

        public void endVisit(TypeDeclaration typeDeclaration, BlockScope scope) {
            endVisitType();
        }

        public void endVisit(TypeDeclaration typeDeclaration, ClassScope scope) {
            endVisitType();
        }

        private void endVisitType() {
            this.typePtr--;
        }

        public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
            if (constructorDeclaration.isDefaultConstructor())
                return false;

            constructorDeclaration.traverse(this.methodVisitor, scope);
            return false;
        }

        public boolean visit(Initializer initializer, MethodScope scope) {
            if (initializer.block == null)
                return false;
            this.methodVisitor.enclosingType = this.types[this.typePtr];
            initializer.traverse(this.methodVisitor, scope);
            return false;
        }

        public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
            methodDeclaration.traverse(this.methodVisitor, scope);
            return false;
        }

        private boolean visit(TypeDeclaration typeDeclaration) {
            if (this.types.length <= ++this.typePtr) {
                int length = this.typePtr;
                System.arraycopy(this.types, 0, this.types = new TypeDeclaration[length * 2 + 1], 0, length);
            }
            this.types[this.typePtr] = typeDeclaration;
            return true;
        }

        public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) {
            return this.visit(typeDeclaration);
        }

        public boolean visit(TypeDeclaration typeDeclaration, ClassScope scope) {
            return this.visit(typeDeclaration);
        }
    }

    MethodVisitor methodVisitor = new MethodVisitor();
    TypeVisitor typeVisitor = new TypeVisitor();
    methodVisitor.typeVisitor = typeVisitor;
    typeVisitor.methodVisitor = methodVisitor;

    if (this.referenceContext instanceof AbstractMethodDeclaration) {
        ((AbstractMethodDeclaration) this.referenceContext).traverse(methodVisitor, (ClassScope) null);
    } else if (this.referenceContext instanceof TypeDeclaration) {
        TypeDeclaration typeContext = (TypeDeclaration) this.referenceContext;

        int length = typeContext.fields.length;
        for (int i = 0; i < length; i++) {
            final FieldDeclaration fieldDeclaration = typeContext.fields[i];
            switch (fieldDeclaration.getKind()) {
            case AbstractVariableDeclaration.INITIALIZER:
                Initializer initializer = (Initializer) fieldDeclaration;
                if (initializer.block == null)
                    break;
                methodVisitor.enclosingType = typeContext;
                initializer.traverse(methodVisitor, (MethodScope) null);
                break;
            }
        }
    }
}