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
Record like property access support #2994
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7d7d072
Record like naming
bbakerman 91f703e
Record like naming - tweaks
bbakerman 0cafaae
Record like naming - javadoc tweaks
bbakerman 5c64285
Merge remote-tracking branch 'origin/master' into record_support
bbakerman ebc445e
Updated record like code including the Lambda mechanism
bbakerman 759b1fa
Added more tests and a flag to turn Lambda off
bbakerman 2921e63
JavaDoc update
bbakerman c3a69e2
More work on record like access
bbakerman File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
|
@@ -42,15 +43,32 @@ public static Optional<Function<Object, Object>> createGetter(Class<?> sourceCla | |
Function<Object, Object> getterFunction = mkCallFunction(sourceClass, candidateMethod.getName(), candidateMethod.getReturnType()); | ||
return Optional.of(getterFunction); | ||
} catch (Throwable ignore) { | ||
// | ||
// if we cant make a dynamic lambda here, then we give up and let the old property fetching code do its thing | ||
// this can happen on runtimes such as GraalVM native where LambdaMetafactory is not supported | ||
// and will throw something like : | ||
// | ||
// com.oracle.svm.core.jdk.UnsupportedFeatureError: Defining hidden classes at runtime is not supported. | ||
// at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.VMError.unsupportedFeature(VMError.java:89) | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
|
||
private static Method getCandidateMethod(Class<?> sourceClass, String propertyName) { | ||
List<Method> allGetterMethods = findGetterMethodsForProperty(sourceClass, propertyName); | ||
// property() methods first | ||
Predicate<Method> recordLikePredicate = method -> isRecordLike(method) && propertyName.equals(decapitalize(method.getName())); | ||
List<Method> recordLikeMethods = findMethodsForProperty(sourceClass, | ||
recordLikePredicate); | ||
if (!recordLikeMethods.isEmpty()) { | ||
return recordLikeMethods.get(0); | ||
} | ||
|
||
// getProperty() POJO methods next | ||
Predicate<Method> getterPredicate = method -> isGetterNamed(method) && propertyName.equals(mkPropertyNameGetter(method)); | ||
List<Method> allGetterMethods = findMethodsForProperty(sourceClass, | ||
getterPredicate); | ||
List<Method> pojoGetterMethods = allGetterMethods.stream() | ||
.filter(LambdaFetchingSupport::isPossiblePojoMethod) | ||
.collect(toList()); | ||
|
@@ -60,10 +78,8 @@ private static Method getCandidateMethod(Class<?> sourceClass, String propertyNa | |
method = findBestBooleanGetter(pojoGetterMethods); | ||
} | ||
return checkForSingleParameterPeer(method, allGetterMethods); | ||
} else { | ||
return null; | ||
} | ||
|
||
return null; | ||
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. Support in the lambda code to find |
||
} | ||
|
||
private static Method checkForSingleParameterPeer(Method candidateMethod, List<Method> allMethods) { | ||
|
@@ -88,21 +104,18 @@ private static Method findBestBooleanGetter(List<Method> methods) { | |
/** | ||
* Finds all methods in a class hierarchy that match the property name - they might not be suitable but they | ||
* | ||
* @param sourceClass the class we are looking to work on | ||
* @param propertyName the name of the property | ||
* @param sourceClass the class we are looking to work on | ||
* | ||
* @return a list of getter methods for that property | ||
*/ | ||
private static List<Method> findGetterMethodsForProperty(Class<?> sourceClass, String propertyName) { | ||
private static List<Method> findMethodsForProperty(Class<?> sourceClass, Predicate<Method> predicate) { | ||
List<Method> methods = new ArrayList<>(); | ||
Class<?> currentClass = sourceClass; | ||
while (currentClass != null) { | ||
Method[] declaredMethods = currentClass.getDeclaredMethods(); | ||
for (Method declaredMethod : declaredMethods) { | ||
if (isGetterNamed(declaredMethod)) { | ||
if (nameMatches(propertyName, declaredMethod)) { | ||
methods.add(declaredMethod); | ||
} | ||
if (predicate.test(declaredMethod)) { | ||
methods.add(declaredMethod); | ||
} | ||
} | ||
currentClass = currentClass.getSuperclass(); | ||
|
@@ -113,12 +126,6 @@ private static List<Method> findGetterMethodsForProperty(Class<?> sourceClass, S | |
.collect(toList()); | ||
} | ||
|
||
|
||
private static boolean nameMatches(String propertyName, Method declaredMethod) { | ||
String methodPropName = mkPropertyName(declaredMethod); | ||
return propertyName.equals(methodPropName); | ||
} | ||
|
||
private static boolean isPossiblePojoMethod(Method method) { | ||
return !isObjectMethod(method) && | ||
returnsSomething(method) && | ||
|
@@ -127,6 +134,13 @@ private static boolean isPossiblePojoMethod(Method method) { | |
isPublic(method); | ||
} | ||
|
||
private static boolean isRecordLike(Method method) { | ||
return !isObjectMethod(method) && | ||
returnsSomething(method) && | ||
hasNoParameters(method) && | ||
isPublic(method); | ||
} | ||
|
||
private static boolean isBooleanGetter(Method method) { | ||
Class<?> returnType = method.getReturnType(); | ||
return isGetterNamed(method) && (returnType.equals(Boolean.class) || returnType.equals(Boolean.TYPE)); | ||
|
@@ -153,7 +167,7 @@ private static boolean isObjectMethod(Method method) { | |
return method.getDeclaringClass().equals(Object.class); | ||
} | ||
|
||
private static String mkPropertyName(Method method) { | ||
private static String mkPropertyNameGetter(Method method) { | ||
// | ||
// getFooName becomes fooName | ||
// isFoo becomes foo | ||
|
This file contains 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.
I unwound the nesting of the exceptions - there was no need for it and I think this is clearer code