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.util.ArrayList; 8 import java.util.Collections; 9 import java.util.List; 10 11 import net.sourceforge.pmd.dcd.ClassLoaderUtil; 12 13 /** 14 * Represents a Class in a UsageGraph. Contains lists of FieldNodes, 15 * ConstructorNodes, and MethodNodes. 16 */ 17 public class ClassNode implements NodeVisitorAcceptor, Comparable<ClassNode> { 18 19 private final String name; 20 21 private WeakReference<Class<?>> typeReference; 22 23 private List<FieldNode> fieldNodes; 24 25 private List<ConstructorNode> constructorNodes; 26 27 private List<MethodNode> methodNodes; 28 29 public ClassNode(String name) { 30 this.name = name; 31 } 32 33 public Object accept(NodeVisitor visitor, Object data) { 34 visitor.visitFields(this, data); 35 visitor.visitConstructors(this, data); 36 visitor.visitMethods(this, data); 37 return data; 38 } 39 40 public String getName() { 41 return name; 42 } 43 44 public Class<?> getType() { 45 Class<?> type = typeReference == null ? null : typeReference.get(); 46 if (type == null) { 47 type = ClassLoaderUtil.getClass(ClassLoaderUtil.fromInternalForm(name)); 48 typeReference = new WeakReference<Class<?>>(type); 49 } 50 return type; 51 } 52 53 public FieldNode defineField(String name, String desc) { 54 if (fieldNodes == null) { 55 fieldNodes = new ArrayList<FieldNode>(1); 56 } 57 for (FieldNode fieldNode : fieldNodes) { 58 if (fieldNode.equals(name, desc)) { 59 return fieldNode; 60 } 61 } 62 FieldNode fieldNode = new FieldNode(this, name, desc); 63 fieldNodes.add(fieldNode); 64 return fieldNode; 65 } 66 67 public ConstructorNode defineConstructor(String name, String desc) { 68 if (constructorNodes == null) { 69 constructorNodes = new ArrayList<ConstructorNode>(1); 70 } 71 for (ConstructorNode constructorNode : constructorNodes) { 72 if (constructorNode.equals(name, desc)) { 73 return constructorNode; 74 } 75 } 76 77 ConstructorNode constructorNode = new ConstructorNode(this, name, desc); 78 constructorNodes.add(constructorNode); 79 return constructorNode; 80 } 81 82 public MethodNode defineMethod(String name, String desc) { 83 if (methodNodes == null) { 84 methodNodes = new ArrayList<MethodNode>(1); 85 } 86 for (MethodNode methodNode : methodNodes) { 87 if (methodNode.equals(name, desc)) { 88 return methodNode; 89 } 90 } 91 92 MethodNode methodNode = new MethodNode(this, name, desc); 93 methodNodes.add(methodNode); 94 return methodNode; 95 } 96 97 public List<FieldNode> getFieldNodes() { 98 return fieldNodes != null ? fieldNodes : Collections.<FieldNode> emptyList(); 99 } 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 }