jdk/test/java/lang/management/CompositeData/OpenTypeConverter.java
author amlu
Fri, 18 Nov 2016 14:39:03 +0800
changeset 42166 5582c83b1156
parent 5506 202f599c92aa
permissions -rw-r--r--
8169836: ProblemList update for java/lang/management/MemoryMXBean/PendingAllGC.sh Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug     5024531
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Utility class to convert a struct-like class to a CompositeData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.openmbean.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static javax.management.openmbean.SimpleType.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A converter utiltiy class to automatically convert a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * class to a CompositeType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class OpenTypeConverter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final WeakHashMap<Class,OpenType> convertedTypes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        new WeakHashMap<Class,OpenType>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final OpenType[] simpleTypes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        BIGDECIMAL, BIGINTEGER, BOOLEAN, BYTE, CHARACTER, DATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        DOUBLE, FLOAT, INTEGER, LONG, OBJECTNAME, SHORT, STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        VOID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (int i = 0; i < simpleTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            final OpenType t = simpleTypes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            Class c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                c = Class.forName(t.getClassName(), false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                  String.class.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                // the classes that these predefined types declare must exist!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                c = null; // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            convertedTypes.put(c, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (c.getName().startsWith("java.lang.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    final Field typeField = c.getField("TYPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    final Class primitiveType = (Class) typeField.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    convertedTypes.put(primitiveType, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                } catch (NoSuchFieldException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    // OK: must not be a primitive wrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    // Should not reach here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static class InProgress extends OpenType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        private static final String description =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                  "Marker to detect recursive type use -- internal use only!";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        InProgress() throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            super("java.lang.String", "java.lang.String", description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public boolean isValue(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final OpenType inProgress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        OpenType t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            t = new InProgress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // Should not reach here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        inProgress = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // Convert a class to an OpenType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static synchronized OpenType toOpenType(Class c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        OpenType t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        t = convertedTypes.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (t instanceof InProgress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                throw new OpenDataException("Recursive data structure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        convertedTypes.put(c, inProgress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (Enum.class.isAssignableFrom(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            t = STRING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        else if (c.isArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            t = makeArrayType(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            t = makeCompositeType(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        convertedTypes.put(c, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static OpenType makeArrayType(Class c) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        for (dim = 0; c.isArray(); dim++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            c = c.getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return new ArrayType(dim, toOpenType(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private static OpenType makeCompositeType(Class c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Make a CompositeData containing all the getters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        final Method[] methods = c.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        final List<String> names = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        final List<OpenType> types = new ArrayList<OpenType>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        /* Select public methods that look like "T getX()" or "boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
           isX() or hasX()", where T is not void and X is not the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
           string.  Exclude "Class getClass()" inherited from Object.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        for (int i = 0; i < methods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            final Method method = methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            final String name = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            final Class type = method.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            final String rest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (name.startsWith("get"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                rest = name.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            else if (name.startsWith("is") && type == boolean.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                rest = name.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            else if (name.startsWith("has") && type == boolean.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                rest = name.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (rest.equals("") || method.getParameterTypes().length > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                || type == void.class || rest.equals("Class"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            names.add(decapitalize(rest));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            types.add(toOpenType(type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        final String[] nameArray = names.toArray(new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return new CompositeType(c.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                 c.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                 nameArray, // field names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                 nameArray, // field descriptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                 types.toArray(new OpenType[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Utility method to take a string and convert it to normal Java variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * name capitalization.  This normally means converting the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * character from upper case to lower case, but in the (unusual) special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * case when there is more than one character and both the first and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * second characters are upper case, we leave it alone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * as "URL".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param  name The string to be decapitalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return  The decapitalized version of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private static String decapitalize(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (name == null || name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        Character.isUpperCase(name.charAt(0))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        char chars[] = name.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        chars[0] = Character.toLowerCase(chars[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return new String(chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
}