Search in sources :

Example 6 with CtScanner

use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.

the class JavaDocTest method testJavadocNotPresentInAST.

@Test
public void testJavadocNotPresentInAST() throws Exception {
    Launcher launcher = new Launcher();
    launcher.getEnvironment().setCommentEnabled(false);
    launcher.getEnvironment().setNoClasspath(true);
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/javadoc/testclasses/");
    launcher.run();
    new CtScanner() {

        @Override
        public void scan(CtElement element) {
            if (element != null) {
                assertEquals(0, element.getComments().size());
            }
            super.scan(element);
        }

        @Override
        public void visitCtComment(CtComment comment) {
            fail("Shouldn't have comment in the model.");
            super.visitCtComment(comment);
        }
    }.scan(launcher.getModel().getRootPackage());
}
Also used : CtComment(spoon.reflect.code.CtComment) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Example 7 with CtScanner

use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.

the class VariableAccessTest method testMultipleDeclarationsOfLocalVariable.

@Test
public void testMultipleDeclarationsOfLocalVariable() {
    final class CtLocalVariableReferenceScanner extends CtScanner {

        @Override
        public <T> void visitCtLocalVariableReference(final CtLocalVariableReference<T> reference) {
            assertNotNull(reference.getDeclaration());
            final CtLocalVariable decl = reference.getDeclaration();
            assertEquals(decl.getPosition().getLine(), 7);
            assertTrue(decl.getDefaultExpression() instanceof CtLiteral);
            final CtLiteral literal = (CtLiteral) decl.getDefaultExpression();
            assertEquals(literal.getValue(), 42);
            super.visitCtLocalVariableReference(reference);
        }
    }
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/test/resources/reference-test/MultipleDeclarationsOfLocalVariable.java");
    launcher.buildModel();
    new CtLocalVariableReferenceScanner().scan(launcher.getModel().getRootPackage());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Example 8 with CtScanner

use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.

the class Refactoring method copyMethod.

/**
 * See doc in {@link CtMethod#copyMethod()}
 */
public static CtMethod<?> copyMethod(final CtMethod<?> method) {
    CtMethod<?> clone = method.clone();
    String tentativeTypeName = method.getSimpleName() + "Copy";
    CtType parent = method.getParent(CtType.class);
    while (parent.getMethodsByName(tentativeTypeName).size() > 0) {
        tentativeTypeName += "X";
    }
    final String cloneMethodName = tentativeTypeName;
    clone.setSimpleName(cloneMethodName);
    parent.addMethod(clone);
    new CtScanner() {

        @Override
        public <T> void visitCtExecutableReference(CtExecutableReference<T> reference) {
            CtExecutable<T> declaration = reference.getDeclaration();
            if (declaration == null) {
                return;
            }
            if (declaration == method) {
                reference.setSimpleName(cloneMethodName);
            }
            if (reference.getDeclaration() != clone) {
                throw new SpoonException("post condition broken " + reference);
            }
            super.visitCtExecutableReference(reference);
        }
    }.scan(clone);
    return clone;
}
Also used : CtType(spoon.reflect.declaration.CtType) SpoonException(spoon.SpoonException) CtScanner(spoon.reflect.visitor.CtScanner) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 9 with CtScanner

use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.

the class FieldAccessTest method testFieldAccessOnUnknownType.

@Test
public void testFieldAccessOnUnknownType() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/FieldAccessRes.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    class CounterScanner extends CtScanner {

        private int visited = 0;

        @Override
        public <T> void visitCtFieldWrite(CtFieldWrite<T> fieldWrite) {
            visited++;
            assertEquals("array", ((CtVariableWrite) fieldWrite.getTarget()).getVariable().getSimpleName());
            assertEquals("length", fieldWrite.getVariable().getSimpleName());
        }
    }
    CounterScanner scanner = new CounterScanner();
    launcher.getFactory().Class().get("FieldAccessRes").accept(scanner);
    assertEquals(1, scanner.visited);
}
Also used : CtVariableWrite(spoon.reflect.code.CtVariableWrite) CtFieldWrite(spoon.reflect.code.CtFieldWrite) Launcher(spoon.Launcher) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Example 10 with CtScanner

use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.

the class VariableAccessTest method testReferenceToLocalVariableDeclaredInLoop.

@Test
public void testReferenceToLocalVariableDeclaredInLoop() {
    final class CtLocalVariableReferenceScanner extends CtScanner {

        @Override
        public <T> void visitCtLocalVariableReference(final CtLocalVariableReference<T> reference) {
            assertNotNull(reference.getDeclaration());
            assertEquals(reference.getDeclaration().getSimpleName(), reference.getSimpleName());
            assertEquals(reference.getDeclaration().getType(), reference.getType());
            super.visitCtLocalVariableReference(reference);
        }
    }
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/test/resources/reference-test/ChangeScanner.java");
    launcher.buildModel();
    new CtLocalVariableReferenceScanner().scan(launcher.getModel().getRootPackage());
}
Also used : CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference) Launcher(spoon.Launcher) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Aggregations

CtScanner (spoon.reflect.visitor.CtScanner)18 CtElement (spoon.reflect.declaration.CtElement)9 Test (org.junit.Test)7 Launcher (spoon.Launcher)6 ArrayList (java.util.ArrayList)4 SpoonException (spoon.SpoonException)4 CtType (spoon.reflect.declaration.CtType)3 CtBlock (spoon.reflect.code.CtBlock)2 CtComment (spoon.reflect.code.CtComment)2 CtLocalVariable (spoon.reflect.code.CtLocalVariable)2 CtClass (spoon.reflect.declaration.CtClass)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 CtField (spoon.reflect.declaration.CtField)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtParameter (spoon.reflect.declaration.CtParameter)2 CtLocalVariableReference (spoon.reflect.reference.CtLocalVariableReference)2 CtTypeReference (spoon.reflect.reference.CtTypeReference)2 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)2 URLClassLoader (java.net.URLClassLoader)1 ArrayDeque (java.util.ArrayDeque)1