use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class CtTypeParameterTest method checkType.
private void checkType(CtType<?> type) throws NoSuchFieldException, SecurityException {
List<CtTypeParameter> formalTypeParameters = type.getFormalCtTypeParameters();
for (CtTypeParameter ctTypeParameter : formalTypeParameters) {
checkTypeParamErasureOfType(ctTypeParameter, type.getActualClass());
}
for (CtTypeMember member : type.getTypeMembers()) {
if (member instanceof CtFormalTypeDeclarer) {
CtFormalTypeDeclarer ftDecl = (CtFormalTypeDeclarer) member;
formalTypeParameters = ftDecl.getFormalCtTypeParameters();
if (member instanceof CtExecutable<?>) {
CtExecutable<?> exec = (CtExecutable<?>) member;
for (CtTypeParameter ctTypeParameter : formalTypeParameters) {
checkTypeParamErasureOfExecutable(ctTypeParameter);
}
for (CtParameter<?> param : exec.getParameters()) {
checkParameterErasureOfExecutable(param);
}
} else if (member instanceof CtType<?>) {
CtType<?> nestedType = (CtType<?>) member;
// recursive call for nested type
checkType(nestedType);
}
}
}
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class TestCompilationUnit method testGetUnitTypeWorksWithDeclaredType.
@Test
public void testGetUnitTypeWorksWithDeclaredType() {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/api/testclasses/Bar.java");
launcher.buildModel();
CtType type = launcher.getFactory().Type().get(Bar.class);
CompilationUnit compilationUnit = type.getPosition().getCompilationUnit();
assertEquals(CompilationUnit.UNIT_TYPE.TYPE_DECLARATION, compilationUnit.getUnitType());
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class EnumsTypeTest method testEnumsType.
@Test
public void testEnumsType() throws Exception {
// contract: shadow enum should still be considered as an enum
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/reference-test/EnumsRef.java");
Factory factory = launcher.getFactory();
List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test/EnumJar.jar");
String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
factory.getEnvironment().setSourceClasspath(dependencyClasspath);
assertEquals(1, classpath.size());
launcher.buildModel();
List<CtAssignment> assignments = Query.getElements(factory, new TypeFilter<>(CtAssignment.class));
CtTypeReference typeRefFromSource = assignments.get(0).getType();
CtType typeFromSource = typeRefFromSource.getTypeDeclaration();
assertTrue(typeRefFromSource.isEnum());
assertTrue(typeFromSource.isEnum());
assertTrue(typeFromSource instanceof CtEnum);
CtTypeReference typeRefFromJar = assignments.get(1).getType();
CtType typeFromJar = typeRefFromJar.getTypeDeclaration();
// fail
assertTrue(typeRefFromJar.isEnum());
// fail
assertTrue(typeFromJar.isEnum());
// fail
assertTrue(typeFromJar instanceof CtEnum);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class FilterTest method testNameFilterWithGenericType.
@Test
public void testNameFilterWithGenericType() {
// contract: NamedElementFilter of T should only return T elements
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/internal4/Constants.java");
spoon.buildModel();
CtType type = spoon.getFactory().Type().get(Constants.class);
List<CtMethod> ctMethods = type.getElements(new NamedElementFilter<>(CtMethod.class, "CONSTANT"));
assertTrue(ctMethods.isEmpty());
List<CtField> ctFields = type.getElements(new NamedElementFilter<>(CtField.class, "CONSTANT"));
assertEquals(1, ctFields.size());
assertTrue(ctFields.get(0) instanceof CtField);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class FilterTest method testClassCastExceptionOnForEach.
@Test
public void testClassCastExceptionOnForEach() throws Exception {
// contract: bound query, without any mapping
// This test could fail with a version of JDK <= 8.0.40.
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
class Context {
int count = 0;
}
{
Context context = new Context();
// contract: if the query produces elements which cannot be cast to forEach consumer, then they are ignored
launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
context.count++;
});
assertTrue(context.count > 0);
}
{
Context context = new Context();
// contract: if the for each implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach((CtType t) -> {
context.count++;
throw new ClassCastException("TEST");
});
fail("It must fail, because body of forEach should be called and thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the for each implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).forEach(new CtConsumer<CtType>() {
@Override
public void accept(CtType t) {
context.count++;
throw new ClassCastException("TEST");
}
});
fail("It must fail, because body of forEach should be called and thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the select implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).select(new Filter<CtType>() {
@Override
public boolean matches(CtType element) {
context.count++;
throw new ClassCastException("TEST");
}
}).list();
fail("It must fail, because body of select thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the select implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).select((CtType element) -> {
context.count++;
throw new ClassCastException("TEST");
}).list();
fail("It must fail, because body of select thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtFunction) implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtFunction<CtType, Object>() {
@Override
public Object apply(CtType input) {
context.count++;
throw new ClassCastException("TEST");
}
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtFunction) implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input) -> {
context.count++;
throw new ClassCastException("TEST");
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtConsumableFunction) implementation made by local class throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map(new CtConsumableFunction<CtType>() {
@Override
public void apply(CtType input, CtConsumer<Object> outputConsumer) {
context.count++;
throw new ClassCastException("TEST");
}
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
{
Context context = new Context();
// contract: if the map(CtConsumableFunction) implementation made by lambda throws CCE then it is reported
try {
launcher.getFactory().Package().getRootPackage().filterChildren(null).map((CtType input, CtConsumer<Object> outputConsumer) -> {
context.count++;
throw new ClassCastException("TEST");
}).failurePolicy(QueryFailurePolicy.IGNORE).list();
fail("It must fail, because body of map thrown CCE");
} catch (ClassCastException e) {
assertTrue(context.count > 0);
assertEquals("TEST", e.getMessage());
}
}
}
Aggregations