View Javadoc

1   package org.sourceforge.jemm.database;
2   
3   import java.io.Serializable;
4   
5   /**
6    * A fieldInfo represents all the information to do with a Field in a Java class.
7    * 
8    * The name, its type for storage purposes and the class which originally declared the
9    * field. The class is stored to counter the potential problems shown by the following where
10   * both X should be stored twice by JEMM:
11   * 
12   * public class A {
13   *   private int X;
14   * }
15   * 
16   * public class B extends A {
17   *   private int X;
18   * }
19   * 
20   * @see ClassInfo
21   * 
22   * @author Rory Graves
23   * @author Paul Keeble
24   */
25  public class FieldInfo implements Serializable {
26  	
27  	private static final long serialVersionUID = 1L;
28  
29  	/** The name of the field */
30  	public final String fieldName;
31  	/** The type of the field */
32  	public final FieldType fieldType;
33  	/** The declaring class of the field */
34  	public final String className;
35  
36  	/**
37  	 * Create a FieldInfo to represent the given class field.
38  	 * @param fieldName The name of the field.
39  	 * @param className The declaring class of the field.
40  	 * @param fieldType The type of the field.
41  	 */
42  	public FieldInfo(String fieldName, String className, FieldType fieldType) {
43  		if (fieldName == null || fieldType == null || className == null)
44  			throw new IllegalArgumentException(
45  					"fieldName and fieldType must both be set");
46  
47  		this.fieldName = fieldName;
48  		this.className = className;
49  		this.fieldType = fieldType;
50  	}
51  
52  	/**
53  	 * Creats a FieldInfo to represent the given class field.
54  	 * @param fieldName The name of the field.
55  	 * @param classType The declaring class type
56  	 * @param fieldType The type of the field.
57  	 */
58  	public FieldInfo(String fieldName, Class<?> classType, FieldType fieldType) {
59  		this(fieldName, classType.getName(), fieldType);
60  	}
61  
62  	@Override
63  	public int hashCode() {
64  		final int prime = 31;
65  		int result = 1;
66  		result = prime * result + fieldName.hashCode();
67  		result = prime * result + fieldType.hashCode();
68  		result = prime * result + className.hashCode();
69  		return result;
70  	}
71  
72  	@Override
73  	public boolean equals(Object obj) {
74  		if (this == obj)
75  			return true;
76  		if (obj == null)
77  			return false;
78  		if (getClass() != obj.getClass())
79  			return false;
80  		FieldInfo other = (FieldInfo) obj;
81  
82  		return fieldName.equals(other.fieldName)
83  				&& fieldType.equals(other.fieldType)
84  				&& className.equals(other.className);
85  	}
86  
87  	@Override
88  	public String toString() {
89  		StringBuilder sb = new StringBuilder("FieldInfo(");
90  		sb.append(fieldName).append(",");
91  		sb.append(className).append(",");
92  		sb.append(fieldType).append(')');
93  
94  		return sb.toString();
95  	}
96  }