org.springframework.core.Constants类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(278)

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

Constants介绍

[英]This class can be used to parse other classes containing constant definitions in public static final members. The asXXXX methods of this class allow these constant values to be accessed via their string names.

Consider class Foo containing public final static int CONSTANT1 = 66;An instance of this class wrapping Foo.class will return the constant value of 66 from its asNumber method given the argument "CONSTANT1".

This class is ideal for use in PropertyEditors, enabling them to recognize the same names as the constants themselves, and freeing them from maintaining their own mapping.
[中]此类可用于解析公共静态最终成员中包含常量定义的其他类。此类的asXXXX方法允许通过字符串名访问这些常量值。
考虑包含公共最终静态int 1=66的类FoO;此类的一个实例包装了Foo。类将从给定参数“CONSTANT1”的asNumber方法返回常量值66。
该类非常适合在PropertyEditor中使用,使它们能够识别与常量本身相同的名称,并使它们不必维护自己的映射。

代码示例

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

/**
 * Set the system property mode by the name of the corresponding constant,
 * e.g. "SYSTEM_PROPERTIES_MODE_OVERRIDE".
 * @param constantName name of the constant
 * @see #setSystemPropertiesMode
 */
public void setSystemPropertiesModeName(String constantName) throws IllegalArgumentException {
  this.systemPropertiesMode = constants.asNumber(constantName).intValue();
}

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

/**
 * Return all values of the group of constants for the
 * given bean property name.
 * @param propertyName the name of the bean property
 * @return the set of values
 * @see #propertyToConstantNamePrefix
 */
public Set<Object> getValuesForProperty(String propertyName) {
  return getValues(propertyToConstantNamePrefix(propertyName));
}

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

/**
 * Return an identifying description for this transaction definition.
 * <p>Available to subclasses, for inclusion in their {@code toString()} result.
 */
protected final StringBuilder getDefinitionDescription() {
  StringBuilder result = new StringBuilder();
  result.append(constants.toCode(this.propagationBehavior, PREFIX_PROPAGATION));
  result.append(',');
  result.append(constants.toCode(this.isolationLevel, PREFIX_ISOLATION));
  if (this.timeout != TIMEOUT_DEFAULT) {
    result.append(',');
    result.append(PREFIX_TIMEOUT).append(this.timeout);
  }
  if (this.readOnly) {
    result.append(',');
    result.append(READ_ONLY_MARKER);
  }
  return result;
}

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

/**
 * Return all names of the group of constants for the
 * given bean property name.
 * @param propertyName the name of the bean property
 * @return the set of values
 * @see #propertyToConstantNamePrefix
 */
public Set<String> getNamesForProperty(String propertyName) {
  return getNames(propertyToConstantNamePrefix(propertyName));
}

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

/**
 * Look up the given value within the group of constants for
 * the given bean property name. Will return the first match.
 * @param value constant value to look up
 * @param propertyName the name of the bean property
 * @return the name of the constant field
 * @throws ConstantException if the value wasn't found
 * @see #propertyToConstantNamePrefix
 */
public String toCodeForProperty(Object value, String propertyName) throws ConstantException {
  return toCode(value, propertyToConstantNamePrefix(propertyName));
}

代码示例来源:origin: org.apache.camel/camel-spring

@Override
public void setSystemPropertiesModeName(String constantName) throws IllegalArgumentException {
  super.setSystemPropertiesModeName(constantName);
  Constants constants = new Constants(PropertyPlaceholderConfigurer.class);
  this.systemPropertiesMode = constants.asNumber(constantName).intValue();
}

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

