use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class NoClasspathTest method test.
@Test
public void test() throws Exception {
// do we still have a correct model when the complete classpath is not given as input?
Launcher spoon = new Launcher();
spoon.getEnvironment().setNoClasspath(true);
spoon.getEnvironment().setLevel("OFF");
spoon.addInputResource("./src/test/resources/spoon/test/noclasspath/fields");
spoon.getEnvironment().setSourceOutputDirectory(new File("target/spooned/apitest"));
spoon.run();
Factory factory = spoon.getFactory();
CtClass<Object> clazz = factory.Class().get("Foo");
assertEquals("Foo", clazz.getSimpleName());
CtTypeReference<?> superclass = clazz.getSuperclass();
// "Unknown" is not in the classpath at all
assertEquals("Unknown", superclass.getSimpleName());
try {
superclass.getActualClass();
fail();
} catch (SpoonClassNotFoundException e) {
// expected
}
assertNull(superclass.getDeclaration());
// should be empty as in noClasspath the actual class cannot be retrieved
assertTrue(superclass.getAllFields().isEmpty());
// now we really make sure we don't have the class in the classpath
try {
superclass.getActualClass();
fail();
} catch (SpoonClassNotFoundException e) {
// expected
}
{
CtMethod<?> method = clazz.getMethod("method", new CtTypeReference[0]);
assertNotNull(method);
List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(1, invocations.size());
CtInvocation<?> c = invocations.get(0);
assertEquals("method", c.getExecutable().getSimpleName());
assertEquals("x.method()", method.getBody().getStatement(1).toString());
}
{
CtMethod<?> method = clazz.getMethod("m2", new CtTypeReference[0]);
assertNotNull(method);
List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(3, invocations.size());
CtInvocation<?> c = invocations.get(1);
assertEquals("second", c.getExecutable().getSimpleName());
assertEquals("x.first().second().third()", method.getBody().getStatement(1).toString());
}
{
CtMethod<?> method = clazz.getMethod("m1", new CtTypeReference[0]);
assertNotNull(method);
List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(1, invocations.size());
invocations.get(0);
assertEquals("x.y.z.method()", method.getBody().getStatement(0).toString());
}
{
CtMethod<?> method = clazz.getMethod("m3", new CtTypeReference[0]);
assertNotNull(method);
List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(1, invocations.size());
invocations.get(0);
CtLocalVariable<?> statement = method.getBody().getStatement(0);
CtFieldAccess<?> fa = (CtFieldAccess<?>) statement.getDefaultExpression();
assertTrue(fa.getTarget() instanceof CtInvocation);
assertEquals("field", fa.getVariable().getSimpleName());
assertEquals("int x = first().field", statement.toString());
}
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class NoClasspathTest method testInheritanceInNoClassPathWithClasses.
@Test
public void testInheritanceInNoClassPathWithClasses() throws IOException {
// contract: when using noclasspath in combination with a source classpath
// spoon is able to resolve the inheritance between classes contained in source cp
String sourceInputDirPath = "./src/test/resources/spoon/test/inheritance";
String targetBinPath = "./target/spoon-nocp-bin";
Launcher spoon = new Launcher();
spoon.getEnvironment().setShouldCompile(true);
spoon.addInputResource(sourceInputDirPath);
spoon.setBinaryOutputDirectory(targetBinPath);
spoon.run();
spoon = new Launcher();
spoon.getEnvironment().setNoClasspath(true);
spoon.getEnvironment().setSourceClasspath(new String[] { targetBinPath });
spoon.addInputResource(sourceInputDirPath + "/AnotherClass.java");
spoon.buildModel();
CtType anotherclass = spoon.getFactory().Type().get("org.acme.AnotherClass");
assertEquals(1, anotherclass.getFields().size());
CtField field = (CtField) anotherclass.getFields().get(0);
CtTypeReference myClassReference = spoon.getFactory().Type().createReference("fr.acme.MyClass");
assertEquals(myClassReference, field.getType());
assertNotNull(myClassReference.getActualClass());
CtTypeReference myInterfaceReference = spoon.getFactory().Type().createReference("org.myorganization.MyInterface");
assertTrue(myClassReference.isSubtypeOf(myInterfaceReference));
assertTrue(field.getType().isSubtypeOf(myInterfaceReference));
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class AnnotationTest method annotationOverrideFQNIsOK.
@Test
public void annotationOverrideFQNIsOK() {
Launcher spoon = new Launcher();
Factory factory = spoon.getFactory();
factory.getEnvironment().setNoClasspath(true);
spoon.addInputResource("./src/test/resources/noclasspath/annotation/issue1307/SpecIterator.java");
spoon.buildModel();
List<CtAnnotation> overrideAnnotations = factory.getModel().getElements(new TypeFilter<CtAnnotation>(CtAnnotation.class));
for (CtAnnotation annotation : overrideAnnotations) {
CtTypeReference typeRef = annotation.getAnnotationType();
if (typeRef.getSimpleName().equals("Override")) {
assertThat(typeRef.getQualifiedName(), is("java.lang.Override"));
}
}
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class CastTest method testTypeAnnotationOnCast.
@Test
public void testTypeAnnotationOnCast() throws Exception {
final CtType<Castings> aCastings = buildClass(Castings.class);
final CtLocalVariable local = aCastings.getMethod("bar").getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
assertEquals(1, ((CtTypeReference) local.getDefaultExpression().getTypeCasts().get(0)).getAnnotations().size());
assertEquals("((java.lang.@spoon.test.casts.Castings.TypeAnnotation(integer = 1)" + System.lineSeparator() + "String) (\"\"))", local.getDefaultExpression().toString());
}
use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class ReplaceScanner method getGetterType.
private CtTypeReference getGetterType(Factory factory, CtInvocation getter) {
CtTypeReference getterType;
final CtTypeReference type = getter.getType();
if (type instanceof CtTypeParameterReference) {
getterType = getTypeFromTypeParameterReference((CtTypeParameterReference) getter.getExecutable().getDeclaration().getType());
} else {
getterType = type.clone();
}
getterType.getActualTypeArguments().clear();
return getterType;
}
Aggregations