Search in sources :

Example 81 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class VisibilityTest method testVisibilityOfClassesNamedByClassesInJavaLangPackage.

@Test
public void testVisibilityOfClassesNamedByClassesInJavaLangPackage() throws Exception {
    final File sourceOutputDir = new File("target/spooned/spoon/test/visibility_package/testclasses");
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    launcher.getEnvironment().setSourceOutputDirectory(sourceOutputDir);
    final Factory factory = launcher.getFactory();
    final SpoonModelBuilder compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/visibility/testclasses/"));
    compiler.setSourceOutputDirectory(sourceOutputDir);
    compiler.build();
    compiler.generateProcessedSourceFiles(OutputType.CLASSES);
    // Class must be imported.
    final CtClass<?> aDouble = (CtClass<?>) factory.Type().get(spoon.test.visibility.testclasses.internal.Double.class);
    assertNotNull(aDouble);
    assertEquals(spoon.test.visibility.testclasses.internal.Double.class, aDouble.getActualClass());
    // Class mustn't be imported.
    final CtClass<?> aFloat = (CtClass<?>) factory.Type().get(spoon.test.visibility.testclasses.Float.class);
    assertNotNull(aFloat);
    assertEquals(spoon.test.visibility.testclasses.Float.class, aFloat.getActualClass());
    canBeBuilt(new File("./target/spooned/spoon/test/visibility_package/testclasses/"), 7);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Factory(spoon.reflect.factory.Factory) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 82 with CtClass

use of spoon.reflect.declaration.CtClass in project dspot by STAMP-project.

the class DSpotUtils method addGeneratedTestToExistingClass.

private static CtClass<?> addGeneratedTestToExistingClass(CtType<?> type, String pathname) {
    Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource(pathname);
    launcher.buildModel();
    final CtClass<?> existingAmplifiedTest = launcher.getFactory().Class().get(type.getQualifiedName());
    type.getMethods().stream().filter(testCase -> !existingAmplifiedTest.getMethods().contains(testCase)).forEach(existingAmplifiedTest::addMethod);
    return existingAmplifiedTest;
}
Also used : Arrays(java.util.Arrays) JavaOutputProcessor(spoon.support.JavaOutputProcessor) Launcher(spoon.Launcher) Main(fr.inria.stamp.Main) CtComment(spoon.reflect.code.CtComment) LoggerFactory(org.slf4j.LoggerFactory) Function(java.util.function.Function) CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) PATH_SEPARATOR(fr.inria.diversify.utils.AmplificationHelper.PATH_SEPARATOR) Environment(spoon.compiler.Environment) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Predicate(java.util.function.Predicate) QueueProcessingManager(spoon.support.QueueProcessingManager) FileOutputStream(java.io.FileOutputStream) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Factory(spoon.reflect.factory.Factory) FileFileFilter(org.apache.commons.io.filefilter.FileFileFilter) Collectors(java.util.stream.Collectors) File(java.io.File) Processor(spoon.processing.Processor) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) InputStream(java.io.InputStream) Launcher(spoon.Launcher)

Example 83 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class TypeFactory method get.

/**
 * Gets a created type from its qualified name.
 *
 * @return a found type or null if does not exist
 */
@SuppressWarnings("unchecked")
public <T> CtType<T> get(final String qualifiedName) {
    int packageIndex = qualifiedName.lastIndexOf(CtPackage.PACKAGE_SEPARATOR);
    CtPackage pack;
    if (packageIndex > 0) {
        pack = factory.Package().get(qualifiedName.substring(0, packageIndex));
    } else {
        pack = factory.Package().getRootPackage();
    }
    if (pack != null) {
        CtType<T> type = pack.getType(qualifiedName.substring(packageIndex + 1));
        if (type != null) {
            return type;
        }
    }
    int inertTypeIndex = qualifiedName.lastIndexOf(CtType.INNERTTYPE_SEPARATOR);
    if (inertTypeIndex > 0) {
        String s = qualifiedName.substring(0, inertTypeIndex);
        CtType<T> t = factory.Type().get(s);
        if (t == null) {
            return null;
        }
        String className = qualifiedName.substring(inertTypeIndex + 1);
        final CtTypeReference<T> reference = t.getReference();
        if (reference.isLocalType()) {
            final List<CtClass<T>> enclosingClasses = t.getElements(new TypeFilter<CtClass<T>>(CtClass.class) {

                @Override
                public boolean matches(CtClass<T> element) {
                    return super.matches(element) && element.getQualifiedName().equals(qualifiedName);
                }
            });
            if (enclosingClasses.size() == 0) {
                return null;
            }
            return enclosingClasses.get(0);
        }
        try {
            // If the class name can't be parsed in integer, the method throws an exception.
            // If the class name is an integer, the class is an anonymous class, otherwise,
            // it is a standard class.
            Integer.parseInt(className);
            final List<CtNewClass> anonymousClasses = t.getElements(new TypeFilter<CtNewClass>(CtNewClass.class) {

                @Override
                public boolean matches(CtNewClass element) {
                    return super.matches(element) && element.getAnonymousClass().getQualifiedName().equals(qualifiedName);
                }
            });
            if (anonymousClasses.size() == 0) {
                return null;
            }
            return anonymousClasses.get(0).getAnonymousClass();
        } catch (NumberFormatException e) {
            return t.getNestedType(className);
        }
    }
    return null;
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) CtPackage(spoon.reflect.declaration.CtPackage)

