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

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

Introduction

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

Prototype

@Override
    public abstract void traverse(ASTVisitor visitor, ClassScope scope);

Source Link

Usage

From source file:com.google.gwt.dev.javac.UnusedImportsRemover.java

License:Apache License

void execImpl(CompilationUnitDeclaration cud) {
    if (cud.imports == null) {
        return;//from   ww  w  .j  a v a2s . c om
    }

    AccumulateNamesVisitor astVisitor = new AccumulateNamesVisitor();

    if (cud.types != null) {
        for (TypeDeclaration typeDecl : cud.types) {
            typeDecl.traverse(astVisitor, cud.scope);
        }
    }

    // for some reason JDT does not traverse package annotations even if the traversal started at
    // the Compilation unit declaration. Hence we do it manually.
    if (cud.currentPackage != null && cud.currentPackage.annotations != null) {
        for (Annotation annotation : cud.currentPackage.annotations) {
            annotation.traverse(astVisitor, (BlockScope) null);
        }
    }

    List<ImportReference> newImports = new ArrayList<ImportReference>();
    for (ImportReference importRef : cud.imports) {
        String importName = new String(importRef.getImportName()[importRef.getImportName().length - 1]);
        if (importName.equals("*") ||
        // very hacky it seems that this is the only way
        // to notice a import static blah.Blah.*;
                importRef.trailingStarPosition > 0 || usedNames.contains(importName)) {
            // Either a * or a possible reference, so keep it.
            newImports.add(importRef);
        }
    }
    if (newImports.size() != cud.imports.length) {
        cud.imports = newImports.toArray(new ImportReference[newImports.size()]);
    }
}

From source file:spoon.support.builder.JDTTreeBuilder.java

License:Open Source License

@SuppressWarnings("unchecked")
public boolean visit(Argument argument, BlockScope scope) {
    CtParameter<?> p = factory.Core().createParameter();
    p.setSimpleName(new String(argument.name));
    p.setVarArgs(argument.isVarArgs());//from   w ww. ja v a2s  .  c o m
    p.setModifiers(getModifier(argument.modifiers));
    if (argument.type != null)
        p.setType(references.getTypeReference(argument.type.resolvedType));
    context.enter(p, argument);
    if (argument.initialization != null)
        argument.initialization.traverse(this, scope);

    if (argument.annotations != null)
        for (Annotation a : argument.annotations)
            a.traverse(this, scope);
    return false;
}

From source file:spoon.support.builder.JDTTreeBuilder.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override// ww  w  .  ja  v  a  2 s  .com
public boolean visit(LocalDeclaration localDeclaration, BlockScope scope) {
    CtLocalVariable<?> v = factory.Core().createLocalVariable();
    v.setSimpleName(new String(localDeclaration.name));
    v.setType(references.getTypeReference(localDeclaration.type.resolvedType));
    v.setModifiers(getModifier(localDeclaration.modifiers));
    context.enter(v, localDeclaration);

    if (localDeclaration.initialization != null) {
        context.arguments.push(v);
        localDeclaration.initialization.traverse(this, scope);
        context.arguments.pop();
    }

    if (localDeclaration.annotations != null)
        for (Annotation a : localDeclaration.annotations)
            a.traverse(this, scope);

    return false;
}

