#831 - Polishing.

This commit is contained in:
Greg Turnquist
2019-03-11 21:44:34 -05:00
parent 520fdc8d91
commit 122391e60d
8 changed files with 48 additions and 72 deletions

View File

@@ -15,7 +15,9 @@
*/
package org.springframework.hateoas;
import lombok.AccessLevel;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import java.io.Serializable;
@@ -34,6 +36,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
* @author Oliver Drotbohm
*/
@Value
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
class StringLinkRelation implements LinkRelation, Serializable {
private static final long serialVersionUID = -3904935345545567957L;
@@ -41,13 +44,6 @@ class StringLinkRelation implements LinkRelation, Serializable {
@NonNull String relation;
private StringLinkRelation(String relation) {
Assert.notNull(relation, "relation must not be null!");
this.relation = relation;
}
/**
* Returns a (potentially cached) {@link LinkRelation} for the given value.
*

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.hateoas.client;
import net.minidev.json.JSONArray;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
@@ -27,6 +25,7 @@ import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.minidev.json.JSONArray;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkRelation;
import org.springframework.hateoas.Links;
@@ -166,12 +165,12 @@ public class JsonPathLinkDiscoverer implements LinkDiscoverer {
JSONArray jsonArray = (JSONArray) parseResult;
return jsonArray.stream() //
.flatMap(it -> it instanceof JSONArray ? ((JSONArray) it).stream() : Stream.of(it)) //
.flatMap(it -> JSONArray.class.isInstance(it) ? ((JSONArray) it).stream() : Stream.of(it)) //
.map(it -> extractLink(it, rel)) //
.collect(Collectors.collectingAndThen(Collectors.toList(), Links::of));
}
return Links.of(parseResult instanceof Map //
return Links.of(Map.class.isInstance(parseResult) //
? extractLink(parseResult, rel) //
: new Link(parseResult.toString(), rel));
}

View File

@@ -110,7 +110,7 @@ class WebMvcHateoasConfiguration {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (!(RestTemplate.class.isInstance(bean))) {
if (!RestTemplate.class.isInstance(bean)) {
return bean;
}

View File

@@ -66,20 +66,20 @@ public class PropertyUtils {
return findProperties(((EntityModel<?>) object).getContent());
}
return getPropertyDescriptors(object.getClass())
.collect(HashMap::new,
(hashMap, descriptor) -> {
try {
Method readMethod = descriptor.getReadMethod();
ReflectionUtils.makeAccessible(readMethod);
hashMap.put(descriptor.getName(), readMethod.invoke(object));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
},
HashMap::putAll);
return getPropertyDescriptors(object.getClass()) //
.collect(HashMap::new, //
(hashMap, descriptor) -> {
try {
Method readMethod = descriptor.getReadMethod();
ReflectionUtils.makeAccessible(readMethod);
hashMap.put(descriptor.getName(), readMethod.invoke(object));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}, //
HashMap::putAll);
}
public static List<String> findPropertyNames(ResolvableType resolvableType) {
if (WebStack.WEBFLUX.isAvailable()) {
@@ -92,14 +92,14 @@ public class PropertyUtils {
if (resolvableType.getRawClass() == null) {
return Collections.emptyList();
}
if (resolvableType.getRawClass().equals(EntityModel.class)) {
Class<?> genericEntityModelParameter = resolvableType.resolveGeneric(0);
if (genericEntityModelParameter == null) {
return Collections.emptyList();
}
return findPropertyNames(genericEntityModelParameter);
} else {
return findPropertyNames(resolvableType.getRawClass());
@@ -108,13 +108,13 @@ public class PropertyUtils {
public static List<String> findPropertyNames(Class<?> clazz) {
return getPropertyDescriptors(clazz)
.map(FeatureDescriptor::getName)
.collect(Collectors.toList());
return getPropertyDescriptors(clazz) //
.map(FeatureDescriptor::getName) //
.collect(Collectors.toList());
}
public static <T> T createObjectFromProperties(Class<T> clazz, Map<String, Object> properties) {
T obj = BeanUtils.instantiateClass(clazz);
properties.forEach((key, value) -> {
@@ -142,10 +142,10 @@ public class PropertyUtils {
private static Stream<PropertyDescriptor> getPropertyDescriptors(Class<?> clazz) {
return Arrays.stream(BeanUtils.getPropertyDescriptors(clazz))
.filter(descriptor -> !FIELDS_TO_IGNORE.contains(descriptor.getName()))
.filter(descriptor -> !descriptorToBeIgnoredByJackson(clazz, descriptor))
.filter(descriptor -> !toBeIgnoredByJackson(clazz, descriptor.getName()))
.filter(descriptor -> !readerIsNotToBeIgnoredByJackson(descriptor));
.filter(descriptor -> !FIELDS_TO_IGNORE.contains(descriptor.getName()))
.filter(descriptor -> !descriptorToBeIgnoredByJackson(clazz, descriptor))
.filter(descriptor -> !toBeIgnoredByJackson(clazz, descriptor.getName()))
.filter(descriptor -> !readerIsNotToBeIgnoredByJackson(descriptor));
}
/**
@@ -184,15 +184,15 @@ public class PropertyUtils {
*/
private static boolean toBeIgnoredByJackson(@Nullable Annotation[] annotations) {
if (annotations != null) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().equals(JsonIgnore.class)) {
return (Boolean) AnnotationUtils.getAnnotationAttributes(annotation).get("value");
}
}
if (annotations == null) {
return false;
}
return false;
return Arrays.stream(annotations) //
.filter(annotation -> annotation.annotationType().equals(JsonIgnore.class)) //
.findFirst() //
.map(annotation -> (Boolean) AnnotationUtils.getAnnotationAttributes(annotation).get("value")) //
.orElse(false);
}
/**
@@ -206,20 +206,15 @@ public class PropertyUtils {
Annotation[] annotations = AnnotationUtils.getAnnotations(clazz);
if (annotations != null) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().equals(JsonIgnoreProperties.class)) {
String[] namesOfPropertiesToIgnore = (String[]) AnnotationUtils.getAnnotationAttributes(annotation).get("value");
for (String propertyToIgnore : namesOfPropertiesToIgnore) {
if (propertyToIgnore.equalsIgnoreCase(field)) {
return true;
}
}
}
}
if (annotations == null) {
return false;
}
return false;
return Arrays.stream(annotations) //
.filter(annotation -> annotation.annotationType().equals(JsonIgnoreProperties.class)) //
.map(annotation -> (String[]) AnnotationUtils.getAnnotationAttributes(annotation).get("value")) //
.flatMap(Arrays::stream) //
.anyMatch(propertyName -> propertyName.equalsIgnoreCase(field));
}
}

View File

@@ -798,7 +798,7 @@ class Jackson2CollectionJsonModule extends SimpleModule {
return resources.getContent().stream().map(content -> {
if (!(EntityModel.class.isInstance(content))) {
if (!EntityModel.class.isInstance(content)) {
return new CollectionJsonItem<>().withRawData(content);
}

View File

@@ -51,7 +51,7 @@ public class HalLinkDiscoverer extends JsonPathLinkDiscoverer {
@SuppressWarnings("unchecked")
protected Link extractLink(Object element, LinkRelation rel) {
if (!(Map.class.isInstance(element))) {
if (!Map.class.isInstance(element)) {
return super.extractLink(element, rel);
}

View File

@@ -431,7 +431,7 @@ public class Jackson2HalModule extends SimpleModule {
Object firstElement = list.get(0);
if (!(HalLink.class.isInstance(firstElement))) {
if (!HalLink.class.isInstance(firstElement)) {
serializeContents(list, jgen, provider);
return;
}