[ 
https://issues.apache.org/jira/browse/BEAM-4453?focusedWorklogId=120047&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-120047
 ]

ASF GitHub Bot logged work on BEAM-4453:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 06/Jul/18 21:19
            Start Date: 06/Jul/18 21:19
    Worklog Time Spent: 10m 
      Work Description: apilloud commented on a change in pull request #5873: 
[BEAM-4453] Add schema support for Java POJOs and Java Beans
URL: https://github.com/apache/beam/pull/5873#discussion_r200739701
 
 

 ##########
 File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/JavaBeanUtils.java
 ##########
 @@ -0,0 +1,335 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.beam.sdk.schemas.utils;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import net.bytebuddy.ByteBuddy;
+import net.bytebuddy.description.method.MethodDescription.ForLoadedMethod;
+import net.bytebuddy.dynamic.DynamicType;
+import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
+import net.bytebuddy.dynamic.scaffold.InstrumentedType;
+import net.bytebuddy.implementation.FixedValue;
+import net.bytebuddy.implementation.Implementation;
+import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
+import net.bytebuddy.implementation.bytecode.ByteCodeAppender.Size;
+import net.bytebuddy.implementation.bytecode.StackManipulation;
+import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
+import net.bytebuddy.implementation.bytecode.member.MethodReturn;
+import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
+import net.bytebuddy.matcher.ElementMatchers;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.ConvertType;
+import org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.ConvertValueForGetter;
+import org.apache.beam.sdk.schemas.utils.ReflectUtils.ClassWithSchema;
+import org.apache.beam.sdk.schemas.utils.StaticSchemaInference.MethodType;
+import org.apache.beam.sdk.schemas.utils.StaticSchemaInference.TypeInformation;
+import org.apache.beam.sdk.values.reflect.FieldValueGetter;
+import org.apache.beam.sdk.values.reflect.FieldValueSetter;
+
+/** A set of utilities yo generate getter and setter classes for JavaBean 
objects. */
+@Experimental(Kind.SCHEMAS)
+public class JavaBeanUtils {
+  /** Create a {@link Schema} for a Java Bean class. */
+  public static Schema schemaFromJavaBeanClass(Class<?> clazz) {
+    return StaticSchemaInference.schemaFromClass(clazz, 
JavaBeanUtils::typeInformationFromClass);
+  }
+
+  private static List<TypeInformation> typeInformationFromClass(Class<?> 
clazz) {
+    try {
+      List<TypeInformation> getterTypes =
+          ReflectUtils.getMethods(clazz)
+              .stream()
+              .filter(ReflectUtils::isGetter)
+              .map(m -> new TypeInformation(m, MethodType.GETTER))
+              .collect(Collectors.toList());
+
+      Map<String, TypeInformation> setterTypes =
+          ReflectUtils.getMethods(clazz)
+              .stream()
+              .filter(ReflectUtils::isSetter)
+              .map(m -> new TypeInformation(m, MethodType.SETTER))
+              .collect(Collectors.toMap(TypeInformation::getName, 
Function.identity()));
+      validateJavaBean(getterTypes, setterTypes);
+      return getterTypes;
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  // Make sure that there are matching setters and getters.
+  private static void validateJavaBean(
+      List<TypeInformation> getters, Map<String, TypeInformation> setters) {
+    for (TypeInformation type : getters) {
+      TypeInformation setterType = setters.get(type.getName());
+      if (setterType == null) {
+        throw new RuntimeException(
+            "JavaBean contained a setter for field "
+                + type.getName()
+                + "but did not contain a matching getter.");
+      }
+      if (!type.equals(setterType)) {
+        throw new RuntimeException(
+            "JavaBean contained mismatching setter for field" + 
type.getName());
+      }
+    }
+  }
+
+  // Static ByteBuddy instance used by all helpers.
+  private static final ByteBuddy BYTE_BUDDY = new ByteBuddy();
+
+  // The list of getters for a class is cached, so we only create the classes 
the first time
+  // getSetters is called.
+  private static final Map<ClassWithSchema, List<FieldValueGetter>> 
CACHED_GETTERS =
+      Maps.newConcurrentMap();
+
+  /**
+   * Return the list of {@link FieldValueGetter}s for a Java Bean class
+   *
+   * <p>The returned list is ordered by the order of fields in the schema.
+   */
+  public static List<FieldValueGetter> getGetters(Class<?> clazz, Schema 
schema) {
+    return CACHED_GETTERS.computeIfAbsent(
+        new ClassWithSchema(clazz, schema),
+        c -> {
+          try {
+            Map<String, FieldValueGetter> getterMap =
+                ReflectUtils.getMethods(clazz)
+                    .stream()
+                    .filter(ReflectUtils::isGetter)
+                    .map(JavaBeanUtils::createGetter)
+                    .collect(Collectors.toMap(FieldValueGetter::name, 
Function.identity()));
+            List<FieldValueGetter> getters = Lists.newArrayList();
+            for (int i = 0; i < schema.getFieldCount(); ++i) {
+              getters.add(getterMap.get(schema.getField(i).getName()));
+            }
+            System.out.println("Returning for Schema " + schema + " getters " 
+ getters);
 
 Review comment:
   Remove printf

----------------------------------------------------------------
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:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 120047)
    Time Spent: 1h 40m  (was: 1.5h)

> Provide automatic schema registration for POJOs
> -----------------------------------------------
>
>                 Key: BEAM-4453
>                 URL: https://issues.apache.org/jira/browse/BEAM-4453
>             Project: Beam
>          Issue Type: Sub-task
>          Components: sdk-java-core
>            Reporter: Reuven Lax
>            Assignee: Reuven Lax
>            Priority: Major
>          Time Spent: 1h 40m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to