org.apache.shiro.util.Assert类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(206)

本文整理了Java中org.apache.shiro.util.Assert类的一些代码示例,展示了Assert类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assert类的具体详情如下:
包路径:org.apache.shiro.util.Assert
类名称:Assert

Assert介绍

[英]Assertion utility class that assists in validating arguments. Useful for identifying programmer errors early and clearly at runtime. Usage also reduces a program's cyclomatic complexity.

For example, if the contract of a public method states it does not allow null arguments, Assert can be used to validate that contract. Doing this clearly indicates a contract violation when it occurs and protects the class's invariants.

Typically used to validate method arguments rather than configuration properties, to check for cases that are usually programmer errors rather than configuration errors. In contrast to config initialization code, there is usally no point in falling back to defaults in such methods.

This class is similar to JUnit's assertion library. If an argument value is deemed invalid, an IllegalArgumentException is thrown (typically). For example:

Assert.notNull(clazz, "The class must not be null"); 
Assert.isTrue(i > 0, "The value must be greater than zero");

Mainly for internal use within the framework; consider Jakarta's Commons Lang >= 2.0 for a more comprehensive suite of assertion utilities.

Gratefully borrowed from the Spring Framework, also Apache 2.0 licensed
[中]

代码示例

代码示例来源:origin: apache/shiro

/**
 * 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");
}

代码示例来源:origin: apache/shiro

/**
 * Assert a boolean expression, throwing <code>IllegalArgumentException</code>
 * if the test result is <code>false</code>.
 * <pre class="code">Assert.isTrue(i &gt; 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");
}

代码示例来源:origin: apache/shiro

/**
 * 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, "");
}

代码示例来源:origin: apache/shiro

/**
 * 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, "");
}

代码示例来源:origin: apache/shiro

/**
 * 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");
}

代码示例来源:origin: org.apache.shiro/shiro-lang

/**
 * 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, "");
}

代码示例来源:origin: apache/shiro

@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;
}

代码示例来源:origin: apache/shiro

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;
}

代码示例来源:origin: apache/shiro

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());
}

代码示例来源:origin: org.apache.shiro/shiro-lang

/**
 * 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, "");
}

代码示例来源:origin: org.springframework.data/spring-data-gemfire

public CacheServerFactoryBean gemfireCacheServer(GemFireCache gemfireCache) {
  Assert.isInstanceOf(Cache.class, gemfireCache,
    "GemFireCache must be an instance of org.apache.geode.cache.Cache");

代码示例来源:origin: org.apache.shiro/shiro-lang

/**
 * 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");
}

代码示例来源:origin: apache/shiro

/**
 * 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);
  }
}

代码示例来源:origin: apache/shiro

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());
}

代码示例来源:origin: org.springframework.data/spring-data-gemfire

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"));
}

代码示例来源:origin: org.springframework.data/spring-data-geode

public CacheServerFactoryBean gemfireCacheServer(GemFireCache gemfireCache) {
  Assert.isInstanceOf(Cache.class, gemfireCache,
    "GemFireCache must be an instance of org.apache.geode.cache.Cache");

代码示例来源:origin: org.apache.shiro/shiro-config-ogdl

@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;
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * 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();
}

代码示例来源:origin: org.apache.shiro/shiro-lang

/**
 * Assert a boolean expression, throwing <code>IllegalArgumentException</code>
 * if the test result is <code>false</code>.
 * <pre class="code">Assert.isTrue(i &gt; 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");
}

代码示例来源:origin: org.springframework.data/spring-data-geode

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"));
}

相关文章