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.Method;
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 Method in a UsageGraph.
16   */
17  @SuppressWarnings("PMD.OverrideBothEqualsAndHashcode")
18  public class MethodNode extends MemberNode<MethodNode, Method> {
19  
20  	private WeakReference<Method> methodReference;
21  
22  	public MethodNode(ClassNode classNode, String name, String desc) {
23  		super(classNode, name, desc);
24  		// getMember();
25  	}
26  
27  	public Method getMember() {
28  		Method method = methodReference == null ? null : methodReference.get();
29  		if (method == null) {
30  			SignatureReader signatureReader = new SignatureReader(desc);
31  			TypeSignatureVisitor visitor = new TypeSignatureVisitor();
32  			signatureReader.accept(visitor);
33  			method = ClassLoaderUtil.getMethod(super.getClassNode().getType(), name, visitor.getMethodParameterTypes());
34  			methodReference = new WeakReference<Method>(method);
35  		}
36  		return method;
37  	}
38  
39  	public int compareTo(MethodNode that) {
40  		// Order by method name
41  		int cmp = this.getName().compareTo(that.getName());
42  		if (cmp == 0) {
43  			// Order by parameter list length
44  			cmp = this.getMember().getParameterTypes().length - that.getMember().getParameterTypes().length;
45  			if (cmp == 0) {
46  				// Order by parameter class name
47  				for (int i = 0; i < this.getMember().getParameterTypes().length; i++) {
48  					cmp = this.getMember().getParameterTypes()[i].getName().compareTo(
49  							that.getMember().getParameterTypes()[i].getName());
50  					if (cmp != 0) {
51  						break;
52  					}
53  				}
54  			}
55  		}
56  		return cmp;
57  	}
58  
59  	public boolean equals(Object obj) {
60  		if (obj instanceof MethodNode) {
61  			MethodNode that = (MethodNode)obj;
62  			return super.equals(that);
63  		}
64  		return false;
65  	}
66  }