Java reflection Technology and class use example _java

Source: Internet
Author: User
Tags reflection

Copy Code code as follows:

Package com.java.db;
Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;

Import com.java.entity.BookShelf;
Import Com.java.util.GetMetaDataCloumName;
public class Getnewinstances<t> {
 class[] cl = {};
 object[] ob = {};
 /**
  * Set to empty after each run is not cumulative
  */
 public void Setnulltoarrays () {
   this.cl = new class[]{};
  this.ob = new object[]{};
 }
 /**
  * Copy object array
  *
  * @param obj
  *     & The actual value required in the nbsp;      construction method
  * @return
  */
 public object[] Getobjectarrays (Object obj) {
   ob = arrays.copyof (ob,ob.length + 1);
   ob[ob.length-1] = obj;
   return ob;
 }

/**
* Copy Class array
*
* @param CLA
* The class to add
*
* @return
*/
@SuppressWarnings ("Unchecked")
Public class[] Getclassarrays (class<?> CLA) {
if (CLA!= null) {
CL = arrays.copyof (cl,cl.length + 1);
CL[CL.LENGTH-1] = CLA;
return CL;
}else{
return CL;
}
}

/**
* Get an instance of the class
*
* @param clazz
* The class to instantiate
* @return the class after instantiation
* @throws instantiationexception
* @throws illegalaccessexception
* @throws IllegalArgumentException
* @throws SecurityException
* @throws InvocationTargetException
* @throws nosuchmethodexception
*/
@SuppressWarnings ("Unchecked")
Public Object getclassnewinstance (class<?> clazz)
Throws Instantiationexception, Illegalaccessexception,
IllegalArgumentException, SecurityException,
InvocationTargetException, Nosuchmethodexception {
Object OJ = null;
Constructor[] cons = Clazz.getdeclaredconstructors ()//Get constructor
class[] cla = Cons[1].getparametertypes ();
System.out.println ("Prompts the user to add the size of the field constructor parameter:" +cla.length);
for (int i = 0; i < cla.length; i++) {
String classstr = cla[i].tostring ();
System.out.println ("type of parameter:" +CLASSSTR);
if (Classstr.equals ("Class java.lang.String")) {
Getclassarrays (String.class);
else if (classstr.equals ("int")) {
Getclassarrays (Int.class);
else if (classstr.equals ("double")) {
Getclassarrays (Double.class);
else if (Classstr.equals ("boolean")) {
Getclassarrays (Boolean.class);
else if (classstr.equals ("float")) {
Getclassarrays (Float.class);
else if (classstr.equals ("Class Java.lang.Integer")) {
Getclassarrays (Integer.class);
}else if (classstr.equals ("Class Java.lang.Float")) {
Getclassarrays (Float.class);
}
}
OJ = Clazz.newinstance ()//Returns the current object the concrete instantiation constructs in Bdoperater
return OJ;
}
/**
* Get concrete instance class by constructor
* @param clazz
* @return
* @throws IllegalArgumentException
* @throws SecurityException
* @throws instantiationexception
* @throws illegalaccessexception
* @throws InvocationTargetException
* @throws nosuchmethodexception
*/
Public Object Getobjcon (class<?> clazz) throws IllegalArgumentException, SecurityException, Instantiationexception, Illegalaccessexception, InvocationTargetException, nosuchmethodexception{
Object Obj=null;
obj = This.getclassnewinstance (clazz);
return obj;
}
/**
* Get an instance of the object
* @param clazz
* @return
* @throws instantiationexception
* @throws illegalaccessexception
*/
Public Object getnewinstance (Class clazz) throws Instantiationexception, illegalaccessexception{
Object obj = null;
obj = Clazz.newinstance ();
return obj;
}
/**
* All properties in the class based on reflection
* @param clazz The class that requires the property to be fetched
* @return Property Collection
* @throws SecurityException
* @throws IllegalArgumentException
* @throws instantiationexception
* @throws illegalaccessexception
* @throws InvocationTargetException
* @throws nosuchmethodexception
*/
Public field[] Getfielsdarray (class<object> clazz) throws SecurityException, IllegalArgumentException, Instantiationexception, Illegalaccessexception, InvocationTargetException, nosuchmethodexception{
field[] fields = NULL;
Fields = Clazz.getdeclaredfields ();
return fields;
}
/**
* Get setter Format properties based on string
* @param the attributes that STR needs to be formatted
* @return
*/
public string Getsetterstr (String str) {
String info = null;
String strvalue = str.substring (0,1). toUpperCase ();
info = "Set" +strvalue+str.substring (1,str.length ());
return info;
}
/**
* Restore the SETXX to XX
* @param str
* @return
*/
public string Setsetstr (String str) {
String info = null;
String strvalue = str.substring (3,str.length ());
String lower = strvalue.substring (0). toLowerCase (). substring (0,1);
info = lower+str.substring (4,str.length ());
return info;
}
/**
* Get the method in the class
* @param clazz needed to get the method of the class
* @return
*/
Public method[] Getmethodsarray (Class clazz) {
Method[] methods = Clazz.getmethods ();
return methods;
}
/**
* Instantiate constructors based on list<map>
* @param listmap
* @param clazz
* @param tablename Database name
* @return
* @throws nosuchmethodexception
* @throws InvocationTargetException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws illegalaccessexception
* @throws instantiationexception
*/
@SuppressWarnings ({"Unchecked"})
Public list<object> Getlistbymap (list<map<string,object>> listmap,class clazz,String tableName) Throws Instantiationexception, Illegalaccessexception, IllegalArgumentException, SecurityException, InvocationTargetException, nosuchmethodexception{
list<object> listlast = new arraylist<object> ();
list<string> Metalist = getmetadatacloumname.getcloumnamelist (tablename);
Iterator<map<string,object>> it = Listmap.iterator ();
while (It.hasnext ()) {
map<string,object> map = It.next ();
iterator<string> Iitt = Metalist.iterator ();
while (Iitt.hasnext ()) {
String info = Iitt.next ();
This.getobjectarrays (Map.get (info));
}
System.out.println ("Call Reflection:" +this.cl.length+ "" +this.ob.length);
Object tobj = this.getclassnewinstance (clazz). GetClass (). GetConstructor (this.cl). newinstance (THIS.OB);
Listlast.add (Tobj);
This.setnulltoarrays ();
}
return listlast;
}
public static void Main (string[] args) {
Getnewinstances ge = new Getnewinstances ();
System.out.println (Ge.getsetterstr ("nameSpace") = = "Setnamespace");
System.out.println ("1a" = "1a");
System.out.println (Ge.setsetstr ("Setnamespace"));
}
}

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.