+ @SuppressWarnings("unchecked")
+ public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(final StmtContext<?, ?, ?> ctx,
+ final Class<DT> declaredType) {
+ return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
+ final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+ for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+ if (producesDeclared(subStmtContext,declaredType)) {
+ return (StmtContext<AT, ?, ?>) subStmtContext;
+ }
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <AT,DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllDeclaredSubstatement(
+ final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+ ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
+ for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+ if (producesDeclared(subStmtContext,declaredType)) {
+ listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
+ }
+ }
+ return listBuilder.build();
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstEffectiveSubstatement(
+ final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+ for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
+ if (producesDeclared(subStmtContext,declaredType)) {
+ return (StmtContext<AT, ?, ?>) subStmtContext;
+ }
+ }
+ return null;
+ }
+
+ public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
+ final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+ StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
+ return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, declaredType);
+ }
+
+ @SafeVarargs
+ public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
+ int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
+ if (startIndex >= types.length) {
+ return null;
+ }
+
+ for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+ if (producesDeclared(subStmtContext,types[startIndex])) {
+ return startIndex + 1 == types.length ? subStmtContext
+ : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
+ }
+ }
+ return null;
+ }
+
+ public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
+ final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
+ for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+ if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
+ return subStmtContext;
+ }
+ if (sublevel > 1) {
+ final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
+ subStmtContext, declaredType, --sublevel);
+ if (result != null) {
+ return result;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
+ final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+ for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+ if (producesDeclared(subStmtContext, declaredType)) {
+ return subStmtContext;
+ }
+
+ final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
+ if (result != null) {
+ return result;
+ }
+ }
+
+ return null;
+ }
+
+ public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
+ final Class<? extends DeclaredStatement<?>> type) {