FileDocCategorySizeDatePackage
DynamicTypeChecking.javaAPI DocExample3399Sun Dec 14 22:47:38 GMT 2003oreilly.hcj.reflection

DynamicTypeChecking

public class DynamicTypeChecking extends Object
Demonstrates Dynamic class type checking.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.3 $

Fields Summary
Constructors Summary
Methods Summary
public static booleanisNumber(java.lang.Class dataType)
Demonstrates usage of isAssignableFrom on a class object.

param
dataType The data type to check.
return
The value true if the class is a descendant of java.lang.Number or false if it isnt.

		return Number.class.isAssignableFrom(dataType);
	
public static voidmain(java.lang.String[] args)
Demonstration Method.

param
args Command Line arguments.

		System.out.println("Using isAssignableFrom:");
		useAssignable(InstanceOfDemo.OBJECT_SET);
		System.out.println("\nUsing isInstance:");
		useIsInstance(InstanceOfDemo.OBJECT_SET);
	
public static voiduseAssignable(java.util.Set inputSet)
Demonstrates use of isAssignableFrom on objects in a set.

param
inputSet The set to check.

		final Iterator iter = inputSet.iterator();
		Object obj = null;

		while (iter.hasNext()) {
			obj = iter.next();
			if (obj != null) {
				if (Number.class.isAssignableFrom(obj.getClass())) {
					System.out.println(obj);
				}
			}
		}
	
public static voiduseIsInstance(java.util.Set inputSet)
Demonstrates usage of isInstance on objects in a set.

param
inputSet The set to check.

		final Iterator iter = inputSet.iterator();
		Object obj = null;

		while (iter.hasNext()) {
			obj = iter.next();
			if (Number.class.isInstance(obj)) {
				System.out.println(obj);
			}
		}
	
public static voiduseIsInstance2(java.lang.Class dataType, java.util.Set inputSet)
Demonstration method for class comparison based on passed class and object.

param
dataType The data type to check.
param
inputSet The input set to check.
throws
NullPointerException If the data type given is null.

		if (dataType == null) {
			throw new NullPointerException();
		}

		final Iterator iter = inputSet.iterator();
		Object obj = null;

		while (iter.hasNext()) {
			obj = iter.next();
			if (dataType.isInstance(obj)) {
				System.out.println(obj);
			}
		}