Example usage for org.apache.commons.lang3.reflect FieldUtils readStaticField

List of usage examples for org.apache.commons.lang3.reflect FieldUtils readStaticField

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect FieldUtils readStaticField.

Prototype

public static Object readStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException 

Source Link

Document

Reads the named public static Field .

Usage

From source file:com.holonplatform.core.internal.property.DefaultPropertySetRefIntrospector.java

@Override
public PropertySet<?> getPropertySet(PropertySetRef annotation) throws PropertySetIntrospectionException {
    ObjectUtils.argumentNotNull(annotation, "PropertySetRef annotation must be not null");

    // reference class
    final Class<?> cls = annotation.value();
    if (cls == null) {
        throw new PropertySetIntrospectionException("[PropertySetRef] missing value");
    }/* w  ww .j av a  2 s. co  m*/

    // field name
    String fieldName = AnnotationUtils.getStringValue(annotation.field());

    boolean wasNullFieldName = (fieldName == null);

    if (fieldName == null) {

        // check cache
        synchronized (cache) {
            final CacheKey key = new CacheKey(cls, null);
            if (cache.containsKey(key)) {
                return cache.get(key);
            }
        }

        if (PropertySet.class == cls) {
            throw new PropertySetIntrospectionException(
                    "Invalid PropertySetRef class value: [" + cls.getName() + "]");
        }

        // If the class itself is a PropertySet, try to instantiate it
        if (PropertySet.class.isAssignableFrom(cls)) {
            try {
                return (PropertySet<?>) cls.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new PropertySetIntrospectionException(
                        "[PropertySetRef] Failed to instantiate PropertySet class [" + cls.getName() + "]", e);
            }
        }

        // Look for a public static PropertySet type field
        List<String> candidateFieldNames = new LinkedList<>();
        Field[] flds = cls.getDeclaredFields();
        if (flds != null) {
            for (Field fld : flds) {
                if (Modifier.isStatic(fld.getModifiers()) && Modifier.isPublic(fld.getModifiers())
                        && PropertySet.class.isAssignableFrom(fld.getType())) {
                    candidateFieldNames.add(fld.getName());
                }
            }
        }

        if (candidateFieldNames.isEmpty()) {
            throw new PropertySetIntrospectionException(
                    "[PropertySetRef] Cannot find any valid public static PropertySet type field in class ["
                            + cls.getName() + "]");
        }

        if (candidateFieldNames.size() > 1) {
            throw new PropertySetIntrospectionException(
                    "[PropertySetRef] More than one valid PropertySet type field found in class ["
                            + cls.getName()
                            + "]: please specify the field name to use in PropertySetRef annotation. Detected PropertySet fields: ["
                            + candidateFieldNames + "]");
        }

        fieldName = candidateFieldNames.get(0);

    } else {

        // check cache
        synchronized (cache) {
            final CacheKey key = new CacheKey(cls, fieldName);
            if (cache.containsKey(key)) {
                return cache.get(key);
            }
        }

    }

    if (LOGGER.isEnabled(Level.DEBUG)) {
        final String fn = fieldName;
        LOGGER.debug(() -> "Get PropertySet using PropertySetRef annotation for class [" + cls
                + "] and field name [" + fn + "]");
    }

    // Read the PropertySet field
    try {
        Object value = FieldUtils.readStaticField(cls, fieldName);

        if (value == null) {
            throw new PropertySetIntrospectionException("[PropertySetRef] The field [" + fieldName
                    + "] in class [" + cls.getName() + "] has null value");
        }

        if (!PropertySet.class.isAssignableFrom(value.getClass())) {
            throw new PropertySetIntrospectionException(
                    "[PropertySetRef] The field [" + fieldName + "] in class [" + cls.getName()
                            + "] is not of PropertySet type but [" + value.getClass().getName() + "]");
        }

        final PropertySet<?> propertySet = (PropertySet<?>) value;

        // put in cache and return
        if (wasNullFieldName) {
            cache.putIfAbsent(new CacheKey(cls, null), propertySet);
        }
        PropertySet<?> existing = cache.putIfAbsent(new CacheKey(cls, fieldName), propertySet);
        return (existing != null ? existing : propertySet);

    } catch (IllegalAccessException e) {
        throw new PropertySetIntrospectionException(
                "[PropertySetRef] Failed to read field [" + fieldName + "] from class [" + cls.getName() + "]",
                e);
    }
}

From source file:org.apache.servicecomb.it.junit.ITJUnitUtils.java

private static void initClasses(Class<?>[] classes) throws Throwable {
    for (Class<?> cls : classes) {
        for (Field field : FieldUtils.getAllFieldsList(cls)) {
            if (Consumers.class.isAssignableFrom(field.getType())
                    || GateRestTemplate.class.isAssignableFrom(field.getType())
                    || ITSCBRestTemplate.class.isAssignableFrom(field.getType())) {
                Object target = FieldUtils.readStaticField(field, true);
                MethodUtils.invokeMethod(target, "init");
            }// w ww .j  a v  a 2  s.  c  o  m
        }
    }
}