001 /**
002 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003 */
004 package net.sourceforge.pmd.dcd.graph;
005
006 import java.lang.ref.WeakReference;
007 import java.util.ArrayList;
008 import java.util.Collections;
009 import java.util.List;
010
011 import net.sourceforge.pmd.dcd.ClassLoaderUtil;
012
013 /**
014 * Represents a Class in a UsageGraph. Contains lists of FieldNodes,
015 * ConstructorNodes, and MethodNodes.
016 */
017 public class ClassNode implements NodeVisitorAcceptor, Comparable<ClassNode> {
018
019 private final String name;
020
021 private WeakReference<Class<?>> typeReference;
022
023 private List<FieldNode> fieldNodes;
024
025 private List<ConstructorNode> constructorNodes;
026
027 private List<MethodNode> methodNodes;
028
029 public ClassNode(String name) {
030 this.name = name;
031 }
032
033 public Object accept(NodeVisitor visitor, Object data) {
034 visitor.visitFields(this, data);
035 visitor.visitConstructors(this, data);
036 visitor.visitMethods(this, data);
037 return data;
038 }
039
040 public String getName() {
041 return name;
042 }
043
044 public Class<?> getType() {
045 Class<?> type = typeReference == null ? null : typeReference.get();
046 if (type == null) {
047 type = ClassLoaderUtil.getClass(ClassLoaderUtil.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, name, desc);
078 constructorNodes.add(constructorNode);
079 return constructorNode;
080 }
081
082 public MethodNode defineMethod(String name, String desc) {
083 if (methodNodes == null) {
084 methodNodes = new ArrayList<MethodNode>(1);
085 }
086 for (MethodNode methodNode : methodNodes) {
087 if (methodNode.equals(name, desc)) {
088 return methodNode;
089 }
090 }
091
092 MethodNode methodNode = new MethodNode(this, name, desc);
093 methodNodes.add(methodNode);
094 return methodNode;
095 }
096
097 public List<FieldNode> getFieldNodes() {
098 return fieldNodes != null ? fieldNodes : Collections.<FieldNode> emptyList();
099 }
100
101 public List<ConstructorNode> getConstructorNodes() {
102 return constructorNodes != null ? constructorNodes : Collections.<ConstructorNode> emptyList();
103 }
104
105 public List<MethodNode> getMethodNodes() {
106 return methodNodes != null ? methodNodes : Collections.<MethodNode> emptyList();
107 }
108
109 public int compareTo(ClassNode that) {
110 return this.name.compareTo(that.name);
111 }
112
113 public boolean equals(Object obj) {
114 if (obj instanceof ClassNode) {
115 return this.name.equals(((ClassNode)obj).name);
116 }
117 return false;
118 }
119
120 public int hashCode() {
121 return name.hashCode();
122 }
123 }
|