OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 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;
182 ilm 19
import java.math.BigDecimal;
20
import java.util.Objects;
21
import java.util.function.BiPredicate;
17 ilm 22
 
23
/**
24
 * Utilitaires pour les exceptions.
25
 *
26
 * @author Sylvain CUAZ 25 nov. 2004
27
 */
28
public class ExceptionUtils {
29
    /** Static only. */
30
    private ExceptionUtils() {
31
        super();
32
    }
33
 
34
    /**
35
     * Crée une exception avec message et cause.
36
     *
37
     * @param <T> le type d'exception à créer.
38
     * @param exnClass la classe de l'exception à créer, eg IOException.class.
39
     * @param msg le message.
40
     * @param cause la cause.
41
     * @return une exception initialisée.
42
     */
43
    static public <T extends Exception> T createExn(Class<T> exnClass, String msg, Throwable cause) {
44
        T instance = null;
45
        try {
46
            Constructor<T> ctor = exnClass.getConstructor(new Class[] { String.class });
47
            instance = ctor.newInstance(new Object[] { msg });
48
        } catch (Exception exn) {
49
            throw new IllegalArgumentException(exnClass + " has no working String constructor");
50
        }
51
        instance.initCause(cause);
52
        return instance;
53
    }
54
 
132 ilm 55
    /*
56
     * return an exception so that the caller can "throw ExceptionUtils.throwExn(exn,
57
     * SQLException.class, RuntimeException.class);" otherwise the compiler will complain that the
58
     * method doesn't return. Obviously the return will never be used.
59
     */
60
    static public <T extends Throwable, X extends T, Y extends T> X throwExn(T toThrow, Class<X> classX, Class<Y> classY) throws X, Y {
61
        if (classX.isInstance(toThrow))
62
            throw classX.cast(toThrow);
63
        else
64
            throw classY.cast(toThrow);
65
    }
66
 
17 ilm 67
    /**
68
     * Crée une RuntimeException.
69
     *
70
     * @param <T> le type d'exception à créer.
71
     * @param exnClass la classe de l'exception à créer, eg IllegalArgumentException.class.
72
     * @param msg le message.
73
     * @param cause la cause.
74
     * @return une RuntimeException initialisée.
75
     * @throws IllegalArgumentException if exnClass is not Runtime.
76
     * @see #createExn(Class, String, Throwable)
77
     * @deprecated use {@link #createExn(Class, String, Throwable)}
78
     */
79
    static public <T extends RuntimeException> T createRTExn(Class<T> exnClass, String msg, Throwable cause) {
80
        if (!RuntimeException.class.isAssignableFrom(exnClass))
81
            throw new IllegalArgumentException(exnClass + " is not a Runtime exception");
82
 
83
        return createExn(exnClass, msg, cause);
84
    }
85
 
86
    static public String getStackTrace(Throwable cause) {
156 ilm 87
        final StringWriter res = new StringWriter(8192);
93 ilm 88
        final PrintWriter pw = new PrintWriter(res);
89
        try {
90
            cause.printStackTrace(pw);
91
        } finally {
92
            pw.close();
93
        }
94
        if (pw.checkError())
95
            Log.get().warning("Error while writing " + cause);
17 ilm 96
        return res.toString();
97
    }
182 ilm 98
 
99
    static public void requireEquals(final BigDecimal a, final BigDecimal b, final String msg) {
100
        require((bd1, bd2) -> bd1.compareTo(bd2) == 0, a, b, msg);
101
    }
102
 
103
    static public void requireEquals(final Object a, final Object b, final String msg) {
104
        require(Objects::equals, a, b, msg);
105
    }
106
 
107
    static public <T> void require(final BiPredicate<T, T> pred, final T a, final T b, final String msg) {
108
        if (!pred.test(a, b))
109
            throw new IllegalArgumentException(msg + ", expected " + a + " but got " + b);
110
    }
111
 
112
    static public void requireSame(final Object a, final Object b, final String msg) {
113
        if (a != b)
114
            throw new IllegalArgumentException(msg + ", expected same reference to " + a + " but got " + b);
115
    }
116
 
117
    static public void requireEquals(final int a, final int b, final String msg) {
118
        if (a != b)
119
            throw new IllegalArgumentException(msg + ", expected " + a + " but got " + b);
120
    }
17 ilm 121
}