OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 144 Rev 156
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.utils;
14
 package org.openconcerto.utils;
15
 
15
 
-
 
16
import java.beans.Expression;
16
import java.lang.reflect.Array;
17
import java.lang.reflect.Array;
17
import java.lang.reflect.Constructor;
18
import java.lang.reflect.Constructor;
18
import java.lang.reflect.GenericArrayType;
19
import java.lang.reflect.GenericArrayType;
19
import java.lang.reflect.ParameterizedType;
20
import java.lang.reflect.ParameterizedType;
20
import java.lang.reflect.Type;
21
import java.lang.reflect.Type;
Line 27... Line 28...
27
import java.util.Map;
28
import java.util.Map;
28
 
29
 
29
public final class ReflectUtils {
30
public final class ReflectUtils {
30
 
31
 
31
    /**
32
    /**
32
     * Find a constructor with compatible parameters.
33
     * Find a constructor with compatible parameters. NOTE: This follow a simpler algorithm than the
-
 
34
     * one in the JLS (<a href=
-
 
35
     * "https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.12.2.5">15.12.2.5.
-
 
36
     * Choosing the Most Specific Method</a>).
33
     * 
37
     * 
34
     * @param cls the class to find a constructor for, not <code>null</code>.
38
     * @param cls the class to find a constructor for, not <code>null</code>.
35
     * @param parameterTypes types of parameters the constructor must accept.
39
     * @param parameterTypes types of parameters the constructor must accept.
36
     * @return the first matching constructor, <code>null</code> if no matching constructor found.
40
     * @return the first matching constructor, <code>null</code> if no matching constructor found.
37
     */
41
     */
Line 56... Line 60...
56
            }
60
            }
57
            return null;
61
            return null;
58
        }
62
        }
59
    }
63
    }
60
 
64
 
-
 
65
    @SuppressWarnings("unchecked")
-
 
66
    static public final <T> T createInstance(final Class<T> clazz, Object... args) throws Exception {
-
 
67
        // too difficult to order superclasses and all implemented interfaces of args in order to
-
 
68
        // find the most specific constructor
-
 
69
        return (T) new Expression(clazz, "new", args).getValue();
-
 
70
    }
-
 
71
 
-
 
72
    /**
-
 
73
     * Return the class with the passed name if it is a subclass of <code>clazz</code>.
-
 
74
     * 
-
 
75
     * @param name the name of the class, not <code>null</code>.
-
 
76
     * @param clazz the superclass, not <code>null</code>.
-
 
77
     * @return the requested class, <code>null</code> if {@link ClassNotFoundException not found} or
-
 
78
     *         not a {@link Class#asSubclass(Class) subclass} of <code>clazz</code>.
-
 
79
     */
-
 
80
    static public final <T> Class<? extends T> getSubclass(final String name, final Class<T> clazz) {
-
 
81
        return getSubclass(name, clazz, clazz.getClassLoader());
-
 
82
    }
-
 
83
 
-
 
84
    static public final <T> Class<? extends T> getSubclass(final String name, final Class<T> clazz, final ClassLoader cl) {
-
 
85
        final Class<?> res;
-
 
86
        try {
-
 
87
            res = Class.forName(name, true, cl);
-
 
88
        } catch (ClassNotFoundException e) {
-
 
89
            return null;
-
 
90
        }
-
 
91
        return clazz.isAssignableFrom(res) ? res.asSubclass(clazz) : null;
-
 
92
    }
-
 
93
 
61
    static private Map<Type, Type> resolveTypes(Class<?> c, Class<?> raw) {
94
    static private Map<Type, Type> resolveTypes(Class<?> c, Class<?> raw) {
62
        final Map<Type, Type> res = new HashMap<Type, Type>();
95
        final Map<Type, Type> res = new HashMap<Type, Type>();
63
        if (!raw.isAssignableFrom(c))
96
        if (!raw.isAssignableFrom(c))
64
            return res;
97
            return res;
65
 
98