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, final boolean forceAccess)
        throws IllegalAccessException 

Source Link

Document

Reads the named static Field .

Usage

From source file:com.github.srgg.yads.JsonUnitInitializer.java

public static ObjectMapper initialize() {
    // making JsonAssert to be more tolerant to JSON format
    final Object converter;
    try {/*from w w w . ja v a2 s  .com*/
        converter = FieldUtils.readStaticField(JsonUtils.class, "converter", true);
        final List<NodeFactory> factories = (List<NodeFactory>) FieldUtils.readField(converter, "factories",
                true);

        ObjectMapper mapper;
        for (NodeFactory nf : factories) {
            if (nf.getClass().getSimpleName().equals("Jackson2NodeFactory")) {
                mapper = (ObjectMapper) FieldUtils.readField(nf, "mapper", true);
                mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true)
                        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
                        .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
                        .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
                        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

                return mapper;
            }
        }
    } catch (IllegalAccessException e) {
        throw new IllegalStateException("Can't initialize Jackson2 ObjectMapper because of UE", e);
    }

    throw new IllegalStateException("Can't initialize Jackson2 ObjectMapper, Jackson2NodeFactory is not found");
}

From source file:com.mycompany.selenium.factory.tests.Init.java

private static Page getPage(String packageName, String title) throws Exception {
    Reflections reflections = new Reflections(packageName);
    Set<Class<? extends Page>> allClasses = reflections.getSubTypesOf(Page.class);
    for (Class<? extends Page> page : allClasses) {
        String pageTitle = (String) FieldUtils.readStaticField(page, "title", true);
        if (pageTitle.equals(title)) {
            @SuppressWarnings("unchecked")
            Constructor<Page> c = ((Constructor<Page>) page.getConstructor());
            currentPage = c.newInstance();
            currentPageTitle = title;/*  w w w.j  a v  a2s .  c  om*/
            return currentPage;
        }
    }
    return null;
}

From source file:org.apache.servicecomb.swagger.converter.TestConverterMgr.java

