use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class ClassTypingContext method adaptTypeParameter.
/**
* adapts `typeParam` to the {@link CtTypeReference}
* of scope of this {@link ClassTypingContext}
* In can be {@link CtTypeParameterReference} again - depending actual type arguments of this {@link ClassTypingContext}.
*
* @param typeParam to be resolved {@link CtTypeParameter}
* @return {@link CtTypeReference} or {@link CtTypeParameterReference} adapted to scope of this {@link ClassTypingContext}
* or null if `typeParam` cannot be adapted to target `scope`
*/
@Override
protected CtTypeReference<?> adaptTypeParameter(CtTypeParameter typeParam) {
if (typeParam == null) {
throw new SpoonException("You cannot adapt a null type parameter.");
}
CtFormalTypeDeclarer declarer = typeParam.getTypeParameterDeclarer();
if ((declarer instanceof CtType<?>) == false) {
return null;
}
// get the actual type argument values for the declarer of `typeParam`
List<CtTypeReference<?>> actualTypeArguments = resolveActualTypeArgumentsOf(((CtType<?>) declarer).getReference());
if (actualTypeArguments == null) {
if (enclosingClassTypingContext != null) {
// try to adapt parameter using enclosing class typing context
return enclosingClassTypingContext.adaptType(typeParam);
}
return null;
}
return getValue(actualTypeArguments, typeParam, declarer);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class CompilationUnitImpl method getBinaryFiles.
@Override
public List<File> getBinaryFiles() {
final List<File> binaries = new ArrayList<>();
final String output = getFactory().getEnvironment().getBinaryOutputDirectory();
if (output != null) {
final File base = Paths.get(output, getDeclaredPackage().getQualifiedName().replace(".", File.separator)).toFile();
if (base.isDirectory()) {
for (final CtType type : getDeclaredTypes()) {
// Add main type, for instance, 'Foo.class'.
final String nameOfType = type.getSimpleName();
final File fileOfType = new File(base, nameOfType + ".class");
if (fileOfType.isFile()) {
binaries.add(fileOfType);
}
// types of inner types ... and so on.
for (final CtType inner : type.getElements(new TypeFilter<>(CtType.class))) {
// to not add 'type' twice.
if (!inner.equals(type)) {
final String nameOfInner = nameOfType + "$" + inner.getSimpleName();
final File fileOfInnerType = new File(base, nameOfInner + ".class");
if (fileOfInnerType.isFile()) {
binaries.add(fileOfInnerType);
}
}
}
}
}
}
return binaries;
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class AnnotationTest method testAnnotationTypeAndFieldOnField.
@Test
public void testAnnotationTypeAndFieldOnField() throws IOException {
// contract: annotation on field with an annotation type which supports type and field, should be attached both on type and field
// see: https://docs.oracle.com/javase/specs/jls/se9/html/jls-9.html#jls-9.7.3
// in this case, we want to print it only once before the type
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/typeandfield");
launcher.getEnvironment().setNoClasspath(true);
launcher.setSourceOutputDirectory("./target/spooned-typeandfield");
launcher.run();
CtType type = launcher.getFactory().Type().get(SimpleClass.class);
CtField field = type.getField("mandatoryField");
assertEquals(1, field.getAnnotations().size());
CtAnnotation annotation = field.getAnnotations().get(0);
assertEquals("spoon.test.annotation.testclasses.typeandfield.AnnotTypeAndField", annotation.getAnnotationType().getQualifiedName());
CtTypeReference fieldType = field.getType();
assertEquals(1, fieldType.getAnnotations().size());
CtAnnotation anotherAnnotation = fieldType.getAnnotations().get(0);
assertEquals(annotation, anotherAnnotation);
assertEquals("java.lang.String", field.getType().getQualifiedName());
assertEquals(1, field.getType().getAnnotations().size());
List<String> lines = Files.readAllLines(new File("./target/spooned-typeandfield/spoon/test/annotation/testclasses/typeandfield/SimpleClass.java").toPath());
String fileContent = StringUtils.join(lines, "\n");
assertTrue("Content :" + fileContent, fileContent.contains("@spoon.test.annotation.testclasses.typeandfield.AnnotTypeAndField"));
assertTrue("Content :" + fileContent, fileContent.contains("public java.lang.String mandatoryField;"));
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class AnnotationTest method testCreateRepeatableAnnotation.
@Test
public void testCreateRepeatableAnnotation() {
// contract: when creating two repeatable annotations, two annotations should be created
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatable");
spoon.buildModel();
CtType type = spoon.getFactory().Type().get(Repeated.class);
CtMethod firstMethod = (CtMethod) type.getMethodsByName("withoutAnnotation").get(0);
List<CtAnnotation<?>> annotations = firstMethod.getAnnotations();
assertTrue(annotations.isEmpty());
spoon.getFactory().Annotation().annotate(firstMethod, Tag.class, "value", "foo");
assertEquals(1, firstMethod.getAnnotations().size());
spoon.getFactory().Annotation().annotate(firstMethod, Tag.class, "value", "bar");
annotations = firstMethod.getAnnotations();
assertEquals(2, annotations.size());
for (CtAnnotation a : annotations) {
assertEquals("Tag", a.getAnnotationType().getSimpleName());
}
String classContent = type.toString();
assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatable.Tag(\"foo\")"));
assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatable.Tag(\"bar\")"));
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class APITest method testOutputDestinationHandler.
@Test
public void testOutputDestinationHandler() throws IOException {
// contract: files are created in the directory determined by the output destination handler
final File outputDest = Files.createTempDirectory("spoon").toFile();
final OutputDestinationHandler outputDestinationHandler = new OutputDestinationHandler() {
@Override
public Path getOutputPath(CtModule module, CtPackage pack, CtType type) {
String path = "";
if (module != null) {
path += module.getSimpleName() + "_";
}
if (pack != null) {
path += pack.getQualifiedName() + "_";
}
if (type != null) {
path += type.getSimpleName() + ".java";
}
return new File(outputDest, path).toPath();
}
@Override
public File getDefaultOutputDirectory() {
return outputDest;
}
};
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/api/testclasses/Bar.java");
launcher.getEnvironment().setOutputDestinationHandler(outputDestinationHandler);
launcher.run();
File generatedFile = new File(outputDest, "unnamed module_spoon.test.api.testclasses_Bar.java");
assertTrue(generatedFile.exists());
}
Aggregations