001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.ws.util.spring;
015
016import java.util.ArrayList;
017import java.util.List;
018
019/**
020 * Miscellaneous class utility methods. Mainly for internal use within the
021 * framework; consider Jakarta's Commons Lang for a more comprehensive suite
022 * of class utilities.
023 *
024 * @author Keith Donald
025 * @author Rob Harrop
026 * @author Juergen Hoeller
027 * @see ReflectionUtils
028 * @since 1.1
029 */
030public abstract class ClassUtils {
031
032  /**
033   * Return all interfaces that the given class implements as array,
034   * including ones implemented by superclasses.
035   * <p>If the class itself is an interface, it gets returned as sole interface.
036   *
037   * @param clazz the class to analyse for interfaces
038   *
039   * @return all interfaces that the given object implements as array
040   */
041  public static Class[] getAllInterfacesForClass(Class clazz) {
042    Assert.notNull(clazz, "Class must not be null");
043    if (clazz.isInterface()) {
044      return new Class[] {clazz};
045    }
046    List interfaces = new ArrayList();
047    while (clazz != null) {
048      for (int i = 0; i < clazz.getInterfaces().length; i++) {
049        Class ifc = clazz.getInterfaces()[i];
050        if (!interfaces.contains(ifc)) {
051          interfaces.add(ifc);
052        }
053      }
054      clazz = clazz.getSuperclass();
055    }
056    return (Class[]) interfaces.toArray(new Class[interfaces.size()]);
057  }
058}