@Test public void pojoWithNoWhenAnnotationDefaultsToFalse() throws InvalidClassException { SampleRuleWithoutAnnotations sampleRule = new SampleRuleWithoutAnnotations(); RuleAdapter ruleAdapter = new RuleAdapter(sampleRule); Predicate predicate = ruleAdapter.getCondition(); Assert.assertTrue(predicate.test(null)); }
@Override @SuppressWarnings("unchecked") public Predicate<NameValueReferableMap> getCondition() { if (_rule.getCondition() != null) { return _rule.getCondition(); } _rule.setCondition(Arrays.stream(_pojoRule.getClass().getMethods()) .filter(method -> method.getReturnType() == boolean.class || method.getReturnType() == Boolean.class) .filter(method -> Arrays.stream(method.getDeclaredAnnotations()).anyMatch(When.class::isInstance)) .findFirst() .<Predicate>map(method -> object -> { try { return (Boolean) method.invoke(_pojoRule); } catch (InvocationTargetException | IllegalAccessException ex) { if (_actionType == RuleChainActionType.ERROR_ON_FAILURE) { throw new RuleException(ex.getCause() == null ? ex : ex.getCause()); } if (_actionType == RuleChainActionType.STOP_ON_FAILURE) { _rule.setRuleState(RuleState.EXCEPTION); } LOGGER.error( "Unable to validate condition due to an exception. It will be evaluated as false", ex); return false; } }) .orElse(o -> true)); return _rule.getCondition(); }
RuleAdapter implements Rule { @Override @SuppressWarnings("unchecked") public Predicate<NameValueReferableMap> getCondition() { if (_rule.getCondition() != null) { return _rule.getCondition(); } _rule.setCondition(Arrays.stream(_pojoRule.getClass().getMethods()) .filter(method -> method.getReturnType() == boolean.class || method.getReturnType() == Boolean.class) .filter(method -> Arrays.stream(method.getDeclaredAnnotations()).anyMatch(When.class::isInstance)) .findFirst() .<Predicate>map(method -> object -> { try { return (Boolean) method.invoke(_pojoRule); } catch (InvocationTargetException | IllegalAccessException ex) { if (_actionType == RuleChainActionType.ERROR_ON_FAILURE) { throw new RuleException(ex.getCause() == null ? ex : ex.getCause()); } if (_actionType == RuleChainActionType.STOP_ON_FAILURE) { _rule.setRuleState(RuleState.EXCEPTION); } LOGGER.error( "Unable to validate condition due to an exception. It will be evaluated as false", ex); return false; } }) .orElse(o -> true)); return _rule.getCondition(); } }
RuleAdapter implements Rule { @Override @SuppressWarnings("unchecked") public Predicate<NameValueReferableMap> getCondition() { if (_rule.getCondition() != null) { return _rule.getCondition(); } _rule.setCondition(Arrays.stream(_pojoRule.getClass().getMethods()) .filter(method -> method.getReturnType() == boolean.class || method.getReturnType() == Boolean.class) .filter(method -> Arrays.stream(method.getDeclaredAnnotations()).anyMatch(When.class::isInstance)) .findFirst() .<Predicate>map(method -> object -> { try { return (Boolean) method.invoke(_pojoRule); } catch (InvocationTargetException | IllegalAccessException ex) { if (_actionType == RuleChainActionType.ERROR_ON_FAILURE) { throw new RuleException(ex.getCause() == null ? ex : ex.getCause()); } if (_actionType == RuleChainActionType.STOP_ON_FAILURE) { _rule.setRuleState(RuleState.EXCEPTION); } LOGGER.error( "Unable to validate condition due to an exception. It will be evaluated as false", ex); return false; } }) .orElse(o -> true)); return _rule.getCondition(); } RuleAdapter(Object pojoRule, Rule rule); @SuppressWarnings("unchecked")  RuleAdapter(Object pojoRule); }
RuleAdapter implements Rule { @Override @SuppressWarnings("unchecked") public Predicate<NameValueReferableMap> getCondition() { if (_rule.getCondition() != null) { return _rule.getCondition(); } _rule.setCondition(Arrays.stream(_pojoRule.getClass().getMethods()) .filter(method -> method.getReturnType() == boolean.class || method.getReturnType() == Boolean.class) .filter(method -> Arrays.stream(method.getDeclaredAnnotations()).anyMatch(When.class::isInstance)) .findFirst() .<Predicate>map(method -> object -> { try { return (Boolean) method.invoke(_pojoRule); } catch (InvocationTargetException | IllegalAccessException ex) { if (_actionType == RuleChainActionType.ERROR_ON_FAILURE) { throw new RuleException(ex.getCause() == null ? ex : ex.getCause()); } if (_actionType == RuleChainActionType.STOP_ON_FAILURE) { _rule.setRuleState(RuleState.EXCEPTION); } LOGGER.error( "Unable to validate condition due to an exception. It will be evaluated as false", ex); return false; } }) .orElse(o -> true)); return _rule.getCondition(); } RuleAdapter(Object pojoRule, Rule rule); @SuppressWarnings("unchecked")  RuleAdapter(Object pojoRule); @Override void addFacts(NameValueReferable... facts); @Override void addFacts(NameValueReferableMap facts); @Override void setFacts(NameValueReferableMap facts); @Override @SuppressWarnings("unchecked") void setCondition(Predicate condition); @Override void setRuleState(RuleState ruleState); @Override @SuppressWarnings("unchecked") void addAction(Consumer action); @Override @SuppressWarnings("unchecked") void addAction(BiConsumer action); @Override void addFactNameFilter(String... factNames); @Override NameValueReferableMap getFacts(); @Override @SuppressWarnings("unchecked") Predicate<NameValueReferableMap> getCondition(); @Override RuleState getRuleState(); @Override @SuppressWarnings("unchecked") List<Object> getActions(); @Override @SuppressWarnings("unchecked") boolean invoke(NameValueReferableMap facts); @Override @SuppressWarnings("unchecked") void setResult(Result result); @Override Optional<Result> getResult(); }
RuleAdapter implements Rule { @Override @SuppressWarnings("unchecked") public Predicate<NameValueReferableMap> getCondition() { if (_rule.getCondition() != null) { return _rule.getCondition(); } _rule.setCondition(Arrays.stream(_pojoRule.getClass().getMethods()) .filter(method -> method.getReturnType() == boolean.class || method.getReturnType() == Boolean.class) .filter(method -> Arrays.stream(method.getDeclaredAnnotations()).anyMatch(When.class::isInstance)) .findFirst() .<Predicate>map(method -> object -> { try { return (Boolean) method.invoke(_pojoRule); } catch (InvocationTargetException | IllegalAccessException ex) { if (_actionType == RuleChainActionType.ERROR_ON_FAILURE) { throw new RuleException(ex.getCause() == null ? ex : ex.getCause()); } if (_actionType == RuleChainActionType.STOP_ON_FAILURE) { _rule.setRuleState(RuleState.EXCEPTION); } LOGGER.error( "Unable to validate condition due to an exception. It will be evaluated as false", ex); return false; } }) .orElse(o -> true)); return _rule.getCondition(); } RuleAdapter(Object pojoRule, Rule rule); @SuppressWarnings("unchecked")  RuleAdapter(Object pojoRule); @Override void addFacts(NameValueReferable... facts); @Override void addFacts(NameValueReferableMap facts); @Override void setFacts(NameValueReferableMap facts); @Override @SuppressWarnings("unchecked") void setCondition(Predicate condition); @Override void setRuleState(RuleState ruleState); @Override @SuppressWarnings("unchecked") void addAction(Consumer action); @Override @SuppressWarnings("unchecked") void addAction(BiConsumer action); @Override void addFactNameFilter(String... factNames); @Override NameValueReferableMap getFacts(); @Override @SuppressWarnings("unchecked") Predicate<NameValueReferableMap> getCondition(); @Override RuleState getRuleState(); @Override @SuppressWarnings("unchecked") List<Object> getActions(); @Override @SuppressWarnings("unchecked") boolean invoke(NameValueReferableMap facts); @Override @SuppressWarnings("unchecked") void setResult(Result result); @Override Optional<Result> getResult();  }