chaokunyang commented on code in PR #2312:
URL: https://github.com/apache/fory/pull/2312#discussion_r2173208283


##########
java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java:
##########
@@ -232,16 +228,126 @@ private void register(
       if (type.isEnum()) {
         classInfo.serializer = new EnumSerializer(fory, (Class<Enum>) type);
       } else {
-        classInfo.serializer =
-            new LazySerializer.LazyObjectSerializer(
-                fory, type, () -> new ObjectSerializer<>(fory, type));
+        boolean codegen =
+            supportCodegenForJavaSerialization(type) && 
fory.getConfig().isCodeGenEnabled();
+        Class<? extends Serializer> objectSerializerClass =
+            getObjectSerializerClass(
+                type,
+                shareMeta,
+                codegen,
+                new JITContext.SerializerJITCallback<Class<? extends 
Serializer>>() {
+                  @Override
+                  public void onSuccess(Class<? extends Serializer> result) {
+                    setSerializer(
+                        type,
+                        Serializers.newSerializer(fory, type, result),
+                        namespace,
+                        typeName,
+                        xtypeId);
+                    if (classInfoCache.classInfo.cls == type) {
+                      classInfoCache.classInfo = NIL_CLASS_INFO; // clear 
class info cache
+                    }
+                    Preconditions.checkState(getSerializer(type).getClass() == 
result);
+                  }
+
+                  @Override
+                  public Object id() {
+                    return type;
+                  }
+                });
+        classInfo.serializer = Serializers.newSerializer(fory, type, 
objectSerializerClass);
       }
     }
     classInfoMap.put(type, classInfo);
     registeredTypeIds.add(xtypeId);
     xtypeIdToClassMap.put(xtypeId, classInfo);
   }
 
+  /**
+   * Set the serializer for <code>cls</code>, overwrite serializer if exists. 
Note if class info is
+   * already related with a class, this method should try to reuse that class 
info, otherwise jit
+   * callback to update serializer won't take effect in some cases since it 
can't change that
+   * classinfo.
+   */
+  public <T> void setSerializer(
+      Class<T> cls, Serializer<T> serializer, String namespace, String 
typeName, int xtypeId) {
+    addSerializer(cls, serializer, namespace, typeName, xtypeId);
+  }
+
+  /** Ass serializer for specified class. */
+  private void addSerializer(
+      Class<?> type, Serializer<?> serializer, String namespace, String 
typeName, int xtypeId) {
+    Preconditions.checkNotNull(serializer);
+    ClassInfo classInfo = classInfoMap.get(type);
+
+    if (classInfo == null) {
+      classInfo = newClassInfo(type, serializer, namespace, typeName, (short) 
xtypeId);
+      classInfoMap.put(type, classInfo);
+    }
+
+    // 2. Set `Serializer` for `ClassInfo`.
+    classInfo.serializer = serializer;
+  }
+
+  private Class<? extends Serializer> getObjectSerializerClass(
+      Class<?> cls,
+      boolean shareMeta,
+      boolean codegen,
+      JITContext.SerializerJITCallback<Class<? extends Serializer>> callback) {
+    if (codegen) {
+      if (classCtx.contains(cls)) {
+        // avoid potential recursive call for seq codec generation.
+        return CodegenSerializer.LazyInitBeanSerializer.class;
+      } else {
+        try {
+          classCtx.add(cls);
+          Class<? extends Serializer> sc;
+          switch (fory.getCompatibleMode()) {
+            case SCHEMA_CONSISTENT:
+              sc =
+                  fory.getJITContext()
+                      .registerSerializerJITCallback(
+                          () -> ObjectSerializer.class,
+                          () -> loadCodegenSerializer(fory, cls),
+                          callback);
+              return sc;
+            case COMPATIBLE:
+              // If share class meta, compatible serializer won't be 
necessary, class
+              // definition will be sent to peer to create serializer for 
deserialization.
+              sc =
+                  fory.getJITContext()
+                      .registerSerializerJITCallback(
+                          () -> shareMeta ? ObjectSerializer.class : 
CompatibleSerializer.class,
+                          () ->
+                              shareMeta
+                                  ? loadCodegenSerializer(fory, cls)
+                                  : loadCompatibleCodegenSerializer(fory, cls),
+                          callback);
+              return sc;
+            default:
+              throw new UnsupportedOperationException(
+                  String.format("Unsupported mode %s", 
fory.getCompatibleMode()));
+          }
+        } finally {
+          classCtx.remove(cls);
+        }
+      }
+    } else {
+      if (codegen) {
+        LOG.info("Object of type {} can't be serialized by jit", cls);
+      }
+      switch (fory.getCompatibleMode()) {
+        case SCHEMA_CONSISTENT:
+          return ObjectSerializer.class;
+        case COMPATIBLE:
+          return shareMeta ? ObjectSerializer.class : 
CompatibleSerializer.class;

Review Comment:
   CompatibleSerializer.class is not used for xlang serialization, we used meta 
shade mode for xlang forward/backward compatibility, CompatibleSerializer use 
kv format instead



-- 
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