chaokunyang commented on code in PR #1701:
URL: https://github.com/apache/fury/pull/1701#discussion_r1682692665


##########
java/fury-core/src/main/java/org/apache/fury/serializer/AbstractObjectSerializer.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.fury.serializer;
+
+import java.lang.invoke.MethodHandle;
+import java.util.List;
+import org.apache.fury.Fury;
+import org.apache.fury.memory.Platform;
+import org.apache.fury.reflect.FieldAccessor;
+import org.apache.fury.reflect.ReflectionUtils;
+import org.apache.fury.resolver.ClassResolver;
+import org.apache.fury.resolver.FieldResolver.FieldInfo;
+import org.apache.fury.resolver.RefResolver;
+import org.apache.fury.util.record.RecordUtils;
+
+public abstract class AbstractObjectSerializer<T> extends Serializer<T> {
+
+  protected final RefResolver refResolver;
+  protected final ClassResolver classResolver;
+  protected final boolean isRecord;
+  protected final MethodHandle constructor;
+
+  public AbstractObjectSerializer(Fury fury, Class<T> type) {
+    super(fury, type);
+    this.refResolver = fury.getRefResolver();
+    this.classResolver = fury.getClassResolver();
+    this.isRecord = RecordUtils.isRecord(type);
+    if (isRecord) {
+      this.constructor = RecordUtils.getRecordConstructor(type).f1;
+    } else {
+      this.constructor = ReflectionUtils.getCtrHandle(type, false);
+    }
+  }
+
+  public AbstractObjectSerializer(Fury fury, Class<T> type, MethodHandle 
constructor) {
+    super(fury, type);
+    this.refResolver = fury.getRefResolver();
+    this.classResolver = fury.getClassResolver();
+    this.isRecord = RecordUtils.isRecord(type);
+    this.constructor = constructor;
+  }
+
+  @Override
+  public T copy(T originObj) {
+    if (immutable) {
+      return originObj;
+    }
+    if (isRecord) {
+      Object[] fieldValues = copyFields(originObj);
+      try {
+        return (T) constructor.invokeWithArguments(fieldValues);
+      } catch (Throwable e) {
+        Platform.throwException(e);
+      }
+      return originObj;
+    }
+    T newObj = newBean();
+    if (needToCopyRef) {
+      T copyObject = (T) fury.getCopyObject(originObj);
+      if (copyObject != null) {
+        return copyObject;
+      }
+      fury.reference(originObj, newObj);
+    }
+    copyFields(originObj, newObj);
+    return newObj;
+  }
+
+  private Object[] copyFields(T originObj) {
+    return classResolver.getFieldResolver(type).getAllFieldsList().stream()

Review Comment:
   this is expensive, we can `buildFieldInfos(fury, descriptorGrouper)` to 
cache a `InternalFieldInfo[]` instead.
   
   Like:
   
   ```
   if (fieldsInfo == null) {
     fieldsInfo = xxx;
   }
   
   Object[] values = new Object[fieldsInfo.length];
   for (FieldInfo info : fieldsInfo) {
    switch(info.classId) {
   
   }
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to