use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ReferenceQueryTest method getAllTypeReferencesInEnum.
@Test
public void getAllTypeReferencesInEnum() throws Exception {
CtEnum<ReferenceQueryTestEnum> testEnum = build("spoon.test.reflect.visitor", "ReferenceQueryTestEnum");
List<CtTypeReference<?>> enumTypeRefs = Query.getElements(testEnum, new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class));
TypeFactory typeFactory = testEnum.getFactory().Type();
for (Class<?> c : new Class<?>[] { Integer.class, Long.class, Boolean.class, Number.class, String.class, Void.class }) {
Assert.assertTrue("the reference query on the enum should return all the types defined in the enum declaration", enumTypeRefs.contains(typeFactory.createReference(c)));
}
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class TryCatchTest method testRethrowingExceptionsJava7.
@Test
public void testRethrowingExceptionsJava7() throws Exception {
CtClass<?> clazz = build("spoon.test.trycatch", "RethrowingClass");
CtMethod<?> method = (CtMethod<?>) clazz.getMethods().toArray()[0];
Set<CtTypeReference<? extends Throwable>> thrownTypes = method.getThrownTypes();
// Checks we throw 2 exceptions and not one.
assertEquals(2, thrownTypes.size());
CtTry ctTry = clazz.getElements(new TypeFilter<CtTry>(CtTry.class)).get(0);
Class<? extends CtCatchVariableReference> exceptionClass = ctTry.getCatchers().get(0).getParameter().getReference().getClass();
// Checks the exception in the catch isn't on the signature of the method.
for (CtTypeReference<? extends Throwable> thrownType : thrownTypes) {
assertNotEquals(thrownType.getClass(), exceptionClass);
}
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class TryCatchTest method testTryCatchVariableGetType.
@Test
public void testTryCatchVariableGetType() throws Exception {
Factory factory = createFactory();
CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " try{}catch(RuntimeException e){System.exit(0);}" + "}" + "};").compile();
CtTry tryStmt = (CtTry) clazz.getElements(new TypeFilter<>(CtTry.class)).get(0);
List<CtCatch> catchers = tryStmt.getCatchers();
assertEquals(1, catchers.size());
CtCatchVariable<?> catchVariable = catchers.get(0).getParameter();
assertEquals(RuntimeException.class, catchVariable.getType().getActualClass());
assertEquals(1, catchVariable.getMultiTypes().size());
assertEquals(RuntimeException.class, catchVariable.getMultiTypes().get(0).getActualClass());
// contract: the manipulation with catch variable type is possible
catchVariable.setType((CtTypeReference) factory.Type().createReference(IllegalArgumentException.class));
assertEquals(IllegalArgumentException.class, catchVariable.getType().getActualClass());
// contract setType influences multitypes
assertEquals(1, catchVariable.getMultiTypes().size());
assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(0).getActualClass());
catchVariable.setMultiTypes(Collections.singletonList((CtTypeReference) factory.Type().createReference(UnsupportedOperationException.class)));
assertEquals(UnsupportedOperationException.class, catchVariable.getType().getActualClass());
// contract setType influences multitypes
assertEquals(1, catchVariable.getMultiTypes().size());
assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());
catchVariable.setMultiTypes(Arrays.asList(factory.Type().createReference(UnsupportedOperationException.class), factory.Type().createReference(IllegalArgumentException.class)));
assertEquals(2, catchVariable.getMultiTypes().size());
assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());
assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(1).getActualClass());
// contract setMultiTypes influences types, which contains common super class of all multi types
assertEquals(RuntimeException.class, catchVariable.getType().getActualClass());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtProvidedService.
@Override
public void visitCtProvidedService(CtProvidedService moduleProvidedService) {
printer.writeKeyword("provides").writeSpace();
scan(moduleProvidedService.getServiceType());
try (ListPrinter lp = this.elementPrinterHelper.createListPrinter(false, " with", true, false, ",", true, false, null)) {
for (CtTypeReference implementations : moduleProvidedService.getImplementationTypes()) {
lp.printSeparatorIfAppropriate();
scan(implementations);
}
}
printer.writeSeparator(";").writeln();
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method isImported.
private boolean isImported(CtFieldReference fieldReference) {
CtImport fieldImport = fieldReference.getFactory().createImport(fieldReference);
if (this.imports.contains(fieldImport)) {
return true;
} else {
if (fieldReference.getDeclaringType() == null) {
return false;
}
CtTypeReference staticTypeMemberReference = fieldReference.getFactory().Type().createWildcardStaticTypeMemberReference(fieldReference.getDeclaringType());
CtImport staticClassImport = fieldReference.getFactory().createImport(staticTypeMemberReference);
return this.imports.contains(staticClassImport);
}
}
Aggregations