Return true if class a is either equivalent to class b, or if class a is a subclass of class b, i.e. if a either "extends" or "implements" b. : SuperClass « Reflection « Java






Return true if class a is either equivalent to class b, or if class a is a subclass of class b, i.e. if a either "extends" or "implements" b.

        
/*
 * This file is part of aion-emu <aion-emu.com>.
 *
 * aion-emu is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-emu is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aion-emu.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * This class contains utilities that are used when we are working with classes
 * 
 * @author SoulKeeper
 */
public class Util{

  /**
   * Return true if class a is either equivalent to class b, or if class a is a subclass of class b, i.e. if a either
   * "extends" or "implements" b. Note tht either or both "Class" objects may represent interfaces.
   * 
   * @param a
   *            class
   * @param b
   *            class
   * @return true if a == b or a extends b or a implements b
   */
  @SuppressWarnings("rawtypes")
  public static boolean isSubclass(Class<?> a, Class<?> b)
  {
    // We rely on the fact that for any given java class or
    // primtitive type there is a unqiue Class object, so
    // we can use object equivalence in the comparisons.
    if (a == b)
    {
      return true;
    }
    if (a == null || b == null)
    {
      return false;
    }
    for (Class x = a; x != null; x = x.getSuperclass())
    {
      if (x == b)
      {
        return true;
      }
      if (b.isInterface())
      {
        Class[] interfaces = x.getInterfaces();
        for (Class anInterface : interfaces)
        {
          if (isSubclass(anInterface, b))
          {
            return true;
          }
        }
      }
    }
    return false;
  }


}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Getting the Superclass of an Object
2.Superclass of Object is null
3.The superclass of primitive types is always null
4.Although the type of o2 is an interface, getSuperclass() returns the object's superclass
5.Retrieving other information through the class pointer
6.Allows the user to reflectively inspect an object hierarchy