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

tech.sirwellington.alchemy.arguments.DynamicExceptionSupplier Maven / Gradle / Ivy

Go to download

Part of the Alchemy Collection. Easy, Simple, and Robust argument checking logic for your Services, Libraries, and Scripts.

There is a newer version: 2.3
Show newest version
/*
 * Copyright 2015 SirWellington Tech.
 *
 * Licensed 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 tech.sirwellington.alchemy.arguments;

import java.lang.reflect.Constructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.sirwellington.alchemy.annotations.access.Internal;
import tech.sirwellington.alchemy.annotations.concurrency.Immutable;

/**
 * This class uses an Exception class to dynamically create an appropriate wrapper exception.
 *
 * @author SirWellington
 */
@Internal
@Immutable
final class DynamicExceptionSupplier implements ExceptionMapper
{

    private static final Logger LOG = LoggerFactory.getLogger(DynamicExceptionSupplier.class);

    private final Class exceptionClass;
    private final String message;

    DynamicExceptionSupplier(Class exceptionClass, String message)
    {
        Checks.Internal.checkNotNull(exceptionClass, "missing exceptionClass");
        
        this.exceptionClass = exceptionClass;
        this.message = message;
    }

    @Override
    public Ex apply(FailedAssertionException cause)
    {
        Ex instance = tryToCreateInstance(cause);

        return instance;
    }
    private Ex tryToCreateInstance(FailedAssertionException cause)
    {
        try
        {
            if (haveBothAMessageAndACause(message, cause))
            {
                if (throwableClassHasMessageAndCauseConstructor())
                {
                    return exceptionClass.getConstructor(String.class, Throwable.class)
                            .newInstance(message, cause);
                }
                else if (throwableClassHasCauseConstructor())
                {
                    return exceptionClass.getConstructor(Throwable.class)
                            .newInstance(cause);
                }
                else if (throwableClassHasMessageConstructor())
                {
                    return exceptionClass.getConstructor(String.class)
                            .newInstance(message);
                }

            }

            if (haveOnlyACause(message, cause))
            {
                if (throwableClassHasCauseConstructor())
                {
                    return exceptionClass.getConstructor(Throwable.class).newInstance(cause);
                }
            }

            if (haveOnlyAMessage(message, cause))
            {
                if (throwableClassHasMessageConstructor())
                {
                    return exceptionClass.getConstructor(String.class).newInstance(message);
                }
            }

        }
        catch (Exception ex)
        {
            LOG.error("Failed to initialize instance of Exception type {}", exceptionClass, ex);
        }

        try
        {
            if (hasDefaultConstructor())
            {
                return exceptionClass.newInstance();
            }
        }
        catch (Exception ex)
        {
            LOG.warn("Failed to create instance of {} using default constructor", exceptionClass.getName());
        }

        return null;
    }

    @Override
    public String toString()
    {
        return "DynamicExceptionSupplier{" + "exceptionClass=" + exceptionClass + ", message=" + message + '}';
    }

    private boolean hasConstructorWithArguments(Class... classes) throws NoSuchMethodException, SecurityException
    {
        try
        {
            Constructor constructor = exceptionClass.getConstructor(classes);
            return constructor != null;
        }
        catch (NoSuchMethodException ex)
        {
            return false;
        }
    }

    private boolean hasDefaultConstructor() throws NoSuchMethodException, SecurityException
    {
        return hasConstructorWithArguments();
    }

    private boolean throwableClassHasCauseConstructor() throws NoSuchMethodException, SecurityException
    {
        return hasConstructorWithArguments(Throwable.class);
    }

    private boolean throwableClassHasMessageConstructor() throws NoSuchMethodException, SecurityException
    {
        return hasConstructorWithArguments(String.class);
    }

    private boolean throwableClassHasMessageAndCauseConstructor() throws NoSuchMethodException, SecurityException
    {
        return hasConstructorWithArguments(String.class, Throwable.class);
    }

    private boolean haveOnlyAMessage(String message, FailedAssertionException cause)
    {
        return !Checks.Internal.isNullOrEmpty(message) && cause == null;
    }

    private boolean haveOnlyACause(String message, FailedAssertionException cause)
    {
        return cause != null && Checks.Internal.isNullOrEmpty(message);
    }

    private boolean haveBothAMessageAndACause(String message, FailedAssertionException cause)
    {
        return cause != null && !Checks.Internal.isNullOrEmpty(message);
    }
    
    @Internal
    Class getExceptionClass()
    {
        return this.exceptionClass;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy