diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SharingCharSeq.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SharingCharSeq.java index c8f0d659ad..06d1c01447 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SharingCharSeq.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SharingCharSeq.java @@ -24,17 +24,26 @@ public final class SharingCharSeq implements RichCharSequence { private final int offset; private final int count; - private String myStr = null; + private String myStr; public SharingCharSeq(String str) { - this(null, -1, str.length()); - myStr = str; + this(str, 0, str.length()); } - public SharingCharSeq(char[] value, int offset, int count) { + // those are for subsequences + + private SharingCharSeq(String str, int offset, int count) { + this.myStr = str; + this.offset = offset; + this.count = count; + this.value = null; + } + + private SharingCharSeq(char[] value, int offset, int count) { this.value = value; this.offset = offset; this.count = count; + this.myStr = null; } @@ -45,15 +54,12 @@ public final class SharingCharSeq implements RichCharSequence { @Override public char charAt(int index) { - if (value == null) { - return myStr.charAt(index); - } if (index < 0 || index >= count) { throw new IndexOutOfBoundsException("Index out of bounds: " + index + " not in [0," + count + "["); } - return value[offset + index]; + return value == null ? myStr.charAt(offset + index) : value[offset + index]; } @Override @@ -61,7 +67,9 @@ public final class SharingCharSeq implements RichCharSequence { if (start < 0 || end > count || start > end) { throw new IndexOutOfBoundsException("Invalid range: [" + start + "," + end + "[ not in [0," + count + "["); } - return start == end ? EMPTY : new SharingCharSeq(value, offset + start, end - start); + return start == end ? EMPTY + : value == null ? new SharingCharSeq(myStr, offset + start, end - start) + : new SharingCharSeq(value, offset + start, end - start); } @Override 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 bf9062a812..3a57ea62a7 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 @@ -129,12 +129,12 @@ abstract class AbstractJavaNode extends AbstractNode implements JavaNode { @Override public int getStartOffset() { - return ((JavaccToken) jjtGetFirstToken()).getStartInDocument(); + return jjtGetFirstToken().getStartInDocument(); } @Override public int getEndOffset() { - return ((JavaccToken) jjtGetLastToken()).getEndInDocument(); + return jjtGetLastToken().getEndInDocument(); } @@ -146,6 +146,10 @@ abstract class AbstractJavaNode extends AbstractNode implements JavaNode { comment = theComment; } + @Override + public GenericToken jjtGetFirstToken() { + return super.jjtGetFirstToken(); + } @Override public Comment comment() {