Search in sources :

Example 1 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class ExceptionTest method testExceptionInvalidAPI.

@Test
public void testExceptionInvalidAPI() throws Exception {
    try {
        Launcher spoon = new Launcher();
        spoon.getFactory().getEnvironment().setLevel("OFF");
        SpoonModelBuilder comp = spoon.createCompiler();
        comp.setSourceClasspath("does_not_exist.jar");
        fail();
    } catch (InvalidClassPathException e) {
    }
    try {
        Launcher spoon = new Launcher();
        spoon.getFactory().getEnvironment().setLevel("OFF");
        SpoonModelBuilder comp = spoon.createCompiler();
        comp.setSourceClasspath("src");
    } catch (InvalidClassPathException e) {
        fail();
    // you're trying to give source code in the classpath, this should be accepted but causes a warn log entry
    }
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) InvalidClassPathException(spoon.compiler.InvalidClassPathException) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 2 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class GenericsTest method testAccessToGenerics.

@Test
public void testAccessToGenerics() throws Exception {
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/generics/Foo.java", "./src/test/java/spoon/test/generics/Bar.java"));
    compiler.build();
    CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
    CtInterface<?> bar = (CtInterface<?>) factory.Type().get(Bar.class);
    final CtNewClass<?> newAnonymousBar = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {

        @Override
        public boolean matches(CtNewClass<?> element) {
            return element.getAnonymousClass() != null && element.getAnonymousClass().isAnonymous();
        }
    }).get(0);
    final List<CtTypeParameter> barTypeParamGenerics = bar.getFormalCtTypeParameters();
    final CtTypeReference<?> anonymousBar = newAnonymousBar.getType();
    assertEquals("Name of the first generic parameter in Bar interface must to be I.", "I", barTypeParamGenerics.get(0).getSimpleName());
    assertEquals("Name of the first generic parameter in Bar usage must to be K.", "K", anonymousBar.getActualTypeArguments().get(0).getSimpleName());
    assertEquals("Name of the second generic parameter in Bar interface must to be O.", "O", barTypeParamGenerics.get(1).getSimpleName());
    assertEquals("Name of the second generic parameter in Bar usage must to be V.", "V", anonymousBar.getActualTypeArguments().get(1).getSimpleName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtInterface(spoon.reflect.declaration.CtInterface) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 3 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class ImportTest method testNewInnerClassDefinesInItsClassAndSuperClass.

@Test
public void testNewInnerClassDefinesInItsClassAndSuperClass() throws Exception {
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--output-type", "nooutput" });
    Factory factory = spoon.createFactory();
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/imports/testclasses/SuperClass.java", "./src/test/java/spoon/test/imports/testclasses/SubClass.java"));
    compiler.build();
    final CtClass<?> subClass = (CtClass<?>) factory.Type().get(SubClass.class);
    final CtConstructorCall<?> ctConstructorCall = subClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    assertEquals("new spoon.test.imports.testclasses.SubClass.Item(\"\")", ctConstructorCall.toString());
    // here the buggy behavior with type members was encoded
    // so we fix it
    final String expected = "public class SubClass extends spoon.test.imports.testclasses.SuperClass {" + System.lineSeparator() + "    public void aMethod() {" + System.lineSeparator() + "        new spoon.test.imports.testclasses.SubClass.Item(\"\");" + System.lineSeparator() + "    }" + System.lineSeparator() + System.lineSeparator() + "    public static class Item extends spoon.test.imports.testclasses.SuperClass.Item {" + System.lineSeparator() + "        public Item(java.lang.String s) {" + System.lineSeparator() + "            super(1, s);" + System.lineSeparator() + "        }" + System.lineSeparator() + "    }" + System.lineSeparator() + "}";
    assertEquals(expected, subClass.toString());
}
Also used : SubClass(spoon.test.imports.testclasses.SubClass) SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 4 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class ImportTest method testMissingImport.

@Test
public void testMissingImport() throws Exception {
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--output-type", "nooutput" });
    Factory factory = spoon.createFactory();
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setLevel("OFF");
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/import-resources/fr/inria/MissingImport.java"));
    compiler.build();
    CtTypeReference<?> type = factory.Class().getAll().get(0).getFields().get(0).getType();
    assertEquals("Abcd", type.getSimpleName());
    assertEquals("fr.inria.internal", type.getPackage().getSimpleName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 5 with SpoonModelBuilder

use of spoon.SpoonModelBuilder in project spoon by INRIA.

the class SignatureTest method testMethodInvocationSignatureWithVariableAccess.

@Test
public void testMethodInvocationSignatureWithVariableAccess() throws Exception {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    String content = "" + "class PR {" + "static String PRS = null;" + "public Object foo(String p) {" + " int s = 0; 	" + " this.foo(s);" + "this.foo(p);" + " return null;" + "}" + " public Object foo(int p) {" + " String s = null;" + " this.foo(s);" + "this.foo(p);" + "return null;" + "}" + "};";
    SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
    builder.build();
    CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
    assertNotNull(clazz1);
    // **FIRST PART: passing local variable access.
    // /--------From the first method we take the method invocations
    TreeSet<CtMethod<?>> ts = new TreeSet<CtMethod<?>>(new DeepRepresentationComparator());
    ts.addAll(clazz1.getMethods());
    CtMethod[] methodArray = ts.toArray(new CtMethod[0]);
    CtMethod<?> methodInteger = methodArray[0];
    assertEquals("foo(int)", methodInteger.getSignature());
    CtInvocation<?> invoToInt1 = (CtInvocation<?>) methodInteger.getBody().getStatement(1);
    CtExpression<?> argumentToInt1 = invoToInt1.getArguments().get(0);
    // ----------From the second method we take the Method Inv
    CtMethod<?> methodString = (CtMethod<?>) methodArray[1];
    assertEquals("foo(java.lang.String)", methodString.getSignature());
    CtInvocation<?> invoToString = (CtInvocation<?>) methodString.getBody().getStatement(1);
    CtExpression<?> argumentToString = invoToString.getArguments().get(0);
    // we compare the signatures of " this.foo(s);"	from both methods
    assertNotEquals(invoToInt1, invoToString);
    // Now we check that we have two invocation to "foo(s)",
    // but one invocation is with var 's' type integer, the other var 's' type int
    assertNotEquals(argumentToInt1, argumentToString);
    // / ***SECOND PART, passing Parameters
    CtInvocation<?> invoToString2 = (CtInvocation<?>) methodInteger.getBody().getStatement(2);
    CtExpression<?> argumentToString2 = invoToString2.getArguments().get(0);
    CtInvocation<?> invoToInt2 = (CtInvocation<?>) methodString.getBody().getStatement(2);
    CtExpression<?> argumentToInt2 = invoToInt2.getArguments().get(0);
    // /
    // Compare the method invo signature (same real argument's name, different type)
    assertNotEquals(invoToString2, invoToInt2);
    // Compare signature of parameters (same var name, different type)
    assertNotEquals(argumentToString2, argumentToInt2);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) DefaultCoreFactory(spoon.support.DefaultCoreFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) CtClass(spoon.reflect.declaration.CtClass) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) CtInvocation(spoon.reflect.code.CtInvocation) TreeSet(java.util.TreeSet) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) CtMethod(spoon.reflect.declaration.CtMethod) StandardEnvironment(spoon.support.StandardEnvironment) 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