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