use of spoon.reflect.declaration.CtNamedElement in project spoon by INRIA.
the class GenericsTest method testClassTypingContext.
@Test
public void testClassTypingContext() throws Exception {
// contract: a ClassTypingContext enables one to perform type resolution of generic types
Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
CtClass<?> ctClassCelebrationLunch = factory.Class().get(CelebrationLunch.class);
CtTypeReference<?> typeReferenceOfDisgust = ctClassCelebrationLunch.filterChildren(new NamedElementFilter<>(CtNamedElement.class, "disgust")).map((CtTypedElement te) -> {
return te.getType();
}).first();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch<java.lang.Integer, java.lang.Long, java.lang.Double>.WeddingLunch<spoon.test.generics.testclasses.Mole>", typeReferenceOfDisgust.toString());
// method WeddingLunch#eatMe
CtMethod<?> tWeddingLunch_eatMe = typeReferenceOfDisgust.getDeclaration().filterChildren((CtNamedElement e) -> "eatMe".equals(e.getSimpleName())).first();
CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
// method Lunch#eatMe
CtMethod<?> ctClassLunch_eatMe = ctClassLunch.filterChildren((CtNamedElement e) -> "eatMe".equals(e.getSimpleName())).first();
// type of first parameter of method WeddingLunch#eatMe
CtTypeReference<?> ctWeddingLunch_X = tWeddingLunch_eatMe.getParameters().get(0).getType();
// X is the type parameter of WeddingLunch
assertEquals("X", ctWeddingLunch_X.getSimpleName());
// type of first parameter of method Lunch#eatMe
CtTypeReference<?> ctClassLunch_A = ctClassLunch_eatMe.getParameters().get(0).getType();
assertEquals("A", ctClassLunch_A.getSimpleName());
// are these two types same?
ClassTypingContext typingContextOfDisgust = new ClassTypingContext(typeReferenceOfDisgust);
// contract: the class typing context provides its scope
assertSame(typeReferenceOfDisgust.getTypeDeclaration(), typingContextOfDisgust.getAdaptationScope());
// in disgust, X of WeddingLunch is bound to "Model"
assertEquals("spoon.test.generics.testclasses.Mole", typingContextOfDisgust.adaptType(ctWeddingLunch_X).getQualifiedName());
// adapt A to scope of CelebrationLunch<Integer,Long,Double>.WeddingLunch<Mole>
// in disgust, the A of Lunch is bound to "Mole"
assertEquals("spoon.test.generics.testclasses.Mole", typingContextOfDisgust.adaptType(ctClassLunch_A).getQualifiedName());
// I don't understand the goal and utility of this one
assertEquals("java.lang.Double", typingContextOfDisgust.getEnclosingGenericTypeAdapter().adaptType(ctClassLunch_A).getQualifiedName());
// now we resolve those types, but in the context of the declaration, where no concrete types exist
// are these two types same in scope of CelebrationLunch<K,L,M>.WddingLunch<X> class itself
ClassTypingContext sthOftWeddingLunch_X = new ClassTypingContext(typeReferenceOfDisgust.getDeclaration());
// contract: the class typing context provides its scope
assertSame(typeReferenceOfDisgust.getDeclaration(), sthOftWeddingLunch_X.getAdaptationScope());
// in WeddingLunch "X" is still "X"
assertEquals("X", sthOftWeddingLunch_X.adaptType(ctWeddingLunch_X).getQualifiedName());
// in WeddingLunch the "A" from Lunch of is called "X"
assertEquals("X", sthOftWeddingLunch_X.adaptType(ctClassLunch_A).getQualifiedName());
// ?????
// adapt A to scope of enclosing class of CelebrationLunch<K,L,M>.WddingLunch<X>, which is CelebrationLunch<K,L,M>
assertEquals("M", sthOftWeddingLunch_X.getEnclosingGenericTypeAdapter().adaptType(ctClassLunch_A).getQualifiedName());
}
use of spoon.reflect.declaration.CtNamedElement in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method scan.
/**
* The generic scan method for an element.
*/
public DefaultJavaPrettyPrinter scan(CtElement e) {
if (e != null) {
enter(e);
context.elementStack.push(e);
if (env.isPreserveLineNumbers()) {
if (!(e instanceof CtNamedElement)) {
getPrinterHelper().adjustStartPosition(e);
}
}
try {
e.accept(this);
} catch (SpoonException ex) {
throw ex;
} catch (Exception ex) {
String elementInfo = e.getClass().getName();
elementInfo += " on path " + getPath(e) + "\n";
if (e.getPosition() != null) {
elementInfo += "at position " + e.getPosition().toString() + " ";
}
throw new SpoonException("Printing of " + elementInfo + "failed", ex);
}
context.elementStack.pop();
exit(e);
}
return this;
}
use of spoon.reflect.declaration.CtNamedElement in project spoon by INRIA.
the class EqualsChecker method scanCtNamedElement.
@Override
public void scanCtNamedElement(CtNamedElement e) {
final CtNamedElement peek = (CtNamedElement) this.other;
if (!e.getSimpleName().equals(peek.getSimpleName())) {
isNotEqual = true;
return;
}
super.scanCtNamedElement(e);
}
use of spoon.reflect.declaration.CtNamedElement in project dspot by STAMP-project.
the class ChangeDetectorSelector method selectToKeep.
@Override
public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
if (amplifiedTestToBeKept.isEmpty()) {
return amplifiedTestToBeKept;
}
CtType clone = this.currentClassTestToBeAmplified.clone();
clone.setParent(this.currentClassTestToBeAmplified.getParent());
this.currentClassTestToBeAmplified.getMethods().stream().filter(AmplificationChecker::isTest).forEach(clone::removeMethod);
amplifiedTestToBeKept.forEach(clone::addMethod);
DSpotUtils.printCtTypeToGivenDirectory(clone, new File(DSpotCompiler.pathToTmpTestSources));
final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).buildClasspath(this.program.getProgramDir()) + AmplificationHelper.PATH_SEPARATOR + "target/dspot/dependencies/";
DSpotCompiler.compile(DSpotCompiler.pathToTmpTestSources, classpath + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getTestClassesDir(), new File(this.pathToChangedVersionOfProgram + "/" + this.program.getTestClassesDir()));
final TestListener results = TestLauncher.run(this.configuration, classpath + AmplificationHelper.PATH_SEPARATOR + this.pathToChangedVersionOfProgram + "/" + this.program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + this.pathToChangedVersionOfProgram + "/" + this.program.getTestClassesDir(), clone, amplifiedTestToBeKept.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList()));
if (!results.getFailingTests().isEmpty()) {
results.getFailingTests().forEach(failure -> this.failurePerAmplifiedTest.put(amplifiedTestToBeKept.stream().filter(ctMethod -> ctMethod.getSimpleName().equals(failure.getDescription().getMethodName())).findFirst().get(), failure));
}
return amplifiedTestToBeKept;
}
use of spoon.reflect.declaration.CtNamedElement in project spoon by INRIA.
the class CtElementPathBuilder method fromElement.
/**
* Build path to a CtElement el, from one of its parent.
*
* @throws CtPathException is thrown when root is not a parent of el.
*
* @param el : the element to which the CtPath leads to
* @param root : Starting point of the CtPath
* @return CtPath from root to el
*/
public CtPath fromElement(CtElement el, CtElement root) throws CtPathException {
CtPathImpl path = new CtPathImpl();
CtElement cur = el;
while (cur != root) {
CtElement parent = cur.getParent();
CtRole role = cur.getRoleInParent();
if (role == null) {
throw new CtPathException();
}
RoleHandler roleHandler = RoleHandlerHelper.getOptionalRoleHandler(parent.getClass(), role);
if (roleHandler == null) {
throw new CtPathException();
}
CtPathElement pathElement = new CtRolePathElement(role);
switch(roleHandler.getContainerKind()) {
case SINGLE:
break;
case LIST:
// Element needs to be differentiated from its brothers
List list = roleHandler.asList(parent);
// Assumes that List's order is deterministic.
// Can't be replaced by list.indexOf(cur)
// Because objects must be the same (and not just equals)
int index = 0;
for (Object o : list) {
if (o == cur) {
break;
}
index++;
}
pathElement.addArgument("index", index + "");
break;
case SET:
String name;
if (cur instanceof CtNamedElement) {
name = ((CtNamedElement) cur).getSimpleName();
} else if (cur instanceof CtReference) {
name = ((CtReference) cur).getSimpleName();
} else {
throw new CtPathException();
}
pathElement.addArgument("name", name);
break;
case MAP:
Map map = roleHandler.asMap(parent);
String key = null;
for (Object o : map.keySet()) {
if (map.get(o) == cur) {
key = (String) o;
break;
}
}
if (key == null) {
throw new CtPathException();
} else {
pathElement.addArgument("key", key);
}
break;
}
cur = parent;
path.addFirst(pathElement);
}
return path;
}
Aggregations