-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
140 additions
and
18 deletions.
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
76 changes: 76 additions & 0 deletions
76
jr-objects/src/main/java/tools/jackson/jr/ob/impl/RecordsHelpers.java
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package tools.jackson.jr.ob.impl; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
|
||
import tools.jackson.jr.ob.impl.POJODefinition.PropBuilder; | ||
|
||
/** | ||
* Helper class to get Java Record metadata, from Java 8 (not using | ||
* JDK 17 methods) | ||
* | ||
* @since 2.18 | ||
*/ | ||
public final class RecordsHelpers { | ||
private static boolean supportsRecords; | ||
|
||
private static Method getRecordComponentsMethod; | ||
private static Method getTypeMethod; | ||
|
||
static { | ||
Method getRecordComponentsMethod; | ||
Method getTypeMethod; | ||
|
||
try { | ||
getRecordComponentsMethod = Class.class.getMethod("getRecordComponents"); | ||
Class<?> recordComponentClass = Class.forName("java.lang.reflect.RecordComponent"); | ||
getTypeMethod = recordComponentClass.getMethod("getType"); | ||
supportsRecords = true; | ||
} catch (Throwable t) { | ||
getRecordComponentsMethod = null; | ||
getTypeMethod = null; | ||
supportsRecords = false; | ||
} | ||
|
||
RecordsHelpers.getRecordComponentsMethod = getRecordComponentsMethod; | ||
RecordsHelpers.getTypeMethod = getTypeMethod; | ||
} | ||
private RecordsHelpers() {} | ||
|
||
static boolean isRecordConstructor(Class<?> beanClass, Constructor<?> ctor, Map<String, PropBuilder> propsByName) { | ||
if (!supportsRecords || !isRecordType(beanClass)) { | ||
return false; | ||
} | ||
|
||
Class<?>[] parameterTypes = ctor.getParameterTypes(); | ||
if (parameterTypes.length != propsByName.size()) { | ||
return false; | ||
} | ||
|
||
try { | ||
Object[] recordComponents = (Object[]) getRecordComponentsMethod.invoke(beanClass); | ||
Class<?>[] componentTypes = new Class<?>[recordComponents.length]; | ||
for (int i = 0; i < recordComponents.length; i++) { | ||
Object recordComponent = recordComponents[i]; | ||
Class<?> type = (Class<?>) getTypeMethod.invoke(recordComponent); | ||
componentTypes[i] = type; | ||
} | ||
|
||
for (int i = 0; i < parameterTypes.length; i++) { | ||
if (parameterTypes[i] != componentTypes[i]) { | ||
return false; | ||
} | ||
} | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
static boolean isRecordType(Class<?> cls) { | ||
Class<?> parent = cls.getSuperclass(); | ||
return (parent != null) && "java.lang.Record".equals(parent.getName()); | ||
} | ||
} |
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