vadimkolodingg commented on code in PR #6931: URL: https://github.com/apache/ignite-3/pull/6931#discussion_r2559980673
########## modules/marshaller-common/src/main/java/org/apache/ignite/internal/marshaller/Creator.java: ########## @@ -0,0 +1,128 @@ +/* + * 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.marshaller; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Arrays; +import org.jetbrains.annotations.Nullable; + +class Creator<T> { + final Class<T> clazz; + private final FieldAccessor[] accessors; + private @Nullable Annotated defaultConstructor; + private @Nullable Annotated creatorConstructor; + + Creator(Class<T> clazz, FieldAccessor[] accessors) { + this.clazz = clazz; + this.accessors = accessors; + findCreators(clazz); + } + + @FunctionalInterface + interface Annotated { + Object createFrom(FieldAccessor[] accessors, MarshallerReader reader); + } + + Object createFrom(MarshallerReader reader) { + if (creatorConstructor != null) { + return creatorConstructor.createFrom(accessors, reader); + } + if (defaultConstructor != null) { + return defaultConstructor.createFrom(accessors, reader); + } + + throw new IllegalArgumentException("Could not find default (no-args) or canonical (record) constructor for " + clazz.getName()); + } + + private void findCreators(Class<T> clazz) { + Constructor<?> defaultCtor = null; + Constructor<?> creatorCtor = null; + + Constructor<?>[] ctors = clazz.getDeclaredConstructors(); + for (Constructor<?> ctor : ctors) { + if (ctor.isSynthetic()) { + continue; + } + if (ctor.getParameterCount() == 0) { + defaultCtor = ctor; + continue; + } + + if (isCanonicalConstructor(clazz, ctor)) { + creatorCtor = ctor; + } + } Review Comment: Suggested simpler and more rigorous code with `isRecord(clazz)` implies that every implementation have to make own constructor iteration which is not cheap and would make further refactor worse. But in current case `isCanonicalConstructor` uses `clazz.getRecordComponents()` which already have `isRecord` check, so one constructor iteration is simpler and cheaper. -- 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]
