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.boot.context.embedded.undertow.UndertowWebServer.java

@SuppressWarnings("unchecked")
private List<Object> extractListeners() {
    Field listenersField = ReflectionUtils.findField(Undertow.class, "listeners");
    ReflectionUtils.makeAccessible(listenersField);
    return (List<Object>) ReflectionUtils.getField(listenersField, this.undertow);
}

From source file:org.springframework.boot.context.embedded.undertow.UndertowWebServer.java

private UndertowWebServer.Port getPortFromListener(Object listener) {
    Field typeField = ReflectionUtils.findField(listener.getClass(), "type");
    ReflectionUtils.makeAccessible(typeField);
    String protocol = ReflectionUtils.getField(typeField, listener).toString();
    Field portField = ReflectionUtils.findField(listener.getClass(), "port");
    ReflectionUtils.makeAccessible(portField);
    int port = (Integer) ReflectionUtils.getField(portField, listener);
    return new UndertowWebServer.Port(port, protocol);
}

From source file:org.springframework.boot.devtools.autoconfigure.HateoasObjenesisCacheDisabler.java

private void removeObjenesisCache(Class<?> dummyInvocationUtils) {
    try {/*from   ww w  .  j av a2  s .  co  m*/
        Field objenesisField = ReflectionUtils.findField(dummyInvocationUtils, "OBJENESIS");
        if (objenesisField != null) {
            ReflectionUtils.makeAccessible(objenesisField);
            Object objenesis = ReflectionUtils.getField(objenesisField, null);
            Field cacheField = ReflectionUtils.findField(objenesis.getClass(), "cache");
            ReflectionUtils.makeAccessible(cacheField);
            ReflectionUtils.setField(cacheField, objenesis, null);
        }
    } catch (Exception ex) {
        logger.warn("Failed to disable Spring HATEOAS's Objenesis cache. ClassCastExceptions may occur", ex);
    }
}

From source file:org.springframework.boot.diagnostics.FailureAnalyzers.java

private List<FailureAnalyzer> loadFailureAnalyzers(ClassLoader classLoader) {
    List<String> analyzerNames = SpringFactoriesLoader.loadFactoryNames(FailureAnalyzer.class, classLoader);
    List<FailureAnalyzer> analyzers = new ArrayList<FailureAnalyzer>();
    for (String analyzerName : analyzerNames) {
        try {/*from ww w . j a  v  a2  s.c  o m*/
            Constructor<?> constructor = ClassUtils.forName(analyzerName, classLoader).getDeclaredConstructor();
            ReflectionUtils.makeAccessible(constructor);
            analyzers.add((FailureAnalyzer) constructor.newInstance());
        } catch (Throwable ex) {
            log.trace("Failed to load " + analyzerName, ex);
        }
    }
    AnnotationAwareOrderComparator.sort(analyzers);
    return analyzers;
}

From source file:org.springframework.boot.web.client.RestTemplateBuilder.java

private ClientHttpRequestFactory unwrapRequestFactoryIfNecessary(ClientHttpRequestFactory requestFactory) {
    if (!(requestFactory instanceof AbstractClientHttpRequestFactoryWrapper)) {
        return requestFactory;
    }/*from   w  w w . j ava 2s .  co  m*/
    ClientHttpRequestFactory unwrappedRequestFactory = requestFactory;
    Field field = ReflectionUtils.findField(AbstractClientHttpRequestFactoryWrapper.class, "requestFactory");
    ReflectionUtils.makeAccessible(field);
    do {
        unwrappedRequestFactory = (ClientHttpRequestFactory) ReflectionUtils.getField(field,
                unwrappedRequestFactory);
    } while (unwrappedRequestFactory instanceof AbstractClientHttpRequestFactoryWrapper);
    return unwrappedRequestFactory;
}

From source file:org.springframework.boot.web.embedded.undertow.UndertowServletWebServer.java

