use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.
the class TemplateMatcher method getTemplateTypeParameters.
private List<CtTypeReference<?>> getTemplateTypeParameters(final CtClass<? extends Template<?>> templateType) {
final List<CtTypeReference<?>> ts = new ArrayList<>();
final Collection<String> c = Parameters.getNames(templateType);
new CtScanner() {
@Override
public void visitCtTypeParameterReference(CtTypeParameterReference reference) {
if (c.contains(reference.getSimpleName())) {
ts.add(reference);
}
}
@Override
public <T> void visitCtTypeReference(CtTypeReference<T> reference) {
if (c.contains(reference.getSimpleName())) {
ts.add(reference);
}
}
}.scan(templateType);
return ts;
}
use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.
the class TemplateMatcher method getBindedParameter.
@SuppressWarnings("unused")
private String getBindedParameter(String pname) {
// HACK! jeje
final String[] x = new String[1];
x[0] = pname;
new CtScanner() {
@Override
public <T> void visitCtField(CtField<T> f) {
Parameter p = f.getAnnotation(Parameter.class);
if ((p != null) && p.value().equals(x[0])) {
x[0] = f.getSimpleName();
return;
}
super.visitCtField(f);
}
}.scan(templateType);
return x[0];
}
use of spoon.reflect.visitor.CtScanner in project spoon by INRIA.
the class MainTest method checkContractCtScanner.
private void checkContractCtScanner(CtPackage pack) {
class Counter {
int scan, enter, exit = 0;
}
final Counter counter = new Counter();
final Counter counterInclNull = new Counter();
new CtScanner() {
@Override
public void scan(CtElement element) {
counterInclNull.scan++;
if (element != null) {
counter.scan++;
}
super.scan(element);
}
@Override
public void enter(CtElement element) {
counter.enter++;
super.enter(element);
}
@Override
public void exit(CtElement element) {
counter.exit++;
super.exit(element);
}
}.scan(pack);
// contract: when enter is called, exit is also called
assertTrue(counter.enter == counter.exit);
// contract: all scanned elements call enter
assertTrue(counter.enter == counter.scan);
Counter counterBiScan = new Counter();
class ActualCounterScanner extends CtBiScannerDefault {
@Override
public void biScan(CtElement element, CtElement other) {
counterBiScan.scan++;
if (element == null) {
if (other != null) {
Assert.fail("element can't be null if other isn't null.");
}
} else if (other == null) {
Assert.fail("other can't be null if element isn't null.");
} else {
// contract: all elements have been cloned and are still equal
assertEquals(element, other);
assertFalse(element == other);
}
super.biScan(element, other);
}
}
final ActualCounterScanner actual = new ActualCounterScanner();
actual.biScan(pack, pack.clone());
// contract: scan and biscan are executed the same number of times
assertEquals(counterInclNull.scan, counterBiScan.scan);
// for pure beauty: parallel visit of the same tree!
Counter counterBiScan2 = new Counter();
new CtBiScannerDefault() {
@Override
public void biScan(CtElement element, CtElement other) {
counterBiScan2.scan++;
// we have the exact same element
assertSame(element, other);
super.biScan(element, other);
}
}.biScan(pack, pack);
// contract: scan and biscan are executed the same number of times
assertEquals(counterInclNull.scan, counterBiScan2.scan);
}
Aggregations