use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class TypeReferenceTest method loadReferencedClassFromClasspath.
@SuppressWarnings("rawtypes")
@Test
public void loadReferencedClassFromClasspath() throws Exception {
SpoonModelBuilder comp = new Launcher().createCompiler();
Factory factory = comp.getFactory();
String packageName = "spoon.test.reference";
String className = "ReferencingClass";
String qualifiedName = packageName + "." + className;
String referencedQualifiedName = packageName + "." + "ReferencedClass";
// we only create the model for ReferecingClass
List<SpoonResource> fileToBeSpooned = SpoonResourceHelper.resources("./src/test/resources/reference-test/input/" + qualifiedName.replace('.', '/') + ".java");
comp.addInputSources(fileToBeSpooned);
// for ReferecingClass
assertEquals(1, fileToBeSpooned.size());
// Spoon requires the binary version of ReferencedClass
List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test/ReferenceTest.jar");
String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
factory.getEnvironment().setSourceClasspath(dependencyClasspath);
assertEquals(1, classpath.size());
// now we can build the model
comp.build();
// we can get the model of ReferecingClass
CtType<?> theClass = factory.Type().get(qualifiedName);
// now we retrieve the reference to ReferencedClass
CtTypeReference referencedType = null;
ReferenceTypeFilter<CtTypeReference> referenceTypeFilter = new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class);
List<CtTypeReference> elements = Query.getElements(theClass, referenceTypeFilter);
for (CtTypeReference reference : elements) {
if (reference.getQualifiedName().equals(referencedQualifiedName)) {
referencedType = reference;
break;
}
}
assertFalse(referencedType == null);
// we can get the actual class from the reference, because it is loaded from the class path
Class referencedClass = referencedType.getActualClass();
assertEquals(referencedQualifiedName, referencedClass.getName());
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class MethodsRefactoringTest method testCtParameterRemoveRefactoringValidationCheck.
@Test
public void testCtParameterRemoveRefactoringValidationCheck() throws FileNotFoundException {
String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/" + testPackagePath)));
comp.build();
Factory factory = comp.getFactory();
CtType<?> typeR = factory.Class().get(TypeR.class);
CtMethod<?> methodTypeR_method1 = typeR.getMethodsByName("method1").get(0);
CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring().setTarget(methodTypeR_method1.getParameters().get(0));
refactor.setTarget(methodTypeR_method1.getParameters().get(0));
// check that each to be refactored method has one parameter
List<CtExecutable<?>> execs = refactor.getTargetExecutables();
execs.forEach(exec -> {
// check that each to be modified method has one parameter
assertEquals(1, exec.getParameters().size());
});
// try refactor
try {
refactor.refactor();
fail();
} catch (RefactoringException e) {
this.getClass();
}
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class MethodsRefactoringTest method testCtParameterRemoveRefactoring.
@Test
public void testCtParameterRemoveRefactoring() throws FileNotFoundException {
String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/" + testPackagePath)));
comp.build();
Factory factory = comp.getFactory();
CtType<?> typeA = factory.Class().get(TypeA.class);
CtMethod<?> methodTypeA_method1 = typeA.getMethodsByName("method1").get(0);
CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring();
refactor.setTarget(methodTypeA_method1.getParameters().get(0));
// check that expected methods are targets of refactoring
List<CtExecutable<?>> execs = refactor.getTargetExecutables();
execs.forEach(exec -> {
// check that each to be modified method has one parameter
assertEquals(1, exec.getParameters().size());
});
refactor.refactor();
execs.forEach(exec -> {
// check that each to be modified method has no parameter after refactoring
assertEquals(0, exec.getParameters().size());
});
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt("./target/spooned/" + testPackagePath, 8);
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ExecutableReferenceGenericTest method setUp.
@Before
public void setUp() throws Exception {
Launcher spoon = new Launcher();
factory = spoon.createFactory();
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/reference/MyClass.java", "./src/test/java/spoon/test/reference/MyClass2.java", "./src/test/java/spoon/test/reference/MyClass3.java"));
compiler.build();
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ResourceTest method testFilteringFolder.
@Test
public void testFilteringFolder() throws Exception {
// contract: one can filter input files
// the core of this test is the removeIfMatches at the end
// all files
SpoonModelBuilder mb = new Launcher().getModelBuilder();
FilteringFolder resources = new FilteringFolder();
resources.addFolder(new FileSystemFolder("src/test/java/spoon/test/visibility/"));
mb.addInputSource(resources);
mb.build();
int nbAll = mb.getFactory().getModel().getAllTypes().size();
assertEquals(12, nbAll);
SpoonModelBuilder mb2 = new Launcher().getModelBuilder();
FilteringFolder resources2 = new FilteringFolder();
resources2.addFolder(new FileSystemFolder("src/test/java/spoon/test/visibility/packageprotected/"));
mb2.addInputSource(resources2);
mb2.build();
int nbPackageProtected = mb2.getFactory().getModel().getAllTypes().size();
assertEquals(2, nbPackageProtected);
// now the core of this test
SpoonModelBuilder mb3 = new Launcher().getModelBuilder();
FilteringFolder resources3 = new FilteringFolder();
resources3.addFolder(new FileSystemFolder("src/test/java/spoon/test/visibility/"));
// we remove a number of input resources
resources3.removeAllThatMatch(".*packageprotected.*");
mb3.addInputSource(resources3);
mb3.build();
assertEquals(nbAll - nbPackageProtected, mb3.getFactory().getModel().getAllTypes().size());
}
Aggregations