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

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

Introduction

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

Prototype

public void traverse(ASTVisitor visitor, BlockScope scope) 

Source Link

Usage

From source file:ch.uzh.ifi.seal.changedistiller.ast.java.JavaMethodBodyConverter.java

License:Apache License

@Override
public boolean visit(ForStatement forStatement, BlockScope scope) {
    preVisit(forStatement);/* ww w .j a  v a  2  s. c  om*/
    // loop condition
    String value = "";
    if (forStatement.condition != null) {
        value = forStatement.condition.toString();
    }
    pushValuedNode(forStatement, value);
    forStatement.action.traverse(this, scope);

    // loop init
    if (forStatement.initializations != null && forStatement.initializations.length > 0) {
        for (Statement initStatement : forStatement.initializations) {
            push(JavaEntityType.FOR_INIT, initStatement.toString(), initStatement.sourceStart(),
                    initStatement.sourceEnd());

            initStatement.traverse(this, scope);

            pop(initStatement);
        }
    }

    // loop afterthought
    if (forStatement.increments != null && forStatement.increments.length > 0) {
        for (Statement incrementStatement : forStatement.increments) {
            push(JavaEntityType.FOR_INCR, incrementStatement.toString(), incrementStatement.sourceStart(),
                    incrementStatement.sourceEnd());

            incrementStatement.traverse(this, scope);

            pop(incrementStatement);
        }
    }

    return false;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.WithinStatement.java

License:Open Source License

public void traverse(ASTVisitor visitor, BlockScope blockScope) {
    if (visitor.visit(this, blockScope)) {
        this.getTeamExpression().traverse(visitor, blockScope);
        Statement action = getAction();
        if (action != null)
            action.traverse(visitor, blockScope);
    }/* w w w.  ja v a 2  s  . c  o  m*/
    visitor.endVisit(this, blockScope);
}

From source file:org.nabucco.framework.generator.compiler.transformation.java.common.reflection.NabuccoToJavaPropertiesVisitor.java

License:Open Source License

/**
 * Modifies the template 'createPropertyContainer' method by <li>changing the parent class
 * literal or removing it if there is no usable parent</li> <li>add all put statements
 * previously created by the/*from   ww w .j a v  a  2s.  co m*/
 * {@link NabuccoToJavaPropertiesVisitor#createPropertyContainerFragment(String, String, PropertyType, List)}
 * method</li>
 * 
 * @param unit
 *            resulting JavaUnit.
 * 
 * @throws JavaModelException
 *             if model creation/modifications fail.
 */
private void createCreatePropertyContainer(JavaCompilationUnit unit) throws JavaModelException {
    AbstractMethodDeclaration method = JavaAstElementFactory.getInstance().getJavaAstType()
            .getMethod(unit.getType(), SIGNATURE_CREATEPROPERTYCONTAINER);

    JavaAstModelProducer producer = JavaAstModelProducer.getInstance();

    final TypeReference parentType = producer.createTypeReference(this.extention, false);

    // Replace parent class literal
    if (this.extention != null) {

        ASTVisitor vistior = new ASTVisitor() {

            @Override
            public boolean visit(ClassLiteralAccess classLiteral, BlockScope scope) {
                classLiteral.type = parentType;
                return super.visit(classLiteral, scope);
            }
        };

        for (Statement statement : method.statements) {
            statement.traverse(vistior, null);
        }

    } else {
        // remove message send for parent call
        method.statements[method.statements.length - 2] = method.statements[method.statements.length - 1];
        method.statements = Arrays.copyOf(method.statements, 2);
    }

    int returnStatementPos = method.statements.length - 1;

    method.statements = Arrays.copyOf(method.statements,
            method.statements.length + this.propertyContainerStatements.size());

    method.statements[method.statements.length - 1] = method.statements[returnStatementPos];

    for (int i = 0; i < this.propertyContainerStatements.size(); i++) {
        method.statements[returnStatementPos + i] = this.propertyContainerStatements.get(i);
    }

}

From source file:org.nabucco.framework.generator.compiler.transformation.java.common.reflection.NabuccoToJavaReflectionFacade.java

License:Open Source License

/**
 * Mofiy the static getPropertyDescriptor() methods.
 * //from w  ww .  j a v a  2s  .c  o  m
 * @param unit
 *            the java unit
 * 
 * @throws JavaModelException
 */
private void handleStaticMethods(JavaCompilationUnit unit) throws JavaModelException {
    JavaAstType factory = JavaAstElementFactory.getInstance().getJavaAstType();

    TypeDeclaration type = unit.getType();

    final TypeReference classTypeReference = JavaAstModelProducer.getInstance()
            .createTypeReference(factory.getTypeName(type), false);

    AbstractMethodDeclaration[] methods = new AbstractMethodDeclaration[] {
            factory.getMethod(type, GET_PROPERTY_DESCRIPTOR),
            factory.getMethod(type, GET_PROPERTY_DESCRIPTOR_LIST) };

    ASTVisitor alterClassLiteral = new ASTVisitor() {

        @Override
        public boolean visit(ClassLiteralAccess classLiteral, BlockScope scope) {
            classLiteral.type = classTypeReference;
            return super.visit(classLiteral, scope);
        }
    };

    for (AbstractMethodDeclaration currentMethodDeclaration : methods) {
        for (Statement statement : currentMethodDeclaration.statements) {
            statement.traverse(alterClassLiteral, null);
        }
    }

}

From source file:org.nabucco.framework.generator.compiler.transformation.java.datatype.NabuccoToJavaDatatypeVisitor.java

License:Open Source License

/**
 * Find and replace the Class Literal access type.
 * //from   w w w . j  a v  a 2  s .  c  o m
 * @param methodDeclaration
 *            method to search in.
 * @param targetType
 *            the new type.
 */
private void alterClassLiteralAccess(Statement[] methodBody, final TypeReference targetType) {
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(ClassLiteralAccess classLiteral, BlockScope scope) {
            classLiteral.type = targetType;
            return super.visit(classLiteral, scope);
        }
    };
    for (Statement current : methodBody) {
        current.traverse(visitor, (BlockScope) null);
    }
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
    CtConstructor c = factory.Core().createConstructor();
    c.setModifiers(getModifier(constructorDeclaration.modifiers));

    c.setDocComment(getJavaDoc(constructorDeclaration.javadoc, scope.referenceCompilationUnit()));

    context.enter(c, constructorDeclaration);

    if (constructorDeclaration.annotations != null) {
        int annotationsLength = constructorDeclaration.annotations.length;
        for (int i = 0; i < annotationsLength; i++)
            constructorDeclaration.annotations[i].traverse(this, constructorDeclaration.scope);
    }/*from   ww w.  j ava2  s . c o m*/

    context.pushArgument(c);
    if (constructorDeclaration.arguments != null) {
        int argumentLength = constructorDeclaration.arguments.length;
        for (int i = 0; i < argumentLength; i++)
            constructorDeclaration.arguments[i].traverse(this, constructorDeclaration.scope);
    }
    context.popArgument(c);

    if (constructorDeclaration.thrownExceptions != null) {
        for (TypeReference r : constructorDeclaration.thrownExceptions)
            c.getThrownTypes().add(references.getTypeReference(r.resolvedType));
    }
    for (TypeBinding b : constructorDeclaration.binding.typeVariables) {
        c.getFormalTypeParameters().add(references.getBoundedTypeReference(b));
    }

    // Create block
    if (!constructorDeclaration.isAbstract()) {
        CtBlock<?> b = factory.Core().createBlock();
        context.enter(b, constructorDeclaration);
    }

    if (constructorDeclaration.constructorCall != null)
        constructorDeclaration.constructorCall.traverse(this, constructorDeclaration.scope);

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

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override/* w  w w . j a  v a  2  s  . c  o m*/
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.compiler.jdt.JDTTreeBuilder.java

License:Open Source License

@Override
public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
    CtConstructor<?> c = factory.Core().createConstructor();
    c.setModifiers(getModifiers(constructorDeclaration.modifiers));

    c.setDocComment(getJavaDoc(constructorDeclaration.javadoc, scope.referenceCompilationUnit()));

    context.enter(c, constructorDeclaration);

    if (constructorDeclaration.annotations != null) {
        int annotationsLength = constructorDeclaration.annotations.length;
        for (int i = 0; i < annotationsLength; i++) {
            constructorDeclaration.annotations[i].traverse(this, constructorDeclaration.scope);
        }// w  ww . j ava2s .c o  m
    }

    context.pushArgument(c);
    if (constructorDeclaration.arguments != null) {
        int argumentLength = constructorDeclaration.arguments.length;
        for (int i = 0; i < argumentLength; i++) {
            constructorDeclaration.arguments[i].traverse(this, constructorDeclaration.scope);
        }
    }
    context.popArgument(c);

    if (constructorDeclaration.thrownExceptions != null) {
        for (TypeReference r : constructorDeclaration.thrownExceptions) {
            CtTypeReference<? extends Throwable> tr = references.getTypeReference(r.resolvedType);
            c.addThrownType(tr);
        }
    }
    if (constructorDeclaration.binding != null) {
        for (TypeBinding b : constructorDeclaration.binding.typeVariables) {
            c.addFormalTypeParameter(references.getBoundedTypeReference(b));
        }
    }

    // Create block
    if (!constructorDeclaration.isAbstract()) {
        CtBlock<?> b = factory.Core().createBlock();
        context.enter(b, constructorDeclaration);
    }

    if (constructorDeclaration.constructorCall != null) {
        constructorDeclaration.constructorCall.traverse(this, constructorDeclaration.scope);
    }

    if (constructorDeclaration.statements != null) {
        for (Statement s : constructorDeclaration.statements) {
            s.traverse(this, constructorDeclaration.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);//  www  .j  a  va 2 s .com
        }
    }

    // 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;
}