Add TypedNode interface
Signed-off-by: Ross Allan <ross@nallar.me>
diff --git a/javaparser-core/src/main/java/com/github/javaparser/PositionUtils.java b/javaparser-core/src/main/java/com/github/javaparser/PositionUtils.java
index 24a0b04..1fbb10b 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/PositionUtils.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/PositionUtils.java
@@ -22,6 +22,7 @@
package com.github.javaparser;
import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.body.AnnotableNode;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
@@ -105,11 +106,8 @@
}
private static Node beginNodeWithoutConsideringAnnotations(Node node) {
- if (node instanceof MethodDeclaration) {
- MethodDeclaration casted = (MethodDeclaration) node;
- return casted.getType();
- } else if (node instanceof FieldDeclaration) {
- FieldDeclaration casted = (FieldDeclaration) node;
+ if (node instanceof MethodDeclaration || node instanceof FieldDeclaration) {
+ TypedNode casted = (TypedNode) node;
return casted.getType();
} else if (node instanceof ClassOrInterfaceDeclaration) {
ClassOrInterfaceDeclaration casted = (ClassOrInterfaceDeclaration) node;
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/TypedNode.java b/javaparser-core/src/main/java/com/github/javaparser/ast/TypedNode.java
new file mode 100644
index 0000000..4228500
--- /dev/null
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/TypedNode.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
+ * Copyright (C) 2011, 2013-2015 The JavaParser Team.
+ *
+ * This file is part of JavaParser.
+ *
+ * JavaParser can be used either under the terms of
+ * a) the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * b) the terms of the Apache License
+ *
+ * You should have received a copy of both licenses in LICENCE.LGPL and
+ * LICENCE.APACHE. Please refer to those files for details.
+ *
+ * JavaParser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ */
+
+package com.github.javaparser.ast;
+
+import com.github.javaparser.ast.type.Type;
+
+/**
+ * A node having a type.
+ *
+ * The main reason for this interface is to permit users to manipulate homogeneously all nodes with getType/setType
+ * methods
+ *
+ * @since 2.3.1
+ */
+public interface TypedNode {
+ Type getType();
+
+ void setType(Type type);
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/AnnotationMemberDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/AnnotationMemberDeclaration.java
index c29a053..1ccb26b 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/AnnotationMemberDeclaration.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/AnnotationMemberDeclaration.java
@@ -23,6 +23,7 @@
import com.github.javaparser.ast.DocumentableNode;
import com.github.javaparser.ast.NamedNode;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.Expression;
@@ -35,7 +36,7 @@
/**
* @author Julio Vilmar Gesser
*/
-public final class AnnotationMemberDeclaration extends BodyDeclaration implements DocumentableNode, NamedNode {
+public final class AnnotationMemberDeclaration extends BodyDeclaration implements DocumentableNode, NamedNode, TypedNode {
private int modifiers;
@@ -100,6 +101,7 @@
return name;
}
+ @Override
public Type getType() {
return type;
}
@@ -117,6 +119,7 @@
this.name = name;
}
+ @Override
public void setType(Type type) {
this.type = type;
setAsParentNodeOf(type);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/FieldDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/FieldDeclaration.java
index dd898fd..1191e0c 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/FieldDeclaration.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/FieldDeclaration.java
@@ -22,6 +22,7 @@
package com.github.javaparser.ast.body;
import com.github.javaparser.ast.DocumentableNode;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.type.Type;
@@ -36,7 +37,7 @@
/**
* @author Julio Vilmar Gesser
*/
-public final class FieldDeclaration extends BodyDeclaration implements DocumentableNode {
+public final class FieldDeclaration extends BodyDeclaration implements DocumentableNode, TypedNode {
private int modifiers;
@@ -95,6 +96,7 @@
return modifiers;
}
+ @Override
public Type getType() {
return type;
}
@@ -108,6 +110,7 @@
this.modifiers = modifiers;
}
+ @Override
public void setType(Type type) {
this.type = type;
setAsParentNodeOf(this.type);
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 9e57e42..71f86c7 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
@@ -21,10 +21,7 @@
package com.github.javaparser.ast.body;
-import com.github.javaparser.ast.AccessSpecifier;
-import com.github.javaparser.ast.DocumentableNode;
-import com.github.javaparser.ast.NamedNode;
-import com.github.javaparser.ast.TypeParameter;
+import com.github.javaparser.ast.*;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.NameExpr;
@@ -41,7 +38,7 @@
/**
* @author Julio Vilmar Gesser
*/
-public final class MethodDeclaration extends BodyDeclaration implements DocumentableNode, WithDeclaration, NamedNode {
+public final class MethodDeclaration extends BodyDeclaration implements DocumentableNode, WithDeclaration, NamedNode, TypedNode {
private int modifiers;
@@ -152,6 +149,7 @@
return throws_;
}
+ @Override
public Type getType() {
return type;
}
@@ -192,6 +190,7 @@
setAsParentNodeOf(this.throws_);
}
+ @Override
public void setType(final Type type) {
this.type = type;
setAsParentNodeOf(this.type);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/Parameter.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/Parameter.java
index 6113d70..fdfc634 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/Parameter.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/Parameter.java
@@ -21,6 +21,7 @@
package com.github.javaparser.ast.body;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.GenericVisitor;
@@ -31,7 +32,7 @@
/**
* @author Julio Vilmar Gesser
*/
-public final class Parameter extends BaseParameter {
+public final class Parameter extends BaseParameter implements TypedNode {
private Type type;
private boolean isVarArgs;
@@ -65,6 +66,7 @@
v.visit(this, arg);
}
+ @Override
public Type getType() {
return type;
}
@@ -73,6 +75,7 @@
return isVarArgs;
}
+ @Override
public void setType(Type type) {
this.type = type;
setAsParentNodeOf(this.type);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ArrayCreationExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ArrayCreationExpr.java
index f2961e5..4672cae 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ArrayCreationExpr.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ArrayCreationExpr.java
@@ -21,6 +21,7 @@
package com.github.javaparser.ast.expr;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
@@ -32,7 +33,7 @@
/**
* @author Julio Vilmar Gesser
*/
-public final class ArrayCreationExpr extends Expression {
+public final class ArrayCreationExpr extends Expression implements TypedNode {
private Type type;
@@ -100,6 +101,7 @@
return initializer;
}
+ @Override
public Type getType() {
return type;
}
@@ -118,6 +120,7 @@
setAsParentNodeOf(this.initializer);
}
+ @Override
public void setType(Type type) {
this.type = type;
setAsParentNodeOf(this.type);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CastExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CastExpr.java
index 8433869..d4a3cdb 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CastExpr.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CastExpr.java
@@ -21,6 +21,7 @@
package com.github.javaparser.ast.expr;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
@@ -28,7 +29,7 @@
/**
* @author Julio Vilmar Gesser
*/
-public final class CastExpr extends Expression {
+public final class CastExpr extends Expression implements TypedNode {
private Type type;
@@ -62,6 +63,7 @@
return expr;
}
+ @Override
public Type getType() {
return type;
}
@@ -71,6 +73,7 @@
setAsParentNodeOf(this.expr);
}
+ @Override
public void setType(Type type) {
this.type = type;
setAsParentNodeOf(this.type);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ClassExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ClassExpr.java
index efdbce6..3407e98 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ClassExpr.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ClassExpr.java
@@ -21,6 +21,7 @@
package com.github.javaparser.ast.expr;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
@@ -33,7 +34,7 @@
* </code>
* @author Julio Vilmar Gesser
*/
-public final class ClassExpr extends Expression {
+public final class ClassExpr extends Expression implements TypedNode {
private Type type;
@@ -59,10 +60,12 @@
v.visit(this, arg);
}
+ @Override
public Type getType() {
return type;
}
+ @Override
public void setType(Type type) {
this.type = type;
setAsParentNodeOf(this.type);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/InstanceOfExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/InstanceOfExpr.java
index 9a97865..459c1a2 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/InstanceOfExpr.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/InstanceOfExpr.java
@@ -21,6 +21,7 @@
package com.github.javaparser.ast.expr;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
@@ -28,7 +29,7 @@
/**
* @author Julio Vilmar Gesser
*/
-public final class InstanceOfExpr extends Expression {
+public final class InstanceOfExpr extends Expression implements TypedNode {
private Expression expr;
@@ -61,6 +62,7 @@
return expr;
}
+ @Override
public Type getType() {
return type;
}
@@ -70,6 +72,7 @@
setAsParentNodeOf(this.expr);
}
+ @Override
public void setType(final Type type) {
this.type = type;
setAsParentNodeOf(this.type);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/TypeExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/TypeExpr.java
index 636d2c4..482ca9c 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/TypeExpr.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/TypeExpr.java
@@ -21,6 +21,7 @@
package com.github.javaparser.ast.expr;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
@@ -30,7 +31,7 @@
* @author Raquel Pau
*
*/
-public class TypeExpr extends Expression{
+public class TypeExpr extends Expression implements TypedNode {
private Type type;
@@ -51,10 +52,12 @@
v.visit(this, arg);
}
+ @Override
public Type getType() {
return type;
}
+ @Override
public void setType(Type type) {
this.type = type;
setAsParentNodeOf(this.type);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/VariableDeclarationExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/VariableDeclarationExpr.java
index ca03ef3..8d32e5d 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/VariableDeclarationExpr.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/VariableDeclarationExpr.java
@@ -21,6 +21,7 @@
package com.github.javaparser.ast.expr;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.body.ModifierSet;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.type.Type;
@@ -35,7 +36,7 @@
/**
* @author Julio Vilmar Gesser
*/
-public final class VariableDeclarationExpr extends Expression {
+public final class VariableDeclarationExpr extends Expression implements TypedNode {
private int modifiers;
@@ -92,6 +93,7 @@
return modifiers;
}
+ @Override
public Type getType() {
return type;
}
@@ -110,6 +112,7 @@
this.modifiers = modifiers;
}
+ @Override
public void setType(final Type type) {
this.type = type;
setAsParentNodeOf(this.type);
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/type/ReferenceType.java b/javaparser-core/src/main/java/com/github/javaparser/ast/type/ReferenceType.java
index 9141395..4b68eb2 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/type/ReferenceType.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/type/ReferenceType.java
@@ -21,6 +21,7 @@
package com.github.javaparser.ast.type;
+import com.github.javaparser.ast.TypedNode;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
@@ -32,7 +33,7 @@
/**
* @author Julio Vilmar Gesser
*/
-public final class ReferenceType extends Type {
+public final class ReferenceType extends Type implements TypedNode {
private Type type;
@@ -81,6 +82,7 @@
return arrayCount;
}
+ @Override
public Type getType() {
return type;
}
@@ -89,6 +91,7 @@
this.arrayCount = arrayCount;
}
+ @Override
public void setType(final Type type) {
this.type = type;
setAsParentNodeOf(this.type);