1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.model.impl;
7   
8   import java.lang.reflect.Method;
9   import java.util.Collections;
10  import java.util.LinkedList;
11  import java.util.List;
12  
13  public final class ClassIntrospector {
14    private ClassIntrospector() {
15      // disable construction
16    }
17  
18    @SuppressWarnings("PMD.EmptyCatchBlock")
19    public static List<Method> getMatchingMethods(Class<?> clazz, String name, Class<?>... parameterTypes) {
20      List<Method> retval = new LinkedList<>();
21      Class<?> searchClass = clazz;
22      do {
23        try {
24          Method method = searchClass.getDeclaredMethod(name, parameterTypes);
25          retval.add(method);
26        } catch (NoSuchMethodException ex) {
27          // do nothing, no matching method was found
28        }
29      } while ((searchClass = searchClass.getSuperclass()) != null);
30  
31      return retval.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(retval);
32    }
33  
34    @SuppressWarnings("PMD.EmptyCatchBlock")
35    public static Method getMatchingMethod(Class<?> clazz, String name, Class<?>... parameterTypes) {
36      Method retval = null;
37      Class<?> searchClass = clazz;
38      do {
39        try {
40          retval = searchClass.getDeclaredMethod(name, parameterTypes);
41          // stop on first found method
42          break;
43        } catch (NoSuchMethodException ex) {
44          // do nothing, no matching method was found
45        }
46      } while ((searchClass = searchClass.getSuperclass()) != null);
47  
48      return retval;
49    }
50  }