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());
}
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());
}
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;
}
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);
}
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());
}
Aggregations