use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class SnippetCompilationHelper method createWrapperContent.
private static String createWrapperContent(final CtElement element, final Factory f, final CtTypeReference returnType) {
CtClass<?> w = f.Class().create(WRAPPER_CLASS_NAME);
CtBlock body = f.Core().createBlock();
if (element instanceof CtStatement) {
body.addStatement((CtStatement) element);
} else if (element instanceof CtExpression) {
CtReturn ret = f.Core().createReturn();
ret.setReturnedExpression((CtExpression) element);
body.addStatement(ret);
}
Set<ModifierKind> modifiers = EnumSet.of(ModifierKind.STATIC);
Set<CtTypeReference<? extends Throwable>> thrownTypes = new HashSet<>();
thrownTypes.add(f.Class().<Throwable>get(Throwable.class).getReference());
f.Method().create(w, modifiers, returnType, WRAPPER_METHOD_NAME, CtElementImpl.<CtParameter<?>>emptyList(), thrownTypes, body);
String contents = w.toString();
// Clean up (delete wrapper from factory) after it is printed. The DefaultJavaPrettyPrinter needs w in model to be able to print it correctly
w.getPackage().removeType(w);
return contents;
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class CtCatchVariableImpl method getType.
@SuppressWarnings("unchecked")
@Override
@DerivedProperty
public CtTypeReference<T> getType() {
if (types.isEmpty()) {
return null;
} else if (types.size() == 1) {
return (CtTypeReference<T>) types.get(0);
}
// compute common super type of exceptions
List<CtTypeReference<?>> superTypesOfFirst = types.get(0).map(new SuperInheritanceHierarchyFunction().includingInterfaces(false).includingSelf(true).returnTypeReferences(true)).list();
if (superTypesOfFirst.isEmpty()) {
return null;
}
int commonSuperTypeIdx = 0;
// index of Throwable. Last is Object
int throwableIdx = superTypesOfFirst.size() - 2;
for (int i = 1; i < types.size() && commonSuperTypeIdx != throwableIdx; i++) {
CtTypeReference<?> nextException = types.get(i);
while (commonSuperTypeIdx < throwableIdx) {
if (nextException.isSubtypeOf(superTypesOfFirst.get(commonSuperTypeIdx))) {
// nextException is sub type of actually selected commonSuperType
break;
}
// try next super type
commonSuperTypeIdx++;
}
}
return (CtTypeReference<T>) superTypesOfFirst.get(commonSuperTypeIdx);
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class CtTypeTest method testIsSubTypeOfonTypeReferences.
@Test
public void testIsSubTypeOfonTypeReferences() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "-c" });
launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/SubtypeModel.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtType<?> oCtType = factory.Class().get("spoon.test.ctType.testclasses.SubtypeModel");
CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
Map<String, CtTypeReference<?>> nameToTypeRef = new HashMap<>();
O_FooMethod.filterChildren(new TypeFilter<>(CtLocalVariable.class)).forEach((CtLocalVariable var) -> {
nameToTypeRef.put(var.getSimpleName(), var.getType());
});
int[] count = new int[1];
O_FooMethod.filterChildren(new TypeFilter<>(CtAssignment.class)).forEach((CtAssignment ass) -> {
for (CtComment comment : ass.getComments()) {
checkIsNotSubtype(comment, nameToTypeRef);
count[0]++;
}
;
count[0]++;
checkIsSubtype(((CtVariableAccess) ass.getAssigned()).getVariable().getType(), ((CtVariableAccess) ass.getAssignment()).getVariable().getType(), nameToTypeRef);
});
assertTrue(count[0] > (9 * 8));
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ConstructorTest method assertIntersectionTypeInConstructor.
private void assertIntersectionTypeInConstructor(CtTypeReference<?> boundingType1) {
assertTrue(boundingType1 instanceof CtIntersectionTypeReference);
CtIntersectionTypeReference<?> boundingType = boundingType1.asCtIntersectionTypeReference();
final List<CtTypeReference<?>> bounds = boundingType.getBounds().stream().collect(Collectors.toList());
CtTypeReference<?> genericTacos = bounds.get(0);
assertEquals("Tacos", genericTacos.getSimpleName());
assertEquals(1, genericTacos.getAnnotations().size());
assertEquals(1, genericTacos.getActualTypeArguments().size());
CtTypeParameterReference wildcard = (CtTypeParameterReference) genericTacos.getActualTypeArguments().get(0);
assertEquals("?", wildcard.getSimpleName());
assertEquals(1, wildcard.getAnnotations().size());
assertEquals("C", wildcard.getBoundingType().getSimpleName());
assertEquals(1, wildcard.getBoundingType().getAnnotations().size());
assertEquals("Serializable", bounds.get(1).getSimpleName());
assertEquals(1, bounds.get(1).getAnnotations().size());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ExecutableFactoryTest method testCreateReference.
@Test
public void testCreateReference() {
Factory f = createFactory();
ExecutableFactory ef = f.Executable();
String signature = "boolean Object#equals(Object)";
CtExecutableReference<Object> eref = ef.createReference(signature);
String type = eref.getType().getQualifiedName();
String decltype = eref.getDeclaringType().getQualifiedName();
String name = eref.getSimpleName();
List<CtTypeReference<?>> params = eref.getParameters();
List<CtTypeReference<?>> atas = eref.getActualTypeArguments();
Assert.assertEquals("boolean", type);
Assert.assertEquals("Object", decltype);
Assert.assertEquals("equals", name);
Assert.assertEquals(1, params.size());
Assert.assertEquals(0, atas.size());
}
Aggregations