java 通过反射获取类的全局变量、方法、构造方法

作者在 2019-10-29 23:30:46 发布以下内容

package com.str;

public class ZiFuChuan {
	
	public static String ss = "全局变量!!!!!!";
	String s2 = null;
	public int aa = 1 ;
	int aa2;
	double dou = 1.1;
	Double d = new Double(1.1);
	public static void main(String[] args) {}
	
	public ZiFuChuan(){}
	
	public ZiFuChuan(String st){}
	
	public ZiFuChuan(int orgInt,String orgString){}

	public static void tmpArr(int a[],String s,StringBuffer s2){}
}
package com.fanshe;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;

import com.str.ZiFuChuan;

public class GetClass {

	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
		// TODO Auto-generated method stub
		Class classS = ZiFuChuan.class;
		
		//获取包
		Package classPackage = classS.getPackage();
		System.out.println("package "+classPackage.getName());
		//获取类的修饰符
		int mod = classS.getModifiers();
		String classModifier = Modifier.toString(mod);
		System.out.println(classModifier + " class ZiFuChuan {");
		//调用全局变量方法
		getFieldContent(classS);
		//调用构造方法
		getConstructorContent(classS);
		//调用其他方法
		getMethodContent(classS);
		System.out.println("}");
	}
	
	/**
	 * 获取所有全局变量
	 * @param classS
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @throws InstantiationException 
	 */
	public static void getFieldContent(Class classS) throws IllegalArgumentException, IllegalAccessException, InstantiationException{
		
		String typeName = ""; 
		Field[] field =  classS.getDeclaredFields();
		
		 for(Field fi : field){
			 
			 typeName = fi.getType().getName();
			 String xsfTmp = Modifier.toString(fi.getModifiers()) ;
			 
			 //全局变量有修饰符的在变量前加修饰符并且修饰符和数据类型之间用空格分隔,否则不需要加空格
			 String xiushifu = xsfTmp.length() == 0 ? "" : xsfTmp + " ";
			 String value = getValue(typeName,fi,classS);
			 
			 if("null".equals(value)||value == null||"null;".equals(value)){
				 System.out.println("    "+xiushifu+typeName+" "+fi.getName()+"; ");
			 }else{
				 System.out.println("    "+xiushifu+typeName+" "+fi.getName()+" = " + getValue(typeName,fi,classS));
			 }
			 
		 }
	}
	
	public static void getConstructorContent(Class classS){
		
		Constructor[] con = classS.getConstructors();
		for(Constructor c : con){
			int mod = c.getModifiers();
			String ConstructorModifier = Modifier.toString(mod);
			String constructorParameter = getConstructorParameter(c);
			System.out.println("    "+ConstructorModifier+" " + c.getName() + "("+constructorParameter+"){");
			System.out.println("    }");
		}
		
	}
	
	/**
	 * 获取构造方法中的参数
	 * @param c
	 * @return
	 */
	public static String getConstructorParameter(Constructor c){
		String qxTemp = "";
		String qx = "";
		int con = 0;
		Class[] parameterTypeArr = c.getParameterTypes();
		
		//获取构造方法中的参数
		for(Class clas : parameterTypeArr){
			
			qxTemp += clas.getTypeName() + " org"+con+",";
			con++;
			
		}
		
		int qxTempLength = qxTemp.length();
		
		//去掉空参的构造方法
		if(qxTempLength > 0){
			qx = qxTemp.substring(0, qxTempLength-1);
		}
		
		return qx;
	}
	
	/**
	 * 获取除构造方法外其他的方法的逻辑
	 * @param classS
	 */
	public static void getMethodContent(Class classS){
		Method[] method = classS.getDeclaredMethods();
		for(Method m : method){
			
			int mod = m.getModifiers();
			String methodModifier = Modifier.toString(mod);
			//获取方法返回值类型
			Type type = m.getGenericReturnType();
			String methodParameter = getMethodParameter(m);
			System.out.println("    "+methodModifier +" "+ type.getTypeName() +" " + m.getName() + "("+methodParameter+"){");
			System.out.println("    }");
		}
		
	}
	
	/**
	 * 获取其他方法的参数
	 * @param m
	 * @return
	 */
	public static String getMethodParameter(Method m){
		String qxTemp = "";
		String qx = "";
		int con = 0;
		Class[] parameterTypeArr = m.getParameterTypes();
		
		//获取构造方法中的参数
		for(Class clas : parameterTypeArr){
			
			qxTemp += clas.getTypeName()+" org"+con+",";
			con++;
			
		}
		
		int qxTempLength = qxTemp.length();
		
		//去掉空参的构造方法
		if(qxTempLength > 0){
			qx = qxTemp.substring(0, qxTempLength-1);
		}
		
		return qx;
		
	}
	/**
	 * 全局变量初始化值
	 * @param typeName
	 * @param fi
	 * @param classS
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static String getValue(String typeName,Field fi,Class classS) throws IllegalArgumentException, IllegalAccessException, InstantiationException{
		String value = "";
		
		Object obj = classS.newInstance();
		fi.setAccessible(true);
		
		String[] types = {"java.lang.Integer",
		        "java.lang.Double",
		        "java.lang.Float",
		        "java.lang.Long",
		        "java.lang.Short",
		        "java.lang.Byte",
		        "java.lang.Boolean",
		        "java.lang.Character",
		        "int","double","long","short","byte","boolean","char","float"};
		
		for(String str : types) {
			
			if(fi.getType().getName().equals("java.lang.String")){
				Object fiObj = fi.get(obj);
				//判断变量是否初始化
				if(fiObj != null){
					//String 类型参数需要用双引号扩上
					value = "\""+fiObj.toString()+"\";";
				}else{
					value = null;
				}
			}else if(fi.getType().getName().equals(str)){
				 value = fi.get(obj).toString()+";";
			}
		    
		}		
		
		return value;
	}
	
}

仅通过java反射机制没有想到获取方法体的办法,有知道的大神,还请不吝赐教


java | 阅读 2899 次
文章评论,共0条
游客请输入验证码