All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.ydb.yoj.databind.schema.ConstructionException Maven / Gradle / Ivy

Go to download

Core data-binding logic used by YOJ (YDB ORM for Java) to convert between Java objects and database rows (or anything representable by a Java Map, really).

The newest version!
package tech.ydb.yoj.databind.schema;

import lombok.NonNull;

import javax.annotation.Nullable;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Pattern;

import static java.lang.String.format;
import static java.util.stream.Collectors.joining;

/**
 * Could not construct object according to {@link Schema} with the specified field value.
 */
public final class ConstructionException extends BindingException {
    public ConstructionException(@NonNull Constructor ctor, @NonNull Object[] args, @Nullable Throwable cause) {
        super(cause, ex -> message(ctor, args, ex));
    }

    private static String message(Constructor ctor, Object[] args, Throwable ex) {
        Class clazz = ctor.getDeclaringClass();
        String shortClassName = Optional.ofNullable(clazz.getCanonicalName())
                .map(n -> n.replaceFirst("^" + Pattern.quote(clazz.getPackageName()) + "\\.", ""))
                .orElse("???");
        String argString = Arrays.stream(args).map(String::valueOf).collect(joining(", "));
        return format("Could not construct new %s(%s)%s", shortClassName, argString, ex == null ? "" : ": " + ex);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy