/** * Assert that an object is not <code>null</code> . * <pre class="code">Assert.notNull(clazz);</pre> * @param object the object to check * @throws IllegalArgumentException if the object is <code>null</code> */ public static void notNull(Object object) { notNull(object, "[Assertion failed] - this argument is required; it must not be null"); }
/** * Assert a boolean expression, throwing <code>IllegalArgumentException</code> * if the test result is <code>false</code>. * <pre class="code">Assert.isTrue(i > 0);</pre> * @param expression a boolean expression * @throws IllegalArgumentException if expression is <code>false</code> */ public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }
/** * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>. * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre> * @param superType the super type to check * @param subType the sub type to check * @throws IllegalArgumentException if the classes are not assignable */ public static void isAssignable(Class superType, Class subType) { isAssignable(superType, subType, ""); }
/** * Assert that the provided object is an instance of the provided class. * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre> * @param clazz the required class * @param obj the object to check * @throws IllegalArgumentException if the object is not an instance of clazz * @see Class#isInstance */ public static void isInstanceOf(Class clazz, Object obj) { isInstanceOf(clazz, obj, ""); }
/** * Assert a boolean expression, throwing {@link IllegalStateException} * if the test result is <code>false</code>. * <p>Call {@link #isTrue(boolean)} if you wish to * throw {@link IllegalArgumentException} on an assertion failure. * <pre class="code">Assert.state(id == null);</pre> * @param expression a boolean expression * @throws IllegalStateException if the supplied expression is <code>false</code> */ public static void state(boolean expression) { state(expression, "[Assertion failed] - this state invariant must be true"); }
/** * Assert that the provided object is an instance of the provided class. * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre> * @param clazz the required class * @param obj the object to check * @throws IllegalArgumentException if the object is not an instance of clazz * @see Class#isInstance */ public static void isInstanceOf(Class clazz, Object obj) { isInstanceOf(clazz, obj, ""); }
@SuppressWarnings({"unchecked"}) public <T> T getBean(String id, Class<T> requiredType) { if (requiredType == null) { throw new NullPointerException("requiredType argument cannot be null."); } Object bean = getBean(id); if (bean == null) { return null; } Assert.state(requiredType.isAssignableFrom(bean.getClass()), "Bean with id [" + id + "] is not of the required type [" + requiredType.getName() + "]."); return (T) bean; }
private String parseBeanId(String lhs) { Assert.notNull(lhs); if (lhs.indexOf('.') < 0) { return lhs; } String classSuffix = ".class"; int index = lhs.indexOf(classSuffix); if (index >= 0) { return lhs.substring(0, index); } return null; }
public void run() { // get the current subject Subject subject = SecurityUtils.getSubject(); // Subject is not authenticated yet Assert.isTrue(!subject.isAuthenticated()); // login the subject with a username / password UsernamePasswordToken token = new UsernamePasswordToken("joe.coder", "password"); subject.login(token); // joe.coder has the "user" role subject.checkRole("user"); // joe.coder does NOT have the admin role Assert.isTrue(!subject.hasRole("admin")); // joe.coder has the "read" permission subject.checkPermission("read"); // current user is allowed to execute this method. simpleService.readRestrictedCall(); try { // but not this one! simpleService.writeRestrictedCall(); } catch (AuthorizationException e) { log.info("Subject was NOT allowed to execute method 'writeRestrictedCall'"); } // logout subject.logout(); Assert.isTrue(!subject.isAuthenticated()); }
/** * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>. * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre> * @param superType the super type to check * @param subType the sub type to check * @throws IllegalArgumentException if the classes are not assignable */ public static void isAssignable(Class superType, Class subType) { isAssignable(superType, subType, ""); }
public CacheServerFactoryBean gemfireCacheServer(GemFireCache gemfireCache) { Assert.isInstanceOf(Cache.class, gemfireCache, "GemFireCache must be an instance of org.apache.geode.cache.Cache");
/** * Assert a boolean expression, throwing {@link IllegalStateException} * if the test result is <code>false</code>. * <p>Call {@link #isTrue(boolean)} if you wish to * throw {@link IllegalArgumentException} on an assertion failure. * <pre class="code">Assert.state(id == null);</pre> * @param expression a boolean expression * @throws IllegalStateException if the supplied expression is <code>false</code> */ public static void state(boolean expression) { state(expression, "[Assertion failed] - this state invariant must be true"); }
/** * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>. * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre> * @param superType the super type to check against * @param subType the sub type to check * @param message a message which will be prepended to the message produced by * the function itself, and which may be used to provide context. It should * normally end in a ": " or ". " so that the function generate message looks * ok when prepended to it. * @throws IllegalArgumentException if the classes are not assignable */ public static void isAssignable(Class superType, Class subType, String message) { notNull(superType, "Type to check against must not be null"); if (subType == null || !superType.isAssignableFrom(subType)) { throw new IllegalArgumentException(message + subType + " is not assignable to " + superType); } }
public void run() { // get the current subject Subject subject = SecurityUtils.getSubject(); // Subject is not authenticated yet Assert.isTrue(!subject.isAuthenticated()); // login the subject with a username / password UsernamePasswordToken token = new UsernamePasswordToken("joe.coder", "password"); subject.login(token); // joe.coder has the "user" role subject.checkRole("user"); // joe.coder does NOT have the admin role Assert.isTrue(!subject.hasRole("admin")); // joe.coder has the "read" permission subject.checkPermission("read"); // current user is allowed to execute this method. simpleService.readRestrictedCall(); try { // but not this one! simpleService.writeRestrictedCall(); } catch (AuthorizationException e) { log.info("Subject was NOT allowed to execute method 'writeRestrictedCall'"); } // logout subject.logout(); Assert.isTrue(!subject.isAuthenticated()); }
protected static EvictionPolicyMetaData from(AnnotationAttributes evictionPolicyAttributes, ApplicationContext applicationContext) { Assert.isAssignable(EvictionPolicy.class, evictionPolicyAttributes.annotationType()); return from(evictionPolicyAttributes.getEnum("type"), (Integer) evictionPolicyAttributes.get("maximum"), evictionPolicyAttributes.getEnum("action"), resolveObjectSizer(evictionPolicyAttributes.getString("objectSizerName"), applicationContext), evictionPolicyAttributes.getStringArray("regionNames")); }
public CacheServerFactoryBean gemfireCacheServer(GemFireCache gemfireCache) { Assert.isInstanceOf(Cache.class, gemfireCache, "GemFireCache must be an instance of org.apache.geode.cache.Cache");
@SuppressWarnings({"unchecked"}) public <T> T getBean(String id, Class<T> requiredType) { if (requiredType == null) { throw new NullPointerException("requiredType argument cannot be null."); } Object bean = getBean(id); if (bean == null) { return null; } Assert.state(requiredType.isAssignableFrom(bean.getClass()), "Bean with id [" + id + "] is not of the required type [" + requiredType.getName() + "]."); return (T) bean; }
/** * Create a new instance of {@link BeanValidatingItemProcessor}. * @param localValidatorFactoryBean used to configure the Bean Validation validator */ public BeanValidatingItemProcessor(LocalValidatorFactoryBean localValidatorFactoryBean) { Assert.notNull(localValidatorFactoryBean, "localValidatorFactoryBean must not be null"); this.validator = localValidatorFactoryBean.getValidator(); }
/** * Assert a boolean expression, throwing <code>IllegalArgumentException</code> * if the test result is <code>false</code>. * <pre class="code">Assert.isTrue(i > 0);</pre> * @param expression a boolean expression * @throws IllegalArgumentException if expression is <code>false</code> */ public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }
protected static EvictionPolicyMetaData from(AnnotationAttributes evictionPolicyAttributes, ApplicationContext applicationContext) { Assert.isAssignable(EvictionPolicy.class, evictionPolicyAttributes.annotationType()); return from(evictionPolicyAttributes.getEnum("type"), (Integer) evictionPolicyAttributes.get("maximum"), evictionPolicyAttributes.getEnum("action"), resolveObjectSizer(evictionPolicyAttributes.getString("objectSizerName"), applicationContext), evictionPolicyAttributes.getStringArray("regionNames")); }