OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Details | Compare with Previous | 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
 
132 ilm 52
    /*
53
     * return an exception so that the caller can "throw ExceptionUtils.throwExn(exn,
54
     * SQLException.class, RuntimeException.class);" otherwise the compiler will complain that the
55
     * method doesn't return. Obviously the return will never be used.
56
     */
57
    static public <T extends Throwable, X extends T, Y extends T> X throwExn(T toThrow, Class<X> classX, Class<Y> classY) throws X, Y {
58
        if (classX.isInstance(toThrow))
59
            throw classX.cast(toThrow);
60
        else
61
            throw classY.cast(toThrow);
62
    }
63
 
17 ilm 64
    /**
65
     * Crée une RuntimeException.
66
     *
67
     * @param <T> le type d'exception à créer.
68
     * @param exnClass la classe de l'exception à créer, eg IllegalArgumentException.class.
69
     * @param msg le message.
70
     * @param cause la cause.
71
     * @return une RuntimeException initialisée.
72
     * @throws IllegalArgumentException if exnClass is not Runtime.
73
     * @see #createExn(Class, String, Throwable)
74
     * @deprecated use {@link #createExn(Class, String, Throwable)}
75
     */
76
    static public <T extends RuntimeException> T createRTExn(Class<T> exnClass, String msg, Throwable cause) {
77
        if (!RuntimeException.class.isAssignableFrom(exnClass))
78
            throw new IllegalArgumentException(exnClass + " is not a Runtime exception");
79
 
80
        return createExn(exnClass, msg, cause);
81
    }
82
 
83
    static public String getStackTrace(Throwable cause) {
156 ilm 84
        final StringWriter res = new StringWriter(8192);
93 ilm 85
        final PrintWriter pw = new PrintWriter(res);
86
        try {
87
            cause.printStackTrace(pw);
88
        } finally {
89
            pw.close();
90
        }
91
        if (pw.checkError())
92
            Log.get().warning("Error while writing " + cause);
17 ilm 93
        return res.toString();
94
    }
95
}