638 "Cannot convert SortedSet with non-null comparator: " +
639 comparator;
640 throw openDataException(msg, new IllegalArgumentException(msg));
641 }
642 }
643 final Object[] openArray = (Object[])
644 Array.newInstance(getOpenClass().getComponentType(),
645 valueCollection.size());
646 int i = 0;
647 for (Object o : valueCollection)
648 openArray[i++] = elementMapping.toOpenValue(o);
649 return openArray;
650 }
651
652 @Override
653 final Object fromNonNullOpenValue(Object openValue)
654 throws InvalidObjectException {
655 final Object[] openArray = (Object[]) openValue;
656 final Collection<Object> valueCollection;
657 try {
658 valueCollection = cast(collectionClass.newInstance());
659 } catch (Exception e) {
660 throw invalidObjectException("Cannot create collection", e);
661 }
662 for (Object o : openArray) {
663 Object value = elementMapping.fromOpenValue(o);
664 if (!valueCollection.add(value)) {
665 final String msg =
666 "Could not add " + o + " to " +
667 collectionClass.getName() +
668 " (duplicate set element?)";
669 throw new InvalidObjectException(msg);
670 }
671 }
672 return valueCollection;
673 }
674
675 public void checkReconstructible() throws InvalidObjectException {
676 elementMapping.checkReconstructible();
677 }
678
1097 if (setter.getReturnType() != void.class)
1098 throw new Exception();
1099 } catch (Exception e) {
1100 return "not all getters have corresponding setters " +
1101 "(" + getter + ")";
1102 }
1103 setters[i] = setter;
1104 }
1105 this.setters = setters;
1106 return null;
1107 }
1108
1109 Object fromCompositeData(CompositeData cd,
1110 String[] itemNames,
1111 MXBeanMapping[] converters)
1112 throws InvalidObjectException {
1113 Object o;
1114 try {
1115 final Class<?> targetClass = getTargetClass();
1116 ReflectUtil.checkPackageAccess(targetClass);
1117 o = targetClass.newInstance();
1118 for (int i = 0; i < itemNames.length; i++) {
1119 if (cd.containsKey(itemNames[i])) {
1120 Object openItem = cd.get(itemNames[i]);
1121 Object javaItem =
1122 converters[i].fromOpenValue(openItem);
1123 MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
1124 }
1125 }
1126 } catch (Exception e) {
1127 throw invalidObjectException(e);
1128 }
1129 return o;
1130 }
1131
1132 private Method[] setters;
1133 }
1134
1135 /** Builder for when the target class has a constructor that is
1136 annotated with {@linkplain ConstructorParameters @ConstructorParameters}
1137 or {@code @ConstructorProperties} so we can see the correspondence to getters. */
|
638 "Cannot convert SortedSet with non-null comparator: " +
639 comparator;
640 throw openDataException(msg, new IllegalArgumentException(msg));
641 }
642 }
643 final Object[] openArray = (Object[])
644 Array.newInstance(getOpenClass().getComponentType(),
645 valueCollection.size());
646 int i = 0;
647 for (Object o : valueCollection)
648 openArray[i++] = elementMapping.toOpenValue(o);
649 return openArray;
650 }
651
652 @Override
653 final Object fromNonNullOpenValue(Object openValue)
654 throws InvalidObjectException {
655 final Object[] openArray = (Object[]) openValue;
656 final Collection<Object> valueCollection;
657 try {
658 @SuppressWarnings("deprecation")
659 Collection<?> tmp = collectionClass.newInstance();
660 valueCollection = cast(tmp);
661 } catch (Exception e) {
662 throw invalidObjectException("Cannot create collection", e);
663 }
664 for (Object o : openArray) {
665 Object value = elementMapping.fromOpenValue(o);
666 if (!valueCollection.add(value)) {
667 final String msg =
668 "Could not add " + o + " to " +
669 collectionClass.getName() +
670 " (duplicate set element?)";
671 throw new InvalidObjectException(msg);
672 }
673 }
674 return valueCollection;
675 }
676
677 public void checkReconstructible() throws InvalidObjectException {
678 elementMapping.checkReconstructible();
679 }
680
1099 if (setter.getReturnType() != void.class)
1100 throw new Exception();
1101 } catch (Exception e) {
1102 return "not all getters have corresponding setters " +
1103 "(" + getter + ")";
1104 }
1105 setters[i] = setter;
1106 }
1107 this.setters = setters;
1108 return null;
1109 }
1110
1111 Object fromCompositeData(CompositeData cd,
1112 String[] itemNames,
1113 MXBeanMapping[] converters)
1114 throws InvalidObjectException {
1115 Object o;
1116 try {
1117 final Class<?> targetClass = getTargetClass();
1118 ReflectUtil.checkPackageAccess(targetClass);
1119 @SuppressWarnings("deprecation")
1120 Object tmp = targetClass.newInstance();
1121 o = tmp;
1122 for (int i = 0; i < itemNames.length; i++) {
1123 if (cd.containsKey(itemNames[i])) {
1124 Object openItem = cd.get(itemNames[i]);
1125 Object javaItem =
1126 converters[i].fromOpenValue(openItem);
1127 MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
1128 }
1129 }
1130 } catch (Exception e) {
1131 throw invalidObjectException(e);
1132 }
1133 return o;
1134 }
1135
1136 private Method[] setters;
1137 }
1138
1139 /** Builder for when the target class has a constructor that is
1140 annotated with {@linkplain ConstructorParameters @ConstructorParameters}
1141 or {@code @ConstructorProperties} so we can see the correspondence to getters. */
|