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