org.jutil.java.reflect
Class Classes

java.lang.Object
  |
  +--org.jutil.java.reflect.Classes

public class Classes
extends java.lang.Object

Utility methods for class reflection.

Version:
$Revision: 1.8 $
Author:
Jan Dockx, Marko van Dooren

Field Summary
static java.lang.String CVS_REVISION
           
static Filter PACKAGE_ACCESS_FILTER
           
static Filter PRIVATE_ACCESS_FILTER
           
static Filter PROTECTED_ACCESS_FILTER
           
static Filter PUBLIC_ACCESS_FILTER
           
 
Constructor Summary
Classes()
           
 
Method Summary
static boolean areInSamePackage(java.lang.Class t1, java.lang.Class t2)
          // The types to compare are not null.
pre (t1 != null) && (t2 != null);

// The result is true if the 2 given classes are defined
// in the same package.
static java.util.Set getImmediateSuperTypes(java.lang.Class clazz)
          // The given Class must be effective
pre clazz != null;

// The set contains elements of type Class
post (\forall Object o; \result.contains(o); o instanceof Class);
// The set contains no null references
post (\forall Object o; \result.contains(o); o != null);
// The set only contains immediate supertypes of .
post (\forall Class c; \result.contains(c);
c == clazz.getSuperClass() || clazz.getInterfaces().contains(c));
// If the this method is applied to class Class, the result is the empty set.
post (clazz == Class.class) ==> \result.isEmpty();
static java.util.SortedSet getSuperClasses(java.lang.Class clazz)
          // The given Class must be effective
pre clazz != null;

// The set contains elements of type Class
post (\forall Object o; \result.contains(o); o instanceof Class);
// The set contains no null references
post (\forall Class c; \result.contains(c); c != null);
// The set contains only classes, no interfaces.
post (\forall Class c; \result.contains(c); ! c.isInterface());
// The set contains only super classes of clazz.
post (\forall Class c; \result.contains(c); c.isAssignableFrom(clazz));
// The set contains all the super classes of .
post (\forall Class c; (c.isAssignableFrom(clazz)) && (! c.isInterface()); \result.contains(c));
// If is an interface, the set is empty.
post (clazz.isInterface()) ==> \result.size() == 0;

post (* The order of the result set is that subclasses are smaller
than superclasses.*);
static java.util.Set getSuperTypes(java.lang.Class clazz)
          // The given Class must be effective
pre clazz != null;

// The set contains elements of type Class
post (\forall Object o; \result.contains(o); o instanceof Class);
// The set contains no null references
post (\forall Class c; \result.contains(c); c != null);
// The set contains only super types of clazz.
post (\forall Class c; \result.contains(c); c.isAssignableFrom(clazz));
// The set contains .
post \result.contains(clazz);
//If the this method is applied to class Object, the result
//is the singleton {Object}.
post (clazz == Object.class) ==> (\result.contains(Object.class)) &&
(\result.size() == 1);
static java.lang.String packageName(java.lang.Class t)
          // The given class must be effective.
pre t != null;

// The result is never null.
post \result != null;
// The result never contains illegal characters.
post Character.isJavaIdentifierStart(\result.charAt(1));
post (\forall int i; (i>0) && (i<\result.length()); Character.isJavaIdentifierPart(\result.charAt(i)));
The fully qualified package name of type .
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CVS_REVISION

public static final java.lang.String CVS_REVISION

PUBLIC_ACCESS_FILTER

public static final Filter PUBLIC_ACCESS_FILTER

PROTECTED_ACCESS_FILTER

public static final Filter PROTECTED_ACCESS_FILTER

PACKAGE_ACCESS_FILTER

public static final Filter PACKAGE_ACCESS_FILTER

PRIVATE_ACCESS_FILTER

public static final Filter PRIVATE_ACCESS_FILTER
Constructor Detail

Classes

public Classes()
Method Detail

