FileDocCategorySizeDatePackage
FieldModification.javaAPI DocExample2223Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.reflection

FieldModification

public class FieldModification extends Object
Demonstrates how to set public field objects.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.3 $

Fields Summary
Constructors Summary
Methods Summary
public static voidinitPublicIntFields(java.lang.Object obj)
Sets all int fields in an object to 0.

param
obj The object to operate on.
throws
RuntimeException If there is a reflection problem.

		try {
			Field[] fields = obj.getClass()
				                .getFields();
			for (int idx = 0; idx < fields.length; idx++) {
				if (fields[idx].getType() == int.class) {
					fields[idx].setInt(obj, 0);
				}
			}
		} catch (final IllegalAccessException ex) {
			throw new RuntimeException(ex);
		}
	
public static voidinitPublicIntFields2(java.lang.Object obj)
Sets all int fields in an object to 0.

param
obj The object to operate on.
throws
RuntimeException If there is a reflection problem.

		try {
			final Integer value = new Integer(0);
			Field[] fields = obj.getClass()
				                .getFields();
			for (int idx = 0; idx < fields.length; idx++) {
				if (fields[idx].getType() == int.class) {
					fields[idx].set(obj, value);
				}
			}
		} catch (final IllegalAccessException ex) {
			throw new RuntimeException(ex);
		}
	
public static final voidmain(java.lang.String[] args)
Demo Method.

param
args Command line arguments.

		SomeNumbers value = new SomeNumbers();
		System.out.println("Before: " + value);
		initPublicIntFields(value);
		System.out.println("After: " + value);