-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON abstraction for JSON RPC support
[#159302201] Fixes #378
- Loading branch information
1 parent
e28b34a
commit 01f10d6
Showing
15 changed files
with
246 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
// If you have any questions regarding licensing, please contact us at | ||
// [email protected]. | ||
|
||
|
||
package com.rabbitmq.tools.json; | ||
|
||
import org.slf4j.Logger; | ||
|
@@ -34,54 +33,52 @@ | |
*/ | ||
public class JSONUtil { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtil.class); | ||
private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtil.class); | ||
|
||
/** | ||
* Uses reflection to fill public fields and Bean properties of | ||
* the target object from the source Map. | ||
*/ | ||
public static Object fill(Object target, Map<String, Object> source) | ||
throws IntrospectionException, IllegalAccessException, InvocationTargetException | ||
{ | ||
return fill(target, source, true); | ||
throws IntrospectionException, IllegalAccessException, InvocationTargetException { | ||
return fill(target, source, true); | ||
} | ||
|
||
/** | ||
* Uses reflection to fill public fields and optionally Bean | ||
* properties of the target object from the source Map. | ||
*/ | ||
public static Object fill(Object target, Map<String, Object> source, boolean useProperties) | ||
throws IntrospectionException, IllegalAccessException, InvocationTargetException | ||
{ | ||
if (useProperties) { | ||
BeanInfo info = Introspector.getBeanInfo(target.getClass()); | ||
throws IntrospectionException, IllegalAccessException, InvocationTargetException { | ||
if (useProperties) { | ||
BeanInfo info = Introspector.getBeanInfo(target.getClass()); | ||
|
||
PropertyDescriptor[] props = info.getPropertyDescriptors(); | ||
for (int i = 0; i < props.length; ++i) { | ||
PropertyDescriptor prop = props[i]; | ||
String name = prop.getName(); | ||
Method setter = prop.getWriteMethod(); | ||
if (setter != null && !Modifier.isStatic(setter.getModifiers())) { | ||
setter.invoke(target, source.get(name)); | ||
} | ||
} | ||
} | ||
PropertyDescriptor[] props = info.getPropertyDescriptors(); | ||
for (int i = 0; i < props.length; ++i) { | ||
PropertyDescriptor prop = props[i]; | ||
String name = prop.getName(); | ||
Method setter = prop.getWriteMethod(); | ||
if (setter != null && !Modifier.isStatic(setter.getModifiers())) { | ||
setter.invoke(target, source.get(name)); | ||
} | ||
} | ||
} | ||
|
||
Field[] ff = target.getClass().getDeclaredFields(); | ||
for (int i = 0; i < ff.length; ++i) { | ||
Field field = ff[i]; | ||
Field[] ff = target.getClass().getDeclaredFields(); | ||
for (int i = 0; i < ff.length; ++i) { | ||
Field field = ff[i]; | ||
int fieldMod = field.getModifiers(); | ||
if (Modifier.isPublic(fieldMod) && !(Modifier.isFinal(fieldMod) || | ||
Modifier.isStatic(fieldMod))) | ||
{ | ||
try { | ||
field.set(target, source.get(field.getName())); | ||
} catch (IllegalArgumentException iae) { | ||
// no special error processing required | ||
if (Modifier.isPublic(fieldMod) && !(Modifier.isFinal(fieldMod) || | ||
Modifier.isStatic(fieldMod))) { | ||
try { | ||
field.set(target, source.get(field.getName())); | ||
} catch (IllegalArgumentException iae) { | ||
// no special error processing required | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return target; | ||
return target; | ||
} | ||
|
||
/** | ||
|
@@ -90,14 +87,14 @@ public static Object fill(Object target, Map<String, Object> source, boolean use | |
* source Map. | ||
*/ | ||
public static void tryFill(Object target, Map<String, Object> source) { | ||
try { | ||
fill(target, source); | ||
} catch (IntrospectionException ie) { | ||
LOGGER.error("Error in tryFill", ie); | ||
} catch (IllegalAccessException iae) { | ||
LOGGER.error("Error in tryFill", iae); | ||
} catch (InvocationTargetException ite) { | ||
LOGGER.error("Error in tryFill", ite); | ||
} | ||
try { | ||
fill(target, source); | ||
} catch (IntrospectionException ie) { | ||
LOGGER.error("Error in tryFill", ie); | ||
} catch (IllegalAccessException iae) { | ||
LOGGER.error("Error in tryFill", iae); | ||
} catch (InvocationTargetException ite) { | ||
LOGGER.error("Error in tryFill", ite); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.