FileDocCategorySizeDatePackage
HiddenFieldModification.javaAPI DocExample1672Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.reflection

HiddenFieldModification

public class HiddenFieldModification 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 voidinitIntFields(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()
				                .getDeclaredFields();
			for (int idx = 0; idx < fields.length; idx++) {
				if (fields[idx].getType() == int.class) {
					fields[idx].setAccessible(true);
					fields[idx].setInt(obj, 0);
				}
			}
		} 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);
		initIntFields(value);
		System.out.println("After: " + value);