Java Code Examples for org.springframework.util.ReflectionUtils#isPublicStaticFinal()

The following examples show how to use org.springframework.util.ReflectionUtils#isPublicStaticFinal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Constants.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create a new Constants converter class wrapping the given class.
 * <p>All <b>public</b> static final variables will be exposed, whatever their type.
 * @param clazz the class to analyze
 * @throws IllegalArgumentException if the supplied {@code clazz} is {@code null}
 */
public Constants(Class<?> clazz) {
	Assert.notNull(clazz, "Class must not be null");
	this.className = clazz.getName();
	Field[] fields = clazz.getFields();
	for (Field field : fields) {
		if (ReflectionUtils.isPublicStaticFinal(field)) {
			String name = field.getName();
			try {
				Object value = field.get(null);
				this.fieldCache.put(name, value);
			}
			catch (IllegalAccessException ex) {
				// just leave this field and continue
			}
		}
	}
}
 
Example 2
Source File: Constants.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create a new Constants converter class wrapping the given class.
 * <p>All <b>public</b> static final variables will be exposed, whatever their type.
 * @param clazz the class to analyze
 * @throws IllegalArgumentException if the supplied {@code clazz} is {@code null}
 */
public Constants(Class<?> clazz) {
	Assert.notNull(clazz, "Class must not be null");
	this.className = clazz.getName();
	Field[] fields = clazz.getFields();
	for (Field field : fields) {
		if (ReflectionUtils.isPublicStaticFinal(field)) {
			String name = field.getName();
			try {
				Object value = field.get(null);
				this.fieldCache.put(name, value);
			}
			catch (IllegalAccessException ex) {
				// just leave this field and continue
			}
		}
	}
}
 
Example 3
Source File: Constants.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new Constants converter class wrapping the given class.
 * <p>All <b>public</b> static final variables will be exposed, whatever their type.
 * @param clazz the class to analyze
 * @throws IllegalArgumentException if the supplied {@code clazz} is {@code null}
 */
public Constants(Class<?> clazz) {
	Assert.notNull(clazz, "Class must not be null");
	this.className = clazz.getName();
	Field[] fields = clazz.getFields();
	for (Field field : fields) {
		if (ReflectionUtils.isPublicStaticFinal(field)) {
			String name = field.getName();
			try {
				Object value = field.get(null);
				this.fieldCache.put(name, value);
			}
			catch (IllegalAccessException ex) {
				// just leave this field and continue
			}
		}
	}
}
 
Example 4
Source File: Constants.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new Constants converter class wrapping the given class.
 * <p>All <b>public</b> static final variables will be exposed, whatever their type.
 * @param clazz the class to analyze
 * @throws IllegalArgumentException if the supplied {@code clazz} is {@code null}
 */
public Constants(Class<?> clazz) {
	Assert.notNull(clazz);
	this.className = clazz.getName();
	Field[] fields = clazz.getFields();
	for (Field field : fields) {
		if (ReflectionUtils.isPublicStaticFinal(field)) {
			String name = field.getName();
			try {
				Object value = field.get(null);
				this.fieldCache.put(name, value);
			}
			catch (IllegalAccessException ex) {
				// just leave this field and continue
			}
		}
	}
}
 
Example 5
Source File: SpringMethodRule.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Throw an {@link IllegalStateException} if the supplied {@code testClass}
 * does not declare a {@code public static final SpringClassRule} field
 * that is annotated with {@code @ClassRule}.
 */
private static SpringClassRule validateSpringClassRuleConfiguration(Class<?> testClass) {
	Field ruleField = null;

	for (Field field : testClass.getFields()) {
		if (ReflectionUtils.isPublicStaticFinal(field) && SpringClassRule.class.isAssignableFrom(field.getType())) {
			ruleField = field;
			break;
		}
	}

	if (ruleField == null) {
		throw new IllegalStateException(String.format(
				"Failed to find 'public static final SpringClassRule' field in test class [%s]. " +
				"Consult the javadoc for SpringClassRule for details.", testClass.getName()));
	}

	if (!ruleField.isAnnotationPresent(ClassRule.class)) {
		throw new IllegalStateException(String.format(
				"SpringClassRule field [%s] must be annotated with JUnit's @ClassRule annotation. " +
				"Consult the javadoc for SpringClassRule for details.", ruleField));
	}

	return (SpringClassRule) ReflectionUtils.getField(ruleField, null);
}