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:org.springframework.cloud.deployer.thin.ThinJarAppWrapper.java

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

From source file:org.springframework.cloud.netflix.eureka.CloudEurekaClient.java

public CloudEurekaClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config,
        DiscoveryClientOptionalArgs args, ApplicationEventPublisher publisher) {
    super(applicationInfoManager, config, args);
    this.applicationInfoManager = applicationInfoManager;
    this.publisher = publisher;
    this.eurekaTransportField = ReflectionUtils.findField(DiscoveryClient.class, "eurekaTransport");
    ReflectionUtils.makeAccessible(this.eurekaTransportField);
}

From source file:org.springframework.cloud.netflix.eureka.CloudEurekaClient.java

EurekaHttpClient getEurekaHttpClient() {
    if (this.eurekaHttpClient.get() == null) {
        try {//from w ww  .  j a va 2 s  .  co  m
            Object eurekaTransport = this.eurekaTransportField.get(this);
            Field registrationClientField = ReflectionUtils.findField(eurekaTransport.getClass(),
                    "registrationClient");
            ReflectionUtils.makeAccessible(registrationClientField);
            this.eurekaHttpClient.compareAndSet(null,
                    (EurekaHttpClient) registrationClientField.get(eurekaTransport));
        } catch (IllegalAccessException e) {
            log.error("error getting EurekaHttpClient", e);
        }
    }
    return this.eurekaHttpClient.get();
}

From source file:org.springframework.cloud.netflix.feign.FeignHttpClientConfigurationTests.java

protected <T> Object getField(Object target, String name) {
    Field field = ReflectionUtils.findField(target.getClass(), name);
    ReflectionUtils.makeAccessible(field);
    Object value = ReflectionUtils.getField(field, target);
    return value;
}

From source file:org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilterTests.java

protected <T> T getField(Object target, String name) {
    Field field = ReflectionUtils.findField(target.getClass(), name);
    ReflectionUtils.makeAccessible(field);
    Object value = ReflectionUtils.getField(field, target);
    return (T) value;
}

From source file:org.springframework.cloud.netflix.zuul.test.ZuulApacheHttpClientConfigurationTests.java

@SuppressWarnings("unchecked")
protected <T> T getField(Object target, String name) {
    Field field = ReflectionUtils.findField(target.getClass(), name);
    ReflectionUtils.makeAccessible(field);
    Object value = ReflectionUtils.getField(field, target);
    return (T) value;
}

From source file:org.springframework.cloud.netflix.zuul.ZuulFilterInitializer.java

private void clearLoaderCache() {
    Field field = ReflectionUtils.findField(FilterLoader.class, "hashFiltersByType");
    ReflectionUtils.makeAccessible(field);
    @SuppressWarnings("rawtypes")
    Map cache = (Map) ReflectionUtils.getField(field, filterLoader);
    cache.clear();/*from w  w w. j  a v  a  2  s  .  com*/
}

