jdk/test/javax/management/remote/mandatory/loading/SingleClassLoader.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 5506 202f599c92aa
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, 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) 2003, 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
 * build         @BUILD_TAG_PLACEHOLDER@
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @COPYRIGHT_MINI_LEGAL_NOTICE_PLACEHOLDER@
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  ClassLoader that knows how to fabricate exactly one class.  The name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  of the class is defined by the parameter singleClassName.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  asked to load a class, this loader first delegates to its parent as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  usual, then, if that doesn't find the class and if the name is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  same as singleClassName, the class is fabricated.  It is a public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  class with no fields or methods and a single public no-arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  constructor that simply calls its parent's no-arg constructor.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  means that the parent must have a public or protected no-arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class SingleClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    SingleClassLoader(String singleClassName, Class superclass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                      ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Constructor superConstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            superConstr = superclass.getDeclaredConstructor(new Class[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throw new IllegalArgumentException("Superclass must have no-arg " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                               "constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        int superConstrMods = superConstr.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if ((superConstrMods & (Modifier.PUBLIC|Modifier.PROTECTED)) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                "Superclass no-arg constructor must be public or protected";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.singleClassName = singleClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        final Class c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            c = makeClass(superclass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.singleClass = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private Class makeClass(Class superclass) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        final String superName = superclass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        dout = new DataOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        String thisNameInternal = singleClassName.replace('.', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String superNameInternal = superName.replace('.', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        dout.writeInt(0xcafebabe); // magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        dout.writeInt(0x0003002d); // major 45 minor 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        dout.writeShort(10);       // cpool count (incl virtual 0 element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        cpoolIndex = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int thisNameConst = writeUTFConst(thisNameInternal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int thisClassConst = writeClassConst(thisNameConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int superNameConst = writeUTFConst(superNameInternal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int superClassConst = writeClassConst(superNameConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int initNameConst = writeUTFConst("<init>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        int voidNoArgSigConst = writeUTFConst("()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        int codeNameConst = writeUTFConst("Code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int superConstructorNameAndTypeConst =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            writeNameAndTypeConst(initNameConst, voidNoArgSigConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        int superConstructorMethodRefConst =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            writeMethodRefConst(superClassConst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                superConstructorNameAndTypeConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        dout.writeShort(Modifier.PUBLIC | 0x20 /*SUPER*/);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        dout.writeShort(thisClassConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        dout.writeShort(superClassConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        dout.writeInt(0);   // n interfaces, n fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        dout.writeShort(1); // n methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // <init> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        dout.writeShort(Modifier.PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        dout.writeShort(initNameConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        dout.writeShort(voidNoArgSigConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        dout.writeShort(1); // attr count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // Code attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        dout.writeShort(codeNameConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        dout.writeInt(17);   // len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        dout.writeShort(1);  // max stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        dout.writeShort(1);  // max locals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        dout.writeInt(5);    // code len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        dout.writeByte(42);  // aload_0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        dout.writeByte(183); // invokespecial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        dout.writeShort(superConstructorMethodRefConst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        dout.writeByte(177); // return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        dout.writeShort(0);  // 0 catches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        dout.writeShort(0);  // 0 method attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        dout.writeShort(0);  // 0 class attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        dout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        byte[] classBytes = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        dout = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            defineClass(singleClassName, classBytes, 0, classBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    protected Class findClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (name.equals(singleClassName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return singleClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private int writeUTFConst(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        dout.writeByte(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        dout.writeUTF(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return cpoolIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private int writeClassConst(int nameIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        dout.writeByte(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        dout.writeShort((short) nameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return cpoolIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private int writeNameAndTypeConst(int nameIndex, int typeIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        dout.writeByte(12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        dout.writeShort((short) nameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        dout.writeShort((short) typeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return cpoolIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private int writeMethodRefConst(int classIndex, int nameAndTypeIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        dout.writeByte(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        dout.writeShort((short) classIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        dout.writeShort((short) nameAndTypeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return cpoolIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private final String singleClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private final Class singleClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private DataOutputStream dout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private int cpoolIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}