001: package net.sourceforge.pmd.dcd.graph;
002:
003: import java.lang.ref.WeakReference;
004: import java.util.ArrayList;
005: import java.util.Collections;
006: import java.util.List;
007:
008: import net.sourceforge.pmd.dcd.ClassLoaderUtil;
009:
010: /**
011: * Represents a Class in a UsageGraph. Contains lists of FieldNodes,
012: * ConstructorNodes, and MethodNodes.
013: */
014: public class ClassNode implements NodeVisitorAcceptor,
015: Comparable<ClassNode> {
016:
017: private final String name;
018:
019: private WeakReference<Class<?>> typeReference;
020:
021: private List<FieldNode> fieldNodes;
022:
023: private List<ConstructorNode> constructorNodes;
024:
025: private List<MethodNode> methodNodes;
026:
027: public ClassNode(String name) {
028: this .name = name;
029: }
030:
031: public Object accept(NodeVisitor visitor, Object data) {
032: visitor.visitFields(this , data);
033: visitor.visitConstructors(this , data);
034: visitor.visitMethods(this , data);
035: return data;
036: }
037:
038: public String getName() {
039: return name;
040: }
041:
042: public Class<?> getType() {
043: Class<?> type = typeReference == null ? null : typeReference
044: .get();
045: if (type == null) {
046: type = ClassLoaderUtil.getClass(ClassLoaderUtil
047: .fromInternalForm(name));
048: typeReference = new WeakReference<Class<?>>(type);
049: }
050: return type;
051: }
052:
053: public FieldNode defineField(String name, String desc) {
054: if (fieldNodes == null) {
055: fieldNodes = new ArrayList<FieldNode>(1);
056: }
057: for (FieldNode fieldNode : fieldNodes) {
058: if (fieldNode.equals(name, desc)) {
059: return fieldNode;
060: }
061: }
062: FieldNode fieldNode = new FieldNode(this , name, desc);
063: fieldNodes.add(fieldNode);
064: return fieldNode;
065: }
066:
067: public ConstructorNode defineConstructor(String name, String desc) {
068: if (constructorNodes == null) {
069: constructorNodes = new ArrayList<ConstructorNode>(1);
070: }
071: for (ConstructorNode constructorNode : constructorNodes) {
072: if (constructorNode.equals(name, desc)) {
073: return constructorNode;
074: }
075: }
076:
077: ConstructorNode constructorNode = new ConstructorNode(this ,
078: name, desc);
079: constructorNodes.add(constructorNode);
080: return constructorNode;
081: }
082:
083: public MethodNode defineMethod(String name, String desc) {
084: if (methodNodes == null) {
085: methodNodes = new ArrayList<MethodNode>(1);
086: }
087: for (MethodNode methodNode : methodNodes) {
088: if (methodNode.equals(name, desc)) {
089: return methodNode;
090: }
091: }
092:
093: MethodNode methodNode = new MethodNode(this , name, desc);
094: methodNodes.add(methodNode);
095: return methodNode;
096: }
097:
098: public List<FieldNode> getFieldNodes() {
099: return fieldNodes != null ? fieldNodes : Collections
100: .<FieldNode> emptyList();
101: }
102:
103: public List<ConstructorNode> getConstructorNodes() {
104: return constructorNodes != null ? constructorNodes
105: : Collections.<ConstructorNode> emptyList();
106: }
107:
108: public List<MethodNode> getMethodNodes() {
109: return methodNodes != null ? methodNodes : Collections
110: .<MethodNode> emptyList();
111: }
112:
113: public int compareTo(ClassNode that) {
114: return this .name.compareTo(that.name);
115: }
116:
117: public boolean equals(Object obj) {
118: if (obj instanceof ClassNode) {
119: return this .name.equals(((ClassNode) obj).name);
120: }
121: return false;
122: }
123:
124: public int hashCode() {
125: return name.hashCode();
126: }
127: }
|