[
https://issues.apache.org/jira/browse/BEAM-6240?focusedWorklogId=181874&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-181874
]
ASF GitHub Bot logged work on BEAM-6240:
----------------------------------------
Author: ASF GitHub Bot
Created on: 07/Jan/19 16:06
Start Date: 07/Jan/19 16:06
Worklog Time Spent: 10m
Work Description: kanterov commented on pull request #7289: [BEAM-6240]
Add a library of schema annotations for POJO and JavaBeans
URL: https://github.com/apache/beam/pull/7289#discussion_r245690816
##########
File path:
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/JavaBeanUtils.java
##########
@@ -218,6 +210,84 @@ private static FieldValueSetter
createSetter(FieldValueTypeInformation typeInfor
.intercept(new InvokeSetterInstruction(method));
}
+ // The list of constructors for a class is cached, so we only create the
classes the first time
+ // getConstructor is called.
+ public static final Map<ClassWithSchema, SchemaUserTypeCreator>
CACHED_CREATORS =
+ Maps.newConcurrentMap();
+
+ public static SchemaUserTypeCreator getConstructorCreator(
+ Class clazz,
+ Constructor constructor,
+ Schema schema,
+ FieldValueTypeSupplier fieldValueTypeSupplier) {
+ return CACHED_CREATORS.computeIfAbsent(
+ new ClassWithSchema(clazz, schema),
+ c -> {
+ List<FieldValueTypeInformation> types =
fieldValueTypeSupplier.get(clazz, schema);
+ return createConstructorCreator(clazz, constructor, schema, types);
+ });
+ }
+
+ public static <T> SchemaUserTypeCreator createConstructorCreator(
+ Class<T> clazz,
+ Constructor<T> constructor,
+ Schema schema,
+ List<FieldValueTypeInformation> types) {
+ try {
+ DynamicType.Builder<SchemaUserTypeCreator> builder =
+ BYTE_BUDDY
+ .subclass(SchemaUserTypeCreator.class)
+ .method(ElementMatchers.named("create"))
+ .intercept(new ConstructorCreateInstruction(types, clazz,
constructor));
+ return builder
+ .make()
+ .load(ReflectHelpers.findClassLoader(),
ClassLoadingStrategy.Default.INJECTION)
+ .getLoaded()
+ .getDeclaredConstructor()
+ .newInstance();
+ } catch (InstantiationException
+ | IllegalAccessException
+ | NoSuchMethodException
+ | InvocationTargetException e) {
+ throw new RuntimeException(
+ "Unable to generate a creator for class " + clazz + " with schema "
+ schema);
+ }
+ }
+
+ public static SchemaUserTypeCreator getStaticCreator(
+ Class clazz, Method creator, Schema schema, FieldValueTypeSupplier
fieldValueTypeSupplier) {
+ return CACHED_CREATORS.computeIfAbsent(
+ new ClassWithSchema(clazz, schema),
+ c -> {
+ List<FieldValueTypeInformation> types =
fieldValueTypeSupplier.get(clazz, schema);
+ return createStaticCreator(clazz, creator, schema, types);
+ });
+ }
+
+ public static <T> SchemaUserTypeCreator createStaticCreator(
+ Class<T> clazz, Method creator, Schema schema,
List<FieldValueTypeInformation> types) {
+ try {
+ DynamicType.Builder<SchemaUserTypeCreator> builder =
+ BYTE_BUDDY
+ .subclass(SchemaUserTypeCreator.class)
+ .method(ElementMatchers.named("create"))
+ .intercept(new StaticFactoryMethodInstruction(types, clazz,
creator));
+
+ return builder
+ .make()
+ .load(ReflectHelpers.findClassLoader(),
ClassLoadingStrategy.Default.INJECTION)
+ .getLoaded()
+ .getDeclaredConstructor()
+ .newInstance();
+ } catch (InstantiationException
+ | IllegalAccessException
+ | NoSuchMethodException
+ | InvocationTargetException e) {
+ throw new RuntimeException(
+ "Unable to generate a creator for class " + clazz + " with schema "
+ schema);
Review comment:
nit: should be `clazz.getName()` or remove "class" from string
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 181874)
Time Spent: 3h (was: 2h 50m)
> Allow users to annotate POJOs and JavaBeans for richer functionality
> --------------------------------------------------------------------
>
> Key: BEAM-6240
> URL: https://issues.apache.org/jira/browse/BEAM-6240
> Project: Beam
> Issue Type: Sub-task
> Components: sdk-java-core
> Reporter: Reuven Lax
> Assignee: Reuven Lax
> Priority: Major
> Time Spent: 3h
> Remaining Estimate: 0h
>
> Desired annotations:
> * SchemaIgnore - ignore this field
> * FieldName - allow the user to explicitly specify a field name
> * SchemaCreate - register a function to be used to create an object (so
> fields can be final, and no default constructor need be assumed).
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)