From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsStreamListenerSetupMethodOrchestrator.java

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void orchestrateStreamListenerSetupMethod(StreamListener streamListener, Method method, Object bean) {
    String[] methodAnnotatedOutboundNames = getOutboundBindingTargetNames(method);
    validateStreamListenerMethod(streamListener, method, methodAnnotatedOutboundNames);
    String methodAnnotatedInboundName = streamListener.value();
    Object[] adaptedInboundArguments = adaptAndRetrieveInboundArguments(method, methodAnnotatedInboundName,
            this.applicationContext, this.streamListenerParameterAdapter);
    try {//w  w w. j ava 2 s .c o m
        ReflectionUtils.makeAccessible(method);
        if (Void.TYPE.equals(method.getReturnType())) {
            method.invoke(bean, adaptedInboundArguments);
        } else {
            Object result = method.invoke(bean, adaptedInboundArguments);

            if (result.getClass().isArray()) {
                Assert.isTrue(methodAnnotatedOutboundNames.length == ((Object[]) result).length,
                        "Result does not match with the number of declared outbounds");
            } else {
                Assert.isTrue(methodAnnotatedOutboundNames.length == 1,
                        "Result does not match with the number of declared outbounds");
            }
            if (result.getClass().isArray()) {
                Object[] outboundKStreams = (Object[]) result;
                int i = 0;
                for (Object outboundKStream : outboundKStreams) {
                    Object targetBean = this.applicationContext.getBean(methodAnnotatedOutboundNames[i++]);
                    for (StreamListenerResultAdapter streamListenerResultAdapter : this.streamListenerResultAdapters) {
                        if (streamListenerResultAdapter.supports(outboundKStream.getClass(),
                                targetBean.getClass())) {
                            streamListenerResultAdapter.adapt(outboundKStream, targetBean);
                            break;
                        }
                    }
                }
            } else {
                Object targetBean = this.applicationContext.getBean(methodAnnotatedOutboundNames[0]);
                for (StreamListenerResultAdapter streamListenerResultAdapter : this.streamListenerResultAdapters) {
                    if (streamListenerResultAdapter.supports(result.getClass(), targetBean.getClass())) {
                        streamListenerResultAdapter.adapt(result, targetBean);
                        break;
                    }
                }
            }
        }
    } catch (Exception ex) {
        throw new BeanInitializationException("Cannot setup StreamListener for " + method, ex);
    }
}

From source file:org.springframework.context.event.ApplicationListenerMethodAdapter.java

/**
 * Invoke the event listener method with the given argument values.
 *///from w ww  .  j  a  v  a 2  s  .  com
@Nullable
protected Object doInvoke(Object... args) {
    Object bean = getTargetBean();
    ReflectionUtils.makeAccessible(this.bridgedMethod);
    try {
        return this.bridgedMethod.invoke(bean, args);
    } catch (IllegalArgumentException ex) {
        assertTargetBean(this.bridgedMethod, bean, args);
        throw new IllegalStateException(getInvocationErrorMessage(bean, ex.getMessage(), args), ex);
    } catch (IllegalAccessException ex) {
        throw new IllegalStateException(getInvocationErrorMessage(bean, ex.getMessage(), args), ex);
    } catch (InvocationTargetException ex) {
        // Throw underlying exception
        Throwable targetException = ex.getTargetException();
        if (targetException instanceof RuntimeException) {
            throw (RuntimeException) targetException;
        } else {
            String msg = getInvocationErrorMessage(bean, "Failed to invoke event listener method", args);
            throw new UndeclaredThrowableException(targetException, msg);
        }
    }
}

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Retrieve the <em>value</em> of a named attribute, given an annotation instance.
 * @param annotation the annotation instance from which to retrieve the value
 * @param attributeName the name of the attribute value to retrieve
 * @return the attribute value, or {@code null} if not found unless the attribute
 * value cannot be retrieved due to an {@link AnnotationConfigurationException},
 * in which case such an exception will be rethrown
 * @see #getValue(Annotation)//  www  .  ja va2s .  c o  m
 * @see #rethrowAnnotationConfigurationException(Throwable)
 */
@Nullable
public static Object getValue(@Nullable Annotation annotation, @Nullable String attributeName) {
    if (annotation == null || !StringUtils.hasText(attributeName)) {
        return null;
    }
    try {
        Method method = annotation.annotationType().getDeclaredMethod(attributeName);
        ReflectionUtils.makeAccessible(method);
        return method.invoke(annotation);
    } catch (InvocationTargetException ex) {
        rethrowAnnotationConfigurationException(ex.getTargetException());
        throw new IllegalStateException(
                "Could not obtain value for annotation attribute '" + attributeName + "' in " + annotation, ex);
    } catch (Throwable ex) {
        handleIntrospectionFailure(annotation.getClass(), ex);
        return null;
    }
}