Removes redundant tests. @param list current tests @param cc compilation context @return if catch clause contains relevant tests
(final ArrayList<Test> list, final CompileContext cc)
| 116 | * @return if catch clause contains relevant tests |
| 117 | */ |
| 118 | boolean simplify(final ArrayList<Test> list, final CompileContext cc) { |
| 119 | final Checks<Test> wildcard = t -> t instanceof final NameTest nt && |
| 120 | nt.scope == NameTest.Scope.ALL; |
| 121 | |
| 122 | // check if all errors are already caught |
| 123 | if(wildcard.any(list)) { |
| 124 | cc.info(OPTSIMPLE_X_X, (Supplier<?>) this::description, "*"); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | // drop remaining tests in favor or wildcard test |
| 129 | if(wildcard.any(tests) && tests.size() != 1) { |
| 130 | tests.clear(); |
| 131 | tests.add(NodeTest.ELEMENT); |
| 132 | cc.info(OPTSIMPLE_X_X, (Supplier<?>) this::description, "*"); |
| 133 | } |
| 134 | |
| 135 | // remove redundant tests |
| 136 | final Iterator<Test> iter = tests.iterator(); |
| 137 | while(iter.hasNext()) { |
| 138 | final Test test = iter.next(); |
| 139 | if(list.contains(test)) { |
| 140 | cc.info(OPTREMOVE_X_X, test, (Supplier<?>) this::description); |
| 141 | iter.remove(); |
| 142 | } else { |
| 143 | list.add(test); |
| 144 | } |
| 145 | } |
| 146 | return !tests.isEmpty(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Checks if all errors are caught by this cause. |