类 MapUtil
java.lang.Object
cn.vorbote.core.utils.MapUtil
Map Utility will automatically convert data between Map and instance objects.
- 作者:
- vorbote theodore.hills@vorbote.cn
-
方法概要
修饰符和类型方法说明static <T> TCast the value to the required type.static StringgetFieldValue(Object obj, String fieldName) Get the specified field value, equivalent toobj.getFieldName.static <T> TgetFieldValue(Object obj, String fieldName, Class<T> requiredClass) Get the specified field value, equivalent toobj.getFieldName.static StringGetFieldValue(Object obj, String fieldName) 已过时。static <T> TmapToObject(Map<String, Object> map, Class<T> requiredType) Set an object by reflection, it will create a new instance by this method, you don't have to pass an instance to it anymore.objectToMap(Object obj) Dynamically convert object to dictionary/map.static voidsetFieldValue(Object obj, String fieldName, Object fieldValue) Get the specified field value, equivalent toobj.setFieldName.
-
方法详细资料
-
objectToMap
Dynamically convert object to dictionary/map.- 参数:
obj- Objects that need to be converted to map.- 返回:
- Converted hash map.
- 抛出:
IllegalAccessException- This method will callField.get(Object), if this Field object is enforcing Java language access control and the underlying field is inaccessible.- 另请参阅:
-
mapToObject
public static <T> T mapToObject(Map<String, Object> map, Class<T> requiredType) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessExceptionSet an object by reflection, it will create a new instance by this method, you don't have to pass an instance to it anymore.- 类型参数:
T- The type you want.- 参数:
map- The map which put the data.requiredType- The type you want.- 返回:
- An instance of the class you provided, the instance contains all the field values with the same key in the Map.
- 抛出:
NoSuchMethodException- This method will get a no-param constructor, if the class you provided doesn't contain this class, this exception will be thrown out.IllegalAccessException- This method will use the no-param constructor to create a new instance, if your constructor is modified by modifierprivate,protectedor the default modifier.InvocationTargetException- If the underlying constructor throws an exception, then it will be throws out by this method.InstantiationException- If you provided an abstract class, it will throw out this exception.- 另请参阅:
-
getFieldValue
public static String getFieldValue(Object obj, String fieldName) throws InvocationTargetException, IllegalAccessException Get the specified field value, equivalent toobj.getFieldName. Before using this method, please make sure that you have a getter for that field you need.- 参数:
fieldName- Field nameobj- object- 返回:
- A string of the object corresponding to the object value.
- 抛出:
InvocationTargetException- This method will callMethod.invoke(Object, Object...), if the method object throws an exception, this exception will be thrown.IllegalAccessException- This method will callMethod.invoke(Object, Object...), if this Method object is enforcing Java language access control and the underlying method is inaccessible.- 另请参阅:
-
getMethodName(String, String)getDeclaredMethod(Object, String, Class[])Method.setAccessible(boolean)Method.invoke(Object, Object...)
-
GetFieldValue
已过时。This method will be removed in version 3.5.0
Get the specified field value, equivalent toobj.getFieldName. Before using this method, please make sure that you have a getter for that field you need.- 参数:
fieldName- Field nameobj- object- 返回:
- A string of the object corresponding to the object value.
- 抛出:
Exception- Exceptions will be caused due to other methods, please see upriver methods for details of these Exceptions' info.InvocationTargetException- This method will callMethod.invoke(Object, Object...), if the method object throws an exception, this exception will be thrown.IllegalAccessException- This method will callMethod.invoke(Object, Object...), if the method object is enforcing Java language access control and the underlying method is inaccessible.- 另请参阅:
-
getMethodName(String, String)getDeclaredMethod(Object, String, Class[])Method.setAccessible(boolean)Method.invoke(Object, Object...)
-
getFieldValue
public static <T> T getFieldValue(Object obj, String fieldName, Class<T> requiredClass) throws NoSuchFieldException, IllegalAccessException Get the specified field value, equivalent toobj.getFieldName. Before using this method, please make sure that you have a getter for that field you need.- 类型参数:
T- The type of the value.- 参数:
obj- objectfieldName- Field namerequiredClass- The instance of the specified class.- 返回:
- A string of the object corresponding to the object value
- 抛出:
NoSuchFieldException- This method will callClass.getDeclaredField(String), if the requested field could not be found, this exception will be thrown.IllegalAccessException- This method will callField.get(Object), if this Field object is enforcing Java language access control and the underlying field is inaccessible, this exception will be thrown.- 另请参阅:
-
setFieldValue
public static void setFieldValue(Object obj, String fieldName, Object fieldValue) throws InvocationTargetException, IllegalAccessException Get the specified field value, equivalent toobj.setFieldName. Before using this method, please make sure that you have a setter for that field you need.- 参数:
fieldName- Field name.obj- The object will be set.fieldValue- Field Value.- 抛出:
InvocationTargetException- This method will callMethod.invoke(Object, Object...), if the method object throws an exception, this exception will be thrown.IllegalAccessException- This method will callMethod.invoke(Object, Object...), if this Method object is enforcing Java language access control and the underlying method is inaccessible.- 另请参阅:
-
cast
Cast the value to the required type. The value had better to be an instance ofObjector the super class of the required class.- 类型参数:
T- The required type.- 参数:
value- The value.requiredType- The required type.- 返回:
- The value in the form of the instance of the required type.
-