From source file:spoon.support.builder.JDTTreeBuilder.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   w  ww.ja va2s .  com*/
public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
    CtMethod<?> m = factory.Core().createMethod();
    m.setSimpleName(new String(methodDeclaration.selector));
    m.setType(references.getTypeReference(methodDeclaration.returnType.resolvedType));
    m.setModifiers(getModifier(methodDeclaration.modifiers));
    if (methodDeclaration.thrownExceptions != null) {
        for (TypeReference r : methodDeclaration.thrownExceptions)
            m.getThrownTypes().add(references.getTypeReference(r.resolvedType));
    }
    for (TypeBinding b : methodDeclaration.binding.typeVariables) {
        m.getFormalTypeParameters().add(references.getBoundedTypeReference(b));
    }

    m.setDocComment(getJavaDoc(methodDeclaration.javadoc, scope.referenceCompilationUnit()));

    context.enter(m, methodDeclaration);

    if (methodDeclaration.annotations != null)
        for (Annotation a : methodDeclaration.annotations)
            a.traverse(this, methodDeclaration.scope);

    if (methodDeclaration.arguments != null)
        for (Argument a : methodDeclaration.arguments)
            a.traverse(this, methodDeclaration.scope);

    // Create block
    if (!methodDeclaration.isAbstract() && (methodDeclaration.modifiers & ClassFileConstants.AccNative) == 0) {
        CtBlock<?> b = factory.Core().createBlock();
        context.enter(b, methodDeclaration);
    }

    if (methodDeclaration.statements != null) {
        for (Statement s : methodDeclaration.statements)
            s.traverse(this, methodDeclaration.scope);
    }
    return false;
}

From source file:spoon.support.builder.JDTTreeBuilder.java

License:Open Source License

@Override
public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) {
    CtSimpleType<?> type = createType(memberTypeDeclaration);
    type.setDocComment(getJavaDoc(memberTypeDeclaration.javadoc, scope.referenceCompilationUnit()));
    context.enter(type, memberTypeDeclaration);

    // AST bug HACK
    if (memberTypeDeclaration.annotations != null)
        for (Annotation a : memberTypeDeclaration.annotations) {
            a.traverse(this, (BlockScope) null);
        }//ww  w. j  av  a  2  s .  c om

    return true;
}

From source file:spoon.support.builder.JDTTreeBuilder.java

License:Open Source License

@Override
public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope) {

    if (new String(typeDeclaration.name).equals("package-info")) {
        CtPackage pack = factory.Package()
                .getOrCreate(new String(typeDeclaration.binding.fPackage.readableName()));
        context.enter(pack, typeDeclaration);

        // AST bug HACK
        if (typeDeclaration.annotations != null)
            for (Annotation a : typeDeclaration.annotations) {
                a.traverse(this, (BlockScope) null);
            }/*from   www .  j  a  v  a  2  s.c  o  m*/
        return true;
    } else {
        CtSimpleType<?> type = createType(typeDeclaration);

        type.setDocComment(getJavaDoc(typeDeclaration.javadoc, scope.referenceContext));

        CtPackage pack = null;
        if (!template) {

            if (typeDeclaration.binding.fPackage.shortReadableName() != null
                    && typeDeclaration.binding.fPackage.shortReadableName().length > 0) {
                pack = factory.Package()
                        .getOrCreate(new String(typeDeclaration.binding.fPackage.shortReadableName()));
            } else {
                pack = factory.Package().getOrCreate(CtPackage.TOP_LEVEL_PACKAGE_NAME);
            }
            context.enter(pack, typeDeclaration);
        } else {
            char[] packname = typeDeclaration.binding.fPackage.shortReadableName();
            if (packname.length > 0) {
                pack = factory.Core().createPackage();
                pack.setSimpleName(new String(packname));
                type.setParent(pack);
                context.enter(pack, typeDeclaration);
            }
        }
        context.compilationunitdeclaration = scope.referenceContext;
        context.enter(type, typeDeclaration);

        // AST bug HACK
        if (typeDeclaration.annotations != null)
            for (Annotation a : typeDeclaration.annotations) {
                a.traverse(this, (BlockScope) null);
            }

        if (typeDeclaration.memberTypes != null) {
            int length = typeDeclaration.memberTypes.length;
            for (int i = 0; i < length; i++)
                typeDeclaration.memberTypes[i].traverse(this, typeDeclaration.scope);
        }
        if (typeDeclaration.fields != null) {
            int length = typeDeclaration.fields.length;
            for (int i = 0; i < length; i++) {
                FieldDeclaration field;
                if ((field = typeDeclaration.fields[i]).isStatic()) {
                    field.traverse(this, typeDeclaration.staticInitializerScope);
                } else {
                    field.traverse(this, typeDeclaration.initializerScope);
                }
            }
        }
        if (typeDeclaration.methods != null) {
            int length = typeDeclaration.methods.length;
            for (int i = 0; i < length; i++)
                typeDeclaration.methods[i].traverse(this, typeDeclaration.scope);
        }
        return false;
    }
}

