01: package net.sourceforge.pmd.dcd.graph;
02:
03: import java.lang.ref.WeakReference;
04: import java.lang.reflect.Constructor;
05:
06: import net.sourceforge.pmd.dcd.ClassLoaderUtil;
07: import net.sourceforge.pmd.dcd.asm.TypeSignatureVisitor;
08:
09: import org.objectweb.asm.signature.SignatureReader;
10:
11: /**
12: * Represents a Class Constructor in a UsageGraph.
13: */
14: public class ConstructorNode extends
15: MemberNode<ConstructorNode, Constructor<?>> {
16:
17: private WeakReference<Constructor<?>> constructorReference;
18:
19: public ConstructorNode(ClassNode classNode, String name, String desc) {
20: super (classNode, name, desc);
21: // getMember();
22: }
23:
24: public boolean isStaticInitializer() {
25: return ClassLoaderUtil.CLINIT.equals(name);
26: }
27:
28: public boolean isInstanceInitializer() {
29: return ClassLoaderUtil.INIT.equals(name);
30: }
31:
32: public Constructor<?> getMember() {
33: if (ClassLoaderUtil.CLINIT.equals(name)) {
34: return null;
35: } else {
36: Constructor<?> constructor = constructorReference == null ? null
37: : constructorReference.get();
38: if (constructor == null) {
39: SignatureReader signatureReader = new SignatureReader(
40: desc);
41: TypeSignatureVisitor visitor = new TypeSignatureVisitor();
42: signatureReader.accept(visitor);
43: constructor = ClassLoaderUtil.getConstructor(super
44: .getClassNode().getType(), name, visitor
45: .getMethodParameterTypes());
46: constructorReference = new WeakReference<Constructor<?>>(
47: constructor);
48: }
49: return constructor;
50: }
51: }
52:
53: public String toStringLong() {
54: if (ClassLoaderUtil.CLINIT.equals(name)) {
55: return name;
56: } else {
57: return super .toStringLong();
58: }
59: }
60:
61: public int compareTo(ConstructorNode that) {
62: // Order by name
63: int cmp = this .getName().compareTo(that.getName());
64: if (cmp == 0) {
65: // Order by parameter list length
66: cmp = this .getMember().getParameterTypes().length
67: - that.getMember().getParameterTypes().length;
68: if (cmp == 0) {
69: // Order by parameter class name
70: for (int i = 0; i < this .getMember()
71: .getParameterTypes().length; i++) {
72: cmp = this .getMember().getParameterTypes()[i]
73: .getName().compareTo(
74: that.getMember()
75: .getParameterTypes()[i]
76: .getName());
77: if (cmp != 0) {
78: break;
79: }
80: }
81: }
82: }
83: return cmp;
84: }
85:
86: public boolean equals(Object obj) {
87: if (obj instanceof ConstructorNode) {
88: ConstructorNode that = (ConstructorNode) obj;
89: return super .equals(that);
90: }
91: return false;
92: }
93: }
|