use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class ConcreteSyntaxModelTest method printParameters.
@Test
public void printParameters() {
Node node = JavaParser.parseBodyDeclaration("int x(int y, int z) {}");
assertEquals("int x(int y, int z) {" + EOL + "}", print(node));
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class CommentsInserter method insertComments.
/**
* Comments are attributed to the thing they comment and are removed from
* the comments.
*/
private void insertComments(CompilationUnit cu, TreeSet<Comment> comments) {
if (comments.isEmpty())
return;
/* I should sort all the direct children and the comments, if a comment
is the first thing then it
a comment to the CompilationUnit */
// FIXME if there is no package it could be also a comment to the following class...
// so I could use some heuristics in these cases to distinguish the two
// cases
List<Node> children = cu.getChildrenNodes();
PositionUtils.sortByBeginPosition(children);
Comment firstComment = comments.iterator().next();
if (cu.getPackage() != null && (children.isEmpty() || PositionUtils.areInOrder(firstComment, children.get(0)))) {
cu.setComment(firstComment);
comments.remove(firstComment);
}
}
use of com.github.javaparser.ast.Node in project javaparser by javaparser.
the class CommentsInserter method insertComments.
/**
* This method try to attributes the nodes received to child of the node. It
* returns the node that were not attributed.
*/
void insertComments(Node node, TreeSet<Comment> commentsToAttribute) {
if (commentsToAttribute.isEmpty())
return;
if (node instanceof CompilationUnit) {
insertComments((CompilationUnit) node, commentsToAttribute);
}
// the comments can:
// 1) Inside one of the child, then it is the child that have to
// associate them
// 2) If they are not inside a child they could be preceeding nothing, a
// comment or a child
// if they preceed a child they are assigned to it, otherweise they
// remain "orphans"
List<Node> children = node.getChildrenNodes();
PositionUtils.sortByBeginPosition(children);
for (Node child : children) {
TreeSet<Comment> commentsInsideChild = new TreeSet<>(NODE_BY_BEGIN_POSITION);
for (Comment c : commentsToAttribute) {
if (PositionUtils.nodeContains(child, c, configuration.doNotConsiderAnnotationsAsNodeStartForCodeAttribution)) {
commentsInsideChild.add(c);
}
}
commentsToAttribute.removeAll(commentsInsideChild);
insertComments(child, commentsInsideChild);
}
/* I can attribute in line comments to elements preceeding them, if
there is something contained in their line */
List<Comment> attributedComments = new LinkedList<>();
for (Comment comment : commentsToAttribute) {
if (comment.isLineComment()) {
for (Node child : children) {
if (child.getEnd().line == comment.getBegin().line && attributeLineCommentToNodeOrChild(child, comment.asLineComment())) {
attributedComments.add(comment);
}
}
}
}
/* at this point I create an ordered list of all remaining comments and
children */
Comment previousComment = null;
attributedComments = new LinkedList<>();
List<Node> childrenAndComments = new LinkedList<>();
childrenAndComments.addAll(children);
childrenAndComments.addAll(commentsToAttribute);
PositionUtils.sortByBeginPosition(childrenAndComments, configuration.doNotConsiderAnnotationsAsNodeStartForCodeAttribution);
for (Node thing : childrenAndComments) {
if (thing instanceof Comment) {
previousComment = (Comment) thing;
if (!previousComment.isOrphan()) {
previousComment = null;
}
} else {
if (previousComment != null && !thing.hasComment()) {
if (!configuration.doNotAssignCommentsPrecedingEmptyLines || !thereAreLinesBetween(previousComment, thing)) {
thing.setComment(previousComment);
attributedComments.add(previousComment);
previousComment = null;
}
}
}
}
commentsToAttribute.removeAll(attributedComments);
// all the remaining are orphan nodes
for (Comment c : commentsToAttribute) {
if (c.isOrphan()) {
node.addOrphanComment(c);
}
}
}
use of com.github.javaparser.ast.Node in project AndroidLife by CaMnter.
the class R2ClassBuilder method brewJava.
/**
* JavaPoet 生成 R2
*
* @param rFile R.java File
* @param outputDir R2.java 输出文件夹
* @param packageName 包名
* @param className R2 name
* @throws Exception exception
*/
public static void brewJava(File rFile, File outputDir, String packageName, String className) throws Exception {
/*
* JavaParser 解析 R.java File
* 获取到 TypeDeclaration
*/
CompilationUnit compilationUnit = JavaParser.parse(rFile);
TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
/*
* 定义 R2.java class
*/
TypeSpec.Builder result = TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
/*
* 遍历 R.java File 的每一个节点( 内部类或者接口 --> ClassOrInterfaceDeclaration )
* 添加到 R2.java 内
* 这里是给 TypeSpec 添加生成 内部类 的 语句
*/
for (Node node : resourceClass.getChildNodes()) {
if (node instanceof ClassOrInterfaceDeclaration) {
addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (ClassOrInterfaceDeclaration) node);
}
}
JavaFile finalR = JavaFile.builder(packageName, result.build()).addFileComment("Generated code from gradle plugin. Do not modify!").build();
finalR.writeTo(outputDir);
}
use of com.github.javaparser.ast.Node in project butterknife by JakeWharton.
the class FinalRClassBuilder method brewJava.
public static void brewJava(File rFile, File outputDir, String packageName, String className) throws Exception {
CompilationUnit compilationUnit = JavaParser.parse(rFile);
TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
TypeSpec.Builder result = TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
for (Node node : resourceClass.getChildNodes()) {
if (node instanceof ClassOrInterfaceDeclaration) {
addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (ClassOrInterfaceDeclaration) node);
}
}
JavaFile finalR = JavaFile.builder(packageName, result.build()).addFileComment("Generated code from Butter Knife gradle plugin. Do not modify!").build();
finalR.writeTo(outputDir);
}
Aggregations