Example usage for org.springframework.util ReflectionUtils findField

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

Introduction

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

Prototype

@Nullable
public static Field findField(Class<?> clazz, String name) 

Source Link

Document

Attempt to find a Field field on the supplied Class with the supplied name .

Usage

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 {/*  w  w  w.j a v a 2 s .c o  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.web.client.RestTemplateBuilder.java

private ClientHttpRequestFactory unwrapRequestFactoryIfNecessary(ClientHttpRequestFactory requestFactory) {
    if (!(requestFactory instanceof AbstractClientHttpRequestFactoryWrapper)) {
        return requestFactory;
    }/*from  ww w  . j  a  v  a  2 s. c o 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 getPortFromChannel(BoundChannel channel) {
    SocketAddress socketAddress = channel.getLocalAddress();
    if (socketAddress instanceof InetSocketAddress) {
        String protocol = (ReflectionUtils.findField(channel.getClass(), "ssl") != null) ? "https" : "http";
        return new Port(((InetSocketAddress) socketAddress).getPort(), protocol);
    }/* w  w  w. ja v a2  s.co  m*/
    return null;
}

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

private UndertowWebServer.Port getPortFromChannel(BoundChannel channel) {
    SocketAddress socketAddress = channel.getLocalAddress();
    if (socketAddress instanceof InetSocketAddress) {
        Field sslField = ReflectionUtils.findField(channel.getClass(), "ssl");
        String protocol = (sslField != null) ? "https" : "http";
        return new UndertowWebServer.Port(((InetSocketAddress) socketAddress).getPort(), protocol);
    }/*from  ww w  . java  2s  .  c o  m*/
    return null;
}

From source file:org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationListener.java

private void initField() {
    propertySourcesField = ReflectionUtils.findField(CompositePropertySource.class, "propertySources");
    propertySourcesField.setAccessible(true);
}

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  w  w  w  .j av a  2s. 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.
 *//*from  w ww . ja v  a 2s. 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)));
}