View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.dcd.graph;
5   
6   import java.lang.ref.WeakReference;
7   import java.lang.reflect.Field;
8   
9   import net.sourceforge.pmd.dcd.ClassLoaderUtil;
10  
11  /**
12   * Represents a Class Field in a UsageGraph.
13   */
14  @SuppressWarnings("PMD.OverrideBothEqualsAndHashcode")
15  public class FieldNode extends MemberNode<FieldNode, Field> {
16  
17  	private WeakReference<Field> fieldReference;
18  
19  	public FieldNode(ClassNode classNode, String name, String desc) {
20  		super(classNode, name, desc);
21  		getMember();
22  	}
23  
24  	public Field getMember() {
25  		Field field = fieldReference == null ? null : fieldReference.get();
26  		if (field == null) {
27  			field = ClassLoaderUtil.getField(getClassNode().getType(), name);
28  			this.fieldReference = new WeakReference<Field>(field);
29  		}
30  		return field;
31  	}
32  
33  	public int compareTo(FieldNode that) {
34  		return this.name.compareTo(that.name);
35  	}
36  
37  	public boolean equals(Object obj) {
38  		if (obj instanceof FieldNode) {
39  			FieldNode that = (FieldNode)obj;
40  			return super.equals(that);
41  		}
42  		return false;
43  	}
44  }