use of spoon.reflect.factory.FactoryImpl in project spoon by INRIA.
the class CommentTest method testAddCommentsToSnippet.
@Test
public void testAddCommentsToSnippet() {
Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
factory.getEnvironment().setNoClasspath(true);
factory.getEnvironment().setCommentEnabled(true);
CtStatement statement = factory.Code().createCodeSnippetStatement("System.out.println(\"Caenorhabditis\")");
CtComment comment = factory.createComment("My comment on my statement", CtComment.CommentType.INLINE);
statement.addComment(comment);
CtExpression expression = factory.Code().createCodeSnippetExpression("\"Caenorhabditis\" + \"Caenorhabditis\"");
CtComment commentExpression = factory.createComment("My comment on my expression", CtComment.CommentType.INLINE);
expression.addComment(commentExpression);
assertEquals("// My comment on my statement" + newLine + "System.out.println(\"Caenorhabditis\")", statement.toString());
assertEquals("// My comment on my expression" + newLine + "\"Caenorhabditis\" + \"Caenorhabditis\"", expression.toString());
}
use of spoon.reflect.factory.FactoryImpl in project spoon by INRIA.
the class ConstructorFactoryTest method testCreateDefault.
@Test
public void testCreateDefault() {
Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
ClassFactory classf = factory.Class();
ConstructorFactory ctorf = factory.Constructor();
CtClass<?> ctclass = classf.create("Sample");
ctorf.createDefault(ctclass);
CtConstructor<?> c = ctclass.getConstructor();
Assert.assertEquals(0, c.getParameters().size());
}
use of spoon.reflect.factory.FactoryImpl in project spoon by INRIA.
the class FactoryTest method testFactoryOverriding.
@Test
public void testFactoryOverriding() throws Exception {
@SuppressWarnings("serial")
class MyCtMethod<T> extends CtMethodImpl<T> {
}
;
@SuppressWarnings("serial") final CoreFactory specialCoreFactory = new DefaultCoreFactory() {
@Override
public <T> CtMethod<T> createMethod() {
MyCtMethod<T> m = new MyCtMethod<T>();
m.setFactory(getMainFactory());
return m;
}
};
Launcher launcher = new Launcher() {
@Override
public Factory createFactory() {
return new FactoryImpl(specialCoreFactory, new StandardEnvironment());
}
};
CtClass<?> type = build("spoon.test.testclasses", "SampleClass", launcher.getFactory());
CtMethod<?> m = type.getMethodsByName("method3").get(0);
assertTrue(m instanceof MyCtMethod);
}
use of spoon.reflect.factory.FactoryImpl in project spoon by INRIA.
the class SerializableTest method testSerialCtStatement.
@Test
public void testSerialCtStatement() throws Exception {
Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
CtStatement sta2 = (factory).Code().createCodeSnippetStatement("String hello =\"t1\"; System.out.println(hello)").compile();
byte[] ser = ByteSerialization.serialize(sta2);
CtStatement des = (CtStatement) ByteSerialization.deserialize(ser);
String sigBef = sta2.getShortRepresentation();
String sigAf = des.getShortRepresentation();
CtType<?> typeBef = sta2.getParent(CtType.class);
assertNotNull(typeBef);
assertEquals(sigBef, sigAf);
des.setFactory(factory);
String toSBef = sta2.toString();
String toSgAf = des.toString();
assertEquals(toSBef, toSgAf);
CtType<?> typeDes = des.getParent(CtType.class);
assertNotNull(typeDes);
// After deserialization, getDeclaringType throws an exception
CtType<?> decl = typeDes.getDeclaringType();
assertNull(decl);
CtPackage parentOriginal = (CtPackage) typeBef.getParent();
CtPackage parentDeser = (CtPackage) typeDes.getParent();
assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, parentOriginal.getSimpleName());
assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, parentDeser.getSimpleName());
}
use of spoon.reflect.factory.FactoryImpl 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