From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java

License:Open Source License

@Override
public boolean visit(Argument argument, BlockScope scope) {
    CtParameter<Object> p = factory.Core().createParameter();
    p.setSimpleName(new String(argument.name));
    p.setVarArgs(argument.isVarArgs());/*  w w  w.  ja  v a  2  s .co  m*/
    p.setModifiers(getModifiers(argument.modifiers));
    if (argument.type != null) {
        p.setType(references.getTypeReference(argument.type.resolvedType));
    } else if (argument.binding != null && argument.binding.type != null) {
        context.isLambdaParameterImplicitlyTyped = false;
        if (argument.binding.type instanceof WildcardBinding) {
            p.setType(references.getTypeReference((((WildcardBinding) argument.binding.type).bound)));
        } else {
            p.setType(references.getTypeReference((argument.binding.type)));
        }
        context.isLambdaParameterImplicitlyTyped = true;
    }
    context.enter(p, argument);
    if (argument.initialization != null) {
        argument.initialization.traverse(this, scope);
    }

    if (argument.annotations != null) {
        for (Annotation a : argument.annotations) {
            // TODO Sorry for that but there is a bug in JDT : https://bugs.eclipse.org/bugs/show_bug.cgi?id=459528
            if (isContainsInTypeAnnotation(argument.type.resolvedType, a)) {
                a.traverse(this, scope);
            }
        }
    }
    return false;
}

From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java

License:Open Source License

@Override
public boolean visit(LocalDeclaration localDeclaration, BlockScope scope) {
    CtLocalVariable<Object> v = factory.Core().createLocalVariable();
    v.setSimpleName(new String(localDeclaration.name));
    v.setType(references.getTypeReference(localDeclaration.type.resolvedType));
    v.setModifiers(getModifiers(localDeclaration.modifiers));
    context.enter(v, localDeclaration);//from  w w  w  . j  a  va 2s  . com

    if (localDeclaration.initialization != null) {
        context.arguments.push(v);
        localDeclaration.initialization.traverse(this, scope);
        context.arguments.pop();
    }

    if (localDeclaration.annotations != null) {
        for (Annotation a : localDeclaration.annotations) {
            // TODO Sorry for that but there is a bug in JDT : https://bugs.eclipse.org/bugs/show_bug.cgi?id=459528
            if (isContainsInTypeAnnotation(localDeclaration.type.resolvedType, a)) {
                a.traverse(this, scope);
            }
        }
    }

    return false;
}

