Example usage for org.springframework.util ReflectionUtils makeAccessible

List of usage examples for org.springframework.util ReflectionUtils makeAccessible

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils makeAccessible.

Prototype

@SuppressWarnings("deprecation") 
public static void makeAccessible(Field field) 

Source Link

Document

Make the given field accessible, explicitly setting it accessible if necessary.

Usage

From source file:com.opendesign.utils.CmnUtil.java

/**
 * client to server: html enter  //from w  ww  .  jav a  2 s  . c  o m
 * @param vo
 * @param fieldName
 */
public static void handleHtmlEnterRN2BR(Object vo, String fieldName) {
    if (vo == null) {
        return;
    }

    Class<?> clazz = vo.getClass();
    // 
    Field aField = ReflectionUtils.findField(clazz, fieldName);
    if (aField != null) {
        ReflectionUtils.makeAccessible(aField);
        String contents = (String) ReflectionUtils.getField(aField, vo);
        contents = handleHtmlEnterRN2BR(contents);
        ReflectionUtils.setField(aField, vo, contents);
    }
}

From source file:com.opendesign.utils.CmnUtil.java

/**
 * server to client: html enter  // w  w  w  .j a va2 s.  c  o m
 * @param vo
 * @param fieldName
 */
public static void handleHtmlEnterBR2RN(Object vo, String fieldName) {
    if (vo == null) {
        return;
    }

    Class<?> clazz = vo.getClass();
    // 
    Field aField = ReflectionUtils.findField(clazz, fieldName);
    if (aField != null) {
        ReflectionUtils.makeAccessible(aField);
        String contents = (String) ReflectionUtils.getField(aField, vo);
        contents = handleHtmlEnterBR2RN(contents);
        ReflectionUtils.setField(aField, vo, contents);
    }
}

From source file:com.thoughtworks.go.server.database.SqlSessionFactoryBean.java

private SqlSessionFactory buildSqlSessionFactory() throws IOException {
    SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();

    Configuration baseConfiguration = new XMLConfigBuilder(configLocation.getInputStream()).parse();

    String ibatisConfigXmlLocation = databaseStrategy.getIbatisConfigXmlLocation();
    if (isNotBlank(ibatisConfigXmlLocation)) {
        XMLConfigBuilder builder = new XMLConfigBuilder(
                new ClassPathResource(ibatisConfigXmlLocation).getInputStream());

        // hacky way to "inject" a previous configuration
        Field configurationField = ReflectionUtils.findField(XMLConfigBuilder.class, "configuration");
        ReflectionUtils.makeAccessible(configurationField);
        ReflectionUtils.setField(configurationField, builder, baseConfiguration);

        baseConfiguration = builder.parse();
    }/* ww  w.ja v  a 2s .com*/

    baseConfiguration.setEnvironment(new Environment(getClass().getSimpleName(),
            new SpringManagedTransactionFactory(), this.dataSource));

    return factoryBuilder.build(baseConfiguration);
}

From source file:demo.ChildMethodRule.java

@Override
public Statement apply(Statement base, FrameworkMethod frameworkMethod, Object testInstance) {
    Class<?> testClass = testInstance.getClass();
    Method method = ReflectionUtils.findMethod(SpringClassRule.class, "getTestContextManager", Class.class);
    ReflectionUtils.makeAccessible(method);
    TestContextManager testContextManager = (TestContextManager) ReflectionUtils.invokeMethod(method, null,
            testClass);/*from  w w w . j a  v a2s .  co  m*/
    TestContext testContext = (TestContext) ReflectionTestUtils.getField(testContextManager, "testContext");

    if (logger.isDebugEnabled()) {
        logger.debug("Applying ChildMethodRule to test method [" + frameworkMethod.getMethod() + "].");
    }
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            delegate(base, frameworkMethod, testInstance, testContext);
        }

    };
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Retrieves a PropertyDescriptor for the specified instance and property value
 *
 * @param instance The instance// ww  w .  j  a  v  a2s. c o  m
 * @param propertyValue The value of the property
 * @return The PropertyDescriptor
 */
public static PropertyDescriptor getPropertyDescriptorForValue(Object instance, Object propertyValue) {
    if (instance == null || propertyValue == null) {
        return null;
    }

    PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(instance.getClass());
    for (PropertyDescriptor pd : descriptors) {
        if (isAssignableOrConvertibleFrom(pd.getPropertyType(), propertyValue.getClass())) {
            Object value;
            try {
                ReflectionUtils.makeAccessible(pd.getReadMethod());
                value = pd.getReadMethod().invoke(instance);
            } catch (Exception e) {
                throw new FatalBeanException("Problem calling readMethod of " + pd, e);
            }
            if (propertyValue.equals(value)) {
                return pd;
            }
        }
    }
    return null;
}

From source file:grails.util.GrailsClassUtils.java

/**
 * <p>Get a static field value.</p>
 *
 * @param clazz The class to check for static property
 * @param name The field name/*  w w w.j  a  v  a 2s . c  o m*/
 * @return The value if there is one, or null if unset OR there is no such field
 */
public static Object getStaticFieldValue(Class<?> clazz, String name) {
    Field field = ReflectionUtils.findField(clazz, name);
    if (field != null) {
        ReflectionUtils.makeAccessible(field);
        try {
            return field.get(clazz);
        } catch (IllegalAccessException ignored) {
        }
    }
    return null;
}

From source file:grails.util.GrailsClassUtils.java

/**
 * <p>Get a static property value, which has a public static getter or is just a public static field.</p>
 *
 * @param clazz The class to check for static property
 * @param name The property name//from   ww w. jav  a2  s . co m
 * @return The value if there is one, or null if unset OR there is no such property
 */
public static Object getStaticPropertyValue(Class<?> clazz, String name) {
    Method getter = BeanUtils.findDeclaredMethod(clazz, getGetterName(name), (Class[]) null);
    try {
        if (getter != null) {
            ReflectionUtils.makeAccessible(getter);
            return getter.invoke(clazz);
        }
        return getStaticFieldValue(clazz, name);
    } catch (Exception ignored) {
        // ignored
    }
    return null;
}

From source file:hello.MetricsActivator.java

private static Object getField(Object target, String name) {
    Assert.notNull(target, "Target object must not be null");
    Field field = ReflectionUtils.findField(target.getClass(), name);
    if (field == null) {
        throw new IllegalArgumentException("Could not find field [" + name + "] on target [" + target + "]");
    }/*from   w  w  w  .j a  v  a  2 s.  co m*/

    if (logger.isDebugEnabled()) {
        logger.debug("Getting field [" + name + "] from target [" + target + "]");
    }
    ReflectionUtils.makeAccessible(field);
    return ReflectionUtils.getField(field, target);
}

From source file:io.renren.modules.job.utils.ScheduleRunnable.java

@Override
public void run() {
    try {//from  w ww  . j a v a2 s .c om
        ReflectionUtils.makeAccessible(method);
        if (StringUtils.isNotBlank(params)) {
            method.invoke(target, params);
        } else {
            method.invoke(target);
        }
    } catch (Exception e) {
        throw new RRException("", e);
    }
}

From source file:io.servicecomb.springboot.starter.configuration.CseAutoConfiguration.java

private static void setStatic(Class<?> type, String name, Object value) {
    // Hack a private static field
    Field field = ReflectionUtils.findField(type, name);
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, null, value);
}