Example usage for java.lang Class getSuperclass

List of usage examples for java.lang Class getSuperclass

Introduction

In this page you can find the example usage for java.lang Class getSuperclass.

Prototype

@HotSpotIntrinsicCandidate
public native Class<? super T> getSuperclass();

Source Link

Document

Returns the Class representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class .

Usage

From source file:Main.java

/**
 * Returns the field with the given name on the type lowest in the class hierarchy.
 * //from  www  .  ja  v a 2s .co m
 * @param name The name of the field to get
 * @param type The class on which to search for the field.
 * @return The field with this name lowest in the hierarchy, otherwise null if the field does not exist in the class hierarchy.
 */
public static Field getField(String name, Class<?> type) {
    for (Field field : type.getDeclaredFields()) {
        if (field.getName().equals(name)) {
            return field;
        }
    }

    if (type.getSuperclass() != null) {
        return getField(name, type.getSuperclass());
    }

    return null;
}

From source file:com.lucidtechnics.blackboard.util.Utility.java

public static ArrayList getAllTypes(ArrayList _searchDomainList, ArrayList _allTypesList) {
    for (int i = 0; i < _searchDomainList.size(); i++) {
        Class searchClass = (Class) _searchDomainList.get(i);
        _searchDomainList.remove(searchClass);

        Class superClass = searchClass.getSuperclass();

        if (superClass != null && (_allTypesList.contains(superClass) == false)) {
            _searchDomainList.add(superClass);
            _allTypesList.add(superClass);
        }//from ww  w .j a va2  s.c om

        Class[] interfaceArray = searchClass.getInterfaces();

        if (interfaceArray != null) {
            for (int j = 0; j < interfaceArray.length; j++) {
                if (interfaceArray[j] != null && (_allTypesList.contains(interfaceArray[j]) == false)) {
                    _searchDomainList.add(interfaceArray[j]);
                    _allTypesList.add(interfaceArray[j]);
                }
            }
        }
    }

    if (_searchDomainList.isEmpty() == false) {
        _allTypesList = getAllTypes(_searchDomainList, _allTypesList);
    }

    return _allTypesList;
}

From source file:fit.Binding.java

private static Field[] getAllDeclaredFields(Class<?> clazz) {
    if (clazz.getSuperclass() != null) {
        return (Field[]) ArrayUtils.addAll(getAllDeclaredFields(clazz.getSuperclass()),
                clazz.getDeclaredFields());
    } else {//from   ww  w . ja  v a  2 s.  c  o m
        return clazz.getDeclaredFields();
    }
}

From source file:com.adaptris.core.marshaller.xstream.XStreamUtils.java

/**
* Given a Field of a Class this method will return a Set of a number of
* possible fully qualified reference names for the field. This would be
* based on the class hierarchy eg currentClass:field, parentClass:field,
* grandparentClass:field etc.// ww w  .  jav a  2  s .c  o m
* 
* @param clazz - Parent Class of the given field
* @param field - Given field to process
* @param separator - class-field separator
* @return - Set<String> of possible paths for the field
*/
public static Collection<String> createParentFields(Class<?> clazz, String field, String separator) {
    Set<String> result = new HashSet<String>();
    result.add(clazz.getCanonicalName() + separator + field);
    Class<?> c = clazz;
    while (c.getSuperclass() != null) {
        c = c.getSuperclass();
        result.add(c.getCanonicalName() + separator + field);
    }
    return result;
}

From source file:com.thruzero.common.core.locator.LocatorUtils.java

/**
 * For a given childClass, start at the root base class and read the sections of each sub-class, walking down the class hierarchy
 * to the given childClass. The result is that all parameters from all inherited sections, including the given childClass,
 * are read with each subclass overwriting the common keys of any parent section. If given, optionalBaseInterface will be used as the
 * root base and placed at the top of the inheritance stack.
 *
 * @param sectionName// w w w.ja v  a  2  s.  c  om
 * @param initStrategy
 * @param childClass
 */
public static StringMap getInheritedParameters(final InitializationStrategy initStrategy,
        final Class<?> childClass, final Class<?> optionalBaseInterface) {
    StringMap result = new StringMap();
    List<Class<?>> inheritanceList = new ArrayList<Class<?>>();

    Class<?> nextClass = childClass;
    while (nextClass != null) {
        inheritanceList.add(nextClass);
        nextClass = nextClass.getSuperclass();
    }
    inheritanceList.remove(inheritanceList.size() - 1); // remove java.lang.Object

    if (optionalBaseInterface != null) {
        inheritanceList.add(optionalBaseInterface);
    }

    ListIterator<Class<?>> iter = inheritanceList.listIterator(inheritanceList.size());
    StringMap params;
    while (iter.hasPrevious()) {
        params = initStrategy.getSectionAsStringMap(iter.previous().getName());
        if (params != null) {
            result.putAll(params);
        }
    }

    return result;
}

