You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					
					
						
							127 lines
						
					
					
						
							5.3 KiB
						
					
					
				
			
		
		
	
	
							127 lines
						
					
					
						
							5.3 KiB
						
					
					
				| package com.ynxbd.common.helper.common;
 | |
| //
 | |
| //import javassist.ClassPool;
 | |
| //import javassist.CtClass;
 | |
| //import javassist.CtMethod;
 | |
| //import javassist.Modifier;
 | |
| //import javassist.bytecode.CodeAttribute;
 | |
| //import javassist.bytecode.LocalVariableAttribute;
 | |
| //import javassist.bytecode.MethodInfo;
 | |
| //import weixin.popular.bean.user.User;
 | |
| //
 | |
| //public class MethodUtil {
 | |
| //
 | |
| //    public String test() {
 | |
| //        return "hello";
 | |
| //    }
 | |
| //
 | |
| //    public static void test2(String a, String b, User user) {
 | |
| //        System.out.println(a);
 | |
| //        System.out.println(b);
 | |
| //    }
 | |
| //
 | |
| //    public static void test03(String name, Object... args) {
 | |
| //        System.out.println(name);
 | |
| //        for (Object o : args) {
 | |
| //            System.out.println(o);
 | |
| //        }
 | |
| //    }
 | |
| //
 | |
| //    public static void main(String[] args) {
 | |
| ////        ArrayList<Object> objects = new ArrayList<>();
 | |
| //////        objects.add("123");
 | |
| //////        objects.add(true);
 | |
| ////        test03("123", objects);
 | |
| //        String[] names = getMethodParams("", "getData");
 | |
| //        System.out.println(names);
 | |
| //        if (names == null) return;
 | |
| ////        System.out.println(names);
 | |
| //        for (String name : names) {
 | |
| //            System.out.println(name);
 | |
| //        }
 | |
| //    }
 | |
| //
 | |
| //
 | |
| //    /**
 | |
| //     * 根据类名和方法名,获取方法的参数名
 | |
| //     *
 | |
| //     * @param clazzName  类名
 | |
| //     * @param methodName 方法名
 | |
| //     * @return 参数名数组
 | |
| //     */
 | |
| //    public static String[] getMethodParams(String clazzName, String methodName) {
 | |
| //        try {
 | |
| //            ClassPool pool = ClassPool.getDefault();
 | |
| //            CtClass cc = pool.get(clazzName);
 | |
| //            CtMethod cm = cc.getDeclaredMethod(methodName);
 | |
| //            MethodInfo methodInfo = cm.getMethodInfo();
 | |
| //            CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
 | |
| //            String[] params = new String[cm.getParameterTypes().length];
 | |
| //            LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
 | |
| //            if (attr != null) {
 | |
| //                int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
 | |
| //                for (int i = 0; i < params.length; i++) {
 | |
| //                    params[i] = attr.variableName(i + pos);
 | |
| //                }
 | |
| //                return params;
 | |
| //            }
 | |
| //        } catch (Exception e) {
 | |
| //            e.printStackTrace();
 | |
| //        }
 | |
| //        return null;
 | |
| //
 | |
| //    }
 | |
| 
 | |
| 
 | |
| //初始化
 | |
| //    private void getVar() throws IntrospectionException, IllegalAccessException, InstantiationException, CannotCompileException {
 | |
| //        Class<? extends BaseServlet> clazz = this.getClass();
 | |
| //        ClassPool pool = ClassPool.getDefault();
 | |
| //        CtClass ctClass = pool.makeClass(StringUtils.capitalize(clazz.getName()));
 | |
| //        map = new HashMap<>();
 | |
| //        Field[] fields = clazz.getDeclaredFields();
 | |
| //        for (Field f : fields) {
 | |
| //            if (f.isAnnotationPresent(RequestParam.class)) {
 | |
| //                RequestParam requestParam = f.getAnnotation(RequestParam.class);
 | |
| //                // 注解值c
 | |
| //                Class<?> type = f.getType();
 | |
| ////                System.out.println(f.getType());
 | |
| ////                System.out.println(f.getGenericType());
 | |
| ////                Object o = type.newInstance();
 | |
| ////                Class<? extends Type> aClass = f.getGenericType().getClass();
 | |
| //                System.out.println("++++++++++++++++++++");
 | |
| //                String annotationVal = requestParam.value();
 | |
| //                System.out.println(annotationVal);
 | |
| //                // 属性名
 | |
| //                CtField make = CtField.make(String.format(fieldTemplate, f.getType().getName(), f.getName()), ctClass);
 | |
| //
 | |
| //                String setMethodName = SETTER_STR + StringUtils.capitalize(f.getName());
 | |
| //                String getMethodName = GETTER_STR + StringUtils.capitalize(f.getName());
 | |
| //
 | |
| //                CtMethod setter = CtNewMethod.setter(setMethodName, make);
 | |
| //                ctClass.addMethod(setter);
 | |
| //                CtMethod getter = CtNewMethod.getter(getMethodName, make);
 | |
| //                ctClass.addMethod(getter);
 | |
| //
 | |
| //                System.out.println(setter.toString());
 | |
| //                System.out.println(getter.toString());
 | |
| //                System.out.println("===============");
 | |
| //
 | |
| //                String fieldName = f.getName();
 | |
| //
 | |
| ////                PropertyDescriptor descriptor = new PropertyDescriptor(fieldName, clazz);  //给一个属性,获取值
 | |
| ////                Method getMethod = descriptor.getReadMethod();  //相当于为上面声明的字段设置get方法
 | |
| ////                Method setMethod = descriptor.getWriteMethod();  //为上面声明的字段设置set方法(又称内省)
 | |
| ////                getMethod.setAccessible(true);
 | |
| ////                setMethod.setAccessible(true);
 | |
| ////                System.out.println(setMethod.toString());
 | |
| ////                System.out.println(getMethod.toString());
 | |
| //                if (annotationVal.equals("")) {
 | |
| //                    map.put(annotationVal, new BaseRequest(annotationVal, fieldName, setter, getter));
 | |
| //                } else {
 | |
| //                    map.put(fieldName, new BaseRequest(fieldName,  fieldName, setter, getter));
 | |
| //                }
 | |
| //            }
 | |
| //        }
 | |
| //    }
 | |
| //}
 | |
| 
 |