-
Notifications
You must be signed in to change notification settings - Fork 54
feat: add support for mapping transient fields with entity class hierarchy introspection #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,30 @@ | ||
package com.introproventures.graphql.jpa.query.schema.impl; | ||
|
||
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnore; | ||
|
||
import java.beans.BeanInfo; | ||
import java.beans.IntrospectionException; | ||
import java.beans.Introspector; | ||
import java.beans.PropertyDescriptor; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import javax.persistence.Transient; | ||
|
||
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnore; | ||
|
||
public class IntrospectionUtils { | ||
private static final Map<Class<?>, CachedIntrospectionResult> map = new LinkedHashMap<>(); | ||
|
||
|
@@ -24,25 +33,38 @@ public static CachedIntrospectionResult introspect(Class<?> entity) { | |
} | ||
|
||
public static boolean isTransient(Class<?> entity, String propertyName) { | ||
return isAnnotationPresent(entity, propertyName, Transient.class); | ||
if(!introspect(entity).hasPropertyDescriptor(propertyName)) { | ||
throw new RuntimeException(new NoSuchFieldException(propertyName)); | ||
} | ||
|
||
return Stream.of(isAnnotationPresent(entity, propertyName, Transient.class), | ||
isModifierPresent(entity, propertyName, Modifier::isTransient)) | ||
.anyMatch(it -> it.isPresent() && it.get() == true); | ||
} | ||
|
||
public static boolean isIgnored(Class<?> entity, String propertyName) { | ||
return isAnnotationPresent(entity, propertyName, GraphQLIgnore.class); | ||
return isAnnotationPresent(entity, propertyName, GraphQLIgnore.class) | ||
.orElseThrow(() -> new RuntimeException(new NoSuchFieldException(propertyName))); | ||
} | ||
|
||
private static boolean isAnnotationPresent(Class<?> entity, String propertyName, Class<? extends Annotation> annotation){ | ||
private static Optional<Boolean> isAnnotationPresent(Class<?> entity, String propertyName, Class<? extends Annotation> annotation){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning |
||
return introspect(entity).getPropertyDescriptor(propertyName) | ||
.map(it -> it.isAnnotationPresent(annotation)) | ||
.orElse(false); | ||
.map(it -> it.isAnnotationPresent(annotation)); | ||
} | ||
|
||
private static Optional<Boolean> isModifierPresent(Class<?> entity, String propertyName, Function<Integer, Boolean> function){ | ||
return introspect(entity).getField(propertyName) | ||
.map(it -> function.apply(it.getModifiers())); | ||
} | ||
|
||
public static class CachedIntrospectionResult { | ||
|
||
private final Map<String, CachedPropertyDescriptor> map; | ||
private final Class<?> entity; | ||
private final BeanInfo beanInfo; | ||
|
||
private final Map<String, Field> fields; | ||
|
||
@SuppressWarnings("rawtypes") | ||
public CachedIntrospectionResult(Class<?> entity) { | ||
try { | ||
this.beanInfo = Introspector.getBeanInfo(entity); | ||
|
@@ -54,6 +76,11 @@ public CachedIntrospectionResult(Class<?> entity) { | |
this.map = Stream.of(beanInfo.getPropertyDescriptors()) | ||
.map(CachedPropertyDescriptor::new) | ||
.collect(Collectors.toMap(CachedPropertyDescriptor::getName, it -> it)); | ||
|
||
this.fields = iterate((Class) entity, k -> Optional.ofNullable(k.getSuperclass())) | ||
.flatMap(k -> Arrays.stream(k.getDeclaredFields())) | ||
.filter(f -> map.containsKey(f.getName())) | ||
.collect(Collectors.toMap(Field::getName, it -> it)); | ||
} | ||
|
||
public Collection<CachedPropertyDescriptor> getPropertyDescriptors() { | ||
|
@@ -64,6 +91,14 @@ public Optional<CachedPropertyDescriptor> getPropertyDescriptor(String fieldName | |
return Optional.ofNullable(map.getOrDefault(fieldName, null)); | ||
} | ||
|
||
public boolean hasPropertyDescriptor(String fieldName) { | ||
return map.containsKey(fieldName); | ||
} | ||
|
||
public Optional<Field> getField(String fieldName) { | ||
return Optional.ofNullable(fields.get(fieldName)); | ||
} | ||
|
||
public Class<?> getEntity() { | ||
return entity; | ||
} | ||
|
@@ -83,6 +118,10 @@ public PropertyDescriptor getDelegate() { | |
return delegate; | ||
} | ||
|
||
public Class<?> getPropertyType() { | ||
return delegate.getPropertyType(); | ||
} | ||
|
||
public String getName() { | ||
return delegate.getName(); | ||
} | ||
|
@@ -92,11 +131,9 @@ public boolean isAnnotationPresent(Class<? extends Annotation> annotation) { | |
} | ||
|
||
private boolean isAnnotationPresentOnField(Class<? extends Annotation> annotation) { | ||
try { | ||
return entity.getDeclaredField(delegate.getName()).isAnnotationPresent(annotation); | ||
} catch (NoSuchFieldException e) { | ||
return false; | ||
} | ||
return Optional.ofNullable(fields.get(delegate.getName())) | ||
.map(f -> f.isAnnotationPresent(annotation)) | ||
.orElse(false); | ||
} | ||
|
||
private boolean isAnnotationPresentOnReadMethod(Class<? extends Annotation> annotation) { | ||
|
@@ -105,4 +142,38 @@ private boolean isAnnotationPresentOnReadMethod(Class<? extends Annotation> anno | |
|
||
} | ||
} | ||
|
||
/** | ||
* The following method is borrowed from Streams.iterate, | ||
* however Streams.iterate is designed to create infinite streams. | ||
* | ||
* This version has been modified to end when Optional.empty() | ||
* is returned from the fetchNextFunction. | ||
*/ | ||
protected static <T> Stream<T> iterate( T seed, Function<T, Optional<T>> fetchNextFunction ) { | ||
Objects.requireNonNull(fetchNextFunction); | ||
|
||
Iterator<T> iterator = new Iterator<T>() { | ||
private Optional<T> t = Optional.ofNullable(seed); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return t.isPresent(); | ||
} | ||
|
||
@Override | ||
public T next() { | ||
T v = t.get(); | ||
|
||
t = fetchNextFunction.apply(v); | ||
|
||
return v; | ||
} | ||
}; | ||
|
||
return StreamSupport.stream( | ||
Spliterators.spliteratorUnknownSize( iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE), | ||
false | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
orElse(false)
was added before to make sure that the application can start even if the field is not found. There may be a case that we have a getter that is not directly connected with any of the class' properties but is still mapped to the column in the database e.g.But you are right that we should probably throw this error anyway and try to deal with such failure on the higher level. What do you think?