Grammar { public static List<Field> getAllRuleFields(Class grammarClass) { List<Field> ruleFields = getRuleFields(grammarClass); Class superClass = grammarClass.getSuperclass(); while (superClass != null) { ruleFields.addAll(getRuleFields(superClass)); superClass = superClass.getSuperclass(); } return ruleFields; } Grammar(); static List<Field> getRuleFields(Class grammarClass); static List<Field> getAllRuleFields(Class grammarClass); Rule rule(GrammarRuleKey ruleKey); abstract Rule getRootRule();  }
@Test public void testGetAllRuleFields() { List<Field> ruleFields = Grammar.getAllRuleFields(MyGrammar.class); assertThat(ruleFields.size()).isEqualTo(5); }
@Test public void should_automatically_instanciate_lexerful_rules() throws IllegalAccessException { List<Field> ruleFields = Grammar.getAllRuleFields(MyGrammar.class); Grammar grammar = new MyGrammar(); for (Field ruleField : ruleFields) { ruleField.setAccessible(true); assertThat(ruleField.get(grammar)).as("Current rule name = " + ruleField.getName()).isNotNull().isInstanceOf(RuleDefinition.class); } }
@Test public void should_automatically_instanciate_lexerless_rules() throws IllegalAccessException { List<Field> ruleFields = Grammar.getAllRuleFields(MyLexerlessGrammar.class); LexerlessGrammar grammar = new MyLexerlessGrammar(); for (Field ruleField : ruleFields) { ruleField.setAccessible(true); assertThat(ruleField.get(grammar)).as("Current rule name = " + ruleField.getName()).isNotNull().isInstanceOf(MutableParsingRule.class); } }