Search in sources :

Example 26 with SpoonModelBuilder

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) ModelUtils.buildClass(spoon.testing.utils.ModelUtils.buildClass) CtNewClass(spoon.reflect.code.CtNewClass) CtClass(spoon.reflect.declaration.CtClass) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 27 with SpoonModelBuilder

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();
    }
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtParameterRemoveRefactoring(spoon.refactoring.CtParameterRemoveRefactoring) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) RefactoringException(spoon.refactoring.RefactoringException) File(java.io.File) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Example 28 with SpoonModelBuilder

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);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtParameterRemoveRefactoring(spoon.refactoring.CtParameterRemoveRefactoring) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) File(java.io.File) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Example 29 with SpoonModelBuilder

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();
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Before(org.junit.Before)

Example 30 with SpoonModelBuilder

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) FilteringFolder(spoon.support.compiler.FilteringFolder) FileSystemFolder(spoon.support.compiler.FileSystemFolder) Test(org.junit.Test)

Aggregations

SpoonModelBuilder (spoon.SpoonModelBuilder)45 Launcher (spoon.Launcher)43 Test (org.junit.Test)35 Factory (spoon.reflect.factory.Factory)29 File (java.io.File)17 CtClass (spoon.reflect.declaration.CtClass)16 CtMethod (spoon.reflect.declaration.CtMethod)6 SpoonResource (spoon.compiler.SpoonResource)5 CtInvocation (spoon.reflect.code.CtInvocation)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 DefaultCoreFactory (spoon.support.DefaultCoreFactory)4 JDTSnippetCompiler (spoon.support.compiler.jdt.JDTSnippetCompiler)4 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)4 Before (org.junit.Before)3 FileNotFoundException (java.io.FileNotFoundException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)2 CtNewClass (spoon.reflect.code.CtNewClass)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 FactoryImpl (spoon.reflect.factory.FactoryImpl)2