1 package org.testng.internal; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Method; 5 6 /** 7 * Encapsulation of either a method or a constructor. 8 * 9 * @author Cedric Beust <cedric@beust.com> 10 */ 11 public class ConstructorOrMethod { 12 13 private Method m_method; 14 private Constructor m_constructor; 15 private boolean m_enabled = true; 16 ConstructorOrMethod(Method m)17 public ConstructorOrMethod(Method m) { 18 m_method = m; 19 } 20 ConstructorOrMethod(Constructor c)21 public ConstructorOrMethod(Constructor c) { 22 m_constructor = c; 23 } 24 getDeclaringClass()25 public Class<?> getDeclaringClass() { 26 return getMethod() != null ? getMethod().getDeclaringClass() : getConstructor().getDeclaringClass(); 27 } 28 getName()29 public String getName() { 30 return getMethod() != null ? getMethod().getName() : getConstructor().getName(); 31 } 32 getParameterTypes()33 public Class[] getParameterTypes() { 34 return getMethod() != null ? getMethod().getParameterTypes() : getConstructor().getParameterTypes(); 35 } 36 getMethod()37 public Method getMethod() { 38 return m_method; 39 } 40 getConstructor()41 public Constructor getConstructor() { 42 return m_constructor; 43 } 44 45 @Override hashCode()46 public int hashCode() { 47 final int prime = 31; 48 int result = 1; 49 result = prime * result + ((getConstructor() == null) ? 0 : getConstructor().hashCode()); 50 result = prime * result + ((getMethod() == null) ? 0 : getMethod().hashCode()); 51 return result; 52 } 53 54 @Override equals(Object obj)55 public boolean equals(Object obj) { 56 if (this == obj) 57 return true; 58 if (obj == null) 59 return false; 60 if (getClass() != obj.getClass()) 61 return false; 62 ConstructorOrMethod other = (ConstructorOrMethod) obj; 63 if (getConstructor() == null) { 64 if (other.getConstructor() != null) 65 return false; 66 } else if (!getConstructor().equals(other.getConstructor())) 67 return false; 68 if (getMethod() == null) { 69 if (other.getMethod() != null) 70 return false; 71 } else if (!getMethod().equals(other.getMethod())) 72 return false; 73 return true; 74 } 75 setEnabled(boolean enabled)76 public void setEnabled(boolean enabled) { 77 m_enabled = enabled; 78 } 79 getEnabled()80 public boolean getEnabled() { 81 return m_enabled; 82 } 83 84 @Override toString()85 public String toString() { 86 if (m_method != null) return m_method.toString(); 87 else return m_constructor.toString(); 88 } 89 } 90