convert CompositeData To String - Java javax.management.openmbean

Java examples for javax.management.openmbean:CompositeData

Description

convert CompositeData To String

Demo Code

/*//from  w ww. j av a 2 s  .c  o  m
 * Copyright (c) 2008-2016 Haulmont.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
//package com.java2s;

import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularType;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object value = "java2s.com";
        System.out.println(convertToString(value));
    }

    public static String convertToString(Object value) {
        if (value == null) {
            return null;
        }

        if (value.getClass().isArray()) {
            StringBuilder b = new StringBuilder("[");
            int length = Array.getLength(value);
            for (int i = 0; i < length; i++) {
                Object o = Array.get(value, i);
                b.append(convertToString(o));
                if (i < length - 1) {
                    b.append(", ");
                }
            }
            return b.append("]").toString();
        } else if (value instanceof CompositeData) {
            return compositeToString((CompositeData) value);
        } else if (value instanceof TabularData) {
            return tabularToString((TabularData) value);
        }
        return value.toString();
    }

    public static boolean isArray(String type) {
        return type != null
                && (type.startsWith("[") && type.length() >= 2
                        && getArrayType(type) != null || type
                            .endsWith("[]"));
    }

    private static String compositeToString(CompositeData compositeData) {
        if (canConvertToTrueObject(compositeData)) {
            try {
                Object trueObject = convertToTrueObject(compositeData);
                return String.valueOf(trueObject);
            } catch (Exception e) {
                return e.getClass().getName() + " " + e.getMessage();
            }
        }

        CompositeType type = compositeData.getCompositeType();

        StringBuilder b = new StringBuilder();
        b.append("[");
        List<String> keys = new ArrayList<>(type.keySet());
        Collections.sort(keys); // alphabetically
        for (String key : keys) {
            b.append(key).append(": ");
            Object value = compositeData.get(key);
            b.append(convertToString(value));
            if (keys.indexOf(key) != keys.size() - 1) {
                b.append(", ");
            }
        }
        b.append("]\n");
        return b.toString();
    }

    private static String tabularToString(TabularData tabularData) {
        TabularType type = tabularData.getTabularType();
        StringBuilder b = new StringBuilder();
        b.append("(").append(type.getTypeName()).append(")\n");
        Collection<CompositeData> values = (Collection) tabularData
                .values();
        for (CompositeData cd : values) {
            b.append(compositeToString(cd));
        }
        return b.toString();
    }

    public static Class getArrayType(String type) {
        if (type.endsWith("[]")) {
            try {
                return Class.forName(type.substring(0, type.length() - 2));
            } catch (ClassNotFoundException e) {
                throw new RuntimeException("Wrong array type");
            }
        }
        switch (type.substring(0, 2)) {
        case "[Z":
            return boolean.class;
        case "[B":
            return byte.class;
        case "[S":
            return short.class;
        case "[I":
            return int.class;
        case "[J":
            return long.class;
        case "[F":
            return float.class;
        case "[D":
            return double.class;
        case "[C":
            return char.class;
        case "[L":
            return Object.class; // any non-primitives(Object)
        }
        throw new RuntimeException("Wrong array type");
    }

    private static boolean canConvertToTrueObject(
            CompositeData compositeData) {
        CompositeType type = compositeData.getCompositeType();
        try {
            Class<?> _class = Class.forName(type.getTypeName());
            Method method = _class.getMethod("from", CompositeData.class);
            if (Modifier.isStatic(method.getModifiers())
                    && Modifier.isPublic(method.getModifiers())
                    && method.getReturnType() == _class) {
                return true;
            }
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            return false;
        }

        return false;
    }

    private static Object convertToTrueObject(CompositeData compositeData) {
        CompositeType type = compositeData.getCompositeType();
        try {
            Class<?> _class = Class.forName(type.getTypeName());
            Method method = _class.getMethod("from", CompositeData.class);
            if (Modifier.isStatic(method.getModifiers())
                    && method.getReturnType() == _class) {
                return method.invoke(null, compositeData);
            }
            return null;
        } catch (ClassNotFoundException | NoSuchMethodException
                | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials