Search in sources :

Example 76 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class TemplateTest method testTemplateWithWrongUsedStringParam.

@Test
public void testTemplateWithWrongUsedStringParam() throws Exception {
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/constructors/C1.java"), SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/constructors/TemplateWithFieldsAndMethods_Wrong.java")).build();
    CtClass<?> c1 = factory.Class().get(C1.class);
    new TemplateWithFieldsAndMethods_Wrong("testparam").apply(c1);
    CtMethod<?> m = c1.getMethod("methodToBeInserted");
    assertNotNull(m);
    // contract: printing of code which contains invalid field reference, fails with nice exception
    try {
        m.getBody().getStatement(0).toString();
    } catch (SpoonException e) {
        assertTrue("The error description doesn't contain name of invalid field. There is:\n" + e.getMessage(), e.getMessage().indexOf("testparam") >= 0);
    }
}
Also used : SpoonException(spoon.SpoonException) TemplateWithFieldsAndMethods_Wrong(spoon.test.template.testclasses.constructors.TemplateWithFieldsAndMethods_Wrong) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 77 with Factory

use of spoon.reflect.factory.Factory 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());
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtPackage(spoon.reflect.declaration.CtPackage) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 78 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class SerializableTest method testSerializationModelStreamer.

@Test
public void testSerializationModelStreamer() throws Exception {
    Factory factory = build("spoon.test.serializable", "Dummy").getFactory();
    ByteArrayOutputStream outstr = new ByteArrayOutputStream();
    new SerializationModelStreamer().save(factory, outstr);
    Factory loadedFactory = new SerializationModelStreamer().load(new ByteArrayInputStream(outstr.toByteArray()));
    assertFalse(factory.Type().getAll().isEmpty());
    assertFalse(loadedFactory.Type().getAll().isEmpty());
    assertEquals(factory.getModel().getRootPackage(), loadedFactory.getModel().getRootPackage());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Factory(spoon.reflect.factory.Factory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) SerializationModelStreamer(spoon.support.SerializationModelStreamer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 79 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class SourcePositionTest method testSourcePosition.

@Test
public void testSourcePosition() throws IOException {
    File modelFile = new File("./src/test/resources/serialization/model");
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/serialization/SomeClass.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    saveFactory(factory, modelFile);
    Factory factoryFromFile = loadFactory(modelFile);
    CtType<?> type = factory.Type().get("SomeClass");
    CtType<?> typeFromFile = factoryFromFile.Type().get("SomeClass");
    // Serialized model should have same valid source positions as the original model
    assertTrue(type.getPosition().getFile().equals(typeFromFile.getPosition().getFile()));
    assertTrue(type.getPosition().getLine() == typeFromFile.getPosition().getLine());
    assertTrue(type.getPosition().getColumn() == typeFromFile.getPosition().getColumn());
    CtField<?> elem1 = type.getField("a");
    CtField<?> elem2 = typeFromFile.getField("a");
    assertTrue(elem1.getPosition().getFile().equals(elem2.getPosition().getFile()));
}
Also used : Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) File(java.io.File) Test(org.junit.Test)

Example 80 with Factory

use of spoon.reflect.factory.Factory 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

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19