@Test
public void constants() {
  Constants c = new Constants(A.class);
  assertEquals(A.class.getName(), c.getClassName());
  assertEquals(9, c.getSize());
  assertEquals(A.DOG, c.asNumber("DOG").intValue());
  assertEquals(A.DOG, c.asNumber("dog").intValue());
  assertEquals(A.CAT, c.asNumber("cat").intValue());
  try {
    c.asNumber("bogus");
    fail("Can't get bogus field");
  }
  catch (Constants.ConstantException expected) {
  }
  assertTrue(c.asString("S1").equals(A.S1));
  try {
    c.asNumber("S1");
    fail("Wrong type");
  }
  catch (Constants.ConstantException expected) {
  }
}

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

/**
 * Set the autodetection mode to use.
 * @throws IllegalArgumentException if the supplied value is not
 * one of the {@code AUTODETECT_} constants
 * @see #setAutodetectModeName(String)
 * @see #AUTODETECT_ALL
 * @see #AUTODETECT_ASSEMBLER
 * @see #AUTODETECT_MBEAN
 * @see #AUTODETECT_NONE
 */
public void setAutodetectMode(int autodetectMode) {
  if (!constants.getValues(CONSTANT_PREFIX_AUTODETECT).contains(autodetectMode)) {
    throw new IllegalArgumentException("Only values of autodetect constants allowed");
  }
  this.autodetectMode = autodetectMode;
}

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

@Test
public void getValuesWithNullPrefix() throws Exception {
  Constants c = new Constants(A.class);
  Set<?> values = c.getValues(null);
  assertEquals("Must have returned *all* public static final values", 7, values.size());
}

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

@Test
public void getValues() {
  Constants c = new Constants(A.class);
  Set<?> values = c.getValues("");
  assertEquals(7, values.size());
  assertTrue(values.contains(Integer.valueOf(0)));
  assertTrue(values.contains(Integer.valueOf(66)));
  assertTrue(values.contains(""));
  values = c.getValues("D");
  assertEquals(1, values.size());
  assertTrue(values.contains(Integer.valueOf(0)));
  values = c.getValues("prefix");
  assertEquals(2, values.size());
  assertTrue(values.contains(Integer.valueOf(1)));
  assertTrue(values.contains(Integer.valueOf(2)));
  values = c.getValuesForProperty("myProperty");
  assertEquals(2, values.size());
  assertTrue(values.contains(Integer.valueOf(1)));
  assertTrue(values.contains(Integer.valueOf(2)));
}

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

@Test
public void withClassThatExposesNoConstants() throws Exception {
  Constants c = new Constants(NoConstants.class);
  assertEquals(0, c.getSize());
  final Set<?> values = c.getValues("");
  assertNotNull(values);
  assertEquals(0, values.size());
}

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

/**
 * Return a constant value as a String.
 * @param code the name of the field (never {@code null})
 * @return the String value
 * Works even if it's not a string (invokes {@code toString()}).
 * @throws ConstantException if the field name wasn't found
 * @see #asObject
 */
public String asString(String code) throws ConstantException {
  return asObject(code).toString();
}

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

