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