Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-utils / src / main / java / org / onap / policy / drools / utils / ReflectionUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-utils
4  * ================================================================================
5  * Copyright (C) 2017-2018, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.utils;
23
24 import lombok.AccessLevel;
25 import lombok.NoArgsConstructor;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Reflection utilities.
31  *
32  */
33 @NoArgsConstructor(access = AccessLevel.PRIVATE)
34 public final class ReflectionUtil {
35
36     private static final Logger logger = LoggerFactory.getLogger(ReflectionUtil.class);
37
38     /**
39      * returns (if exists) a class fetched from a given classloader.
40      *
41      * @param classLoader the class loader
42      * @param className the class name
43      * @return the actual class
44      * @throws IllegalArgumentException if an invalid parameter has been passed in
45      */
46     public static Class<?> fetchClass(ClassLoader classLoader,
47             String className) {
48         if (classLoader == null) {
49             throw new IllegalArgumentException("A class loader must be provided");
50         }
51
52         if (className == null) {
53             throw new IllegalArgumentException("A class name to be fetched in class loader "
54                     + classLoader + " must be provided");
55         }
56
57         try {
58             return Class.forName(className, true, classLoader);
59         } catch (Exception e) {
60             logger.error("class {} fetched in {} does not exist", className, classLoader, e);
61         }
62
63         return null;
64     }
65
66     /**
67      * Is class.
68      *
69      * @param classLoader target class loader
70      * @param classname class name to fetch
71      * @return true if exists
72      * @throws IllegalArgumentException if an invalid parameter has been passed in
73      */
74     public static boolean isClass(ClassLoader classLoader, String classname) {
75         return fetchClass(classLoader, classname) != null;
76     }
77
78     /**
79      * Is it a subclass.
80      *
81      * @param parent superclass
82      * @param presumedSubclass subclass
83      * @return true if it is a subclass
84      */
85     public static boolean isSubclass(Class<?> parent, Class<?> presumedSubclass) {
86         return parent.isAssignableFrom(presumedSubclass);
87     }
88
89 }