diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index cb438bd2f..0651a333d 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -63,7 +63,7 @@ import java.util.List; * satisfy the superclass interface. */ public class ParserRuleContext extends RuleContext { - public static final ParserRuleContext EMPTY = new ParserRuleContext(); + public static final ParserRuleContext EMPTY = new ParserRuleContext(); /** If we are debugging or building a parse tree for a visitor, * we need to track all of the tokens and rule invocations associated @@ -179,13 +179,15 @@ public class ParserRuleContext extends RuleContext { return children!=null ? children.get(i) : null; } - public Object getChild(Class ctxType, int i) { + public T getChild(Class ctxType, int i) { if ( children==null ) throw new UnsupportedOperationException("there are no children"); int j = -1; // what element have we found with ctxType? for (Object o : children) { - if ( ctxType.isAssignableFrom(o.getClass()) ) { + if ( ctxType.isInstance(o) ) { j = j+1; - if ( j == i ) return o; + if ( j == i ) { + return ctxType.cast(o); + } } } return null; @@ -195,11 +197,16 @@ public class ParserRuleContext extends RuleContext { if ( children==null ) throw new UnsupportedOperationException("there are no children"); int j = -1; // what token with ttype have we found? for (Object o : children) { - if ( o instanceof TerminalNode ) { - TerminalNode tnode = (TerminalNode)o; - if ( tnode.getSymbol().getType()==ttype ) { - j++; - if ( j == i ) return tnode.getSymbol(); + if ( o instanceof TerminalNode ) { + TerminalNode tnode = (TerminalNode)o; + if ( tnode.getSymbol() instanceof Token ) { + Token symbol = (Token)tnode.getSymbol(); + if ( symbol.getType()==ttype ) { + j++; + if ( j == i ) { + return symbol; + } + } } } } @@ -218,17 +225,20 @@ public class ParserRuleContext extends RuleContext { return tokens; } - public ParserRuleContext getRuleContext(Class ctxType, int i) { - return (ParserRuleContext)getChild(ctxType, i); + public > T getRuleContext(Class ctxType, int i) { + return getChild(ctxType, i); } - public List getRuleContexts(Class ctxType) { + public > List getRuleContexts(Class ctxType) { if ( children==null ) throw new UnsupportedOperationException("there are no children"); - List contexts = null; + List contexts = null; for (Object o : children) { - if ( o.getClass().isInstance(ctxType) ) { - if ( contexts==null ) contexts = new ArrayList(); - contexts.add((ParserRuleContext)o); + if ( ctxType.isInstance(o) ) { + if ( contexts==null ) { + contexts = new ArrayList(); + } + + contexts.add(ctxType.cast(o)); } } return contexts; @@ -247,7 +257,7 @@ public class ParserRuleContext extends RuleContext { public String toString(@NotNull Recognizer recog, RuleContext stop) { if ( recog==null ) return super.toString(recog, stop); StringBuilder buf = new StringBuilder(); - ParserRuleContext p = this; + ParserRuleContext p = this; buf.append("["); while ( p != null && p != stop ) { ATN atn = recog.getATN(); @@ -258,7 +268,7 @@ public class ParserRuleContext extends RuleContext { // ATNState invoker = atn.states.get(ctx.invokingState); // RuleTransition rt = (RuleTransition)invoker.transition(0); // buf.append(recog.getRuleNames()[rt.target.ruleIndex]); - p = (ParserRuleContext)p.parent; + p = (ParserRuleContext)p.parent; } buf.append("]"); return buf.toString();