com.alibaba.citrus.util.Assert类的使用及代码示例

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

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

Assert介绍

[英]断言工具,用来实现Fail-Fast

注意事项:

  • Assertion是用来明确设置程序中的条件和约定的,它是一种程序员之间交流的工具,而不是和最终用户交流的工具。
  • 一个经过完整单元测试的正确程序,不应该使任何一条assertion失败。
  • 应避免过于复杂的assertion条件,而占用过多的运行时间。除非Assertion失败。
  • Assertion的出错信息不是给最终用户看的,因此没必要写得过于详细,更没必要考虑国际化的问题,以至于浪费CPU的时间。 例如下面的语句是要避免的:
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: webx/citrus

AsyncCallbackAdapter(Object runnable, AsyncContext asyncContext, long defaultTimeout, long defaultCancelingTimeout) {
  assertTrue(runnable instanceof Runnable || runnable instanceof Callable<?>, "runnable or callable");
  this.runnable = runnable;
  this.asyncContext = assertNotNull(asyncContext, "asyncContext");
  this.defaultTimeout = defaultTimeout;
  this.defaultCancelingTimeout = defaultCancelingTimeout;
}

代码示例来源:origin: webx/citrus

@Override
  protected T get(int i) {
    unreachableCode();
    return null;
  }
};

代码示例来源:origin: webx/citrus

/** 不可能发生的异常。 */
public static <T> T unexpectedException(Throwable e) {
  unexpectedException(e, null, (Object[]) null);
  return null;
}

代码示例来源:origin: webx/citrus

private void setSource(AttachmentSource source) {
  assertNull(this.source, ILLEGAL_STATE, "Attachment source already set: %s", this.source);
  this.source = assertNotNull(source, "source");
}

代码示例来源:origin: webx/citrus

/**
 * 从<code>java.lang.reflect.GenericDeclaration</code>创建
 * <code>GenericDeclarationInfo</code>。
 */
private GenericDeclarationInfo buildGenericDeclaration(GenericDeclaration declaration, BuildingCache buildingCache) {
  assertNotNull(declaration, "declaration");
  if (declaration instanceof Class<?>) {
    Class<?> clazz = (Class<?>) declaration;
    assertTrue(!clazz.isArray(), "declaration should not be array class: %s", clazz.getName());
    return buildRawType(clazz, buildingCache);
  } else if (declaration instanceof Method) {
    return buildMethod((Method) declaration, buildingCache);
  } else if (declaration instanceof Constructor<?>) {
    return buildConstructor((Constructor<?>) declaration, buildingCache);
  } else {
    unreachableCode("Unknown generic declaration: %s", declaration);
    return null;
  }
}

代码示例来源:origin: webx/citrus

/** 创建系统默认的locale信息。 */
public LocaleInfo() {
  this.locale = assertNotNull(Locale.getDefault(), "system locale");
  this.charset = assertNotNull(Charset.defaultCharset(), "system charset");
}

代码示例来源:origin: webx/citrus

@Override
  protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    Object beanOrRef = SpringExtUtil.parseBean(element, parserContext, parserContext.getContainingBeanDefinition());
    AbstractBeanDefinition abd;

    if (beanOrRef instanceof BeanReference) {
      BeanDefinitionBuilder refBean = BeanDefinitionBuilder.genericBeanDefinition(BeanReferenceFactoryBean.class);

      refBean.addPropertyValue("targetBeanName", ((RuntimeBeanReference) beanOrRef).getBeanName());
      abd = refBean.getBeanDefinition();
    } else if (beanOrRef instanceof BeanDefinitionHolder) {
      BeanDefinition bd = ((BeanDefinitionHolder) beanOrRef).getBeanDefinition();
      assertTrue(bd instanceof AbstractBeanDefinition, "unexpected bean definition type: %s", bd);
      abd = (AbstractBeanDefinition) bd;
    } else {
      abd = null;
      unreachableCode("unexpected bean definition type: %s", beanOrRef);
    }

    return abd;
  }
}

代码示例来源:origin: webx/citrus

private String getPrefix(String name) {
  String prefix = null;
  int index = name.indexOf(":");
  if (index >= 0) {
    prefix = name.substring(0, index);
    assertTrue(!prefix.contains("*") && !prefix.contains("?"), "invalid folder or file name: %s", name);
  }
  return prefix;
}

代码示例来源:origin: webx/citrus

/** 不支持的操作。 */
public static <T> T unsupportedOperation() {
  unsupportedOperation(null, (Object[]) null);
  return null;
}

