use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CtTypeParameterTest method testTypeSame.
@Test
public void testTypeSame() throws Exception {
CtClass<?> ctModel = (CtClass<?>) ModelUtils.buildClass(ErasureModelA.class);
CtTypeParameter tpA = ctModel.getFormalCtTypeParameters().get(0);
CtTypeParameter tpB = ctModel.getFormalCtTypeParameters().get(1);
CtTypeParameter tpC = ctModel.getFormalCtTypeParameters().get(2);
CtTypeParameter tpD = ctModel.getFormalCtTypeParameters().get(3);
CtConstructor<?> ctModelCons = ctModel.getConstructors().iterator().next();
CtMethod<?> ctModelMethod = ctModel.getMethodsByName("method").get(0);
CtMethod<?> ctModelMethod2 = ctModel.getMethodsByName("method2").get(0);
CtClass<?> ctModelB = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelB")).first();
CtTypeParameter tpA2 = ctModelB.getFormalCtTypeParameters().get(0);
CtTypeParameter tpB2 = ctModelB.getFormalCtTypeParameters().get(1);
CtTypeParameter tpC2 = ctModelB.getFormalCtTypeParameters().get(2);
CtTypeParameter tpD2 = ctModelB.getFormalCtTypeParameters().get(3);
CtConstructor<?> ctModelBCons = ctModelB.getConstructors().iterator().next();
CtMethod<?> ctModelBMethod = ctModelB.getMethodsByName("method").get(0);
// the type parameters of ErasureModelA and ErasureModelA$ModelB are same if they are on the same position.
checkIsSame(ctModel.getFormalCtTypeParameters(), ctModelB.getFormalCtTypeParameters(), true);
// the type parameters of ErasureModelA#constructor and ErasureModelA$ModelB constructor are same, because constructors has same formal type parameters
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.4
checkIsSame(ctModelCons.getFormalCtTypeParameters(), ctModelBCons.getFormalCtTypeParameters(), true);
// the type parameters of ctModel ErasureModelA#method and ErasureModelA$ModelB#method are same if they are on the same position.
checkIsSame(ctModelMethod.getFormalCtTypeParameters(), ctModelBMethod.getFormalCtTypeParameters(), true);
// the type parameters of ctModel ErasureModelA#constructor and ErasureModelA$ModelB#method are never same, because they have different type of scope (Method!=Constructor)
checkIsSame(ctModelCons.getFormalCtTypeParameters(), ctModelBMethod.getFormalCtTypeParameters(), false);
// the type parameters of ctModel ErasureModelA#method and ErasureModelA#method2 are same, because they have same formal type parameters
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.4
checkIsSame(ctModelMethod.getFormalCtTypeParameters(), ctModelMethod2.getFormalCtTypeParameters(), true);
CtClass<?> ctModelC = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelC")).first();
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CompilationTest method testNewInstanceFromExistingClass.
@Test
public void testNewInstanceFromExistingClass() throws Exception {
CtClass<Bar> barCtType = (CtClass<Bar>) ModelUtils.buildClass(Bar.class);
CtReturn<Integer> m = barCtType.getMethod("m").getBody().getStatement(0);
// we cannot use Bar because it causes a runtime cast exception (2 different Bar from different classloader)
IBar bar = barCtType.newInstance();
int value = bar.m();
assertEquals(1, value);
// change the return value
m.setReturnedExpression(m.getFactory().Code().createLiteral(2));
bar = barCtType.newInstance();
value = bar.m();
assertEquals(2, value);
m.replace(m.getFactory().Code().createCodeSnippetStatement("throw new FooEx()"));
try {
bar = barCtType.newInstance();
value = bar.m();
fail();
} catch (Exception ignore) {
}
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class CompilationTest method testPrecompile.
@Test
public void testPrecompile() {
// without precompile
Launcher l = new Launcher();
l.setArgs(new String[] { "--noclasspath", "-i", "src/test/resources/compilation/" });
l.buildModel();
CtClass klass = l.getFactory().Class().get("compilation.Bar");
// without precompile, actualClass does not exist (an exception is thrown)
try {
klass.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
fail();
} catch (SpoonClassNotFoundException ignore) {
}
// with precompile
Launcher l2 = new Launcher();
l2.setArgs(new String[] { "--precompile", "--noclasspath", "-i", "src/test/resources/compilation/" });
l2.buildModel();
CtClass klass2 = l2.getFactory().Class().get("compilation.Bar");
// with precompile, actualClass is not null
Class actualClass = klass2.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
assertNotNull(actualClass);
assertEquals("IBar", actualClass.getSimpleName());
// precompile can be used to compile processors on the fly
Launcher l3 = new Launcher();
l3.setArgs(new String[] { "--precompile", "--noclasspath", "-i", "src/test/resources/compilation/", "-p", "compilation.SimpleProcessor" });
l3.run();
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class NewClassTest method setUp.
@Before
public void setUp() throws Exception {
final Factory build = build(Foo.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo.class);
newClasses = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return true;
}
});
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class NewClassTest method testMoreThan9NewClass.
@Test
public void testMoreThan9NewClass() throws Exception {
final Factory build = build(Foo2.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo2.class);
List<CtNewClass<?>> elements = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return true;
}
});
assertEquals(13, elements.size());
assertEquals(Foo2.class.getCanonicalName() + "$12", elements.get(11).getAnonymousClass().getQualifiedName());
assertEquals(Foo2.class.getCanonicalName() + "$12$1", elements.get(12).getAnonymousClass().getQualifiedName());
}
Aggregations