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.reflect.Member;
7   import java.util.ArrayList;
8   import java.util.Collections;
9   import java.util.List;
10  
11  /**
12   * Represents a Class Member in a UsageGraph.
13   */
14  public abstract class MemberNode<S extends MemberNode<S, T>, T extends Member> implements NodeVisitorAcceptor,
15  		Comparable<S> {
16  	protected final ClassNode classNode;
17  
18  	protected final String name;
19  
20  	protected final String desc;
21  
22  	private List<MemberNode> uses;
23  
24  	private List<MemberNode> users;
25  
26  	private Object decoration;
27  
28  	public MemberNode(ClassNode classNode, String name, String desc) {
29  		this.classNode = classNode;
30  		this.name = name;
31  		this.desc = desc;
32  	}
33  
34  	public Object accept(NodeVisitor visitor, Object data) {
35  		visitor.visitUses(this, data);
36  		visitor.visitUsers(this, data);
37  		return data;
38  	}
39  
40  	public ClassNode getClassNode() {
41  		return classNode;
42  	}
43  
44  	public String getName() {
45  		return name;
46  	}
47  
48  	public String getDesc() {
49  		return desc;
50  	}
51  
52  	public abstract T getMember();
53  
54  	public void addUse(MemberNode use) {
55  		if (uses == null) {
56  			uses = new ArrayList<MemberNode>(1);
57  		}
58  		if (!uses.contains(use)) {
59  			uses.add(use);
60  		}
61  	}
62  
63  	public List<MemberNode> getUses() {
64  		return uses != null ? uses : Collections.<MemberNode> emptyList();
65  	}
66  
67  	public void addUser(MemberNode user) {
68  		if (users == null) {
69  			users = new ArrayList<MemberNode>(1);
70  		}
71  		if (!users.contains(user)) {
72  			users.add(user);
73  		}
74  	}
75  
76  	public List<MemberNode> getUsers() {
77  		return users != null ? users : Collections.<MemberNode> emptyList();
78  	}
79  
80  	public String toString() {
81  		return name + " " + desc;
82  	}
83  
84  	public String toStringLong() {
85  		return getMember().toString();
86  	}
87  
88  	public abstract boolean equals(Object that);
89  
90  	@SuppressWarnings("PMD.SuspiciousEqualsMethodName")
91  	public boolean equals(S that) {
92  		return equals(that.name, that.desc);
93  	}
94  
95  	public boolean equals(String name, String desc) {
96  		return this.name.equals(name) && this.desc.equals(desc);
97  	}
98  
99  	public int hashCode() {
100 		return name.hashCode() + desc.hashCode();
101 	}
102 }