@Test public void pojoWithNoWhenAnnotationDefaultsToFalse() throws InvalidClassException { SampleRuleWithoutAnnotations sampleRule = new SampleRuleWithoutAnnotations(); RuleAdapter ruleAdapter = new RuleAdapter(sampleRule); Predicate predicate = ruleAdapter.getWhen(); Assert.assertFalse(predicate.test(null)); }
@Override public Predicate getWhen() { if (_rule.getWhen() != null) { return _rule.getWhen(); } return Arrays.stream(_rulePojo.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(_rulePojo); } catch (InvocationTargetException | IllegalAccessException ex) { LOGGER.error( "Unable to validate condition due to an exception. Condition will be interpreted as false", ex); return false; } }) .orElse(o -> false); }
RuleAdapter implements Decision { @Override public Predicate getWhen() { if (_rule.getWhen() != null) { return _rule.getWhen(); } return Arrays.stream(_rulePojo.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(_rulePojo); } catch (InvocationTargetException | IllegalAccessException ex) { LOGGER.error( "Unable to validate condition due to an exception. Condition will be interpreted as false", ex); return false; } }) .orElse(o -> false); } }
RuleAdapter implements Decision { @Override public Predicate getWhen() { if (_rule.getWhen() != null) { return _rule.getWhen(); } return Arrays.stream(_rulePojo.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(_rulePojo); } catch (InvocationTargetException | IllegalAccessException ex) { LOGGER.error( "Unable to validate condition due to an exception. Condition will be interpreted as false", ex); return false; } }) .orElse(o -> false); } @SuppressWarnings("unchecked")  RuleAdapter(Object rulePojo, Rule rule); @SuppressWarnings("unchecked")  RuleAdapter(Object rulePojo); }
RuleAdapter implements Decision { @Override public Predicate getWhen() { if (_rule.getWhen() != null) { return _rule.getWhen(); } return Arrays.stream(_rulePojo.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(_rulePojo); } catch (InvocationTargetException | IllegalAccessException ex) { LOGGER.error( "Unable to validate condition due to an exception. Condition will be interpreted as false", ex); return false; } }) .orElse(o -> false); } @SuppressWarnings("unchecked")  RuleAdapter(Object rulePojo, Rule rule); @SuppressWarnings("unchecked")  RuleAdapter(Object rulePojo); @Override @SuppressWarnings("unchecked") Decision given(Fact... facts); @Override @SuppressWarnings("unchecked") Decision given(List list); @Override @SuppressWarnings("unchecked") Decision given(FactMap facts); @Override @SuppressWarnings("unchecked") Decision given(String name, Object value); @Override Decision givenUnTyped(FactMap facts); @Override Predicate getWhen(); @Override List<Object> getThen(); @Override void run(Object... otherArgs); @Override Decision when(Predicate test); @Override Decision then(Consumer action); @Override Decision then(BiConsumer action); @Override Decision using(String... factName); @Override Decision stop(); @Override Object getResult(); @Override void setResult(Result result); @Override FactMap getFactMap(); @Override void setNextRule(Rule rule); }
RuleAdapter implements Decision { @Override public Predicate getWhen() { if (_rule.getWhen() != null) { return _rule.getWhen(); } return Arrays.stream(_rulePojo.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(_rulePojo); } catch (InvocationTargetException | IllegalAccessException ex) { LOGGER.error( "Unable to validate condition due to an exception. Condition will be interpreted as false", ex); return false; } }) .orElse(o -> false); } @SuppressWarnings("unchecked")  RuleAdapter(Object rulePojo, Rule rule); @SuppressWarnings("unchecked")  RuleAdapter(Object rulePojo); @Override @SuppressWarnings("unchecked") Decision given(Fact... facts); @Override @SuppressWarnings("unchecked") Decision given(List list); @Override @SuppressWarnings("unchecked") Decision given(FactMap facts); @Override @SuppressWarnings("unchecked") Decision given(String name, Object value); @Override Decision givenUnTyped(FactMap facts); @Override Predicate getWhen(); @Override List<Object> getThen(); @Override void run(Object... otherArgs); @Override Decision when(Predicate test); @Override Decision then(Consumer action); @Override Decision then(BiConsumer action); @Override Decision using(String... factName); @Override Decision stop(); @Override Object getResult(); @Override void setResult(Result result); @Override FactMap getFactMap(); @Override void setNextRule(Rule rule);  }