@Test
public void propertyMapGenericSignature() throws IllegalAccessException {
    @SuppressWarnings("unchecked")
    Map<Class<? extends Property>, JavaType> propertyMap = (Map<Class<? extends Property>, JavaType>) FieldUtils
            .readStaticField(ConverterMgr.class, "PROPERTY_MAP", true);

    Assert.assertEquals("Ljava/lang/Boolean;", propertyMap.get(BooleanProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/lang/Float;", propertyMap.get(FloatProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/lang/Double;", propertyMap.get(DoubleProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/math/BigDecimal;", propertyMap.get(DecimalProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/lang/Byte;", propertyMap.get(ByteProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/lang/Short;", propertyMap.get(ShortProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", propertyMap.get(IntegerProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;",
            propertyMap.get(BaseIntegerProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/lang/Long;", propertyMap.get(LongProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/lang/String;", propertyMap.get(StringProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/time/LocalDate;", propertyMap.get(DateProperty.class).getGenericSignature());
    Assert.assertEquals("Ljava/util/Date;", propertyMap.get(DateTimeProperty.class).getGenericSignature());
    Assert.assertEquals("[B;", propertyMap.get(ByteArrayProperty.class).getGenericSignature());
    Assert.assertEquals("Ljavax/servlet/http/Part;", propertyMap.get(FileProperty.class).getGenericSignature());
}

From source file:org.apache.servicecomb.swagger.generator.core.TestClassUtils.java

@Test
public void testCorrectClassNameReservedWords() throws IllegalAccessException {
    @SuppressWarnings("unchecked")
    Set<String> JAVA_RESERVED_WORDS = (Set<String>) FieldUtils.readStaticField(SourceVersion.class, "keywords",
            true);//from   w ww  .j  ava  2  s.  c  o m
    String name = String.join(".", JAVA_RESERVED_WORDS);
    String expectResult = "_" + String.join("._", JAVA_RESERVED_WORDS);

    String result = ClassUtils.correctClassName(name);
    Assert.assertThat(result, is(expectResult));
}

From source file:org.broadleafcommerce.admin.web.rulebuilder.service.options.InventoryTypeEnumOptionsExtensionListener.java

/**
 * Overridden to remove deprecated options
 *//*from w w  w .  ja va 2 s.c  om*/
@Override
protected Map<String, ? extends BroadleafEnumerationType> getTypes(
        Class<? extends BroadleafEnumerationType> clazz) {

    try {
        Map<String, ? extends BroadleafEnumerationType> options = (Map<String, ? extends BroadleafEnumerationType>) FieldUtils
                .readStaticField(clazz, "TYPES", true);
        options.remove("NONE");
        options.remove("BASIC");
        return options;
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.broadleafcommerce.openadmin.web.rulebuilder.enums.AbstractRuleBuilderEnumOptionsExtensionListener.java

@SuppressWarnings("unchecked")
protected Map<String, ? extends BroadleafEnumerationType> getTypes(
        Class<? extends BroadleafEnumerationType> clazz) {
    try {/*from   w  ww.  ja  v a 2s. c om*/
        return (Map<String, ? extends BroadleafEnumerationType>) FieldUtils.readStaticField(clazz, "TYPES",
                true);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.silverpeas.core.test.extention.LocalizationBundleStub.java

@SuppressWarnings("unchecked")
private void init() throws IllegalAccessException {
    currentLocale = Locale.getDefault();
    Locale.setDefault(Locale.FRANCE);
    final String newDefaultLocale = Locale.getDefault().getLanguage();
    reflectionRule.setField(DisplayI18NHelper.class, newDefaultLocale, "defaultLanguage");
    bundleCache = (Map) FieldUtils.readStaticField(ResourceLocator.class, "bundles", true);
    bundleCache.put(bundleName, new LocalizationBundleTest(newDefaultLocale, bundleMap));
    bundleNames.add(bundleName);/* w ww .  j  a  v a 2s.c  o  m*/
    DisplayI18NHelper.getLanguages().forEach(l -> {
        final String bundleLocaleName = bundleName + "_" + l;
        bundleCache.put(bundleLocaleName, new LocalizationBundleTest(l, bundleMap));
        bundleNames.add(bundleLocaleName);
    });
}

From source file:org.silverpeas.core.test.extention.SettingBundleStub.java

@SuppressWarnings("unchecked")
private void init() throws IllegalAccessException {
    bundleCache = (Map) FieldUtils.readStaticField(ResourceLocator.class, "bundles", true);
    bundleCache.put(bundleName, new SettingBundleTest(settingMap));
    settingNames.add(bundleName);// ww w .  j  av a2s .c  o m
}

From source file:org.silverpeas.core.util.AbstractUnitTest.java

@SuppressWarnings("unchecked")
@Before/*w w  w  .j  a  va  2s  .  co  m*/
public void setup() throws Exception {
    currentLocale = Locale.getDefault();
    Locale.setDefault(Locale.FRANCE);
    reflectionRule.setField(DisplayI18NHelper.class, Locale.getDefault().getLanguage(), "defaultLanguage");
    bundleCache = (Map) FieldUtils.readStaticField(ResourceLocator.class, "bundles", true);
    LocalizationBundle unitsBundle = mock(LocalizationBundle.class);
    when(unitsBundle.handleGetObject(anyString())).thenAnswer(invocation -> {
        String key = (String) invocation.getArguments()[0];
        return bundle.get(key);
    });
    bundleCache.put("org.silverpeas.util.multilang.util", unitsBundle);
    bundleCache.put("org.silverpeas.util.multilang.util_" + Locale.getDefault().getLanguage(), unitsBundle);
}

From source file:org.sparkcommerce.admin.web.rulebuilder.service.options.InventoryTypeEnumOptionsExtensionListener.java

/**
 * Overridden to remove deprecated options
 *//*from w  ww.j  a va2s.  com*/
@Override
protected Map<String, ? extends SparkEnumerationType> getTypes(Class<? extends SparkEnumerationType> clazz) {

    try {
        Map<String, ? extends SparkEnumerationType> options = (Map<String, ? extends SparkEnumerationType>) FieldUtils
                .readStaticField(clazz, "TYPES", true);
        options.remove("NONE");
        options.remove("BASIC");
        return options;
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}