SammyVimes commented on a change in pull request #495: URL: https://github.com/apache/ignite-3/pull/495#discussion_r764891190
########## 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() : Review comment: Well, class descriptors for primitives should be already registered, so this should never happen. A test should be covering this situation (and failing with the error). But of course it can be changed -- 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]
