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)));
}
}
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;
}
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());
}
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());
}
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());
}
Aggregations