本文整理了Java中org.tinygroup.commons.tools.Assert
类的一些代码示例,展示了Assert
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assert
类的具体详情如下:
包路径:org.tinygroup.commons.tools.Assert
类名称:Assert
[英]断言工具,用来实现Fail-Fast。
注意事项:
assertTrue(type instanceof MyType, "Unsupported type: " + type);
可以替换成:
assertTrue(type instanceof MyType, "Unsupported type: %s", type);
这样,当assertion顺利通过时,不会占用CPU时间。
此外,部分方法具有返回值,以方便编程,例如:
void foo(String param) {
bar(assertNotNull(param));
}
int bar(String param) {
if (true) {
...
}
return unreachableCode();
}
[中]断言工具,用来实现Fail-Fast。
注意事项:
*断言是用来明确设置程序中的条件和约定的,它是一种程序员之间交流的工具,而不是和最终用户交流的工具。
assertTrue(type instanceof MyType, "Unsupported type: " + type);
可以替换成:
assertTrue(type instanceof MyType, "Unsupported type: %s", type);
这样,当断言顺利通过时,不会占用中央处理器时间。
此外,部分方法具有返回值,以方便编程,例如:
void foo(String param) {
bar(assertNotNull(param));
}
int bar(String param) {
if (true) {
...
}
return unreachableCode();
}
代码示例来源:origin: org.tinygroup/weblayer
public void afterPropertiesSet() throws Exception {
assertNotNull(key, "missing key attribute for parameter");
if (values == null) {
values = EMPTY_STRING_ARRAY;
}
}
代码示例来源:origin: org.tinygroup/weblayer
protected void init() throws Exception {
assertNotNull(key, "no key");
byte[] raw = key.getBytes("UTF-8");
int keySize = getKeySize();
int actualKeySize = raw.length * 8;
assertTrue(keySize == actualKeySize, "Illegal key: expected size=%d, actual size is %d", keySize, actualKeySize);
keySpec = new SecretKeySpec(raw, ALG_NAME);
}
代码示例来源:origin: org.tinygroup/commons
/** 不可能到达的代码。 */
public static <T> T unreachableCode() {
unreachableCode(null, (Object[]) null);
return null;
}
代码示例来源:origin: org.tinygroup/commons
@SuppressWarnings("unchecked")
private <T> T doGet(String attributeName, Class<T> expectedType) {
Assert.hasText(attributeName, "attributeName must not be null or empty");
Object value = this.get(attributeName);
Assert.assertNotNull(value, String.format("Attribute '%s' not found", attributeName));
Assert.isAssignable(expectedType, value.getClass(),
String.format("Attribute '%s' is of type [%s], but [%s] was expected. Cause: ",
attributeName, value.getClass().getSimpleName(), expectedType.getSimpleName()));
return (T) value;
}
}
代码示例来源:origin: org.tinygroup/commons
/** 确保表达式为真,否则抛出<code>IllegalArgumentException</code>。 */
public static void assertTrue(boolean expression) {
assertTrue(expression, null, null, (Object[]) null);
}
代码示例来源:origin: org.tinygroup/commons
/** 不支持的操作。 */
public static <T> T unsupportedOperation() {
unsupportedOperation(null, (Object[]) null);
return null;
}
代码示例来源:origin: org.tinygroup/commons
/** 不可能发生的异常。 */
public static <T> T unexpectedException(Throwable e) {
unexpectedException(e, null, (Object[]) null);
return null;
}
代码示例来源:origin: org.tinygroup/commons
/** 确保表达式为真,否则抛出<code>IllegalArgumentException</code>。 */
public static void assertTrue(boolean expression, String message, Object... args) {
assertTrue(expression, null, message, args);
}
代码示例来源:origin: org.tinygroup/weblayer
public String[] setValue(String[] value) {
unsupportedOperation();
return null;
}
}
代码示例来源:origin: org.tinygroup/weblayer
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
unexpectedException(e);
return null;
}
}
代码示例来源:origin: org.tinygroup/org.tinygroup.weblayerbase
public void afterPropertiesSet() throws Exception {
assertNotNull(key, "missing key attribute for parameter");
if (values == null) {
values = EMPTY_STRING_ARRAY;
}
}
代码示例来源:origin: org.tinygroup/org.tinygroup.weblayerbase
protected void init() throws Exception {
assertNotNull(key, "no key");
byte[] raw = key.getBytes("UTF-8");
int keySize = getKeySize();
int actualKeySize = raw.length * 8;
assertTrue(keySize == actualKeySize, "Illegal key: expected size=%d, actual size is %d", keySize, actualKeySize);
keySpec = new SecretKeySpec(raw, ALG_NAME);
}
代码示例来源:origin: org.tinygroup/commons
/**
* Compare the signatures of the bridge method and the method which it bridges. If
* the parameter and return types are the same, it is a 'visibility' bridge method
* introduced in Java 6 to fix http://bugs.sun.com/view_bug.do?bug_id=6342411.
* See also http://stas-blogspot.blogspot.com/2010/03/java-bridge-methods-explained.html
* @return whether signatures match as described
*/
public static boolean isVisibilityBridgeMethodPair(Method bridgeMethod, Method bridgedMethod) {
Assert.assertTrue(bridgeMethod != null);
Assert.assertTrue(bridgedMethod != null);
if (bridgeMethod == bridgedMethod) {
return true;
}
return Arrays.equals(bridgeMethod.getParameterTypes(), bridgedMethod.getParameterTypes()) &&
bridgeMethod.getReturnType().equals(bridgedMethod.getReturnType());
}
代码示例来源:origin: org.tinygroup/org.tinygroup.weblayerbase
public String[] setValue(String[] value) {
unsupportedOperation();
return null;
}
}
代码示例来源:origin: org.tinygroup/org.tinygroup.weblayerbase
public String generateSessionID() {
assertInitialized();
byte[] bytes = new byte[(length + 3) / 4 * 3];
rnd.nextBytes(bytes);
byte[] b64Encoded = Base64.encodeBase64(bytes);
StringBuilder buf = new StringBuilder(length);
for (int i = 0; i < length; i++) {
char ch = (char) b64Encoded[i];
// 替换掉/和+,因为这两个字符在url中有特殊用处。
switch (ch) {
case '/':
ch = '$';
break;
case '+':
ch = '-';
break;
case '=':
unreachableCode();
}
buf.append(ch);
}
return buf.toString();
}
代码示例来源:origin: org.tinygroup/commons
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
unexpectedException(e);
return null;
}
}
代码示例来源:origin: org.tinygroup/org.tinygroup.weblayerbase
/**
* 创建一个request wrapper。
*
* @param context request context
* @param request request
*/
public AbstractRequestWrapper(WebContext context, HttpServletRequest request) {
super(request);
this.context = assertNotNull(context, "requestContext");
}
代码示例来源:origin: org.tinygroup/commons
/**
* Attempt to find a {@link Field field} on the supplied {@link Class} with the
* supplied <code>name</code> and/or {@link Class type}. Searches all superclasses
* up to {@link Object}.
* @param clazz the class to introspect
* @param name the name of the field (may be <code>null</code> if type is specified)
* @param type the type of the field (may be <code>null</code> if name is specified)
* @return the corresponding Field object, or <code>null</code> if not found
*/
public static Field findField(Class<?> clazz, String name, Class<?> type) {
Assert.assertNotNull(clazz, "Class must not be null");
Assert.assertTrue(name != null || type != null, "Either name or type of the field must be specified");
Class<?> searchType = clazz;
while (!Object.class.equals(searchType) && searchType != null) {
Field[] fields = searchType.getDeclaredFields();
for (Field field : fields) {
if ((name == null || name.equals(field.getName())) && (type == null || type.equals(field.getType()))) {
return field;
}
}
searchType = searchType.getSuperclass();
}
return null;
}
代码示例来源:origin: org.tinygroup/commons
/** 设置新匹配。 */
public void setMatchResults(MatchResult... results) {
assertTrue(!isEmptyArray(results), "results");
if (this.results.length != results.length) {
throw new IllegalArgumentException("expected " + this.results.length + " MatchResults");
}
for (int i = 0; i < results.length; i++) {
this.results[i] = results[i];
}
}
代码示例来源:origin: org.tinygroup/org.tinygroup.weblayerbase
protected void add(String key, String value) {
unsupportedOperation("You should extend class " + getClass().getSimpleName()
+ " and override method add(String, String)");
}
内容来源于网络,如有侵权,请联系作者删除!