private Port getPortFromListener(Object listener) {
    Field typeField = ReflectionUtils.findField(listener.getClass(), "type");
    ReflectionUtils.makeAccessible(typeField);
    String protocol = ReflectionUtils.getField(typeField, listener).toString();
    Field portField = ReflectionUtils.findField(listener.getClass(), "port");
    ReflectionUtils.makeAccessible(portField);
    int port = (Integer) ReflectionUtils.getField(portField, listener);
    return new Port(port, protocol);
}

From source file:org.springframework.cloud.config.server.environment.HttpClientConfigurableHttpConnectionFactoryTest.java

private HttpClient getActualHttpClient(HttpConnection actualConnection) {
    Field clientField = ReflectionUtils.findField(actualConnection.getClass(), "client");
    ReflectionUtils.makeAccessible(clientField);
    return (HttpClient) ReflectionUtils.getField(clientField, actualConnection);
}

From source file:org.springframework.cloud.config.server.environment.HttpClientConfigurableHttpConnectionFactoryTest.java

private HttpClientBuilder getActualHttpClientBuilder(HttpConnection actualConnection) {
    HttpClient actualHttpClient = getActualHttpClient(actualConnection);
    Field closeablesField = ReflectionUtils.findField(actualHttpClient.getClass(), "closeables");
    ReflectionUtils.makeAccessible(closeablesField);
    List<?> closables = (List<?>) ReflectionUtils.getField(closeablesField, actualHttpClient);
    return closables.stream().map(o -> {
        Field builderField = Arrays.stream(o.getClass().getDeclaredFields())
                .filter(field -> HttpClientBuilder.class.isAssignableFrom(field.getType())).findFirst()
                .orElse(null);//from   ww w .java2 s.c o m
        if (builderField != null) {
            ReflectionUtils.makeAccessible(builderField);
            return ReflectionUtils.getField(builderField, o);
        }
        return null;
    }).filter(Objects::nonNull).map(HttpClientBuilder.class::cast).findFirst().get();
}

From source file:org.springframework.cloud.dataflow.rest.util.HttpClientConfigurerTests.java

/**
 * Test ensuring that the {@link AuthScope} is set for the target host.
 *//* w w w .j av  a2s .  c o  m*/
@Test
public void testThatHttpClientWithProxyIsCreatedAndHasCorrectCredentialsProviders() throws Exception {
    final URI targetHost = new URI("http://test.com");
    final HttpClientConfigurer builder = HttpClientConfigurer.create(targetHost);
    builder.basicAuthCredentials("foo", "password");
    builder.withProxyCredentials(URI.create("https://spring.io"), null, null);

    final Field credentialsProviderField = ReflectionUtils.findField(HttpClientConfigurer.class,
            "credentialsProvider");
    ReflectionUtils.makeAccessible(credentialsProviderField);
    CredentialsProvider credentialsProvider = (CredentialsProvider) credentialsProviderField.get(builder);
    Assert.assertNotNull(credentialsProvider.getCredentials(new AuthScope("test.com", 80)));
    Assert.assertNull(credentialsProvider.getCredentials(new AuthScope("spring.io", 80)));
}

From source file:org.springframework.cloud.dataflow.rest.util.HttpClientConfigurerTests.java

/**
 * Test ensuring that the {@link AuthScope} is set for the target host and the proxy server.
 *//* w ww .j ava2 s.co  m*/
@Test
public void testThatHttpClientWithProxyIsCreatedAndHasCorrectCredentialsProviders2() throws Exception {
    final URI targetHost = new URI("http://test.com");
    final HttpClientConfigurer builder = HttpClientConfigurer.create(targetHost);
    builder.basicAuthCredentials("foo", "password");
    builder.withProxyCredentials(URI.create("https://spring.io"), "proxyuser", "proxypassword");

    final Field credentialsProviderField = ReflectionUtils.findField(HttpClientConfigurer.class,
            "credentialsProvider");
    ReflectionUtils.makeAccessible(credentialsProviderField);
    CredentialsProvider credentialsProvider = (CredentialsProvider) credentialsProviderField.get(builder);
    Assert.assertNotNull(credentialsProvider.getCredentials(new AuthScope("test.com", 80)));
    Assert.assertNotNull(credentialsProvider.getCredentials(new AuthScope("spring.io", 80)));
}