View Javadoc

1   package org.sourceforge.jemm.client;
2   
3   import java.lang.reflect.Field;
4   
5   /**
6    * The unique key that identifies a field from a particular class.
7    * 
8    * All fields need to be keyed based on the declaring class as well as
9    * the name, the combination of which uniquely defines any field.
10   * 
11   * @author Paul Keeble
12   *
13   */
14  public class FieldKey {
15  	private String name;
16  	private Class<?> clazz;
17  	
18  	public FieldKey(Class<?> declaringClass, String name) {
19  		if(declaringClass == null || name == null)
20  			throw new IllegalArgumentException("DeclaringClass/fieldName may not be null");
21  		this.clazz = declaringClass;
22  		this.name = name;
23  	}
24  
25  	public String getName() {
26  		return name;
27  	}
28  
29  	public Class<?> getClazz() {
30  		return clazz;
31  	}
32  	
33  	public String getClazzName() {
34  		return clazz.getName();
35  	}
36  	
37  	public Field getField() throws SecurityException, NoSuchFieldException {
38  		return clazz.getDeclaredField(name);
39  	}
40  	
41  	public boolean equals(Object obj) {
42  		if(!(obj instanceof FieldKey))
43  			return false;
44  		FieldKey rhs = (FieldKey)obj;
45  		return name.equals(rhs.name) 
46  			&& clazz.equals(rhs.clazz);
47  	}
48  	
49  	public int hashCode() {
50  		return name.hashCode() + clazz.hashCode();
51  	}
52  	
53  	public String toString() {
54  		return "FieldKey(" + clazz.getName() + ", " + name +")";
55  	}
56  }