use of spoon.reflect.code.CtComment in project spoon by INRIA.
the class CommentTest method testWildComments.
@Test
public void testWildComments() {
// contract: tests that value of comment is correct even for wild combinations of characters. See WildComments class for details
Factory f = getSpoonFactory();
CtClass<?> type = (CtClass<?>) f.Type().get(WildComments.class);
List<CtLiteral<String>> literals = (List) ((CtNewArray<?>) type.getField("comments").getDefaultExpression()).getElements();
assertTrue(literals.size() > 10);
/*
* each string literal has a comment and string value, which defines expected value of it's comment
*/
for (CtLiteral<String> literal : literals) {
assertEquals(1, literal.getComments().size());
CtComment comment = literal.getComments().get(0);
String expected = literal.getValue();
assertEquals(literal.getPosition().toString(), expected, comment.getContent());
}
}
use of spoon.reflect.code.CtComment 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.code.CtComment in project spoon by INRIA.
the class CtTypeTest method testIsSubTypeOfonTypeReferences.
@Test
public void testIsSubTypeOfonTypeReferences() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "-c" });
launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/SubtypeModel.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtType<?> oCtType = factory.Class().get("spoon.test.ctType.testclasses.SubtypeModel");
CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
Map<String, CtTypeReference<?>> nameToTypeRef = new HashMap<>();
O_FooMethod.filterChildren(new TypeFilter<>(CtLocalVariable.class)).forEach((CtLocalVariable var) -> {
nameToTypeRef.put(var.getSimpleName(), var.getType());
});
int[] count = new int[1];
O_FooMethod.filterChildren(new TypeFilter<>(CtAssignment.class)).forEach((CtAssignment ass) -> {
for (CtComment comment : ass.getComments()) {
checkIsNotSubtype(comment, nameToTypeRef);
count[0]++;
}
;
count[0]++;
checkIsSubtype(((CtVariableAccess) ass.getAssigned()).getVariable().getType(), ((CtVariableAccess) ass.getAssignment()).getVariable().getType(), nameToTypeRef);
});
assertTrue(count[0] > (9 * 8));
}
use of spoon.reflect.code.CtComment in project spoon by INRIA.
the class CtElementImpl method getDocComment.
public String getDocComment() {
for (CtComment ctComment : comments) {
if (ctComment.getCommentType() == CtComment.CommentType.JAVADOC) {
StringBuffer result = new StringBuffer();
result.append(ctComment.getContent() + System.lineSeparator());
for (CtJavaDocTag tag : ((CtJavaDoc) ctComment).getTags()) {
// the tag already contains a new line
result.append(tag.toString());
}
return result.toString();
}
}
return "";
}
use of spoon.reflect.code.CtComment in project spoon by INRIA.
the class CtElementImpl method setComments.
@Override
public <E extends CtElement> E setComments(List<CtComment> comments) {
if (comments == null || comments.isEmpty()) {
this.comments = CtElementImpl.emptyList();
return (E) this;
}
getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, COMMENT, this.comments, new ArrayList<>(this.comments));
this.comments.clear();
for (CtComment comment : comments) {
addComment(comment);
}
return (E) this;
}
Aggregations