Example 84 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class TypeFactory method createTypeAdapter.

/**
 * Create a {@link GenericTypeAdapter} for adapting of formal type parameters from any compatible context to the context of provided `formalTypeDeclarer`
 *
 * @param formalTypeDeclarer
 * 		the target scope of the returned {@link GenericTypeAdapter}
 */
public GenericTypeAdapter createTypeAdapter(CtFormalTypeDeclarer formalTypeDeclarer) {
    class Visitor extends CtAbstractVisitor {

        GenericTypeAdapter adapter;

        @Override
        public <T> void visitCtClass(CtClass<T> ctClass) {
            adapter = new ClassTypingContext(ctClass);
        }

        @Override
        public <T> void visitCtInterface(CtInterface<T> intrface) {
            adapter = new ClassTypingContext(intrface);
        }

        @Override
        public <T> void visitCtMethod(CtMethod<T> m) {
            adapter = new MethodTypingContext().setMethod(m);
        }

        @Override
        public <T> void visitCtConstructor(CtConstructor<T> c) {
            adapter = new MethodTypingContext().setConstructor(c);
        }
    }
    Visitor visitor = new Visitor();
    ((CtElement) formalTypeDeclarer).accept(visitor);
    return visitor.adapter;
}
Also used : CtAbstractVisitor(spoon.reflect.visitor.CtAbstractVisitor) CtClass(spoon.reflect.declaration.CtClass) CtInterface(spoon.reflect.declaration.CtInterface) ClassTypingContext(spoon.support.visitor.ClassTypingContext) GenericTypeAdapter(spoon.support.visitor.GenericTypeAdapter) CtAbstractVisitor(spoon.reflect.visitor.CtAbstractVisitor) MethodTypingContext(spoon.support.visitor.MethodTypingContext) CtElement(spoon.reflect.declaration.CtElement) CtMethod(spoon.reflect.declaration.CtMethod) CtConstructor(spoon.reflect.declaration.CtConstructor)

Example 85 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class GenericsTest method testGetDeclarationOfTypeParameterReference.

@Test
public void testGetDeclarationOfTypeParameterReference() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/ExtendedPaella.java");
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/Paella.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtClass extendedPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "ExtendedPaella")).get(0);
    List<CtTypeParameter> typeParameterList = extendedPaella.getFormalCtTypeParameters();
    assertEquals(1, typeParameterList.size());
    CtMethod totoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "toto")).get(0);
    CtTypeReference returnTypeToto = totoMethod.getType();
    CtTypeReference paramToto = ((CtParameter) totoMethod.getParameters().get(0)).getType();
    CtType declaration = returnTypeToto.getDeclaration();
    assertSame(typeParameterList.get(0), declaration);
    assertSame(typeParameterList.get(0), paramToto.getDeclaration());
    CtMethod machinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "machin")).get(0);
    CtTypeReference returnTypeMachin = machinMethod.getType();
    List<CtTypeParameter> formalCtTypeParameters = machinMethod.getFormalCtTypeParameters();
    assertEquals(1, formalCtTypeParameters.size());
    CtType declarationMachin = returnTypeMachin.getDeclaration();
    assertNotSame(typeParameterList.get(0), declarationMachin);
    assertSame(formalCtTypeParameters.get(0), declarationMachin);
    CtClass innerPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "InnerPaella")).get(0);
    List<CtTypeParameter> innerTypeParametersList = innerPaella.getFormalCtTypeParameters();
    assertEquals(typeParameterList.get(0), innerTypeParametersList.get(0).getSuperclass().getDeclaration());
    CtMethod innerMachinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerMachin")).get(0);
    CtTypeReference returnTypeInnerMachin = innerMachinMethod.getType();
    CtTypeReference paramInnerMachinType = ((CtParameter) innerMachinMethod.getParameters().get(0)).getType();
    List<CtTypeParameter> innerMachinFormalCtType = innerMachinMethod.getFormalCtTypeParameters();
    assertSame(typeParameterList.get(0), returnTypeInnerMachin.getDeclaration());
    assertSame(innerMachinFormalCtType.get(0), paramInnerMachinType.getDeclaration());
    CtMethod innerTotoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerToto")).get(0);
    CtTypeReference returnInnerToto = innerTotoMethod.getType();
    CtTypeReference paramInnerToto = ((CtParameter) innerTotoMethod.getParameters().get(0)).getType();
    List<CtTypeParameter> innerTotoFormatCtType = innerTotoMethod.getFormalCtTypeParameters();
    assertSame(innerTotoFormatCtType.get(0), paramInnerToto.getDeclaration());
    assertSame(innerTypeParametersList.get(0), returnInnerToto.getDeclaration());
}
Also used : LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14