자바 구현 코드 - 반사

5876 단어
코드 는 다음 과 같 습 니 다:
package     ;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;

import org.junit.jupiter.api.Test;

public class    {

	public void      ()
			throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {

		// ArrayList a=new ArrayList();
		// int b[]={1,2,3};
		// new onlyTest().function(b);
		// Enum b=new Enum("afd",3);
		// Class> cls;
		try {
			Object cls = (Object) Class.forName("java.util.ArrayList").getConstructor().newInstance();
			Object clsa = (Object) Class.forName("java.util.ArrayList").getConstructor().newInstance();
			System.out.println(clsa.getClass().getName());
			System.out.println(Class.forName("java.util.ArrayList").getClassLoader());

			// System.out.println(cls.);
			ArrayList cl = (ArrayList) clsa;
			// cl.add("dfa");
			// System.out.println(cl.get(0));
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String s = "abcd";
		System.out.println("s = " + s); //    
		//   String   value  
		Field valueField = String.class.getDeclaredField("value");
		//   value       
		valueField.setAccessible(true);
		//   s    value    
		char[] value = (char[]) valueField.get(s);
		//   value         5   
		value[3] = 'e';
		System.out.println("s = " + s); //    

		//        
		char[] a = "ghjk".toCharArray();
		System.arraycopy(a, 0, value, 0, a.length);
		System.out.println("s = " + s); //    
		valueField.set(s, new char[] { '1', '2', '3' });
		//       ,     value  ,          ,      count  ,    s       
		// Field countField = String.class.getDeclaredField("value");
		// countField.setAccessible(true);
		// countField.set(s, 3);
		//
		System.out.println("s = " + s + " " + s.length()); // 123
	}

	@SuppressWarnings("null")
	@Test
	public void       ()
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Object model = new user("Yien", 222, 100.1);
		Field[] field = model.getClass().getDeclaredFields(); //           ,  Field  
		String content = "";
		for (int j = 0; j < field.length; j++) { //       
			String name = field[j].getName(); //        
			name = name.substring(0, 1).toUpperCase() + name.substring(1); //          ,    get,set  
			String type = field[j].getGenericType().toString(); //        
			if (type.equals("class java.lang.String")) { //   type    ,     "class
															// ",     
				Method m = model.getClass().getMethod("get" + name);
				String value = (String) m.invoke(model); //   getter       
				if (value != null) {
					content += value + "\t";
				}
			}
			if (type.equals("class java.lang.Integer")) {
				Method m = model.getClass().getMethod("get" + name);
				Integer value = (Integer) m.invoke(model);
				if (value != null) {
					content += value + "\t";
				}
			}
			if (type.equals("class java.lang.Short")) {
				Method m = model.getClass().getMethod("get" + name);
				Short value = (Short) m.invoke(model);
				if (value != null) {
					content += value + "\t";
				}
			}
			if (type.equals("class java.lang.Double")) {
				Method m = model.getClass().getMethod("get" + name);
				Double value = (Double) m.invoke(model);
				if (value != null) {
					content += value + "\t";
				}
			}
			if (type.equals("class java.lang.Boolean")) {
				Method m = model.getClass().getMethod("get" + name);
				Boolean value = (Boolean) m.invoke(model);
				if (value != null) {
					String cc = value == true ? "1" : "0";
					content += cc + "\t";
				}
			}
			if (type.equals("class java.util.Date")) {
				Method m = model.getClass().getMethod("get" + name);
				Date value = (Date) m.invoke(model);
				if (value != null) {
					content += value + "\t";
					System.out.println(type + "attribute value:" + value.toLocaleString());
				}
			}
		}
		System.out.println(content);
	}

	static class user {
		private String name;
		private Integer id;
		private Double price;

		/**
		 * @param name
		 * @param id
		 * @param price
		 */
		public user(String name, Integer id, Double price) {
			super();
			this.name = name;
			this.id = id;
			this.price = price;
		}

		/**
		 * @return the name
		 */
		public String getName() {
			return name;
		}

		/**
		 * @param name
		 *            the name to set
		 */
		public void setName(String name) {
			this.name = name;
		}

		/**
		 * @return the id
		 */
		public int getId() {
			return id;
		}

		/**
		 * @param id
		 *            the id to set
		 */
		public void setId(int id) {
			this.id = id;
		}

		/**
		 * @return the price
		 */
		public double getPrice() {
			return price;
		}

		/**
		 * @param price
		 *            the price to set
		 */
		public void setPrice(double price) {
			this.price = price;
		}

	}

}

좋은 웹페이지 즐겨찾기