SammyVimes commented on a change in pull request #495: URL: https://github.com/apache/ignite-3/pull/495#discussion_r764916079
########## 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); + Method readResolve = getReadResolve(clazz); + + SerializationType type; + + if (overrideSerialization) { + if (writeReplace == null && readResolve == null) { + type = SerializationType.SERIALIZABLE_OVERRIDE; + } else if (writeReplace != null && readResolve == null) { + type = SerializationType.SERIALIZABLE_OVERRIDE_WRITE_REPLACE; + } else if (writeReplace == null) { + type = SerializationType.SERIALIZABLE_OVERRIDE_READ_RESOLVE; + } else { + type = SerializationType.SERIALIZABLE_OVERRIDE_WRITE_REPLACE_READ_RESOLVE; + } + } else { + if (writeReplace == null && readResolve == null) { + type = SerializationType.SERIALIZABLE; + } else if (writeReplace != null && readResolve == null) { + type = SerializationType.SERIALIZABLE_WRITE_REPLACE; + } else if (writeReplace == null) { + type = SerializationType.SERIALIZABLE_READ_RESOLVE; + } else { + type = SerializationType.SERIALIZABLE_WRITE_REPLACE_READ_RESOLVE; + } + } + + return new ClassDescriptor( + className(clazz), + descriptorId, + fields(clazz), + type, + isFinal(clazz) + ); + } + + /** + * Parses the arbitrary class (not serializable or externalizable) definition. + * + * @param descriptorId Descriptor id of the class. + * @param clazz Arbitrary class. + * @return Class descriptor. + */ + private ClassDescriptor arbitrary(int descriptorId, Class<?> clazz) { + return new ClassDescriptor( + className(clazz), + descriptorId, + fields(clazz), + SerializationType.ARBITRARY, + isFinal(clazz) + ); + } + + /** + * Gets field descriptors of the class. If a field's type doesn't have an id yet, generates it. + * + * @param clazz Class. + * @return List of field descriptor. + */ + private List<FieldDescriptor> fields(Class<?> clazz) { + if (clazz.getSuperclass() != Object.class) { + // TODO: IGNITE-15945 add support for the inheritance + throw new UnsupportedOperationException("IGNITE-15945"); + } + + Field[] fields = clazz.getDeclaredFields(); + + List<FieldDescriptor> descs = new ArrayList<>(fields.length); + + for (Field field : fields) { + Class<?> type = field.getType(); + + int modifiers = field.getModifiers(); + + // Ignore static and transient field. + if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) { + continue; + } + + String name = field.getName(); + + // Get or create type id. + int typeId = context.getId(type); + + FieldDescriptor fieldDescriptor = new FieldDescriptor(name, type, typeId); + + descs.add(fieldDescriptor); + } + + return descs; + } + + /** + * Gets a method with the signature + * {@code ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException}. + * + * @param clazz Class. + * @return Method. + */ + private Method getWriteReplace(Class<? extends Serializable> clazz) { + try { + Method writeReplace = clazz.getDeclaredMethod("writeReplace"); + + if (!methodThrowsExceptions(writeReplace, + Collections.singletonList(ObjectStreamException.class))) { Review comment: It's from the Serializable javadocs -- 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]
