Merge pull request #187 from ftomassetti/parse_annotations_around_typerefs
Parse annotations around reference types
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/MethodDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/MethodDeclaration.java
index 4c8f5eb..4c5a4a5 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/MethodDeclaration.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/MethodDeclaration.java
@@ -29,6 +29,7 @@
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
+import com.github.javaparser.ast.type.ReferenceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
@@ -54,7 +55,7 @@
private int arrayCount;
- private List<NameExpr> throws_;
+ private List<ReferenceType> throws_;
private BlockStmt body;
@@ -78,7 +79,7 @@
public MethodDeclaration(final int modifiers, final List<AnnotationExpr> annotations,
final List<TypeParameter> typeParameters, final Type type, final String name,
- final List<Parameter> parameters, final int arrayCount, final List<NameExpr> throws_, final BlockStmt block) {
+ final List<Parameter> parameters, final int arrayCount, final List<ReferenceType> throws_, final BlockStmt block) {
super(annotations);
setModifiers(modifiers);
setTypeParameters(typeParameters);
@@ -93,7 +94,7 @@
public MethodDeclaration(final int beginLine, final int beginColumn, final int endLine, final int endColumn,
final int modifiers, final List<AnnotationExpr> annotations,
final List<TypeParameter> typeParameters, final Type type, final String name,
- final List<Parameter> parameters, final int arrayCount, final List<NameExpr> throws_, final BlockStmt block) {
+ final List<Parameter> parameters, final int arrayCount, final List<ReferenceType> throws_, final BlockStmt block) {
super(beginLine, beginColumn, endLine, endColumn, annotations);
setModifiers(modifiers);
setTypeParameters(typeParameters);
@@ -145,7 +146,7 @@
return parameters;
}
- public List<NameExpr> getThrows() {
+ public List<ReferenceType> getThrows() {
throws_ = ensureNotNull(throws_);
return throws_;
}
@@ -185,7 +186,7 @@
setAsParentNodeOf(this.parameters);
}
- public void setThrows(final List<NameExpr> throws_) {
+ public void setThrows(final List<ReferenceType> throws_) {
this.throws_ = throws_;
setAsParentNodeOf(this.throws_);
}
@@ -276,7 +277,7 @@
sb.append(")");
if (includingThrows) {
boolean firstThrow = true;
- for (NameExpr thr : getThrows()) {
+ for (ReferenceType thr : getThrows()) {
if (firstThrow) {
firstThrow = false;
sb.append(" throws ");
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/CloneVisitor.java b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/CloneVisitor.java
index 17d976e..1aa5a4e 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/CloneVisitor.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/CloneVisitor.java
@@ -281,7 +281,7 @@
List<TypeParameter> typeParameters = visit(_n.getTypeParameters(), _arg);
Type type_ = cloneNodes(_n.getType(), _arg);
List<Parameter> parameters = visit(_n.getParameters(), _arg);
- List<NameExpr> throws_ = visit(_n.getThrows(), _arg);
+ List<ReferenceType> throws_ = visit(_n.getThrows(), _arg);
BlockStmt block = cloneNodes(_n.getBody(), _arg);
Comment comment = cloneNodes(_n.getComment(), _arg);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/DumpVisitor.java b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/DumpVisitor.java
index ada8ce5..c82877c 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/DumpVisitor.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/DumpVisitor.java
@@ -1061,8 +1061,8 @@
if (!isNullOrEmpty(n.getThrows())) {
printer.print(" throws ");
- for (final Iterator<NameExpr> i = n.getThrows().iterator(); i.hasNext();) {
- final NameExpr name = i.next();
+ for (final Iterator<ReferenceType> i = n.getThrows().iterator(); i.hasNext();) {
+ final ReferenceType name = i.next();
name.accept(this, arg);
if (i.hasNext()) {
printer.print(", ");
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/GenericVisitorAdapter.java b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/GenericVisitorAdapter.java
index db729bd..5baff83 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/GenericVisitorAdapter.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/GenericVisitorAdapter.java
@@ -1068,7 +1068,7 @@
}
}
if (n.getThrows() != null) {
- for (final NameExpr name : n.getThrows()) {
+ for (final ReferenceType name : n.getThrows()) {
{
R result = name.accept(this, arg);
if (result != null) {
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/ModifierVisitorAdapter.java b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/ModifierVisitorAdapter.java
index 78cd30a..6315d32 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/ModifierVisitorAdapter.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/ModifierVisitorAdapter.java
@@ -675,10 +675,10 @@
}
removeNulls(parameters);
}
- final List<NameExpr> throwz = n.getThrows();
+ final List<ReferenceType> throwz = n.getThrows();
if (throwz != null) {
for (int i = 0; i < throwz.size(); i++) {
- throwz.set(i, (NameExpr) throwz.get(i).accept(this, arg));
+ throwz.set(i, (ReferenceType) throwz.get(i).accept(this, arg));
}
removeNulls(throwz);
}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/VoidVisitorAdapter.java b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/VoidVisitorAdapter.java
index 17da52a..cf4c8cf 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/VoidVisitorAdapter.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/VoidVisitorAdapter.java
@@ -559,7 +559,7 @@
}
}
if (n.getThrows() != null) {
- for (final NameExpr name : n.getThrows()) {
+ for (final ReferenceType name : n.getThrows()) {
name.accept(this, arg);
}
}
diff --git a/javaparser-core/src/main/javacc/java_1_8.jj b/javaparser-core/src/main/javacc/java_1_8.jj
index ec4d466..7834b06 100644
--- a/javaparser-core/src/main/javacc/java_1_8.jj
+++ b/javaparser-core/src/main/javacc/java_1_8.jj
@@ -1302,12 +1302,10 @@
{
List ret = new LinkedList();
ClassOrInterfaceType cit;
- AnnotationExpr ann;
- List annotations = null;
}
{
- "implements" (ann = Annotation() { annotations = add(annotations, ann);})* cit = ClassOrInterfaceType() { cit.setAnnotations(annotations); ret.add(cit); }
- ( "," cit = ClassOrInterfaceType() { ret.add(cit); } )*
+ "implements" cit = ClassOrInterfaceTypeWithAnnotations() { ret.add(cit); }
+ ( "," cit = ClassOrInterfaceTypeWithAnnotations() { ret.add(cit); } )*
{
if (isInterface)
throwParseException(token, "An interface cannot implement other interfaces");
@@ -1571,15 +1569,16 @@
BlockStmt block = null;
int line = modifier.beginLine;
int column = modifier.beginColumn;
+ Type throwType;
}
{
// Modifiers already matched in the caller!
[ typeParameters = TypeParameters() { int[] lineCol=(int[])typeParameters.remove(0); if(line==-1){ line=lineCol[0]; column=lineCol[1];} } ]
type = ResultType() { if(line==-1){line=type.getBeginLine(); column=type.getBeginColumn();}}
name = Name() parameters = FormalParameters() ( "[" "]" { arrayCount++; } )*
- [ "throws" throws_ = NameList() ]
+ [ "throws" throwType = ReferenceTypeWithAnnotations() { throws_ = add(throws_, throwType); }
+ ("," throwType = ReferenceTypeWithAnnotations() { throws_ = add(throws_, throwType); })* ]
( block = Block() | ";" )
-
{
MethodDeclaration tmp = new MethodDeclaration(line, column, token.endLine, token.endColumn, modifier.modifiers, modifier.annotations, typeParameters, type, null, parameters, arrayCount, throws_, block);
tmp.setNameExpr(name);
@@ -1587,6 +1586,23 @@
}
}
+ReferenceType ReferenceTypeWithAnnotations():
+{
+ List annotations = new ArrayList();
+ AnnotationExpr annotation = null;
+ ReferenceType type;
+}
+{
+ (annotation = Annotation() { annotations = add(annotations, annotation); } )* type = ReferenceType() {
+ if (type.getAnnotations() != null) {
+ type.getAnnotations().addAll(annotations);
+ } else {
+ type.setAnnotations(annotations);
+ }
+ return type;
+ }
+}
+
List FormalParameters():
{
List ret = null;
@@ -1796,6 +1812,24 @@
{ return ret; }
}
+ClassOrInterfaceType ClassOrInterfaceTypeWithAnnotations():
+{
+ List annotations = new ArrayList();
+ AnnotationExpr annotation = null;
+ ClassOrInterfaceType type;
+}
+{
+ (annotation = Annotation() { annotations = add(annotations, annotation); } )* type = ClassOrInterfaceType() {
+ if (type.getAnnotations() != null) {
+ type.getAnnotations().addAll(annotations);
+ } else {
+ type.setAnnotations(annotations);
+ }
+ return type;
+ }
+}
+
+
List TypeArguments():
{
List ret = new LinkedList();
diff --git a/javaparser-testing/src/test/resources/com/github/javaparser/bdd/visitor_scenarios.story b/javaparser-testing/src/test/resources/com/github/javaparser/bdd/visitor_scenarios.story
index 0c87c62..27bccb0 100644
--- a/javaparser-testing/src/test/resources/com/github/javaparser/bdd/visitor_scenarios.story
+++ b/javaparser-testing/src/test/resources/com/github/javaparser/bdd/visitor_scenarios.story
@@ -73,5 +73,4 @@
Given a VoidVisitorAdapter with a visit method that asserts sensible line positions
When the "JavaConcepts.java" is parsed
When the CompilationUnit is visited by the PositionTestVisitor
-Then the total number of nodes visited is 1334
-
+Then the total number of nodes visited is 1338