Search in sources :

Example 6 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory 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)

Example 7 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory in project spoon by INRIA.

the class SignatureTest method testUnboundFieldSignature.

@Test
public void testUnboundFieldSignature() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    String content = "" + "class PR {" + "public java.io.File foo(String p) {" + " this.mfield = p; 	" + " return null;" + "}" + "};";
    SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
    try {
        builder.build();
        fail();
    } catch (Exception e) {
    // must fail
    }
    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
    CtMethod<?> methodString = (CtMethod<?>) clazz1.getMethods().toArray()[0];
    assertEquals("foo(java.lang.String)", methodString.getSignature());
    CtAssignment<?, ?> invoToInt1 = (CtAssignment<?, ?>) methodString.getBody().getStatement(0);
    CtExpression<?> left = invoToInt1.getAssigned();
    assertEquals("this.mfield", left.toString());
    // null because noclasspath
    assertEquals(null, left.getType());
    assertEquals("this.mfield = p", invoToInt1.toString());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtAssignment(spoon.reflect.code.CtAssignment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) CtClass(spoon.reflect.declaration.CtClass) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) CtMethod(spoon.reflect.declaration.CtMethod) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 8 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory in project spoon by INRIA.

the class SignatureTest method testLiteralSignature.

@Test
public void testLiteralSignature() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    CtStatement sta1 = (factory).Code().createCodeSnippetStatement("System.out.println(\"hello\")").compile();
    CtStatement sta2 = (factory).Code().createCodeSnippetStatement("String hello =\"t1\"; System.out.println(hello)").compile();
    CtStatement sta2bis = ((CtBlock<?>) sta2.getParent()).getStatement(1);
    // equals depends on deep equality
    assertFalse(sta1.equals(sta2bis));
    String parameterWithQuotes = ((CtInvocation<?>) sta1).getArguments().get(0).toString();
    assertEquals("\"hello\"", parameterWithQuotes);
    (factory).Code().createCodeSnippetStatement("Integer.toBinaryString(20)").compile();
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtBlock(spoon.reflect.code.CtBlock) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 9 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory in project spoon by INRIA.

the class SignatureTest method testMethodInvocationSignatureStaticFieldsVariables.

@Test
public void testMethodInvocationSignatureStaticFieldsVariables() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    CtStatement sta1 = (factory).Code().createCodeSnippetStatement("Integer.toBinaryString(Integer.MAX_VALUE)").compile();
    CtStatement sta2 = (factory).Code().createCodeSnippetStatement("Integer.toBinaryString(Integer.MIN_VALUE)").compile();
    String signature1 = ((CtInvocation) sta1).getExecutable().getSignature();
    String signature2 = ((CtInvocation) sta2).getExecutable().getSignature();
    assertEquals(signature1, signature2);
    assertFalse(sta1.equals(sta2));
    CtStatement stb1 = (factory).Code().createCodeSnippetStatement("Integer.toBinaryString(20)").compile();
    CtStatement stb2 = (factory).Code().createCodeSnippetStatement("Integer.toBinaryString(30)").compile();
    String signature1b = ((CtInvocation) sta1).getExecutable().getSignature();
    String signature2b = ((CtInvocation) sta2).getExecutable().getSignature();
    assertEquals(signature1b, signature2b);
    assertFalse(stb1.equals(stb2));
    CtStatement stc1 = (factory).Code().createCodeSnippetStatement("String.format(\"format1\",\"f2\" )").compile();
    CtStatement stc2 = (factory).Code().createCodeSnippetStatement("String.format(\"format2\",\"f2\" )").compile();
    String signaturestc1 = ((CtInvocation) sta1).getExecutable().getSignature();
    String signaturestc2 = ((CtInvocation) sta2).getExecutable().getSignature();
    assertEquals(signaturestc1, signaturestc2);
    assertFalse(stc1.equals(stc2));
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 10 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory in project spoon by INRIA.

the class CommentTest method testSnippedWithComments.

@Test
public void testSnippedWithComments() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setCommentEnabled(true);
    String content = "//class comment\n" + "class PR {\n" + "/**\n * method javadoc comment */\n" + "public java.io.File foo(String p) {\n" + "/* method body comment*/\n" + " return /*inline comment*/ null;" + "}" + "};\n" + "// after class comment";
    JDTSnippetCompiler builder = new JDTSnippetCompiler(factory, content);
    builder.build();
    CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
    assertNotNull(clazz1);
    assertEquals(1, clazz1.getComments().size());
    assertEquals("class comment", clazz1.getComments().get(0).getContent());
    assertEquals(1, builder.getSnippetCompilationUnit().getDeclaredTypes().size());
    assertTrue(clazz1 == builder.getSnippetCompilationUnit().getDeclaredTypes().get(0));
    CtMethod<?> methodString = (CtMethod<?>) clazz1.getMethods().toArray()[0];
    // we don't call getSignature in order to encapsulate a little bit the changes
    // for the next time we will change the signature :-)
    assertEquals("foo", methodString.getSimpleName());
    assertEquals(1, methodString.getComments().size());
    assertEquals("method javadoc comment", methodString.getComments().get(0).getContent());
    CtReturn<?> returnSt = methodString.getBody().getStatement(0);
    assertEquals(2, returnSt.getComments().size());
    assertEquals("method body comment", returnSt.getComments().get(0).getContent());
    assertEquals("inline comment", returnSt.getComments().get(1).getContent());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) DefaultCoreFactory(spoon.support.DefaultCoreFactory) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) CtMethod(spoon.reflect.declaration.CtMethod) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Aggregations

DefaultCoreFactory (spoon.support.DefaultCoreFactory)12 FactoryImpl (spoon.reflect.factory.FactoryImpl)11 StandardEnvironment (spoon.support.StandardEnvironment)11 Test (org.junit.Test)10 Factory (spoon.reflect.factory.Factory)10 CtStatement (spoon.reflect.code.CtStatement)4 CtClass (spoon.reflect.declaration.CtClass)3 CtMethod (spoon.reflect.declaration.CtMethod)3 JDTSnippetCompiler (spoon.support.compiler.jdt.JDTSnippetCompiler)3 SpoonModelBuilder (spoon.SpoonModelBuilder)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CoreFactory (spoon.reflect.factory.CoreFactory)2 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 Launcher (spoon.Launcher)1 FactoryAccessor (spoon.processing.FactoryAccessor)1 CtAssignment (spoon.reflect.code.CtAssignment)1 CtBlock (spoon.reflect.code.CtBlock)1 CtComment (spoon.reflect.code.CtComment)1 CtExpression (spoon.reflect.code.CtExpression)1