CollectionMapping(Type targetType, ArrayType<?> openArrayType, Class<?> openArrayClass, MXBeanMapping elementMapping) { super(targetType, openArrayType); this.elementMapping = elementMapping; /* Determine the concrete class to be used when converting back to this Java type. We convert all Lists to ArrayList and all Sets to TreeSet. (TreeSet because it is a SortedSet, so works for both Set and SortedSet.) */ Type raw = ((ParameterizedType) targetType).getRawType(); Class<?> c = (Class<?>) raw; final Class<?> collC; if (c == List.class) collC = ArrayList.class; else if (c == Set.class) collC = HashSet.class; else if (c == SortedSet.class) collC = TreeSet.class; else { // can't happen assert(false); collC = null; } collectionClass = Util.cast(collC); }
CollectionConverter(Type targetType, ArrayType openArrayType, Class openArrayClass, OpenTypeConverter elementConverter) { super(targetType, openArrayType, openArrayClass); this.elementConverter = elementConverter; /* * Determine the concrete class to be used when converting back to this Java type. We convert * all Lists to ArrayList and all Sets to TreeSet. (TreeSet because it is a SortedSet, so works * for both Set and SortedSet.) */ Type raw = ((ParameterizedType) targetType).getRawType(); Class c = (Class<?>) raw; if (c == List.class) collectionClass = ArrayList.class; else if (c == Set.class) collectionClass = HashSet.class; else if (c == SortedSet.class) collectionClass = TreeSet.class; else { // can't happen assert (false); collectionClass = null; } }
private static boolean isTypeMatched(OpenType<?> ot1, OpenType<?> ot2) { if (ot1 instanceof CompositeType) { if (! (ot2 instanceof CompositeType)) return false; if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2)) return false; } else if (ot1 instanceof TabularType) { if (! (ot2 instanceof TabularType)) return false; if (!isTypeMatched((TabularType) ot1, (TabularType) ot2)) return false; } else if (ot1 instanceof ArrayType) { if (! (ot2 instanceof ArrayType)) return false; if (!isTypeMatched((ArrayType<?>) ot1, (ArrayType<?>) ot2)) { return false; } } else if (!ot1.equals(ot2)) { return false; } return true; }
private ArrayType<?> getVersionedArrayType(ArrayType<?> type, String version) throws OpenDataException { if (type.isPrimitiveArray()) { return type; } OpenType<?> ot = getVersionedType( type.getElementOpenType(), version ); if (ot instanceof SimpleType) { return new ArrayType<>((SimpleType<?>)ot, type.isPrimitiveArray()); } else { return new ArrayType<>(type.getDimension(), ot); } }
@Before public void before() throws OpenDataException { exceptionDetailsItemNames = new String[]{ "lastException", "lastExceptionCausedObject", "lastExceptionStackTrace", "lastExceptionTimestamp", "totalExceptions" }; exceptionDetailsItemTypes = new OpenType<?>[]{ SimpleType.STRING, SimpleType.STRING, new ArrayType<>(1, SimpleType.STRING), SimpleType.LONG, SimpleType.INTEGER }; }
private static OpenType getOpenType(Object value) throws OpenDataException { if (value == null) { return SimpleType.VOID; } //if (OpenType.ALLOWED_CLASSNAMES_LIST.contains(name)) { int dim = 0; Class<?> cls = value.getClass(); while (cls.isArray()) { cls = value.getClass().getComponentType(); dim++; } SimpleType<?> type = getTypeForName(cls.getName()); if (type != null && dim > 0) { if (cls.isPrimitive() && dim == 1) { return new ArrayType<>(type, true); } return new ArrayType<>(dim, type); } return type; }
public void test_from_scenario2() throws Exception { initialValues[0] = "1"; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { // Expected } }
public void test_from_scenario4() throws Exception { initialValues[0] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario5() throws Exception { initialValues[1] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { // Expected } }
public void test_from_scenario6() throws Exception { initialValues[2] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario7() throws Exception { initialValues[3] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario8() throws Exception { initialValues[4] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario9() throws Exception { initialValues[5] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario10() throws Exception { initialValues[6] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario11() throws Exception { initialValues[7] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario12() throws Exception { initialValues[8] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario14() throws Exception { initialValues[10] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw NullPointerException"); } catch (NullPointerException e) { // Expected } }
public void test_from_scenario16() throws Exception { initialValues[12] = null; ArrayType stackTraceArray = new ArrayType(1, stackTraceElementType); OpenType[] types = { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.STRING, SimpleType.LONG, SimpleType.STRING, stackTraceArray, SimpleType.STRING }; CompositeType compositeType = getCompositeType(initialNames, types); CompositeData data = new CompositeDataSupport(compositeType, initialNames, initialValues); try { ThreadInfo.from(data); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { // Expected } }
@Test public void testByteArrayObject() throws Exception { ModelNode description = createDescription(ModelType.OBJECT, ModelType.BYTES); TypeConverter converter = getConverter(description); assertMapType(assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, ArrayType.getPrimitiveArrayType(byte[].class)); ModelNode node = new ModelNode(); node.get("one").set(new byte[] {1,2}); node.get("two").set(new byte[] {3,4}); TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node)); Assert.assertEquals(2, tabularData.size()); Assert.assertTrue(Arrays.equals(new byte[] {1,2}, (byte[])tabularData.get(new Object[] {"one"}).get("value"))); Assert.assertTrue(Arrays.equals(new byte[] {3,4}, (byte[])tabularData.get(new Object[] {"two"}).get("value"))); //Allow plain map as well? Yeah why not! Map<String, byte[]> map = new HashMap<String, byte[]>(); map.put("one", new byte[] {1,2}); map.put("two", new byte[] {3,4}); Assert.assertEquals(node, converter.toModelNode(map)); }
@Test public void testSimpleTypeExpressionList() throws Exception { ModelNode description = createDescription(ModelType.LIST, ModelType.INT); description.get(EXPRESSIONS_ALLOWED).set(true); TypeConverter converter = getConverter(description); assertCast(ArrayType.class, converter.getOpenType()); ModelNode node = new ModelNode(); node.add().set(new ValueExpression("${this.should.not.exist.!!!!!:1}")); node.add().set(new ValueExpression("${this.should.not.exist.!!!!!:2}")); String[] data = assertCast(String[].class, converter.fromModelNode(node)); Assert.assertEquals("${this.should.not.exist.!!!!!:1}", data[0]); Assert.assertEquals("${this.should.not.exist.!!!!!:2}", data[1]); ModelNode newNode = converter.toModelNode(data); Assert.assertEquals(node, newNode); }
@Test public void testSimpleTypeExpressionList() throws Exception { ModelNode description = createDescription(ModelType.LIST, ModelType.INT); description.get(EXPRESSIONS_ALLOWED).set(true); TypeConverter converter = getConverter(description); ArrayType<?> arrayType = assertCast(ArrayType.class, converter.getOpenType()); Assert.assertEquals(SimpleType.INTEGER, arrayType.getElementOpenType()); ModelNode node = new ModelNode(); node.add().set(new ValueExpression("${this.should.not.exist.!!!!!:1}")); node.add().set(new ValueExpression("${this.should.not.exist.!!!!!:2}")); Integer[] data = assertCast(Integer[].class, converter.fromModelNode(node)); Assert.assertEquals(Integer.valueOf(1), data[0]); Assert.assertEquals(Integer.valueOf(2), data[1]); }
public static Object[] getItemValues(Object bean) throws OpenDataException { Class<?> beanClass = bean.getClass(); List<Object> items = new ArrayList<>(); for (Method method : getGetters(beanClass)) { Object value; try { value = method.invoke(bean, (Object[]) null); } catch (Exception ex) { OpenDataException ode = new OpenDataException("Cannot obtain vaue from: " + method); ode.initCause(ex); throw ode; } Class<?> valueType = value.getClass(); OpenType<?> openType = getOpenType(valueType); if (openType == SimpleType.STRING) { items.add(value); } else if (openType instanceof ArrayType) { items.add(value); } else if (openType instanceof CompositeType) { items.add(toCompositeData(value)); } else { throw new OpenDataException("Unsupported open type for: " + openType); } } return items.toArray(new Object[items.size()]); }
/** * initialises the openmbean data types */ private static void init() throws OpenDataException { _msgContentAttributeTypes[0] = SimpleType.LONG; // For message id _msgContentAttributeTypes[1] = SimpleType.STRING; // For MimeType _msgContentAttributeTypes[2] = SimpleType.STRING; // For Encoding _msgContentAttributeTypes[3] = new ArrayType(1, SimpleType.BYTE); // For message content _msgContentType = new CompositeType("Message Content", "AMQ Message Content", VIEW_MSG_CONTENT_COMPOSITE_ITEM_NAMES_DESC.toArray(new String[VIEW_MSG_CONTENT_COMPOSITE_ITEM_NAMES_DESC.size()]), VIEW_MSG_CONTENT_COMPOSITE_ITEM_NAMES_DESC.toArray(new String[VIEW_MSG_CONTENT_COMPOSITE_ITEM_NAMES_DESC.size()]), _msgContentAttributeTypes); _msgAttributeTypes[0] = SimpleType.LONG; // For message id _msgAttributeTypes[1] = new ArrayType(1, SimpleType.STRING); // For header attributes _msgAttributeTypes[2] = SimpleType.LONG; // For size _msgAttributeTypes[3] = SimpleType.BOOLEAN; // For redelivered _msgAttributeTypes[4] = SimpleType.LONG; // For queue position _messageDataType = new CompositeType("Message", "AMQ Message", VIEW_MSGS_COMPOSITE_ITEM_NAMES_DESC.toArray(new String[VIEW_MSGS_COMPOSITE_ITEM_NAMES_DESC.size()]), VIEW_MSGS_COMPOSITE_ITEM_NAMES_DESC.toArray(new String[VIEW_MSGS_COMPOSITE_ITEM_NAMES_DESC.size()]), _msgAttributeTypes); _messagelistDataType = new TabularType("Messages", "List of messages", _messageDataType, VIEW_MSGS_TABULAR_UNIQUE_INDEX.toArray(new String[VIEW_MSGS_TABULAR_UNIQUE_INDEX.size()])); }