public boolean contains(Node other){ if (getBeginLine()>other.getBeginLine()) return false; if (getBeginLine()==other.getBeginLine() && getBeginColumn()>other.getBeginColumn()) return false; if (getEndLine()<other.getEndLine()) return false; if (getEndLine()==other.getEndLine() && getEndColumn()<other.getEndColumn()) return false; return true; }
/** walks up the tree until reaching the CompilationUnit. */ private CompilationUnit getCompilationUnit(Node n) { Node unit = n; while (!(unit instanceof CompilationUnit) && unit != null) { unit = unit.getParentNode(); } return (CompilationUnit) unit; }
protected <T extends Node> T cloneNodes(T _node, Object _arg) { if (_node == null) return null; Node r = _node.accept(this, _arg); if (r == null) return null; return (T) r; } }
public boolean isPositionedBefore(int line, int column){ if (line==ABSOLUTE_END_LINE) return true; if (getEndLine()<line){ return true; } else if (getEndLine()==line){ return getEndColumn()<column; } else { return false; } } }
public boolean isPositionedAfter(int line, int column){ if (line==ABSOLUTE_BEGIN_LINE) return true; if (getBeginLine()>line){ return true; } else if (getBeginLine()==line){ return getBeginColumn()>column; } else { return false; } }
final String clip(Node node) { CU cu = getCU(); // Get offset of the fragment int from = cu.sb.offset(Position.get(node.getBeginLine() - 1, 0)); int to = cu.sb.offset(Position.get(node.getEndLine() - 1, node.getEndColumn())); // Get relevant chars return cu.source.substring(from, to); }
/** * This is the list of Comment which are contained in the Node either because * they are properly associated to one of its children or because they are floating * around inside the Node. * @return */ public List<Comment> getAllContainedComments(){ List<Comment> comments = new LinkedList<Comment>(); comments.addAll(getOrphanComments()); for (Node child : getChildrenNodes()){ if (child.getComment()!=null){ comments.add(child.getComment()); } comments.addAll(child.getAllContainedComments()); } return comments; }
private void printOrphanCommentsBeforeThisChildNode(final Node node){ if (node instanceof Comment) return; Node parent = node.getParentNode(); if (parent==null) return; List<Node> everything = new LinkedList<Node>(); everything.addAll(parent.getChildrenNodes()); sortByBeginPosition(everything); int positionOfTheChild = -1; for (int i=0;i<everything.size();i++){ if (everything.get(i)==node) positionOfTheChild=i; } if (positionOfTheChild==-1) throw new RuntimeException("My index not found!!! "+node); int positionOfPreviousChild = -1; for (int i=positionOfTheChild-1;i>=0 && positionOfPreviousChild==-1;i--){ if (!(everything.get(i) instanceof Comment)) positionOfPreviousChild = i; } for (int i=positionOfPreviousChild+1;i<positionOfTheChild;i++){ Node nodeToPrint = everything.get(i); if (!(nodeToPrint instanceof Comment)) throw new RuntimeException("Expected comment, instead "+nodeToPrint.getClass()+". Position of previous child: "+positionOfPreviousChild+", position of child "+positionOfTheChild); nodeToPrint.accept(this,null); } }
List<Node> children = node.getChildrenNodes(); sortByBeginPosition(children); if (child.contains(c)){ commentsInsideChild.add(c); } else { if (previousComment!=null){ thing.setComment(previousComment); attributedComments.add(previousComment); previousComment = null; node.addOrphanComment(c);
private static void placeOrphanComment(Node node,Comment comment){ for (Node child : node.getChildrenNodes()){ if (child.contains(comment)){ placeOrphanComment(child,comment); return; } } node.addOrphanComment(comment); }
public static Position endOf(Node node){ return new Position(node.getEndColumn(),node.getEndColumn()); }
private void printOrphanCommentsEnding(final Node node){ List<Node> everything = new LinkedList<Node>(); everything.addAll(node.getChildrenNodes()); sortByBeginPosition(everything); if (everything.size()==0) return; int commentsAtEnd = 0; boolean findingComments = true; while (findingComments){ Node last = everything.get(everything.size()-1-commentsAtEnd); findingComments = (last instanceof Comment); if (findingComments) commentsAtEnd++; } for (int i=0;i<commentsAtEnd;i++){ everything.get(everything.size()-commentsAtEnd+i).accept(this,null); } }
public static Position beginOf(Node node){ return new Position(node.getBeginColumn(),node.getBeginColumn()); }
public void visitDepthFirst(Node node){ process(node); for (Node child : node.getChildrenNodes()){ visitDepthFirst(child); } }
public static boolean areInOrder(Node a, Node b){ return (a.getBeginLine()<b.getBeginLine()) || (a.getBeginLine()==b.getBeginLine() && a.getBeginColumn()<b.getBeginColumn() ); } }
@Override public final NodeData resolve(Node node, String mappedClass) { if (!(node instanceof FieldDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on FieldDeclaration"); } final FieldDeclaration field = (FieldDeclaration) node; if (ResolverUtil.canFieldBeAnnotated(field)) { final TypeDeclaration dclr = (TypeDeclaration) node.getParentNode(); final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String fullyQualifiedClass = pckg + "." + name; final boolean mappedColumn = OjbUtil.isMappedColumn(mappedClass, ParserUtil.getFieldName(field), descriptorRepositories); if (mappedColumn) { return getAnnotationNodes(fullyQualifiedClass, ParserUtil.getFieldName(field), mappedClass); } } return null; }
private <T extends Node> boolean nodeEquals(final T n1, final T n2) { if (n1 == n2) { return true; } if (n1 == null) { if (n2 == null) { return true; } return false; } else if (n2 == null) { return false; } if (n1.getClass() != n2.getClass()) { return false; } return n1.accept(this, n2).booleanValue(); }
/** * Return the String representation of this node. * * @return the String representation of this node */ @Override public final String toString() { final DumpVisitor visitor = new DumpVisitor(); accept(visitor, null); return visitor.getSource(); }