getImmediateSuperTypes

public static java.util.Set getImmediateSuperTypes(java.lang.Class clazz)
// The given Class must be effective
pre clazz != null;

// The set contains elements of type Class
post (\forall Object o; \result.contains(o); o instanceof Class);
// The set contains no null references
post (\forall Object o; \result.contains(o); o != null);
// The set only contains immediate supertypes of .
post (\forall Class c; \result.contains(c);
c == clazz.getSuperClass() || clazz.getInterfaces().contains(c));
// If the this method is applied to class Class, the result is the empty set.
post (clazz == Class.class) ==> \result.isEmpty();

All immediate syper types of . These are the interfaces and classes mentioned in the implements and extends clauses, or Object when there is no extends.

If the this method is applied to class Class, the result is the empty set.

Parameters:
clazz - The class to get all immediate super types for.

getSuperTypes

public static java.util.Set getSuperTypes(java.lang.Class clazz)
// The given Class must be effective
pre clazz != null;

// The set contains elements of type Class
post (\forall Object o; \result.contains(o); o instanceof Class);
// The set contains no null references
post (\forall Class c; \result.contains(c); c != null);
// The set contains only super types of clazz.
post (\forall Class c; \result.contains(c); c.isAssignableFrom(clazz));
// The set contains .
post \result.contains(clazz);
//If the this method is applied to class Object, the result
//is the singleton {Object}.
post (clazz == Object.class) ==> (\result.contains(Object.class)) &&
(\result.size() == 1);

All syper types of . This is the transitive closure of the immediate super types of .

Parameters:
clazz - The class to get all super types for.

getSuperClasses

public static java.util.SortedSet getSuperClasses(java.lang.Class clazz)
// The given Class must be effective
pre clazz != null;

// The set contains elements of type Class
post (\forall Object o; \result.contains(o); o instanceof Class);
// The set contains no null references
post (\forall Class c; \result.contains(c); c != null);
// The set contains only classes, no interfaces.
post (\forall Class c; \result.contains(c); ! c.isInterface());
// The set contains only super classes of clazz.
post (\forall Class c; \result.contains(c); c.isAssignableFrom(clazz));
// The set contains all the super classes of .
post (\forall Class c; (c.isAssignableFrom(clazz)) && (! c.isInterface()); \result.contains(c));
// If is an interface, the set is empty.
post (clazz.isInterface()) ==> \result.size() == 0;

post (* The order of the result set is that subclasses are smaller
than superclasses.*);

All super classes of , including and Object.

If is an interface, the set is empty.

Parameters:
clazz - The class to get all super classes for. //MvDMvDMvD : The following 2 postconditions are not required I think.

areInSamePackage

public static boolean areInSamePackage(java.lang.Class t1,
                                       java.lang.Class t2)
// The types to compare are not null.
pre (t1 != null) && (t2 != null);

// The result is true if the 2 given classes are defined
// in the same package. False otherwise.
post \result == (t1.getPackage().getName().equals(t2.getPackage().getName()));
// null is returned if no package can be found for one
// of the classes.
post (t1.getPackage() == null || t2.getPackage() == null) ==> \result == false
Check whether and are defined in the same package.
Parameters:
t1 - The first type to compare.
t2 - The second type to compare.

packageName

public static java.lang.String packageName(java.lang.Class t)
// The given class must be effective.
pre t != null;

// The result is never null.
post \result != null;
// The result never contains illegal characters.
post Character.isJavaIdentifierStart(\result.charAt(1));
post (\forall int i; (i>0) && (i<\result.length()); Character.isJavaIdentifierPart(\result.charAt(i)));
The fully qualified package name of type . The empty string represents the unnamed package.
Parameters:
t - The type to get the fully qualified package name for. //MvDMvDMvD : for JDK 1.2+ this can be specified in terms of getPackage().getName()
Returns:
The fully qualified package name of . The empty string represents the unnamed package.