Java Reflect Bits

Source: Internet
Author: User
public class Reflectutil {public static class Reflectclass (String className) {try {return class.forname Classnam
		e);
		catch (ClassNotFoundException e) {e.printstacktrace ();
	return null; public static Object getclassinstance (Class clazz, httpservletrequest request, httpservletresponse response) {T
			ry {Object ins = clazz.newinstance (); Invoke Sethttpservlet of Baseaction to initialize instance of Xxxaction Invokemethodwithargs (Clazz.getsuperclass (),
			INS, "Sethttpservlet", New Class[]{httpservletrequest.class, httpservletresponse.class}, request, response);
			Setattributesforinstance (Clazz,ins,request,null);
		return ins;
		catch (Exception e) {e.printstacktrace ();
	return null; private static void Setattributesforinstance (Class clazz, Object ins, httpservletrequest request, String prefix) t Hrows Exception {if (Prefix!=null&&prefix.indexof (".")! =-1) return; Do not assign value to level two attribute field[] fields = clazz.getdeclAredfields ();
			for (Field f:fields) {f.setaccessible (true);
			String Typeclass = F.gettype (). Getsimplename ();
			String fname = F.getname (); if (prefix!=null) fname = prefix+ "."
			+fname;
			String paramvalue = Request.getparameter (fname); try {if ("int". Equalsignorecase (typeclass) | |
				Integer ". Equalsignorecase (Typeclass)) {if (paramvalue!= null) f.set (INS, Integer.parseint (paramvalue));
				}else if ("float". Equalsignorecase (Typeclass)) {if (paramvalue!= null) f.set (INS, float.parsefloat (paramvalue));
				}else if ("Double". Equalsignorecase (Typeclass)) {if (paramvalue!= null) f.set (INS, double.parsedouble (paramvalue));
				}else if ("Long" equalsignorecase (Typeclass)) {if (paramvalue!= null) f.set (INS, Long.parselong (paramvalue));
				}else if ("short". Equalsignorecase (Typeclass)) {if (paramvalue!= null) f.set (INS, Short.parseshort (paramvalue)); }else if ("Boolean". Equalsignorecase (Typeclass)) {if (paramvalue!= null) f.set (INS, boolean.parsebooleAn (paramvalue)); }else if ("Object". Equalsignorecase (typeclass) | | "
				String ". Equalsignorecase (Typeclass)) {if (paramvalue!= null) f.set (ins, paramvalue);
					}else{Object fieldins = F.gettype (). GetConstructor (). newinstance ();
					F.set (ins, fieldins);
				Setattributesforinstance (F.gettype (), fieldins, request, fname);
			} catch (IllegalArgumentException e) {e.printstacktrace ();
			catch (Illegalaccessexception e) {e.printstacktrace (); }} public static object Invokemethodnoargs (Class clazz, Object classinstance, String methodname) {object result
		= NULL;
			try {Method method = Clazz.getmethod (methodname);
			Method.setaccessible (TRUE);
		result = Method.invoke (classinstance);
		catch (Exception e) {e.printstacktrace ();
	return result; public static Object Invokemethodwithargs (Class clazz, Object classinstance, String methodname, class[] Argtype, Obje Ct...
		args) {Object result = null; try {Method method = Clazz.getDeclaredmethod (methodname, Argtype);
			Class returntype = Method.getreturntype ();
			Method.setaccessible (TRUE);
		result = Method.invoke (classinstance, args);
		catch (Exception e) {e.printstacktrace ();
	return result; }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.