From source file:Main.java

public static Class getDeclaredMethodClass(Class clazz, String methodName, Class... params) {
    do {// www  .  ja  va 2s . c  o m
        try {
            clazz.getDeclaredMethod(methodName, params);
            return clazz;
        } catch (NoSuchMethodException e) {
            clazz = clazz.getSuperclass();
            if (clazz == null) {
                return null;
            }
        }
    } while (true);
}

From source file:net.buffalo.protocal.util.ClassUtil.java

private static HashMap getFieldMap(Class cl) {
    HashMap fieldMap = new HashMap();
    for (; cl != null; cl = cl.getSuperclass()) {
        Field[] fields = cl.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers()))
                continue;
            field.setAccessible(true);/*from  w  ww.jav  a2s .  co m*/
            fieldMap.put(field.getName(), field);
        }
    }

    return fieldMap;
}

From source file:Main.java

public static Class<?> getClassByPropertyName(Class<?> clazz, String propertyName) {
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getName().equals(propertyName)) {
            return field.getType();
        }/*from ww w.  j ava2  s .  c  o m*/
    }
    Class<?> superClass = clazz.getSuperclass();
    if (!superClass.equals(Object.class)) {
        return getClassByPropertyName(superClass, propertyName);
    }
    return null;
}

From source file:Main.java

public static List<Class<?>> getSuperClasss(Class<?> sourceClass, boolean isAddCurrentClass) {
    List<Class<?>> classList = new ArrayList<Class<?>>();
    Class<?> classs;/*from   w w  w.j  av a 2s.c o  m*/
    if (isAddCurrentClass) {
        classs = sourceClass;
    } else {
        classs = sourceClass.getSuperclass();
    }
    while (classs != null) {
        classList.add(classs);
        classs = classs.getSuperclass();
    }
    return classList;
}

From source file:com.examples.with.different.packagename.ClassHierarchyIncludingInterfaces.java

public static Iterable<Class<?>> hierarchy(final Class<?> type, final Interfaces interfacesBehavior) {
    final Iterable<Class<?>> classes = new Iterable<Class<?>>() {

        @Override//from   ww w . j  a  va2s  .  com
        public Iterator<Class<?>> iterator() {
            final MutableObject<Class<?>> next = new MutableObject<Class<?>>(type);
            return new Iterator<Class<?>>() {

                @Override
                public boolean hasNext() {
                    return next.getValue() != null;
                }

                @Override
                public Class<?> next() {
                    final Class<?> result = next.getValue();
                    next.setValue(result.getSuperclass());
                    return result;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }

            };
        }

    };
    if (interfacesBehavior != Interfaces.INCLUDE) {
        return classes;
    }
    return new Iterable<Class<?>>() {

        @Override
        public Iterator<Class<?>> iterator() {
            final Set<Class<?>> seenInterfaces = new HashSet<Class<?>>();
            final Iterator<Class<?>> wrapped = classes.iterator();

            return new Iterator<Class<?>>() {
                Iterator<Class<?>> interfaces = Collections.<Class<?>>emptySet().iterator();

                @Override
                public boolean hasNext() {
                    return interfaces.hasNext() || wrapped.hasNext();
                }

                @Override
                public Class<?> next() {
                    if (interfaces.hasNext()) {
                        final Class<?> nextInterface = interfaces.next();
                        seenInterfaces.add(nextInterface);
                        return nextInterface;
                    }
                    final Class<?> nextSuperclass = wrapped.next();
                    final Set<Class<?>> currentInterfaces = new LinkedHashSet<Class<?>>();
                    walkInterfaces(currentInterfaces, nextSuperclass);
                    interfaces = currentInterfaces.iterator();
                    return nextSuperclass;
                }

                private void walkInterfaces(final Set<Class<?>> addTo, final Class<?> c) {
                    for (final Class<?> iface : c.getInterfaces()) {
                        if (!seenInterfaces.contains(iface)) {
                            addTo.add(iface);
                        }
                        walkInterfaces(addTo, iface);
                    }
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }

            };
        }
    };
}