/** * Parse the routeFile given while building the instance and fill the model. * @param model The EIP Model to fill with parsed elements from routeFile. * @throws InvalidArgumentException if given file is not a valid Spring integration file */ public void parseAndFillModel(EIPModel model) throws Exception { // Read source content. routeSource = parseRouteClass(); // Parse and get the compilation unit. ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(routeSource.toCharArray()); parser.setResolveBindings(false); routeCU = (CompilationUnit) parser.createAST(null); // Visit and build a comment map before parsing. for (Object comment : routeCU.getCommentList()) { ((Comment) comment).accept(this); } // Initialize and build a route. route = EipFactory.eINSTANCE.createRoute(); model.getOwnedRoutes().add(route); // routeCU.accept(this); }
@Override public boolean visit(CompilationUnit node) { //System.out.println("Found: " + node.getClass()); // TODO Non-javadoc comments. // It looks like the way to do this is add a doComments() method // and call it before and after every AST node. Not ideal, but // it would work. Can probably use pre/postVisit methods for this // purpose for (Object o : node.getCommentList()) { if (!(o instanceof Javadoc)) { comments.add((Comment)o); } } /*List l = node.getCommentList(); for (Object o : l) { Comment c = (Comment)o; println("COMMENT"); println(c.toString()); println("----"); println(c.getAlternateRoot().getClass().toString()); println("/COMMENT"); }*/ return super.visit(node); }
private void doComments(ASTNode node, boolean post) { Comment c = comments.peek(); while (c != null && c.getStartPosition() > endOfLastNode) { int startOfNextNode = post ? siblingOrParentPosition(node) : node.getStartPosition(); if (c.getStartPosition() + c.getLength() < startOfNextNode) { comments.remove().accept(this); c = comments.peek(); } else { break; } } endOfLastNode = node.getStartPosition() + node.getLength(); }
public void run() { parser.parse(new TagVisitor()); CommentVisitor commentVisitor = new CommentVisitor(); for (Comment comment : (List<Comment>) cunit.getCommentList()) comment.accept(commentVisitor); Collections.sort(variables); }
/** * @see ASTVisitor#visit(CompilationUnit) */ @SuppressWarnings("unchecked") @Override public boolean visit(CompilationUnit unit) { List<Comment> commentList = unit.getCommentList(); for (Comment comment : commentList) { comment.delete(); } this.numberOfLinesOfCode = (double) unit.toString().split("\n").length; return false; }
/** * Adds copyright header to the compilation unit * * @param compilationUnit * compilation unit affected * @return compilation unit change */ public CompilationUnitChange addCopyrightsHeader(final CompilationUnit compilationUnit) { final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement(); change = new CompilationUnitChange(ADD_COPYRIGHT, unit); rewriter = ASTRewrite.create(compilationUnit.getAST()); final ListRewrite listRewrite = rewriter.getListRewrite(compilationUnit.getPackage(), PackageDeclaration.ANNOTATIONS_PROPERTY); final Comment placeHolder = (Comment) rewriter.createStringPlaceholder(getCopyrightText() + NEW_LINE_SEPARATOR, ASTNode.BLOCK_COMMENT); listRewrite.insertFirst(placeHolder, null); rewriteCompilationUnit(unit, getNewUnitSource(unit, null)); return change; }
/** * Replaces copyright header to the compilation unit * * @param compilationUnit * compilation unit affected * @return compilation unit change */ public CompilationUnitChange replaceCopyrightsHeader(final CompilationUnit compilationUnit) { final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement(); change = new CompilationUnitChange(OVERRIDE_COPYRIGHT, unit); rewriter = ASTRewrite.create(compilationUnit.getAST()); final List<Comment> comments = getCommentList(compilationUnit); Comment copyrightComment = null; if (!comments.isEmpty()) { copyrightComment = comments.get(0); } rewriteCompilationUnit(unit, getNewUnitSource(unit, copyrightComment)); return change; }
/** * Checks whether {@link CompilationUnit} has copyright header * * @param compilationUnit * checked compilation unit * @return true if {@link CompilationUnit} has copyright header */ public boolean hasCopyrightsComment(final CompilationUnit compilationUnit) { final List<Comment> comments = getCommentList(compilationUnit); boolean hasCopyrights = false; if (!comments.isEmpty()) { final PackageDeclaration packageNode = compilationUnit.getPackage(); final boolean commentBeforePackage = comments.get(0).getStartPosition() <= packageNode.getStartPosition(); final boolean hasJavaDoc = packageNode.getJavadoc() != null; hasCopyrights = commentBeforePackage || hasJavaDoc; } return hasCopyrights; }
/** * Gets compilation unit's source * * @param unit * affected compilation unit * @param comment * comment to be replaced; set null if comment is not present * @return new compilation unit's source */ private String getNewUnitSource(final ICompilationUnit unit, final Comment comment) { String source = null; try { source = unit.getSource(); if (comment != null) { final int endOfComment = comment.getLength() + comment.getStartPosition(); source = source.replace(source.substring(0, endOfComment), getCopyrightText()); } } catch (final JavaModelException e) { ConsoleUtils.printError(e.getMessage()); } return source; }
private final CommentItem extractCommentItem(final int index) { if (commentsVisited[index]) { return null; } else { final Comment comment = (Comment) comments.get(index); if (comment.isDocComment()) { /* Interpret DocComment as Line or Block */ comment.delete(); } final int start = comment.getStartPosition(); final int end = start + comment.getLength(); final String value = this.src.substring(start, end); return new CommentItem(comment, value); } }
private final int getNodeFirstLeadingCommentIndex(final ASTNode node) { if (node instanceof PackageDeclaration) { if (commentsVisited.length > 0) { final Comment comment = (Comment) comments.get(0); if (comment.getStartPosition() + comment.getLength() <= ((PackageDeclaration) node).getName() .getStartPosition()) { return 0; } } return -1; } else { return cu.firstLeadingCommentIndex(node); } }
/** Extract comment content from source. */ private String getCommentContent(Comment comment) { int start = comment.getStartPosition(); int end = start + comment.getLength(); String content = routeSource.substring(start, end); if (content.startsWith("//")) { content = content.substring(2).trim(); } return content; }
@Override public void postVisit(ASTNode node) { //super.postVisit(node); // Skip CU if (node instanceof CompilationUnit || node instanceof Comment) { return; } doComments(node, true); }
private void serializeAll(CompilationUnit cu, ASTNode node, JsonGenerator jG, SerializerProvider provider) throws IOException { List<StructuralPropertyDescriptor> descriptorList = node.structuralPropertiesForType(); jG.writeStartObject(); final int Ntype = node.getNodeType(); String ClassName = node.nodeClassForType(Ntype).getName().substring(25); jG.writeFieldName("internalClass"); jG.writeString(ClassName); for (StructuralPropertyDescriptor descriptor : descriptorList) { Object child = node.getStructuralProperty(descriptor); if (child instanceof List) { serializeChildList(cu, (List<ASTNode>) child, jG, descriptor, provider); } else if (child instanceof ASTNode) { serializeChild(cu, (ASTNode) child, jG, descriptor, provider); } else if (child != null) { jG.writeFieldName(descriptor.getId()); jG.writeString(child.toString()); serializePosition(cu, node, jG); } } if (node == cu) { List<Comment> cl = cu.getCommentList(); if (!cl.isEmpty()) { jG.writeFieldName("comments"); jG.writeStartArray(); for (Comment c: (List<Comment>) cu.getCommentList()) { if (c.getParent() != null) continue; jG.writeStartObject(); final int type = c.getNodeType(); String name = c.nodeClassForType(type).getName().substring(25); jG.writeFieldName("internalClass"); jG.writeString(name); serializePosition(cu, (ASTNode)c, jG); jG.writeEndObject(); } jG.writeEndArray(); } } jG.writeEndObject(); }
protected CommentItem(final Comment node, final String value) { this.node = node; this.value = value; }
@Override public final boolean visit(final Comment node) { return false; }
@Override public void modifyCompilationUnit(CompilationUnit astRoot, IProgressMonitor monitor) throws JavaModelException, CoreException, BadLocationException { final ICompilationUnit adapter = (ICompilationUnit) astRoot .getJavaElement().getAdapter(IOpenable.class); modifiedDocument = false; if (adapter != null) { document = new Document(adapter.getSource()); List<Comment> commentList = astRoot.getCommentList(); offset = 0; for (Comment comment : commentList) { comment.accept(new ASTVisitor() { @Override public boolean visit(BlockComment node) { int startPosition = node.getStartPosition(); int endPosition = startPosition + node.getLength(); try { int lineOfOffset = document .getLineOfOffset(startPosition - offset); startPosition = document .getLineOffset(lineOfOffset); int replaceLength = endPosition - startPosition + LINE_DELIMITER_LENGTH - offset; if (startPosition > -1 && document .get() .substring( startPosition, startPosition + replaceLength) .contains(NON_JAVADOC_COMMENT)) { int replaceStart = startPosition - LINE_DELIMITER_LENGTH; document.replace(replaceStart, replaceLength, EMPTY_STRING); offset = endPosition - startPosition + LINE_DELIMITER_LENGTH; modifiedDocument = true; } } catch (BadLocationException e) { e.printStackTrace(); } return super.visit(node); } }); } if (modifiedDocument) { adapter.getBuffer().setContents(document.get()); adapter.save(monitor, true); } } }
public boolean visit(Comment node) { return visit((ASTNode)node); }
public void endVisit(Comment node) { endVisit((ASTNode)node); }
@Override public boolean visit(BlockComment node) { return visit((Comment)node); }
@Override public void endVisit(BlockComment node) { endVisit((Comment)node); }
@Override public boolean visit(Javadoc node) { return visit((Comment)node); }
@Override public void endVisit(Javadoc node) { endVisit((Comment)node); }
@Override public boolean visit(LineComment node) { return visit((Comment)node); }
@Override public void endVisit(LineComment node) { endVisit((Comment)node); }
/** * Returns list of {@link Comment} * * @param compilationUnit * compilation unit to be analyzed * @return lists of comments */ @SuppressWarnings("unchecked") private List<Comment> getCommentList(final CompilationUnit compilationUnit) { return compilationUnit.getCommentList(); }