use of spoon.support.StandardEnvironment 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());
}
use of spoon.support.StandardEnvironment in project spoon by INRIA.
the class GenericsTest method testBugCommonCollection.
@Test
public void testBugCommonCollection() throws Exception {
try {
CtClass<?> type = build("spoon.test.generics", "BugCollection");
CtField<?> INSTANCE = type.getElements(new NamedElementFilter<>(CtField.class, "INSTANCE")).get(0);
// assertTrue(INSTANCE.getDefaultExpression().getType().getActualTypeArguments().get(0)
// instanceof CtAnnonTypeParameterReference);
assertEquals("public static final spoon.test.generics.ACLass<?> INSTANCE = new spoon.test.generics.ACLass();", INSTANCE.toString());
CtField<?> INSTANCE2 = type.getElements(new NamedElementFilter<>(CtField.class, "INSTANCE2")).get(0);
INSTANCE2.setAnnotations(new ArrayList<CtAnnotation<?>>());
assertEquals("public static final spoon.test.generics.ACLass<?> INSTANCE2 = new spoon.test.generics.ACLass();", INSTANCE2.toString());
CtClass<?> ComparableComparator = type.getPackage().getElements(new NamedElementFilter<>(CtClass.class, "ComparableComparator")).get(0);
assertTrue(ComparableComparator.toString().startsWith("class ComparableComparator<E extends java.lang.Comparable<? super E>>"));
CtField<?> x = type.getElements(new NamedElementFilter<>(CtField.class, "x")).get(0);
CtTypeReference<?> ref = x.getType();
DefaultJavaPrettyPrinter pp = new DefaultJavaPrettyPrinter(new StandardEnvironment());
// qualifed name
assertEquals("java.util.Map$Entry", ref.getQualifiedName());
// toString uses DefaultJavaPrettyPrinter
assertEquals("java.util.Map.Entry", ref.toString());
// now visitCtTypeReference
assertEquals(java.util.Map.class, ref.getDeclaringType().getActualClass());
pp.visitCtTypeReference(ref);
assertEquals("java.util.Map.Entry", pp.getResult().toString());
CtField<?> y = type.getElements(new NamedElementFilter<>(CtField.class, "y")).get(0);
assertEquals("java.util.Map.Entry<?, ?> y;", y.toString());
CtField<?> z = type.getElements(new NamedElementFilter<>(CtField.class, "z")).get(0);
assertEquals("java.util.Map.Entry<java.lang.String, java.lang.Integer> z;", z.toString());
// now as local variables
CtLocalVariable<?> lx = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "lx")).get(0);
assertEquals("java.util.Map.Entry lx", lx.toString());
CtLocalVariable<?> ly = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "ly")).get(0);
assertEquals("java.util.Map.Entry<?, ?> ly", ly.toString());
CtLocalVariable<?> lz = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "lz")).get(0);
assertEquals("java.util.Map.Entry<java.lang.String, java.lang.Integer> lz", lz.toString());
CtLocalVariable<?> it = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "it")).get(0);
assertEquals("java.util.Iterator<java.util.Map.Entry<?, ?>> it", it.toString());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
use of spoon.support.StandardEnvironment 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();
}
use of spoon.support.StandardEnvironment 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));
}
use of spoon.support.StandardEnvironment 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());
}
Aggregations