From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
    CtMethod<Object> m = factory.Core().createMethod();
    m.setSimpleName(new String(methodDeclaration.selector));
    m.setType(references.getTypeReference(methodDeclaration.returnType.resolvedType));
    m.setModifiers(getModifiers(methodDeclaration.modifiers));
    m.setDefaultMethod(methodDeclaration.isDefaultMethod());
    if (methodDeclaration.thrownExceptions != null) {
        for (TypeReference r : methodDeclaration.thrownExceptions) {
            CtTypeReference<? extends Throwable> tr = references.getTypeReference(r.resolvedType);
            m.addThrownType(tr);/*from  ww w  .ja  v  a2 s . c  om*/
        }
    }

    // this may happen when working
    if (methodDeclaration.binding != null) {
        // with incomplete classpath
        for (TypeBinding b : methodDeclaration.binding.typeVariables) {
            m.addFormalTypeParameter(references.getBoundedTypeReference(b));
        }
    }

    if (scope != null) {
        m.setDocComment(getJavaDoc(methodDeclaration.javadoc, scope.referenceCompilationUnit()));
    } else if (methodDeclaration.scope != null) {
        m.setDocComment(
                getJavaDoc(methodDeclaration.javadoc, methodDeclaration.scope.referenceCompilationUnit()));
    } else {
        // null scope for "+methodDeclaration
    }

    context.enter(m, methodDeclaration);

    if (methodDeclaration.annotations != null) {
        for (Annotation a : methodDeclaration.annotations) {
            // TODO Sorry for that but there is a bug in JDT : https://bugs.eclipse.org/bugs/show_bug.cgi?id=459528
            if (isContainsInTypeAnnotation(methodDeclaration.returnType.resolvedType, a)) {
                a.traverse(this, methodDeclaration.scope);
            }
        }
    }

    if (methodDeclaration.arguments != null) {
        for (Argument a : methodDeclaration.arguments) {
            a.traverse(this, methodDeclaration.scope);
        }
    }

    // Create block
    if (!methodDeclaration.isAbstract() && (methodDeclaration.modifiers & ClassFileConstants.AccNative) == 0) {
        CtBlock<?> b = factory.Core().createBlock();
        context.enter(b, methodDeclaration);
    }

    if (methodDeclaration.statements != null) {
        for (Statement s : methodDeclaration.statements) {
            s.traverse(this, methodDeclaration.scope);
        }
    }
    return false;
}

From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java

License:Open Source License

@Override
public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope) {
    if (new String(typeDeclaration.name).equals("package-info")) {
        CtPackage pack = factory.Package()
                .getOrCreate(new String(typeDeclaration.binding.fPackage.readableName()));
        pack.setDocComment(this.getJavaDoc(typeDeclaration.javadoc, scope.referenceContext));

        context.compilationunitdeclaration = scope.referenceContext;
        context.enter(pack, typeDeclaration);

        return true;
    } else {//  w  w  w .  j ava 2s  .c  o  m
        CtType<?> type = createType(typeDeclaration);

        type.setDocComment(getJavaDoc(typeDeclaration.javadoc, scope.referenceContext));

        CtPackage pack = null;
        if (typeDeclaration.binding.fPackage.shortReadableName() != null
                && typeDeclaration.binding.fPackage.shortReadableName().length > 0) {
            pack = factory.Package()
                    .getOrCreate(new String(typeDeclaration.binding.fPackage.shortReadableName()));
        } else {
            pack = factory.Package().getOrCreate(CtPackage.TOP_LEVEL_PACKAGE_NAME);
        }
        pack.addType(type);
        context.enter(pack, typeDeclaration);
        context.compilationunitdeclaration = scope.referenceContext;
        context.enter(type, typeDeclaration);

        // AST bug HACK
        if (typeDeclaration.annotations != null) {
            for (Annotation a : typeDeclaration.annotations) {
                a.traverse(this, (BlockScope) null);
            }
        }

        if (typeDeclaration.memberTypes != null) {
            int length = typeDeclaration.memberTypes.length;
            for (int i = 0; i < length; i++) {
                typeDeclaration.memberTypes[i].traverse(this, typeDeclaration.scope);
            }
        }
        if (typeDeclaration.fields != null) {
            int length = typeDeclaration.fields.length;
            for (int i = 0; i < length; i++) {
                FieldDeclaration field;
                if ((field = typeDeclaration.fields[i]).isStatic()) {
                    field.traverse(this, typeDeclaration.staticInitializerScope);
                } else {
                    field.traverse(this, typeDeclaration.initializerScope);
                }
            }
        }
        if (typeDeclaration.methods != null) {
            int length = typeDeclaration.methods.length;
            for (int i = 0; i < length; i++) {
                typeDeclaration.methods[i].traverse(this, typeDeclaration.scope);
            }
        }
        return false;
    }
}