jdk/test/javax/management/ObjectName/CompressedStorageTest.java
author rprotacio
Wed, 02 Mar 2016 15:10:38 -0500
changeset 36396 26a241a959de
parent 32415 24c214d2ccba
child 42338 a60f280f803c
permissions -rw-r--r--
8145098: JNI GetVersion should return JNI_VERSION_9 Summary: Updated JNI_VERSION for current version to be JNI_VERSION_9 Reviewed-by: hseigel, gtriantafill, dholmes, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32415
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
     1
/*
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
     4
 *
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
     7
 * published by the Free Software Foundation.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
     8
 *
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    13
 * accompanied this code).
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    14
 *
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    18
 *
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    21
 * questions.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    22
 */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    23
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    24
/*
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    25
 * @test
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    26
 * @bug 8041565
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    27
 * @summary Tests the limits imposed on the domain name part of an
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    28
 *          ObjectName instance
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    29
 * @author Jaroslav Bachorik
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    30
 * @modules java.management
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    31
 * @run main CompressedStorageTest
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    32
 */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    33
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    34
import java.lang.reflect.Field;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    35
import java.lang.reflect.InvocationTargetException;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    36
import java.lang.reflect.Method;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    37
import java.util.function.Consumer;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    38
import javax.management.MalformedObjectNameException;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    39
import javax.management.ObjectName;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    40
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    41
public class CompressedStorageTest {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    42
    private static Method setDomainLengthM;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    43
    private static Field compressedStorageFld;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    44
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    45
    private static int DOMAIN_PATTERN;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    46
    private static int PROPLIST_PATTERN;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    47
    private static int PROPVAL_PATTERN;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    48
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    49
    private static Method setDomainPattern;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    50
    private static Method setPropertyListPattern;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    51
    private static Method setPropertyValuePattern;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    52
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    53
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    54
    static {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    55
        try {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    56
            Class<?> clz = ObjectName.class;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    57
            setDomainLengthM = clz.getDeclaredMethod("setDomainLength", int.class);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    58
            setDomainLengthM.setAccessible(true);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    59
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    60
            compressedStorageFld = clz.getDeclaredField("_compressed_storage");
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    61
            compressedStorageFld.setAccessible(true);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    62
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    63
            setDomainPattern = clz.getDeclaredMethod("setDomainPattern", boolean.class);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    64
            setDomainPattern.setAccessible(true);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    65
            setPropertyListPattern = clz.getDeclaredMethod("setPropertyListPattern", boolean.class);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    66
            setPropertyListPattern.setAccessible(true);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    67
            setPropertyValuePattern = clz.getDeclaredMethod("setPropertyValuePattern", boolean.class);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    68
            setPropertyValuePattern.setAccessible(true);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    69
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    70
            DOMAIN_PATTERN = getStaticIntFld("DOMAIN_PATTERN");
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    71
            PROPLIST_PATTERN = getStaticIntFld("PROPLIST_PATTERN");
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    72
            PROPVAL_PATTERN = getStaticIntFld("PROPVAL_PATTERN");
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    73
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    74
        } catch (Exception e) {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    75
            throw new Error(e);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    76
        }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    77
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    78
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    79
    public static void main(String[] args) throws Exception {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    80
        testZeroLength();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    81
        testNegativeLength();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    82
        testMaxLength();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    83
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    84
        testSetDomainPattern();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    85
        testSetPropertyListPattern();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    86
        testSetPropertyValuePattern();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    87
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    88
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    89
    private static ObjectName getObjectName()
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    90
    throws MalformedObjectNameException {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    91
        return new ObjectName("domain", "key", "value");
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    92
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    93
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    94
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    95
     * Test for accepting 0 being passed as argument to
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    96
     * {@linkplain ObjectName#setDomainLength(int)}.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    97
     *
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    98
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
    99
    private static void testZeroLength() throws Exception {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   100
        setDomainNameLength(0);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   101
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   102
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   103
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   104
     * Test for rejecting negative value being passed as argument to
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   105
     * {@linkplain ObjectName#setDomainLength(int)}.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   106
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   107
    private static void testNegativeLength() throws Exception {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   108
        try {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   109
            setDomainNameLength(-1);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   110
        } catch (MalformedObjectNameException e) {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   111
            return;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   112
        }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   113
        fail("Allowing negative domain name length");
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   114
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   115
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   116
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   117
     * Test for rejecting value exceeding the maximum allowed length
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   118
     * being passed as argument to {@linkplain ObjectName#setDomainLength(int)}.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   119
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   120
    private static void testMaxLength() throws Exception {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   121
        try {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   122
            setDomainNameLength(Integer.MAX_VALUE / 4 + 1);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   123
        } catch (MalformedObjectNameException e) {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   124
            return;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   125
        }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   126
        fail("Maximum domain name length is not respected");
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   127
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   128
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   129
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   130
     * Tests that calling {@linkplain ObjectName#setDomainPattern(boolean)}
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   131
     * results in setting correct bits in {@linkplain ObjectName#_compressed_storage}.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   132
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   133
    private static void testSetDomainPattern() throws Exception {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   134
        ObjectName on = getObjectName();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   135
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   136
        checkMask(DOMAIN_PATTERN, setDomainPattern, on);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   137
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   138
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   139
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   140
     * Tests that calling {@linkplain ObjectName#setPropertyListPattern(boolean)}
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   141
     * results in setting correct bits in {@linkplain ObjectName#_compressed_storage}.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   142
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   143
    private static void testSetPropertyListPattern() throws Exception {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   144
        ObjectName on = getObjectName();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   145
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   146
        checkMask(PROPLIST_PATTERN, setPropertyListPattern, on);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   147
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   148
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   149
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   150
     * Tests that calling {@linkplain ObjectName#setPropertyValuePattern(boolean)}
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   151
     * results in setting correct bits in {@linkplain ObjectName#_compressed_storage}.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   152
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   153
    private static void testSetPropertyValuePattern() throws Exception {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   154
        ObjectName on = getObjectName();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   155
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   156
        checkMask(PROPVAL_PATTERN, setPropertyValuePattern, on);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   157
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   158
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   159
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   160
     * Helper method to call {@linkplain ObjectName#setDomainLength(int)}
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   161
     * method via reflection.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   162
     * @param len The domain name length
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   163
     * @throws MalformedObjectNameException Propagated from
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   164
     *           {@linkplain ObjectName#setDomainLength(int)} invocation.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   165
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   166
    private static void setDomainNameLength(int len)
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   167
    throws MalformedObjectNameException {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   168
        try {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   169
            setDomainLengthM.invoke(getObjectName(), len);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   170
        } catch (InvocationTargetException e) {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   171
            Throwable cause = e.getCause();
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   172
            if (cause instanceof MalformedObjectNameException) {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   173
                throw (MalformedObjectNameException)cause;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   174
            }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   175
            throw new Error(cause);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   176
        } catch (IllegalAccessException | IllegalArgumentException e) {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   177
            throw new Error(e);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   178
        }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   179
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   180
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   181
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   182
     * Helper method to assert that a particular boolean setter affects only
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   183
     * a particular bit in the {@linkplain ObjectName#_compressed_storage} field.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   184
     * @param mask bitmask for storing the boolean value
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   185
     * @param setter setter method reference
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   186
     * @param on {@linkplain ObjectName} instance
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   187
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   188
    private static void checkMask(int mask, Method setter, ObjectName on)
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   189
    throws Exception {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   190
        int valBefore = compressedStorageFld.getInt(on);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   191
        setter.invoke(on, true);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   192
        int valAfter = compressedStorageFld.getInt(on);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   193
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   194
        checkMask(mask, valAfter ^ valBefore);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   195
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   196
        valBefore = valAfter;
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   197
        setter.invoke(on, false);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   198
        valAfter = compressedStorageFld.getInt(on);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   199
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   200
        checkMask(mask, valAfter ^ valBefore);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   201
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   202
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   203
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   204
     * Compare the changed bits with the given mask.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   205
     * @param mask bitmask
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   206
     * @param val the changed bits; may be 0 if there was no change
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   207
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   208
    private static void checkMask(int mask, int val) {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   209
        if (val != 0 && val != mask) {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   210
            fail("Invalid mask: expecting '" +
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   211
                    Integer.toBinaryString(mask) + "' , received '" +
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   212
                    Integer.toBinaryString(val) + "'");
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   213
        }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   214
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   215
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   216
    /**
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   217
     * Helper method to obtain the value of a static field via reflection.
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   218
     * @param name static field name
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   219
     * @return static field value
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   220
     */
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   221
    private static int getStaticIntFld(String name) throws Exception {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   222
        Field fld = ObjectName.class.getDeclaredField(name);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   223
        fld.setAccessible(true);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   224
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   225
        return fld.getInt(null);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   226
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   227
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   228
    private static void fail(String msg) {
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   229
        throw new Error(msg);
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   230
    }
24c214d2ccba 8041565: JMX ObjectName could be refactored to save memory
jbachorik
parents:
diff changeset
   231
}