Search in sources :

Example 61 with CtClass

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

the class SpoonArchitectureEnforcerTest method testStaticClasses.

@Test
public void testStaticClasses() throws Exception {
    // contract: helper classes only have static methods and a private constructor
    // spoon.compiler.SpoonResourceHelper
    // spoon.reflect.visitor.Query
    // spoon.support.compiler.jdt.JDTTreeBuilderQuery
    // spoon.support.compiler.SnippetCompilationHelper
    // spoon.support.util.ByteSerialization
    // spoon.support.util.RtHelper
    // spoon.support.visitor.equals.CloneHelper
    // spoon.template.Substitution
    // spoon.testing.utils.Check
    // spoon.testing.utils.ProcessorUtils
    // spoon.testing.Assert
    SpoonAPI spoon = new Launcher();
    spoon.addInputResource("src/main/java/");
    spoon.buildModel();
    for (CtClass<?> klass : spoon.getModel().getElements(new TypeFilter<CtClass>(CtClass.class) {

        @Override
        public boolean matches(CtClass element) {
            return element.getSuperclass() == null && super.matches(element) && element.getMethods().size() > 0 && element.getElements(new TypeFilter<>(CtMethod.class)).stream().allMatch(x -> x.hasModifier(ModifierKind.STATIC));
        }
    })) {
        assertTrue(klass.getElements(new TypeFilter<>(CtConstructor.class)).stream().allMatch(x -> x.hasModifier(ModifierKind.PRIVATE)));
    }
}
Also used : Launcher(spoon.Launcher) CtInterface(spoon.reflect.declaration.CtInterface) CtCodeElement(spoon.reflect.code.CtCodeElement) CtPackage(spoon.reflect.declaration.CtPackage) StringUtils(org.apache.commons.lang3.StringUtils) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CtType(spoon.reflect.declaration.CtType) SpoonAPI(spoon.SpoonAPI) ABSTRACT(spoon.test.metamodel.MMTypeKind.ABSTRACT) Assert.fail(org.junit.Assert.fail) CtConstructor(spoon.reflect.declaration.CtConstructor) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) SpoonMetaModel(spoon.test.metamodel.SpoonMetaModel) CtField(spoon.reflect.declaration.CtField) CtInheritanceScanner(spoon.reflect.visitor.CtInheritanceScanner) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) AbstractManualProcessor(spoon.processing.AbstractManualProcessor) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) AbstractProcessor(spoon.processing.AbstractProcessor) CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) CtMethod(spoon.reflect.declaration.CtMethod) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) SpoonAPI(spoon.SpoonAPI) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 62 with CtClass

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

the class ReplaceScanner method createInvocation.

private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candidate, List<CtExpression<?>> invArgs, CtInvocation getter, Class getterTypeClass) {
    CtInvocation<?> invocation;
    Type type;
    if (getterTypeClass.equals(Collection.class) || getterTypeClass.equals(List.class)) {
        invocation = factory.Code().createInvocation(null, this.list, invArgs);
        type = Type.LIST;
    } else if (getterTypeClass.equals(Map.class)) {
        invocation = factory.Code().createInvocation(null, this.map, invArgs);
        type = Type.MAP;
    } else if (getterTypeClass.equals(Set.class)) {
        invocation = factory.Code().createInvocation(null, this.set, invArgs);
        type = Type.SET;
    } else {
        invocation = factory.Code().createInvocation(null, this.element, invArgs);
        type = Type.ELEMENT;
    }
    // Listener
    final String name = getter.getExecutable().getSimpleName().substring(3);
    final String listenerName = getter.getExecutable().getDeclaringType().getSimpleName() + name + "ReplaceListener";
    CtClass listener;
    if (listeners.containsKey(listenerName)) {
        listener = listeners.get(listenerName);
    } else {
        final CtTypeReference getterType = getGetterType(factory, getter);
        listener = createListenerClass(factory, listenerName, getterType, type);
        final CtMethod setter = getSetter(name, getter.getTarget().getType().getDeclaration());
        final CtField field = updateField(listener, setter.getDeclaringType().getReference());
        updateConstructor(listener, setter.getDeclaringType().getReference());
        updateSetter(factory, (CtMethod<?>) listener.getMethodsByName("set").get(0), getterType, field, setter);
        // Add auto-generated comment.
        final CtComment comment = factory.Core().createComment();
        comment.setCommentType(CtComment.CommentType.INLINE);
        comment.setContent("auto-generated, see " + ReplacementVisitorGenerator.class.getName());
        listener.addComment(comment);
        listeners.put(listenerName, listener);
    }
    invocation.addArgument(getConstructorCall(listener, factory.Code().createVariableRead(candidate.getParameters().get(0).getReference(), false)));
    return invocation;
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtComment(spoon.reflect.code.CtComment) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtField(spoon.reflect.declaration.CtField) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) CtMethod(spoon.reflect.declaration.CtMethod)

Example 63 with CtClass

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

the class ImportBuilderTest method testWithNoImport.

@Test
public void testWithNoImport() {
    // contract: when the source code has no import, none is created when building model
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/A.java");
    spoon.getEnvironment().setAutoImports(true);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get(A.class);
    CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
    assertTrue(unitA.getImports().isEmpty());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 64 with CtClass

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

the class ImportBuilderTest method testWithStaticStarredImportFromInterface.

@Test
public void testWithStaticStarredImportFromInterface() {
    // contract: when a starred import is used with a target package, all classes of the package should be imported
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/StarredImport.java");
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/fullpack/");
    spoon.getEnvironment().setAutoImports(true);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get(StarredImport.class);
    CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
    Collection<CtImport> imports = unitA.getImports();
    assertEquals(1, imports.size());
    Iterator<CtImport> iterator = imports.iterator();
    CtImport ctImport = iterator.next();
    assertEquals(CtImportKind.ALL_TYPES, ctImport.getImportKind());
    assertTrue(ctImport.getReference() instanceof CtPackageReference);
    CtPackageReference ref = (CtPackageReference) ctImport.getReference();
    assertEquals("spoon.test.jdtimportbuilder.testclasses.fullpack", ref.getQualifiedName());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) CtPackageReference(spoon.reflect.reference.CtPackageReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 65 with CtClass

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

the class ImportBuilderTest method testWithImportFromItf.

@Test
public void testWithImportFromItf() {
    // contract: When using starred static import of an interface, it imports a starred type
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/resources/jdtimportbuilder/");
    spoon.getEnvironment().setAutoImports(true);
    spoon.getEnvironment().setShouldCompile(true);
    spoon.setSourceOutputDirectory("./target/spoon-jdtimport-itfimport");
    spoon.run();
    CtClass classStatic = spoon.getFactory().Class().get("jdtimportbuilder.ItfImport");
    CompilationUnit unitStatic = spoon.getFactory().CompilationUnit().getMap().get(classStatic.getPosition().getFile().getPath());
    Collection<CtImport> imports = unitStatic.getImports();
    assertEquals(1, imports.size());
    CtImport ctImport = imports.iterator().next();
    assertEquals(CtImportKind.ALL_STATIC_MEMBERS, ctImport.getImportKind());
    assertEquals("import static jdtimportbuilder.itf.DumbItf.*;\n", ctImport.toString());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) Launcher(spoon.Launcher) 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