2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.utils;
24 import lombok.AccessLevel;
25 import lombok.NoArgsConstructor;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Reflection utilities.
33 @NoArgsConstructor(access = AccessLevel.PRIVATE)
34 public final class ReflectionUtil {
36 private static final Logger logger = LoggerFactory.getLogger(ReflectionUtil.class);
39 * returns (if exists) a class fetched from a given classloader.
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
46 public static Class<?> fetchClass(ClassLoader classLoader,
48 if (classLoader == null) {
49 throw new IllegalArgumentException("A class loader must be provided");
52 if (className == null) {
53 throw new IllegalArgumentException("A class name to be fetched in class loader "
54 + classLoader + " must be provided");
58 return Class.forName(className, true, classLoader);
59 } catch (Exception e) {
60 logger.error("class {} fetched in {} does not exist", className, classLoader, e);
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
74 public static boolean isClass(ClassLoader classLoader, String classname) {
75 return fetchClass(classLoader, classname) != null;
81 * @param parent superclass
82 * @param presumedSubclass subclass
83 * @return true if it is a subclass
85 public static boolean isSubclass(Class<?> parent, Class<?> presumedSubclass) {
86 return parent.isAssignableFrom(presumedSubclass);