代码示例来源:origin: webx/citrus

/** 确保对象为空,否则抛出<code>IllegalArgumentException</code>。 */
public static <T> T assertNull(T object) {
  return assertNull(object, null, null, (Object[]) null);
}

代码示例来源:origin: webx/citrus

private void setSource(AttachmentSource source) {
  assertNull(this.source, ILLEGAL_STATE, "Attachment source already set: %s", this.source);
  this.source = assertNotNull(source, "source");
}

代码示例来源:origin: webx/citrus

public ServletRequestContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) {
  super(getServletBaseURL(request), getServletResourcePath(request));
  this.request = assertNotNull(request, "request is null");
  this.response = assertNotNull(response, "response is null");
  this.servletContext = assertNotNull(servletContext, "servletContext is null");
}

代码示例来源:origin: webx/citrus

@Override
  protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    Object beanOrRef = SpringExtUtil.parseBean(element, parserContext, parserContext.getContainingBeanDefinition());
    AbstractBeanDefinition abd;

    if (beanOrRef instanceof BeanReference) {
      BeanDefinitionBuilder refBean = BeanDefinitionBuilder.genericBeanDefinition(BeanReferenceFactoryBean.class);

      refBean.addPropertyValue("targetBeanName", ((RuntimeBeanReference) beanOrRef).getBeanName());
      abd = refBean.getBeanDefinition();
    } else if (beanOrRef instanceof BeanDefinitionHolder) {
      BeanDefinition bd = ((BeanDefinitionHolder) beanOrRef).getBeanDefinition();
      assertTrue(bd instanceof AbstractBeanDefinition, "unexpected bean definition type: %s", bd);
      abd = (AbstractBeanDefinition) bd;
    } else {
      abd = null;
      unreachableCode("unexpected bean definition type: %s", beanOrRef);
    }

    return abd;
  }
}

代码示例来源:origin: webx/citrus

/** 创建一个<code>CitrusMessageFormatter</code>实例。 */
public CitrusMessageFormatter() {
  String name = getClass().getName();
  assertTrue(name.startsWith(PREFIX) && name.endsWith(SUFFIX), "Unsupported Message class: ", name);
  this.bundleName = RB_PACKAGE
           + name.substring(PREFIX.length(), name.length() - SUFFIX.length()).replace('.', '_');
}

代码示例来源:origin: webx/citrus

public String[] setValue(String[] value) {
    unsupportedOperation();
    return null;
  }
}

代码示例来源:origin: webx/citrus

/** 确保对象为空,否则抛出<code>IllegalArgumentException</code>。 */
public static <T> T assertNull(T object, String message, Object... args) {
  return assertNull(object, null, message, args);
}

代码示例来源:origin: webx/citrus

AsyncCallbackAdapter(Object runnable, AsyncContext asyncContext, long defaultTimeout, long defaultCancelingTimeout) {
  assertTrue(runnable instanceof Runnable || runnable instanceof Callable<?>, "runnable or callable");
  this.runnable = runnable;
  this.asyncContext = assertNotNull(asyncContext, "asyncContext");
  this.defaultTimeout = defaultTimeout;
  this.defaultCancelingTimeout = defaultCancelingTimeout;
}

代码示例来源:origin: webx/citrus

public Entry(String path, Schema schema) {
  this.path = assertNotNull(trimToNull(path), "path");
  this.directory = path.endsWith("/");
  int fromIndex = this.directory ? path.length() - 2 : path.length();
  this.name = path.substring(path.lastIndexOf("/", fromIndex) + 1);
  this.root = false;
  this.schema = schema;
  this.subEntries = createTreeMap();
  if (this.directory) {
    assertNull(schema, "schema");
  } else {
    assertNotNull(schema, "schema");
  }
}

代码示例来源:origin: webx/citrus

private void update(Node[] nodes, Map<String, String> params, Map<String, Template> subtemplates) {
  assertNotRef();
  this.nodes = defaultIfEmptyArray(nodes, EMPTY_NODES);
  this.params = createArrayHashMap(assertNotNull(params, "params").size());
  this.subtemplates = createArrayHashMap(assertNotNull(subtemplates, "subtemplates").size());
  this.params.putAll(params);
  this.subtemplates.putAll(subtemplates);
}

代码示例来源:origin: webx/citrus

@Override
  protected T get(int i) {
    unreachableCode();
    return null;
  }
};

相关文章