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