@Test
public void toCode() {
  Constants c = new Constants(A.class);
  assertEquals("DOG", c.toCode(Integer.valueOf(0), ""));
  assertEquals("DOG", c.toCode(Integer.valueOf(0), "D"));
  assertEquals("DOG", c.toCode(Integer.valueOf(0), "DO"));
  assertEquals("DOG", c.toCode(Integer.valueOf(0), "DoG"));
  assertEquals("DOG", c.toCode(Integer.valueOf(0), null));
  assertEquals("CAT", c.toCode(Integer.valueOf(66), ""));
  assertEquals("CAT", c.toCode(Integer.valueOf(66), "C"));
  assertEquals("CAT", c.toCode(Integer.valueOf(66), "ca"));
  assertEquals("CAT", c.toCode(Integer.valueOf(66), "cAt"));
  assertEquals("CAT", c.toCode(Integer.valueOf(66), null));
  assertEquals("S1", c.toCode("", ""));
  assertEquals("S1", c.toCode("", "s"));
  assertEquals("S1", c.toCode("", "s1"));
  assertEquals("S1", c.toCode("", null));
  try {
    c.toCode("bogus", "bogus");
    fail("Should have thrown ConstantException");
    c.toCode("bogus", null);
    fail("Should have thrown ConstantException");
  assertEquals("MY_PROPERTY_NO", c.toCodeForProperty(Integer.valueOf(1), "myProperty"));
  assertEquals("MY_PROPERTY_YES", c.toCodeForProperty(Integer.valueOf(2), "myProperty"));
  try {
    c.toCodeForProperty("bogus", "bogus");
    fail("Should have thrown ConstantException");

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

@Test
public void getNames() {
  Constants c = new Constants(A.class);
  Set<?> names = c.getNames("");
  assertEquals(c.getSize(), names.size());
  assertTrue(names.contains("DOG"));
  assertTrue(names.contains("CAT"));
  assertTrue(names.contains("S1"));
  names = c.getNames("D");
  assertEquals(1, names.size());
  assertTrue(names.contains("DOG"));
  names = c.getNames("d");
  assertEquals(1, names.size());
  assertTrue(names.contains("DOG"));
}

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

@Test
public void ctorWithNullClass() throws Exception {
  try {
    new Constants(null);
    fail("Must have thrown IllegalArgumentException");
  }
  catch (IllegalArgumentException expected) {}
}

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

@Test
public void suffixAccess() {
  Constants c = new Constants(A.class);
  Set<?> names = c.getNamesForSuffix("_PROPERTY");
  assertEquals(2, names.size());
  assertTrue(names.contains("NO_PROPERTY"));
  assertTrue(names.contains("YES_PROPERTY"));
  Set<?> values = c.getValuesForSuffix("_PROPERTY");
  assertEquals(2, values.size());
  assertTrue(values.contains(Integer.valueOf(3)));
  assertTrue(values.contains(Integer.valueOf(4)));
}

代码示例来源:origin: stackoverflow.com

HashMap<String,String> pairs= new HashMap<String,String>();
 Constants constants= new Constants();
 Field[] f = constants.getClass().getFields();
 for (int i = 0; i < f.length; i++) {
   pairs.put(f[i].getName().toLowerCase(),f[i].get(constants).toString());
 }
 System.out.println(pairs);

代码示例来源:origin: stackoverflow.com

Constants cons = new Constants();
imageUrls = cons.getImagePath(Path.toString());

代码示例来源:origin: spring-cloud/spring-cloud-aws

public void setDefaultTransactionIsolationName(String constantName) {
  if (constantName == null) {
    throw new IllegalArgumentException("Isolation name must not be null");
  }
  Constants constants = new Constants(Connection.class);
  setDefaultTransactionIsolation(constants.asNumber(PREFIX_ISOLATION + constantName).intValue());
}

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

/**
 * Set the isolation level. Must be one of the isolation constants
 * in the TransactionDefinition interface. Default is ISOLATION_DEFAULT.
 * <p>Exclusively designed for use with {@link #PROPAGATION_REQUIRED} or
 * {@link #PROPAGATION_REQUIRES_NEW} since it only applies to newly started
 * transactions. Consider switching the "validateExistingTransactions" flag to
 * "true" on your transaction manager if you'd like isolation level declarations
 * to get rejected when participating in an existing transaction with a different
 * isolation level.
 * <p>Note that a transaction manager that does not support custom isolation levels
 * will throw an exception when given any other level than {@link #ISOLATION_DEFAULT}.
 * @throws IllegalArgumentException if the supplied value is not one of the
 * {@code ISOLATION_} constants
 * @see #ISOLATION_DEFAULT
 */
public final void setIsolationLevel(int isolationLevel) {
  if (!constants.getValues(PREFIX_ISOLATION).contains(isolationLevel)) {
    throw new IllegalArgumentException("Only values of isolation constants allowed");
  }
  this.isolationLevel = isolationLevel;
}

相关文章