Browse Source

Tidy up compiler warnings for generics

pull/110/head
Dave Syer 9 years ago
parent
commit
07f3efe5fe
  1. 30
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/actuator/FeaturesEndpoint.java
  2. 33
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/actuator/HasFeatures.java
  3. 42
      spring-cloud-commons/src/test/java/org/springframework/cloud/client/actuator/FeaturesEndpointTests.java

30
spring-cloud-commons/src/main/java/org/springframework/cloud/client/actuator/FeaturesEndpoint.java

@ -3,8 +3,6 @@ package org.springframework.cloud.client.actuator; @@ -3,8 +3,6 @@ package org.springframework.cloud.client.actuator;
import java.util.ArrayList;
import java.util.List;
import lombok.Value;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
@ -12,11 +10,14 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @@ -12,11 +10,14 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import lombok.Value;
/**
* @author Spencer Gibb
*/
@ConfigurationProperties(prefix = "endpoints.features", ignoreUnknownFields = false)
public class FeaturesEndpoint extends AbstractEndpoint<FeaturesEndpoint.Features> implements ApplicationContextAware {
public class FeaturesEndpoint extends AbstractEndpoint<FeaturesEndpoint.Features>
implements ApplicationContextAware {
private final List<HasFeatures> hasFeaturesList;
private ApplicationContext context;
@ -35,10 +36,10 @@ public class FeaturesEndpoint extends AbstractEndpoint<FeaturesEndpoint.Features @@ -35,10 +36,10 @@ public class FeaturesEndpoint extends AbstractEndpoint<FeaturesEndpoint.Features
public Features invoke() {
Features features = new Features();
for (HasFeatures hasFeatures : hasFeaturesList) {
List<Class> abstractFeatures = hasFeatures.getAbstractFeatures();
for (HasFeatures hasFeatures : this.hasFeaturesList) {
List<Class<?>> abstractFeatures = hasFeatures.getAbstractFeatures();
if (abstractFeatures != null) {
for (Class clazz : abstractFeatures) {
for (Class<?> clazz : abstractFeatures) {
addAbstractFeature(features, clazz);
}
}
@ -54,23 +55,24 @@ public class FeaturesEndpoint extends AbstractEndpoint<FeaturesEndpoint.Features @@ -54,23 +55,24 @@ public class FeaturesEndpoint extends AbstractEndpoint<FeaturesEndpoint.Features
return features;
}
private void addAbstractFeature(Features features, Class type) {
private void addAbstractFeature(Features features, Class<?> type) {
String featureName = type.getSimpleName();
try {
Object bean = context.getBean(type);
Object bean = this.context.getBean(type);
Class<?> beanClass = bean.getClass();
addFeature(features, new NamedFeature(featureName, beanClass));
} catch (NoSuchBeanDefinitionException e) {
}
catch (NoSuchBeanDefinitionException e) {
features.getDisabled().add(featureName);
}
}
private void addFeature(Features features, NamedFeature feature) {
Class<?> type = feature.getType();
features.getEnabled().add(new Feature(feature.getName(),
type.getCanonicalName(),
type.getPackage().getImplementationVersion(),
type.getPackage().getImplementationVendor()));
Class<?> type = feature.getType();
features.getEnabled()
.add(new Feature(feature.getName(), type.getCanonicalName(),
type.getPackage().getImplementationVersion(),
type.getPackage().getImplementationVendor()));
}
@Value

33
spring-cloud-commons/src/main/java/org/springframework/cloud/client/actuator/HasFeatures.java

@ -1,31 +1,26 @@ @@ -1,31 +1,26 @@
package org.springframework.cloud.client.actuator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import lombok.Builder;
import lombok.Singular;
import lombok.Value;
/**
* @author Spencer Gibb
*/
@Value
@Builder
public class HasFeatures {
@Singular
private final List<Class> abstractFeatures;
@Singular
private final List<NamedFeature> namedFeatures;
public static HasFeatures abstractFeatures(Class... abstractFeatures) {
private final List<Class<?>> abstractFeatures = new ArrayList<>();
private final List<NamedFeature> namedFeatures = new ArrayList<>();
public static HasFeatures abstractFeatures(Class<?>... abstractFeatures) {
return new HasFeatures(Arrays.asList(abstractFeatures),
Collections.<NamedFeature> emptyList());
}
public static HasFeatures namedFeatures(NamedFeature... namedFeatures) {
return new HasFeatures(Collections.<Class> emptyList(),
return new HasFeatures(Collections.<Class<?>> emptyList(),
Arrays.asList(namedFeatures));
}
@ -38,4 +33,18 @@ public class HasFeatures { @@ -38,4 +33,18 @@ public class HasFeatures {
return namedFeatures(new NamedFeature(name1, type1),
new NamedFeature(name2, type2));
}
public HasFeatures(List<Class<?>> abstractFeatures,
List<NamedFeature> namedFeatures) {
this.abstractFeatures.addAll(abstractFeatures);
this.namedFeatures.addAll(namedFeatures);
}
public List<Class<?>> getAbstractFeatures() {
return this.abstractFeatures;
}
public List<NamedFeature> getNamedFeatures() {
return this.namedFeatures;
}
}

42
spring-cloud-commons/src/test/java/org/springframework/cloud/client/actuator/FeaturesEndpointTests.java

@ -1,10 +1,7 @@ @@ -1,10 +1,7 @@
package org.springframework.cloud.client.actuator;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Before;
@ -16,8 +13,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext @@ -16,8 +13,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* @author Spencer Gibb
@ -29,7 +28,8 @@ public class FeaturesEndpointTests { @@ -29,7 +28,8 @@ public class FeaturesEndpointTests {
@Before
public void setup() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(JacksonAutoConfiguration.class, FeaturesConfig.class, Config.class);
this.context.register(JacksonAutoConfiguration.class, FeaturesConfig.class,
Config.class);
this.context.refresh();
}
@ -42,7 +42,8 @@ public class FeaturesEndpointTests { @@ -42,7 +42,8 @@ public class FeaturesEndpointTests {
@Test
public void invokeWorks() {
FeaturesEndpoint.Features features = this.context.getBean(FeaturesEndpoint.class).invoke();
FeaturesEndpoint.Features features = this.context.getBean(FeaturesEndpoint.class)
.invoke();
assertThat(features, is(notNullValue()));
assertThat(features.getEnabled().size(), is(equalTo(2)));
assertThat(features.getDisabled().size(), is(equalTo(1)));
@ -57,11 +58,11 @@ public class FeaturesEndpointTests { @@ -57,11 +58,11 @@ public class FeaturesEndpointTests {
@Bean
HasFeatures localFeatures() {
return HasFeatures.builder()
.abstractFeature(Foo.class)
.namedFeature(new NamedFeature("Bar Feature", Bar.class))
.abstractFeature(Baz.class)
.build();
HasFeatures features = HasFeatures.namedFeatures(
new NamedFeature("foo", Foo.class),
new NamedFeature("Bar Feature", Bar.class));
features.getAbstractFeatures().add(Bar.class);
return features;
}
}
@ -74,11 +75,16 @@ public class FeaturesEndpointTests { @@ -74,11 +75,16 @@ public class FeaturesEndpointTests {
@Bean
public FeaturesEndpoint cloudEndpoint() {
return new FeaturesEndpoint(hasFeatures);
return new FeaturesEndpoint(this.hasFeatures);
}
}
public static class Foo {}
public static class Bar {}
public static class Baz {}
public static class Foo {
}
public static class Bar {
}
public static class Baz {
}
}

Loading…
Cancel
Save