rpuch commented on a change in pull request #495: URL: https://github.com/apache/ignite-3/pull/495#discussion_r764920693
########## File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptorParser.java ########## @@ -0,0 +1,432 @@ +/* + * 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.ignite.internal.network.serialization; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.ObjectStreamException; +import java.io.Serializable; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import org.apache.ignite.lang.IgniteException; + +/** + * Class parser for the user object serialization. + */ +public class ClassDescriptorParser { + + /** + * Parser context. + */ + private final ClassDescriptorParserContext context; + + /** + * Constructor. + * + * @param ctx Context. + */ + public ClassDescriptorParser(ClassDescriptorParserContext ctx) { + this.context = ctx; + } + + /** + * Parses the class definition and definitions of classes fields if they're not already parsed. + * + * @param clazz Class definition. + * @return Class descriptor. + */ + public ClassDescriptor parse(Class<?> clazz) { + ClassDescriptor classDesc = parse0(clazz); + + Map<Integer, ClassDescriptor> localDescs = new HashMap<>(); + localDescs.put(classDesc.descriptorId(), classDesc); + + Queue<FieldDescriptor> fieldDescriptors = new LinkedList<>(classDesc.fields()); + + while (!fieldDescriptors.isEmpty()) { + FieldDescriptor fieldDescriptor = fieldDescriptors.remove(); + + int typeDescriptorId = fieldDescriptor.typeDescriptorId(); + + if (context.hasDescriptor(typeDescriptorId) || localDescs.containsKey( + typeDescriptorId)) { + continue; + } + + Class<?> fieldClass = fieldDescriptor.clazz(); + + ClassDescriptor fieldClassDesc = parse0(fieldClass); + + localDescs.put(typeDescriptorId, fieldClassDesc); + + fieldDescriptors.addAll(fieldClassDesc.fields()); + } + + context.addDescriptors(localDescs.values()); + + return classDesc; + } + + /** + * Parses the class definition. + * + * @param clazz Class. + * @return Class descriptor. + */ + private ClassDescriptor parse0(Class<?> clazz) { + assert !clazz.isPrimitive() : + clazz + " is a primitive, there should be a default descriptor"; + + int descriptorId = context.getId(clazz); + + if (Externalizable.class.isAssignableFrom(clazz)) { + //noinspection unchecked + return externalizable(descriptorId, (Class<? extends Externalizable>) clazz); + } else if (Serializable.class.isAssignableFrom(clazz)) { + //noinspection unchecked + return serializable(descriptorId, (Class<? extends Serializable>) clazz); + } + + return arbitrary(descriptorId, clazz); + } + + /** + * Parses the externalizable class definition. + * + * @param descriptorId Descriptor id of the class. + * @param clazz Externalizable class. + * @return Class descriptor. + */ + private ClassDescriptor externalizable(int descriptorId, + Class<? extends Externalizable> clazz) { + String className = className(clazz); + + boolean hasPublicNoArgConstructor = true; + + try { + Constructor<? extends Externalizable> ctor = clazz.getConstructor(); + + if (!Modifier.isPublic(ctor.getModifiers())) { + hasPublicNoArgConstructor = false; + } + } catch (NoSuchMethodException e) { + hasPublicNoArgConstructor = false; + } + + if (!hasPublicNoArgConstructor) { + throw new IgniteException( + "Externalizable class " + className + " has no public no-arg constructor"); + } + + return new ClassDescriptor( + className, + descriptorId, + Collections.emptyList(), + SerializationType.EXTERNALIZABLE, + isFinal(clazz) + ); + } + + /** + * Parses the serializable class definition. + * + * @param descriptorId Descriptor id of the class. + * @param clazz Serializable class. + * @return Class descriptor. + */ + private ClassDescriptor serializable(int descriptorId, Class<? extends Serializable> clazz) { + Method writeObject = getWriteObject(clazz); + Method readObject = getReadObject(clazz); + Method readObjectNoData = getReadObjectNoData(clazz); + + boolean overrideSerialization = false; + + if (writeObject != null || readObject != null || readObjectNoData != null) { + if (writeObject == null || readObject == null || readObjectNoData == null) { + throw new IgniteException("!"); + } + + overrideSerialization = true; + } + + Method writeReplace = getWriteReplace(clazz); Review comment: Then how about ``` boolean hasWriteReplace = getWriteReplace(clazz) != null; boolean hasReadResolve = getReadResolve(clazz) != null; ``` and so on. The local variables for methods will be super-easy to add when they are needed. -- 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]
