/*
 * This file is covered by the terms of the Common Public License v1.0.
 *
 * Copyright (c) SZEDER Gábor
 *
 * Parts of this software were developed within the JEOPARD research
 * project, which received funding from the European Union's Seventh
 * Framework Programme under grant agreement No. 216682.
 */

package de.fzi.cjunit.jpf.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static de.fzi.cjunit.util.LineSeparator.lineSeparator;

import de.fzi.cjunit.jpf.exceptioninfo.ExceptionInfo;


public class ExceptionFactory {

	public Throwable createException(ExceptionInfo exceptionInfo)
			throws ClassNotFoundException, IllegalArgumentException,
				InstantiationException, IllegalAccessException,
				InvocationTargetException {
		if (exceptionInfo == null) {
			return null;
		}

		Throwable cause = null;
		if (exceptionInfo.hasCause()) {
			cause = createException(exceptionInfo.getCause());
		}

		Class<?> exceptionClass = Class.forName(
				exceptionInfo.getClassName());
		Constructor<?> constructor = getExceptionConstructor(
				exceptionClass);
		if (constructor == null) {
			throw new RuntimeException(
					"exception with no suitable " +
					"constructor thrown inside JPF" +
					lineSeparator +
					"type: " +
					exceptionInfo.getClassName() +
					lineSeparator +
					"message: " +
					exceptionInfo.getMessage());
		}

		Throwable t = (Throwable) constructor.newInstance(
				new Object[] { exceptionInfo.getMessage() });
		t.initCause(cause);
		t.setStackTrace(new StackFrameConverter().toStackTrace(
					exceptionInfo.getStackTrace()));
		return t;
	}

	public Throwable createException(String exceptionClassName,
			String exceptionMessage,
			StackTraceElement[] stackTrace)
			throws ClassNotFoundException, IllegalArgumentException,
				InstantiationException, IllegalAccessException,
				InvocationTargetException {
		Class<?> exceptionClass = Class.forName(exceptionClassName);
		Constructor<?> constructor = getExceptionConstructor(
				exceptionClass);
		if (constructor == null) {
			throw new RuntimeException(
					"exception with no suitable " +
					"constructor thrown inside JPF" +
					lineSeparator +
					"type: " + exceptionClassName +
					lineSeparator +
					"message: " + exceptionMessage);
		}

		Throwable t = (Throwable) constructor.newInstance(
				new Object[] { exceptionMessage });
		t.setStackTrace(stackTrace);
		return t;
	}

	protected Constructor<?> getExceptionConstructor(
			Class<?> exceptionClass) {
		Constructor<?> constructor = getCheckedConstructor(
				exceptionClass, String.class);
		if (constructor != null)
			return constructor;
		return getCheckedConstructor(
				exceptionClass, Object.class);
	}

	protected Constructor<?> getCheckedConstructor(
			Class<?> exceptionClass, Class<?> constructorArg) {
		Class<?>[] args = new Class[] { constructorArg };
		try {
			return exceptionClass.getConstructor(args);
		} catch (NoSuchMethodException e) { }
		return null;
	}
}

@@ -1642,814 +1642,8 @@
 %09%7D%0A%0A
-%09public Throwable createException(String exceptionClassName,%0A%09%09%09String exceptionMessage,%0A%09%09%09StackTraceElement%5B%5D stackTrace)%0A%09%09%09throws ClassNotFoundException, IllegalArgumentException,%0A%09%09%09%09InstantiationException, IllegalAccessException,%0A%09%09%09%09InvocationTargetException %7B%0A%09%09Class%3C?%3E exceptionClass = Class.forName(exceptionClassName);%0A%09%09Constructor%3C?%3E constructor = getExceptionConstructor(%0A%09%09%09%09exceptionClass);%0A%09%09if (constructor == null) %7B%0A%09%09%09throw new RuntimeException(%0A%09%09%09%09%09%22exception with no suitable %22 +%0A%09%09%09%09%09%22constructor thrown inside JPF%22 +%0A%09%09%09%09%09lineSeparator +%0A%09%09%09%09%09%22type: %22 + exceptionClassName +%0A%09%09%09%09%09lineSeparator +%0A%09%09%09%09%09%22message: %22 + exceptionMessage);%0A%09%09%7D%0A%0A%09%09Throwable t = (Throwable) constructor.newInstance(%0A%09%09%09%09new Object%5B%5D %7B exceptionMessage %7D);%0A%09%09t.setStackTrace(stackTrace);%0A%09%09return t;%0A%09%7D%0A%0A
 %09pro

remove unused method