[#1303] Throw exception instead of returning null.

Extend JavaDoc.
Adapt unit test.

Signed-off-by: Arne Deutsch <Arne.Deutsch@itemis.de>
This commit is contained in:
Arne Deutsch 2020-01-24 11:41:56 +01:00
parent ef65b96ec0
commit 5dec13b7b4
3 changed files with 8 additions and 8 deletions

View file

@ -193,9 +193,8 @@ public abstract class AbstractNodeTest extends Assert {
public abstract void testGetText_Default();
@Test public void testGetText_NoParent() {
AbstractNode node = createNode();
assertNull(node.getText());
@Test(expected = IllegalStateException.class) public void testGetText_NoParent() {
createNode().getText();
}
public abstract void testGetText_Empty();

View file

@ -156,6 +156,7 @@ public interface INode {
/**
* Returns the parsed text that is covered by this node (including hidden tokens). The result is never <code>null</code>
* but may be empty.
* @throws IllegalStateException if the text is not available. A possible reason is, that the node is not part of a valid node model.
* @return the parsed text that is covered by this node (including hidden tokens). Never <code>null</code>.
*/
String getText();

View file

@ -151,12 +151,12 @@ public abstract class AbstractNode implements INode, BidiTreeIterable<INode> {
@Override
public String getText() {
INode rootNode = getRootNode();
if (rootNode != null) {
int offset = getTotalOffset();
int length = getTotalLength();
return rootNode.getText().substring(offset, offset + length);
if (rootNode == null) {
throw new IllegalStateException("Can't compute text, rootNode is 'null'.");
}
return null;
int offset = getTotalOffset();
int length = getTotalLength();
return rootNode.getText().substring(offset, offset + length);
}
@Override