Guest User

Untitled

a guest
Apr 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package com.noahsloan.grails.dwr;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import java.util.Collection;
  6. import java.util.Map;
  7.  
  8. import org.directwebremoting.convert.BeanConverter;
  9. import org.directwebremoting.convert.CollectionConverter;
  10. import org.directwebremoting.extend.InboundContext;
  11. import org.directwebremoting.extend.Property;
  12. import org.directwebremoting.extend.TypeHintContext;
  13. import org.directwebremoting.util.Logger;
  14.  
  15. import com.sun.tools.jdi.LinkedHashMap;
  16.  
  17. /**
  18. * Converter for Grails ORM classes. Uses the hasMany map to determine the type
  19. * of collections so we can convert them inbound.
  20. *
  21. * @author noah
  22. *
  23. */
  24. @SuppressWarnings("unchecked")
  25. public class DwrGormConverter extends BeanConverter {
  26.  
  27. private static final Logger LOG = org.directwebremoting.util.Logger
  28. .getLogger(DwrGormConverter.class);
  29.  
  30. protected TypeHintContext createTypeHintContext(InboundContext inctx,
  31. final Property property) {
  32. // check collections
  33. if (Collection.class.isAssignableFrom(property.getPropertyType())) {
  34. final Class type = getCollectionType(property);
  35. if (type != null) {
  36. return createHint(property.getSetter(), type);
  37. }
  38. }
  39. return super.createTypeHintContext(inctx, property);
  40. }
  41.  
  42. /**
  43. * Pulled out for readability. Creates a type hint for the
  44. * {@link CollectionConverter} with the given type.
  45. *
  46. * @param setter TODO is this necessary?
  47. * @param type
  48. * @return
  49. */
  50. protected TypeHintContext createHint(final Method setter,
  51. final Class type) {
  52. return new TypeHintContext(converterManager, setter, 0) {
  53. public TypeHintContext createChildContext(int newParameterNumber) {
  54. return new TypeHintContext(converterManager, setter, 0) {
  55. public Class getExtraTypeInfo() {
  56. return type;
  57. }
  58. };
  59. }
  60. };
  61. }
  62.  
  63. protected static Map<Class, Map<String, Class>> _cache = new LinkedHashMap();
  64.  
  65. /**
  66. * Determines the type of the collection by examining the declaring class'
  67. * static hasMany property.
  68. *
  69. * @param property
  70. * @return the type of the elements of the collection property.
  71. */
  72. protected Class getCollectionType(Property property) {
  73. // determine the class the collection is attached to by examining
  74. // the setter (which is the domain class)
  75. Method setter = property.getSetter();
  76. if (setter == null) {
  77. // XXX in theory this should not happen because BeanConverter should
  78. // always provide PropertyDescriptionPropertys
  79. LOG.error("setter for "
  80. + property.getName() + " is null."
  81. + " Property is not a PropertyDescriptionProperty.");
  82. return null;
  83. }
  84. Class gormType = setter.getDeclaringClass();
  85. if (_cache.containsKey(gormType)) {
  86. Map<String, Class> map = _cache.get(gormType);
  87. return map == null ? null : map.get(property.getName());
  88. }
  89. try {
  90. Field field = gormType.getDeclaredField("hasMany");
  91. Map<String, Class> map = null;
  92. if (field != null) {
  93. if (!field.isAccessible()) {
  94. field.setAccessible(true);
  95. }
  96. map = ((Map<String, Class>) field.get(null));
  97. }
  98. _cache.put(gormType, map);
  99. return map == null ? null : map.get(property.getName());
  100. } catch (Exception e) {
  101. LOG.error("Exception getting GORM collection type info.", e);
  102. return null;
  103. }
  104. }
  105.  
  106. }
Add Comment
Please, Sign In to add comment