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

org.valkyriercp.application.exceptionhandling.ThrowExceptionCommand Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package org.valkyriercp.application.exceptionhandling;

import org.valkyriercp.application.ApplicationWindow;
import org.valkyriercp.command.support.ApplicationWindowAwareCommand;

import javax.swing.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * A development command used to test the exception handler configuration.
 * Pops up a dialog and allows the user to type in the full classname of an exception to throw.
 * 

* It is usually not used in production and therefor configured with this property: * <property name="visible" value="${development.mode}" /> * @author gds */ public class ThrowExceptionCommand extends ApplicationWindowAwareCommand { private static final String THROWABLE_MESSAGE = "Throwable message made by ThrowExceptionCommand"; private final static String ID = "throwExceptionCommand"; public ThrowExceptionCommand() { super(ID); } @Override protected void doExecuteCommand() { List> throwableClassList = askUserThrowableClassList(); if (throwableClassList != null) { throwThrowable(throwableClassList); } } private List> askUserThrowableClassList() { String throwableClassListString = (String) JOptionPane.showInputDialog(resolveParentFrame(), "Please fill in the full classname of the exception to throw:\n" + "Causes can optionally be appended by semicolons(;). For example:\n" + " org.springframework.remoting.RemoteLookupFailureException;" + "java.rmi.ConnectException;java.net.ConnectException", "Which exception do you want to throw?", JOptionPane.INFORMATION_MESSAGE, null, null, "java.lang.IllegalStateException"); if (throwableClassListString == null) { // User cancelled return null; } List> throwableClassList = new ArrayList>(); String[] throwableClassListStringTokens = throwableClassListString.split(";"); for (String throwableClassListStringToken : throwableClassListStringTokens) { Class throwableClass; try { throwableClass = (Class) Class.forName(throwableClassListStringToken); } catch (ClassNotFoundException e) { throw new IllegalArgumentException( "ThrowExceptionCommand failure: Class (" + throwableClassListStringToken + ") does not exist.", e); } throwableClassList.add(throwableClass); } return throwableClassList; } private JFrame resolveParentFrame() { ApplicationWindow activeWindow = applicationConfig.windowManager().getActiveWindow(); return (activeWindow == null) ? null : activeWindow.getControl(); } private void throwThrowable(List> throwableClassList) { Class throwableClass = throwableClassList.remove(0); Throwable throwable; try { throwable = buildThrowable(throwableClassList, throwableClass); } catch (InstantiationException e) { throw new IllegalArgumentException( "ThrowExceptionCommand failure: Could not build throwable chain.", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException( "ThrowExceptionCommand failure: Could not build throwable chain.", e); } catch (NoSuchMethodException e) { throw new IllegalArgumentException( "ThrowExceptionCommand failure: Could not build throwable chain.", e); } catch (InvocationTargetException e) { throw new IllegalArgumentException( "ThrowExceptionCommand failure: Could not build throwable chain.", e); } if (throwable instanceof RuntimeException) { throw (RuntimeException) throwable; } else if (throwable instanceof Error) { throw (Error) throwable; } else { throw new IllegalArgumentException("ThrowExceptionCommand failure: The root throwable (" + throwableClass + ") should extend RuntimeException or Error."); } } private Throwable buildThrowable(List> throwableClassList, Class throwableClass) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { Throwable throwable; if (throwableClassList.isEmpty()) { try { throwable = throwableClass.getDeclaredConstructor(String.class).newInstance( THROWABLE_MESSAGE); } catch (NoSuchMethodException e) { throwable = throwableClass.newInstance(); } } else { Throwable cause = buildThrowAndCatch(throwableClassList); try { throwable = throwableClass.getDeclaredConstructor(String.class, Throwable.class) .newInstance(THROWABLE_MESSAGE, cause); } catch (NoSuchMethodException e) { try { throwable = throwableClass.getDeclaredConstructor(String.class, Exception.class) .newInstance(THROWABLE_MESSAGE, cause); } catch (NoSuchMethodException e2) { try { throwable = throwableClass.getDeclaredConstructor(Throwable.class).newInstance(cause); } catch (NoSuchMethodException e3) { throwable = throwableClass.getDeclaredConstructor(Exception.class).newInstance(cause); } } } } return throwable; } private Throwable buildThrowAndCatch(List> throwableClassList) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { Class throwableClass = throwableClassList.remove(0); Throwable throwable = buildThrowable(throwableClassList, throwableClass); try { throw throwable; } catch (Throwable catchedThrowable) { return catchedThrowable; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy