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.ciphertool.genetics.algorithms.selection.ProbabilisticSelectionAlgorithmTest.java

@BeforeClass
public static void setUp() {
    probabilisticSelectionAlgorithm = new ProbabilisticSelectionAlgorithm();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(ProbabilisticSelectionAlgorithm.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, probabilisticSelectionAlgorithm, logMock);
}

From source file:com.aestheticsw.jobkeywords.shared.config.LogInjector.java

@Override
public Object postProcessBeforeInitialization(final Object bean, String name) throws BeansException {
    ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            // make the field accessible if defined private
            ReflectionUtils.makeAccessible(field);
            if (field.getAnnotation(Log.class) != null) {
                Logger log = LoggerFactory.getLogger(bean.getClass());
                field.set(bean, log);//w ww. ja v a 2 s  .  c  o m
            }
        }
    });
    return bean;
}

From source file:com.ciphertool.genetics.algorithms.selection.TruncationSelectionAlgorithmTest.java

@BeforeClass
public static void setUp() {
    truncationSelectionAlgorithm = new TruncationSelectionAlgorithm();

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(TruncationSelectionAlgorithm.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, truncationSelectionAlgorithm, logMock);
}

From source file:eu.artofcoding.beetlejuice.spring.Slf4jPostProcessor.java

public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {
    ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
        @SuppressWarnings("unchecked")
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);
            // Check if the field is annoted with @Slf4j
            if (field.getAnnotation(Slf4j.class) != null) {
                logger.debug("Injecting Logger into " + beanName + "/" + bean.getClass());
                //Slf4j slf4jAnnotation = field.getAnnotation(Slf4j.class);
                Logger logger = LoggerFactory.getLogger(bean.getClass());
                field.set(bean, logger);
            }//from   ww  w  .  ja  va  2s . c om
        }
    });
    return bean;
}

From source file:io.dyn.core.handler.HandlerMethod.java

public HandlerMethod(Method method, HandlerMethodArgument[] arguments, Guard guard) {
    this.method = method;
    ReflectionUtils.makeAccessible(method);
    this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
    ReflectionUtils.makeAccessible(bridgedMethod);
    this.arguments = arguments;
    this.guard = guard;
    this.invocationHandler = (Proxy.isProxyClass(method.getDeclaringClass())
            ? Proxy.getInvocationHandler(method.getDeclaringClass())
            : null);//from   w w w  . j  a v  a 2s  .com
}

From source file:spring.osgi.utils.OsgiBundleUtils.java

/**
 * Returns the underlying BundleContext for the given Bundle. This uses
 * reflection and highly dependent of the OSGi implementation. Should not be
 * used if OSGi 4.1 is being used.//  w  w  w . j a v  a 2  s.com
 *
 * @param bundle OSGi bundle
 * @return the bundle context for this bundle
 */
public static BundleContext getBundleContext(final Bundle bundle) {
    if (bundle == null)
        return null;

    // try Equinox getContext
    Method meth = ReflectionUtils.findMethod(bundle.getClass(), "getContext", new Class[0]);

    // fallback to getBundleContext (OSGi 4.1)
    if (meth == null)
        meth = ReflectionUtils.findMethod(bundle.getClass(), "getBundleContext", new Class[0]);

    final Method m = meth;

    if (meth != null) {
        ReflectionUtils.makeAccessible(meth);
        return (BundleContext) ReflectionUtils.invokeMethod(m, bundle);
    }

    // fallback to field inspection (KF and Prosyst)
    final BundleContext[] ctx = new BundleContext[1];

    ReflectionUtils.doWithFields(bundle.getClass(), new FieldCallback() {

        public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);
            ctx[0] = (BundleContext) field.get(bundle);
        }
    }, new FieldFilter() {

        public boolean matches(Field field) {
            return BundleContext.class.isAssignableFrom(field.getType());
        }
    });

    return ctx[0];
}

From source file:net.paslavsky.springrest.ToStringMethodInterceptorTest.java

@BeforeMethod
public void setUp(Method method) throws Exception {
    Constructor<?> constructor = ReflectiveMethodInvocation.class.getDeclaredConstructors()[0];
    ReflectionUtils.makeAccessible(constructor);
    invocation = (ReflectiveMethodInvocation) constructor.newInstance(this, this, method, null, getClass(),
            null);//from   ww w. ja  v  a2  s  .c om
}

From source file:com.ryantenney.metrics.spring.GaugeFieldAnnotationBeanPostProcessor.java

@Override
protected void withField(final Object bean, String beanName, Class<?> targetClass, final Field field) {
    ReflectionUtils.makeAccessible(field);

    final Gauge annotation = field.getAnnotation(Gauge.class);
    final String metricName = Util.forGauge(targetClass, field, annotation);

    metrics.register(metricName, new com.codahale.metrics.Gauge<Object>() {
        @Override/*from w  ww.  j  ava2 s . c  o m*/
        public Object getValue() {
            Object value = ReflectionUtils.getField(field, bean);
            if (value instanceof com.codahale.metrics.Gauge) {
                value = ((com.codahale.metrics.Gauge<?>) value).getValue();
            }
            return value;
        }
    });

    LOG.debug("Created gauge {} for field {}.{}", metricName, targetClass.getCanonicalName(), field.getName());
}

From source file:com.ciphertool.genetics.algorithms.selection.TournamentSelectionAlgorithmTest.java

@BeforeClass
public static void setUp() {
    tournamentSelectionAlgorithm = new TournamentSelectionAlgorithm();
    tournamentSelectionAlgorithm.setGroupSize(3);
    TournamentSelector tournamentSelector = new TournamentSelector();
    tournamentSelector.setSelectionAccuracy(0.9);
    tournamentSelectionAlgorithm.setGroupSelector(tournamentSelector);

    logMock = mock(Logger.class);
    Field logField = ReflectionUtils.findField(TournamentSelectionAlgorithm.class, "log");
    ReflectionUtils.makeAccessible(logField);
    ReflectionUtils.setField(logField, tournamentSelectionAlgorithm, logMock);
}

From source file:au.id.hazelwood.xmltvguidebuilder.utils.ClassPathResourceUtilsUnitTest.java

@Test
public void testConstructor() throws Exception {
    Constructor<ClassPathResourceUtils> constructor = ClassPathResourceUtils.class.getDeclaredConstructor();
    assertFalse(constructor.isAccessible());
    ReflectionUtils.makeAccessible(constructor);
    assertNotNull(constructor.newInstance());
}