diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java index dbb7ec3842d..07cc4bacf4c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java @@ -533,7 +533,10 @@ public void setUserData(final Object userData) { this.userData = userData; } - // TODO should we deprecate this too? + /** + * @deprecated Not all nodes have access to their tokens, use the interface {@link TokenBasedNode} instead + */ + @Deprecated public GenericToken jjtGetFirstToken() { return firstToken; } @@ -546,7 +549,10 @@ public void jjtSetFirstToken(final GenericToken token) { this.firstToken = token; } - // TODO should we deprecate this too? + /** + * @deprecated Not all nodes have access to their tokens, use the interface {@link TokenBasedNode} instead + */ + @Deprecated public GenericToken jjtGetLastToken() { return lastToken; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenBasedNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenBasedNode.java new file mode 100644 index 00000000000..b693d63108b --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenBasedNode.java @@ -0,0 +1,27 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast; + +/** + * A {@link Node} that can provide access to the underlying + * {@linkplain GenericToken tokens} produced by the lexer. + */ +public interface TokenBasedNode { + + /** + * Returns the first token producing this node. + * This is not a special token. + */ + T getFirstToken(); + + + /** + * Returns the last token producing this node. + * This is not a special token. + */ + T getLastToken(); + + +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/java/ast/impl/javacc/AbstractJjtreeNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/java/ast/impl/javacc/AbstractJjtreeNode.java new file mode 100644 index 00000000000..9fa13cc09de --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/java/ast/impl/javacc/AbstractJjtreeNode.java @@ -0,0 +1,39 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.ast.impl.javacc; + +import net.sourceforge.pmd.lang.ast.AbstractNode; +import net.sourceforge.pmd.lang.ast.GenericToken; +import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.TokenBasedNode; + +/** + * Base class for node produced by JJTree. JJTree specific functionality + * present on the API of {@link Node} and {@link AbstractNode} will be + * moved here for 7.0.0. + */ +public abstract class AbstractJjtreeNode extends AbstractNode implements TokenBasedNode { + + public AbstractJjtreeNode(int id) { + super(id); + } + + public AbstractJjtreeNode(int id, int theBeginLine, int theEndLine, int theBeginColumn, int theEndColumn) { + super(id, theBeginLine, theEndLine, theBeginColumn, theEndColumn); + } + + + @Override + @SuppressWarnings("unchecked") + public T getFirstToken() { + return (T) super.jjtGetFirstToken(); + } + + @Override + @SuppressWarnings("unchecked") + public T getLastToken() { + return (T) super.jjtGetLastToken(); + } +} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java index 1804cf9e155..7af8f775790 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java @@ -5,12 +5,12 @@ package net.sourceforge.pmd.lang.java.ast; import net.sourceforge.pmd.annotation.InternalApi; -import net.sourceforge.pmd.lang.ast.AbstractNode; +import net.sourceforge.pmd.lang.java.ast.impl.javacc.AbstractJjtreeNode; import net.sourceforge.pmd.lang.symboltable.Scope; @Deprecated @InternalApi -public abstract class AbstractJavaNode extends AbstractNode implements JavaNode { +public abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { protected JavaParser parser; private Scope scope; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaNode.java index 59ba6c3f4a3..4ce760a7796 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaNode.java @@ -6,6 +6,7 @@ import net.sourceforge.pmd.annotation.InternalApi; +import net.sourceforge.pmd.lang.ast.TokenBasedNode; import net.sourceforge.pmd.lang.symboltable.Scope; import net.sourceforge.pmd.lang.symboltable.ScopedNode; @@ -13,7 +14,7 @@ /** * Root interface for all Nodes of the Java AST. */ -public interface JavaNode extends ScopedNode { +public interface JavaNode extends ScopedNode, TokenBasedNode { /** * Calls back the visitor's visit method corresponding to the runtime type of this Node. diff --git a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNode.java b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNode.java index 97c1d3de89e..7cefac38402 100644 --- a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNode.java +++ b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNode.java @@ -4,9 +4,9 @@ package net.sourceforge.pmd.lang.jsp.ast; -import net.sourceforge.pmd.lang.ast.AbstractNode; +import net.sourceforge.pmd.lang.java.ast.impl.javacc.AbstractJjtreeNode; -public class AbstractJspNode extends AbstractNode implements JspNode { +public class AbstractJspNode extends AbstractJjtreeNode implements JspNode { protected JspParser parser; diff --git a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/ast/JspNode.java b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/ast/JspNode.java index 3f51421cc90..6e7441ea925 100644 --- a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/ast/JspNode.java +++ b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/ast/JspNode.java @@ -5,8 +5,9 @@ package net.sourceforge.pmd.lang.jsp.ast; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.TokenBasedNode; -public interface JspNode extends Node { +public interface JspNode extends Node, TokenBasedNode { /** * Accept the visitor. * */ diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/AbstractPLSQLNode.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/AbstractPLSQLNode.java index aebc9680986..310ec785182 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/AbstractPLSQLNode.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/AbstractPLSQLNode.java @@ -7,9 +7,10 @@ package net.sourceforge.pmd.lang.plsql.ast; +import net.sourceforge.pmd.lang.java.ast.impl.javacc.AbstractJjtreeNode; import net.sourceforge.pmd.lang.symboltable.Scope; -public abstract class AbstractPLSQLNode extends net.sourceforge.pmd.lang.ast.AbstractNode implements PLSQLNode { +public abstract class AbstractPLSQLNode extends AbstractJjtreeNode implements PLSQLNode { protected Object value; protected PLSQLParser parser; protected Scope scope; diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/PLSQLNode.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/PLSQLNode.java index c465306830a..c31a01f885a 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/PLSQLNode.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/ast/PLSQLNode.java @@ -5,10 +5,11 @@ package net.sourceforge.pmd.lang.plsql.ast; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.TokenBasedNode; import net.sourceforge.pmd.lang.symboltable.Scope; import net.sourceforge.pmd.lang.symboltable.ScopedNode; -public interface PLSQLNode extends Node, ScopedNode { +public interface PLSQLNode extends Node, ScopedNode, TokenBasedNode { /** Accept the visitor. **/ Object jjtAccept(PLSQLParserVisitor visitor, Object data); diff --git a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/ast/AbstractVFNode.java b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/ast/AbstractVFNode.java index 57e12782d3b..1b8ed642b30 100644 --- a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/ast/AbstractVFNode.java +++ b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/ast/AbstractVFNode.java @@ -4,9 +4,9 @@ package net.sourceforge.pmd.lang.vf.ast; -import net.sourceforge.pmd.lang.ast.AbstractNode; +import net.sourceforge.pmd.lang.java.ast.impl.javacc.AbstractJjtreeNode; -public class AbstractVFNode extends AbstractNode implements VfNode { +public class AbstractVFNode extends AbstractJjtreeNode implements VfNode { protected VfParser parser; diff --git a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/ast/VfNode.java b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/ast/VfNode.java index b8cdadea5a2..c24b4406ab4 100644 --- a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/ast/VfNode.java +++ b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/ast/VfNode.java @@ -5,13 +5,16 @@ package net.sourceforge.pmd.lang.vf.ast; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.TokenBasedNode; + +public interface VfNode extends Node, TokenBasedNode { -public interface VfNode extends Node { /** * Accept the visitor. * */ Object jjtAccept(VfParserVisitor visitor, Object data); + /** * Accept the visitor. * */ diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java index 9e552aac821..721f297630a 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/AbstractVmNode.java @@ -25,12 +25,13 @@ import org.apache.commons.lang3.text.StrBuilder; -import net.sourceforge.pmd.lang.ast.AbstractNode; +import net.sourceforge.pmd.annotation.InternalApi; +import net.sourceforge.pmd.lang.java.ast.impl.javacc.AbstractJjtreeNode; /** * */ -public class AbstractVmNode extends AbstractNode implements VmNode { +public class AbstractVmNode extends AbstractJjtreeNode implements VmNode { /** */ // TODO - It seems that this field is only valid when parsing, and should @@ -93,17 +94,18 @@ public void jjtClose() { endColumn = parser.token.endColumn; } - /** - * @param t - */ + @InternalApi + @Deprecated public void setFirstToken(final Token t) { this.first = t; } + @Override public Token getFirstToken() { return first; } + @Override public Token getLastToken() { return last; } diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/VmNode.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/VmNode.java index 3f053176f12..5e36ce749bb 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/VmNode.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/VmNode.java @@ -5,8 +5,9 @@ package net.sourceforge.pmd.lang.vm.ast; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.TokenBasedNode; -public interface VmNode extends Node { +public interface VmNode extends Node, TokenBasedNode { /** * Accept the visitor. * */