1
2
3
4 package himock.util;
5
6
7 import java.lang.reflect.Method;
8
9 /***
10 * @author Chen Peng ,chen56@msn.com
11 */
12 public class ClassUtil {
13 private static final char INNER_CLASS_SEPARATOR_CHAR = '$';
14
15 public static boolean isContainsThisSignatureDeclaredMethod(Class clazz,
16 Method method) {
17 try {
18 clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
19 } catch (NoSuchMethodException e) {
20 return false;
21 }
22 return true;
23 }
24
25 public static Method getSameSignaturegetDeclaredMethod(Class clazz, Method method) {
26 try {
27 return clazz.getDeclaredMethod(method.getName(),method.getParameterTypes());
28 } catch (SecurityException e) {
29 throw new RuntimeException("SecurityException?whats happen?",e);
30 } catch (NoSuchMethodException e) {
31 throw new NoSuchMethodRuntimeException(clazz,method,e);
32 }
33 }
34
35 static boolean isSameSignature(Method a, Method b) {
36 if( !a.getName().equals( b.getName() ) )
37 return false;
38
39 Class[] aParams = a.getParameterTypes();
40 Class[] bParams = b.getParameterTypes();
41
42 if(aParams.length!=bParams.length)
43 return false;
44
45 for(int i=0;i<aParams.length;i++){
46 if(!aParams[i].equals(bParams[i]))
47 return false;
48 }
49 return true;
50 }
51
52 static public boolean isInnerOrAnonymous(Class cls) {
53 return (cls.getName().indexOf(INNER_CLASS_SEPARATOR_CHAR) >= 0);
54 }
55
56 public static boolean isOuterClassHasDefaultConstructor(Class clazz){
57 if(clazz.isInterface())
58 throw new IllegalArgumentException("不能对interface进行是否有构造器的判断"+clazz);
59 if(isInnerOrAnonymous(clazz))
60 throw new IllegalArgumentException("不能对InnerClass or AnonymousClass进行是否有构造器的判断"+clazz);
61 try {
62 clazz.newInstance();
63 } catch (Exception e1) {
64
65 return false;
66 }
67 return true;
68 }
69 }