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

com.javonet.utils.exceptions.ExceptionThrower Maven / Gradle / Ivy

Go to download

Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. It works on Linux/Windows and MacOS for applications created in JVM, CLR/Netcore, Perl, Python, Ruby, NodeJS, C++ or GoLang and gives you unparalleled freedom and flexibility with native performance in building your mixed-technologies products. Let it be accessing best AI or cryptography libraries, devices SDKs, legacy client modules, internal custom packages or anything from public repositories available on NPM, Nuget, PyPI, Maven/Gradle, RubyGems or GitHub. Get free from programming languages barriers today! For more information check out our guides at https://www.javonet.com/guides/v2/

There is a newer version: 2.4.5
Show newest version
package com.javonet.utils.exceptions;

import com.javonet.utils.Command;
import com.javonet.utils.ExceptionType;

public class ExceptionThrower {
    public static void throwException(Command commandException) {
        StackTraceElement[] stackTrace = null;
        String exceptionMessage = "Java Exception with empty message";
        String exceptionName = "Java Exception";
        String javonetStackCommand = "";
        ExceptionType exceptionType = ExceptionType.EXCEPTION;

        switch (commandException.getPayload().length) {
            case 8:
                stackTrace = getLocalStackTrace(
                        (String) commandException.getPayload()[4],
                        (String) commandException.getPayload()[5],
                        (String) commandException.getPayload()[6],
                        (String) commandException.getPayload()[7]);
            case 4:
                exceptionMessage = (String) commandException.getPayload()[3];
            case 3:
                exceptionName = (String) commandException.getPayload()[2];
            case 2:
                javonetStackCommand = (String) commandException.getPayload()[1];
            case 1:
                exceptionType = ExceptionType.values()[(int) commandException.getPayload()[0]];
                break;
            case 0:
                exceptionType = ExceptionType.EXCEPTION;
        }

        if (stackTrace == null) {
            stackTrace = new StackTraceElement[1];
            stackTrace[0] = new StackTraceElement("", "", "", 0);
        }

        switch (exceptionType) {
            case EXCEPTION:
            case RUNTIME_EXCEPTION:
                RuntimeException runtimeException = new RuntimeException(exceptionMessage);
                runtimeException.setStackTrace(stackTrace);
                throw runtimeException;
            case IO_EXCEPTION:
                UncheckedIOException uncheckedIOException = new UncheckedIOException(exceptionMessage);
                uncheckedIOException.setStackTrace(stackTrace);
                throw uncheckedIOException;
            case ARITHMETIC_EXCEPTION:
            case DIVIDE_BY_ZERO_EXCEPTION:
                ArithmeticException arithmeticException = new ArithmeticException(exceptionMessage);
                arithmeticException.setStackTrace(stackTrace);
                throw arithmeticException;
            case NULL_POINTER_EXCEPTION:
                NullPointerException nullPointerException = new NullPointerException(exceptionMessage);
                nullPointerException.setStackTrace(stackTrace);
                throw nullPointerException;
            case FILE_NOT_FOUND_EXCEPTION:
                UncheckedFileNotFoundException uncheckedFileNotFoundException =
                        new UncheckedFileNotFoundException(exceptionMessage);
                uncheckedFileNotFoundException.setStackTrace(stackTrace);
                throw uncheckedFileNotFoundException;
            case ILLEGAL_ARGUMENT_EXCEPTION:
                IllegalArgumentException illegalArgumentException =
                        new IllegalArgumentException(exceptionMessage);
                illegalArgumentException.setStackTrace(stackTrace);
                throw illegalArgumentException;
            case INDEX_OUT_OF_BOUNDS_EXCEPTION:
                IndexOutOfBoundsException indexOutOfBoundsException =
                        new IndexOutOfBoundsException(exceptionMessage);
                indexOutOfBoundsException.setStackTrace(stackTrace);
                throw indexOutOfBoundsException;
            default:
                throw new RuntimeException(
                        "Unable to process Exception: " + commandException.payloadToString());
        }
    }

    private static StackTraceElement[] getLocalStackTrace(
            String stackTraceClasses,
            String stackTraceMethods,
            String stackTraceLines,
            String stackTraceFiles) {

        String[] stackClasses = stackTraceClasses.split("\\|");
        String[] stackMethods = stackTraceMethods.split("\\|");
        String[] stackLines = stackTraceLines.split("\\|");
        String[] stackFiles = stackTraceFiles.split("\\|");
        StackTraceElement[] result = new StackTraceElement[stackLines.length];
        for (int i = 0; i < stackLines.length; i++) {
            int stackLine = 0;
            try {
                stackLine = Integer.parseInt(stackLines[i]);
            } catch (NumberFormatException ex) {
                stackLine = 0;
            }
            StackTraceElement ste =
                    new StackTraceElement(
                            stackClasses[i], stackMethods[i], stackFiles[i], stackLine);
            result[i] = ste;
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy