OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils;
15
 
16
import java.io.PrintWriter;
17
import java.io.StringWriter;
18
import java.lang.reflect.Constructor;
19
 
20
/**
21
 * Utilitaires pour les exceptions.
22
 *
23
 * @author Sylvain CUAZ 25 nov. 2004
24
 */
25
public class ExceptionUtils {
26
    /** Static only. */
27
    private ExceptionUtils() {
28
        super();
29
    }
30
 
31
    /**
32
     * Crée une exception avec message et cause.
33
     *
34
     * @param <T> le type d'exception à créer.
35
     * @param exnClass la classe de l'exception à créer, eg IOException.class.
36
     * @param msg le message.
37
     * @param cause la cause.
38
     * @return une exception initialisée.
39
     */
40
    static public <T extends Exception> T createExn(Class<T> exnClass, String msg, Throwable cause) {
41
        T instance = null;
42
        try {
43
            Constructor<T> ctor = exnClass.getConstructor(new Class[] { String.class });
44
            instance = ctor.newInstance(new Object[] { msg });
45
        } catch (Exception exn) {
46
            throw new IllegalArgumentException(exnClass + " has no working String constructor");
47
        }
48
        instance.initCause(cause);
49
        return instance;
50
    }
51
 
52
    /**
53
     * Crée une RuntimeException.
54
     *
55
     * @param <T> le type d'exception à créer.
56
     * @param exnClass la classe de l'exception à créer, eg IllegalArgumentException.class.
57
     * @param msg le message.
58
     * @param cause la cause.
59
     * @return une RuntimeException initialisée.
60
     * @throws IllegalArgumentException if exnClass is not Runtime.
61
     * @see #createExn(Class, String, Throwable)
62
     * @deprecated use {@link #createExn(Class, String, Throwable)}
63
     */
64
    static public <T extends RuntimeException> T createRTExn(Class<T> exnClass, String msg, Throwable cause) {
65
        if (!RuntimeException.class.isAssignableFrom(exnClass))
66
            throw new IllegalArgumentException(exnClass + " is not a Runtime exception");
67
 
68
        return createExn(exnClass, msg, cause);
69
    }
70
 
71
    static public String getStackTrace(Throwable cause) {
72
        final StringWriter res = new StringWriter();
73
        cause.printStackTrace(new PrintWriter(res));
74
        return res.toString();
75
    }
76
}