Search in sources :

Example 31 with CtType

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);
}
Also used : CtType(spoon.reflect.declaration.CtType) SpoonException(spoon.SpoonException) CtFormalTypeDeclarer(spoon.reflect.declaration.CtFormalTypeDeclarer) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Example 32 with CtType

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;
}
Also used : CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) File(java.io.File)

Example 33 with CtType

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;"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 34 with CtType

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\")"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 35 with CtType

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());
}
Also used : CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) OutputDestinationHandler(spoon.support.OutputDestinationHandler) CtModule(spoon.reflect.declaration.CtModule) Test(org.junit.Test)

Aggregations

CtType (spoon.reflect.declaration.CtType)134 Test (org.junit.Test)67 Launcher (spoon.Launcher)60 ArrayList (java.util.ArrayList)42 CtMethod (spoon.reflect.declaration.CtMethod)38 CtTypeReference (spoon.reflect.reference.CtTypeReference)30 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)20 File (java.io.File)19 Factory (spoon.reflect.factory.Factory)19 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 List (java.util.List)18 Collectors (java.util.stream.Collectors)17 CtField (spoon.reflect.declaration.CtField)17 CtElement (spoon.reflect.declaration.CtElement)16 CtPackage (spoon.reflect.declaration.CtPackage)16 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)14 IOException (java.io.IOException)12 SpoonException (spoon.SpoonException)12 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)11 Set (java.util.Set)11