Github user ggevay commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2211#discussion_r78003691
  
    --- Diff: flink-core/src/main/resources/PojoSerializerTemplate.ftl ---
    @@ -0,0 +1,372 @@
    +/*
    + * 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 ${packageName};
    +import java.io.IOException;
    +import java.io.ObjectInputStream;
    +import java.io.ObjectOutputStream;
    +import java.lang.reflect.Modifier;
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.LinkedHashMap;
    +import java.util.LinkedHashSet;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Objects;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.TypeExtractor;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +
    +public final class ${className} extends TypeSerializer {
    +   private static byte IS_NULL = 1;
    +   private static byte NO_SUBCLASS = 2;
    +   private static byte IS_SUBCLASS = 4;
    +   private static byte IS_TAGGED_SUBCLASS = 8;
    +   private int numFields;
    +   private ExecutionConfig executionConfig;
    +   private Map<Class, TypeSerializer> subclassSerializerCache;
    +   private final Map<Class, Integer> registeredClasses;
    +   private final TypeSerializer[] registeredSerializers;
    +   Class clazz;
    +   <#list members as m>
    +   ${m}
    +   </#list>
    +   public ${className}(Class clazz, TypeSerializer[] serializerFields, 
ExecutionConfig e) {
    +           this.clazz = clazz;
    +           executionConfig = e;
    +           this.numFields = serializerFields.length;
    +           LinkedHashSet<Class> registeredPojoTypes = 
executionConfig.getRegisteredPojoTypes();
    +           subclassSerializerCache = new HashMap<Class, TypeSerializer>();
    +           List<Class> cleanedTaggedClasses = new 
ArrayList<Class>(registeredPojoTypes.size());
    +           for (Class registeredClass: registeredPojoTypes) {
    +                   if (registeredClass.equals(clazz)) {
    +                           continue;
    +                   }
    +                   if (!clazz.isAssignableFrom(registeredClass)) {
    +                           continue;
    +                   }
    +                   cleanedTaggedClasses.add(registeredClass);
    +           }
    +           this.registeredClasses = new LinkedHashMap<Class, 
Integer>(cleanedTaggedClasses.size());
    +           registeredSerializers = new 
TypeSerializer[cleanedTaggedClasses.size()];
    +           int id = 0;
    +           for (Class registeredClass: cleanedTaggedClasses) {
    +                   this.registeredClasses.put(registeredClass, id);
    +                   TypeInformation typeInfo = 
TypeExtractor.createTypeInfo(registeredClass);
    +                   registeredSerializers[id] = 
typeInfo.createSerializer(executionConfig);
    +                   id++;
    +           }
    +           <#list initMembers as m>
    +           ${m}
    +           </#list>
    +   }
    +   private TypeSerializer getSubclassSerializer(Class subclass) {
    +           TypeSerializer result = 
(TypeSerializer)subclassSerializerCache.get(subclass);
    +           if (result == null) {
    +                   TypeInformation typeInfo = 
TypeExtractor.createTypeInfo(subclass);
    +                   result = typeInfo.createSerializer(executionConfig);
    +                   subclassSerializerCache.put(subclass, result);
    +           }
    +           return result;
    +   }
    +   public boolean isImmutableType() { return false; }
    +   public ${className} duplicate() {
    +           boolean stateful = false;
    +           TypeSerializer[] duplicateFieldSerializers = new 
TypeSerializer[numFields];
    +           <#list duplicateSerializers as ds>
    +           ${ds}
    +           </#list>
    +           if (stateful) {
    +                   return new ${className}(clazz, 
duplicateFieldSerializers, executionConfig);
    +           } else {
    +                   return this;
    +           }
    +   }
    +   public ${typeName} createInstance() {
    +           if (clazz.isInterface() || 
Modifier.isAbstract(clazz.getModifiers())) {
    +                   return null;
    +           }
    +           try {
    +                   ${typeName} t = (${typeName})clazz.newInstance();
    +                   initializeFields(t);
    +                   return t;
    +           }
    +           catch (Exception e) {
    +                   throw new RuntimeException("Cannot instantiate class.", 
e);
    +           }
    +   }
    +   protected void initializeFields(${typeName} t) {
    +           <#list createFields as cf>
    +           ${cf}
    +           </#list>
    +   }
    +   public ${typeName} copy(Object from) {
    +           if (from == null) return null;
    +           Class<?> actualType = from.getClass();
    +           ${typeName} target;
    +           if (actualType == clazz) {
    +                   try {
    +                           target = (${typeName}) 
from.getClass().newInstance();
    +                   }
    +                   catch (Throwable t) {
    +                           throw new RuntimeException("Cannot instantiate 
class.", t);
    +                   }
    +                   <#list copyFields as cf>
    +                   ${cf}
    +                   </#list>
    +                   return target;
    +           } else {
    +                   TypeSerializer subclassSerializer = 
getSubclassSerializer(actualType);
    +                   return (${typeName})subclassSerializer.copy(from);
    +           }
    +   }
    +   public ${typeName} copy(Object from, Object reuse) {
    +           if (from == null) return null;
    +           Class actualType = from.getClass();
    +           if (actualType == clazz) {
    --- End diff --
    
    Is this needed if the type is final?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to