Skip to content

Commit

Permalink
Merge 767ae7b into cb168d9
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid authored Oct 11, 2016
2 parents cb168d9 + 767ae7b commit 4d86834
Show file tree
Hide file tree
Showing 50 changed files with 575 additions and 440 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.utils.PositionUtils;
import com.github.javaparser.utils.Utils;

import java.util.*;

Expand Down Expand Up @@ -60,13 +62,13 @@ private void insertComments(CompilationUnit cu, TreeSet<Comment> comments) {
// so I could use some heuristics in these cases to distinguish the two
// cases

List<Node> children = cu.getChildrenNodes();
List<Node> children = Utils.copyList(cu.getChildrenNodes());
PositionUtils.sortByBeginPosition(children);

Comment firstComment = comments.iterator().next();
if (cu.getPackage() != null
&& (children.isEmpty() || PositionUtils.areInOrder(
firstComment, children.get(0)))) {
firstComment, cu.getPackage()))) {
cu.setComment(firstComment);
comments.remove(firstComment);
}
Expand All @@ -92,7 +94,7 @@ void insertComments(Node node, TreeSet<Comment> commentsToAttribute) {
// if they preceed a child they are assigned to it, otherweise they
// remain "orphans"

List<Node> children = node.getChildrenNodes();
List<Node> children = Utils.copyList(node.getChildrenNodes());
PositionUtils.sortByBeginPosition(children);

for (Node child : children) {
Expand Down Expand Up @@ -127,7 +129,16 @@ && attributeLineCommentToNodeOrChild(child,
Comment previousComment = null;
attributedComments = new LinkedList<>();
List<Node> childrenAndComments = new LinkedList<>();
childrenAndComments.addAll(children);
for (Node child : children) {
// Avoid attributing comments to a meaningless container.
if (child instanceof NodeList) {
for (Node subChild : (NodeList<Node>) child) {
childrenAndComments.add(subChild);
}
} else {
childrenAndComments.add(child);
}
}
childrenAndComments.addAll(commentsToAttribute);
PositionUtils.sortByBeginPosition(childrenAndComments,
configuration.doNotConsiderAnnotationsAsNodeStartForCodeAttribution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static com.github.javaparser.ParseStart.*;
import static com.github.javaparser.Providers.UTF8;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.utils.Utils.assertNotNull;

/**
* Parse Java source code and creates Abstract Syntax Trees.
Expand Down Expand Up @@ -86,6 +87,8 @@ private ASTParser getParserForProvider(Provider provider) {
* @return the parse result, a collection of encountered problems, and some extra data.
*/
public <N extends Node> ParseResult<N> parse(ParseStart<N> start, Provider provider) {
assertNotNull(start);
assertNotNull(provider);
try {
final ASTParser parser = getParserForProvider(provider);
N resultNode = start.parse(parser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ public class ParseResult<T> {
* Used when parsing failed completely with an exception.
*/
ParseResult(Throwable throwable) {
this(Optional.empty(), singletonList(new Problem(throwable.getMessage(), Optional.empty(), Optional.of(throwable))), Optional.empty(), Optional.empty());
this(Optional.empty(), singletonList(
new Problem(createMessage(throwable), Optional.empty(), Optional.of(throwable))), Optional.empty(), Optional.empty());
}

private static String createMessage(Throwable throwable) {
String message = throwable.getMessage();
if (message == null) {
return throwable.getClass().getSimpleName();
}
return message;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

import com.github.javaparser.Range;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;

import java.util.List;

import static com.github.javaparser.utils.Utils.ensureNotNull;
import static com.github.javaparser.utils.Utils.assertNotNull;

/**
* In, for example, <code>int[] a[];</code> there are two ArrayBracketPair objects,
* one for the [] after int, one for the [] after a.
*/
public class ArrayBracketPair extends Node implements NodeWithAnnotations<ArrayBracketPair> {
private List<AnnotationExpr> annotations;
private NodeList<AnnotationExpr> annotations = new NodeList<>();

public ArrayBracketPair(Range range, List<AnnotationExpr> annotations) {
public ArrayBracketPair(Range range, NodeList<AnnotationExpr> annotations) {
super(range);
setAnnotations(annotations);
}
Expand All @@ -31,14 +28,13 @@ public ArrayBracketPair(Range range, List<AnnotationExpr> annotations) {
v.visit(this, arg);
}

public List<AnnotationExpr> getAnnotations() {
annotations = ensureNotNull(annotations);
public NodeList<AnnotationExpr> getAnnotations() {
return annotations;
}

public ArrayBracketPair setAnnotations(List<AnnotationExpr> annotations) {
public ArrayBracketPair setAnnotations(NodeList<AnnotationExpr> annotations) {
setAsParentNodeOf(annotations);
this.annotations = annotations;
this.annotations = assertNotNull(annotations);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;

import java.util.List;

import static com.github.javaparser.utils.Utils.ensureNotNull;
import static com.github.javaparser.utils.Utils.assertNotNull;

/**
* In <code>new int[1][2];</code> there are two ArrayCreationLevel objects,
Expand All @@ -18,9 +16,9 @@
*/
public class ArrayCreationLevel extends Node implements NodeWithAnnotations<ArrayCreationLevel> {
private Expression dimension;
private List<AnnotationExpr> annotations;
private NodeList<AnnotationExpr> annotations = new NodeList<>();

public ArrayCreationLevel(Range range, Expression dimension, List<AnnotationExpr> annotations) {
public ArrayCreationLevel(Range range, Expression dimension, NodeList<AnnotationExpr> annotations) {
super(range);
setDimension(dimension);
setAnnotations(annotations);
Expand All @@ -43,14 +41,13 @@ public Expression getDimension() {
return dimension;
}

public List<AnnotationExpr> getAnnotations() {
annotations = ensureNotNull(annotations);
public NodeList<AnnotationExpr> getAnnotations() {
return annotations;
}

public ArrayCreationLevel setAnnotations(List<AnnotationExpr> annotations) {
public ArrayCreationLevel setAnnotations(NodeList<AnnotationExpr> annotations) {
setAsParentNodeOf(annotations);
this.annotations = annotations;
this.annotations = assertNotNull(annotations);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.imports.*;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import com.github.javaparser.utils.ClassUtils;
Expand All @@ -36,7 +35,9 @@
import java.util.List;
import java.util.stream.Collectors;

import static com.github.javaparser.ast.NodeList.*;
import static com.github.javaparser.ast.expr.NameExpr.name;
import static com.github.javaparser.utils.Utils.assertNotNull;
import static com.github.javaparser.utils.Utils.ensureNotNull;

/**
Expand All @@ -60,21 +61,21 @@ public final class CompilationUnit extends Node {

private PackageDeclaration pakage;

private List<ImportDeclaration> imports;
private NodeList<ImportDeclaration> imports = new NodeList<>();

private List<TypeDeclaration<?>> types;
private NodeList<TypeDeclaration<?>> types = emptyNodeList();

public CompilationUnit() {
}

public CompilationUnit(PackageDeclaration pakage, List<ImportDeclaration> imports, List<TypeDeclaration<?>> types) {
public CompilationUnit(PackageDeclaration pakage, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types) {
setPackage(pakage);
setImports(imports);
setTypes(types);
}

public CompilationUnit(Range range, PackageDeclaration pakage, List<ImportDeclaration> imports,
List<TypeDeclaration<?>> types) {
public CompilationUnit(Range range, PackageDeclaration pakage, NodeList<ImportDeclaration> imports,
NodeList<TypeDeclaration<?>> types) {
super(range);
setPackage(pakage);
setImports(imports);
Expand Down Expand Up @@ -113,8 +114,7 @@ public List<Comment> getComments() {
*
* @return the list of imports or <code>null</code> if there is no import
*/
public List<ImportDeclaration> getImports() {
imports = ensureNotNull(imports);
public NodeList<ImportDeclaration> getImports() {
return imports;
}

Expand All @@ -139,8 +139,7 @@ public PackageDeclaration getPackage() {
* @see EmptyTypeDeclaration
* @see EnumDeclaration
*/
public List<TypeDeclaration<?>> getTypes() {
types = ensureNotNull(types);
public NodeList<TypeDeclaration<?>> getTypes() {
return types;
}

Expand All @@ -161,8 +160,8 @@ public CompilationUnit setComments(List<Comment> comments) {
* @param imports
* the list of imports
*/
public CompilationUnit setImports(List<ImportDeclaration> imports) {
this.imports = imports;
public CompilationUnit setImports(NodeList<ImportDeclaration> imports) {
this.imports = assertNotNull(imports);
setAsParentNodeOf(this.imports);
return this;
}
Expand All @@ -186,8 +185,8 @@ public CompilationUnit setPackage(PackageDeclaration pakage) {
* @param types
* the lis of types
*/
public CompilationUnit setTypes(List<TypeDeclaration<?>> types) {
this.types = types;
public CompilationUnit setTypes(NodeList<TypeDeclaration<?>> types) {
this.types = assertNotNull(types);
setAsParentNodeOf(this.types);
return this;
}
Expand Down
19 changes: 16 additions & 3 deletions javaparser-core/src/main/java/com/github/javaparser/ast/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

import java.util.*;

import static java.util.Collections.*;

/**
* Abstract class for all nodes of the AST.
*
Expand Down Expand Up @@ -241,12 +243,19 @@ public <T> T getParentNodeOfType(Class<T> classType) {
return null;
}

/**
* Contains all nodes that have this node set as their parent.
* You can add nodes to it by setting a node's parent to this node.
* You can remove nodes from it by setting a child node's parent to something other than this node.
*
* @return all nodes that have this node as their parent.
*/
public List<Node> getChildrenNodes() {
return childrenNodes;
return unmodifiableList(childrenNodes);
}

public boolean contains(Node other) {
return range.contains(other.range);
public <N extends Node> boolean containsWithin(N other) {
return range.contains(other.getRange());
}

public void addOrphanComment(Comment comment) {
Expand Down Expand Up @@ -407,6 +416,7 @@ public <M> void setUserData(UserDataKey<M> key, M object) {
* @throws RuntimeException if it fails in an unexpected way
*/
public boolean remove() {
Node parentNode = this.parentNode;
if (parentNode == null)
return false;
boolean success = false;
Expand All @@ -422,6 +432,9 @@ public boolean remove() {
Collection<?> l = (Collection<?>) object;
boolean remove = l.remove(this);
success |= remove;
} else if (NodeList.class.isAssignableFrom(object.getClass())) {
NodeList<Node> l = (NodeList<Node>) object;
success |= l.remove(this);
} else if (Optional.class.equals(f.getType())) {
Optional<?> opt = (Optional<?>) object;
if (opt.isPresent())
Expand Down
Loading

0 comments on commit 4d86834

Please sign in to comment.