hotspot/test/runtime/testlibrary/GeneratedClassLoader.java
author shade
Thu, 02 Oct 2014 17:13:31 +0400
changeset 27003 19617f4c9dd0
parent 17827 de9b8cf69309
permissions -rw-r--r--
8059595: Verifier::verify is wasting time before is_eligible_for_verification check Summary: Do the check earlier. Reviewed-by: hseigel, kamg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17827
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     1
/*
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     4
 *
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     8
 *
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    14
 *
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    18
 *
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    21
 * questions.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    22
 */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    23
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    24
import java.io.DataInputStream;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    25
import java.io.File;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    26
import java.io.FileInputStream;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    27
import java.io.FileWriter;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    28
import java.io.IOException;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    29
import java.io.PrintWriter;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    30
import javax.tools.JavaCompiler;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    31
import javax.tools.ToolProvider;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    32
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    33
/**
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    34
 * A class loader that generates new classes.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    35
 * The generated classes are made by first emitting java sources with nested
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    36
 * static classes, these are then compiled and the class files are read back.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    37
 * Some efforts are made to make the class instances unique and of not insignificant
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    38
 * size.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    39
 */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    40
public class GeneratedClassLoader extends ClassLoader {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    41
    /**
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    42
     * Holds a pair of class bytecodes and class name (for use with defineClass).
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    43
     */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    44
    private static class GeneratedClass {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    45
        public byte[] bytes;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    46
        public String name;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    47
        public GeneratedClass(byte[] bytes, String name) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    48
            this.bytes = bytes; this.name = name;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    49
        }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    50
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    51
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    52
    /**
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    53
     * Used to uniquely name every class generated.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    54
     */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    55
    private static int count = 0;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    56
    /**
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    57
     * Used to enable/disable keeping the class files and java sources for
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    58
     * the generated classes.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    59
     */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    60
    private static boolean deleteFiles = Boolean.parseBoolean(
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    61
        System.getProperty("GeneratedClassLoader.deleteFiles", "true"));
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    62
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    63
    private static String bigstr =
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    64
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    65
        + "In facilisis scelerisque vehicula. Donec congue nisi a "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    66
        + "leo posuere placerat lobortis felis ultrices. Pellentesque "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    67
        + "habitant morbi tristique senectus et netus et malesuada "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    68
        + "fames ac turpis egestas. Nam tristique velit at felis "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    69
        + "iaculis at tempor sem vestibulum. Sed adipiscing lectus "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    70
        + "non mi molestie sagittis. Morbi eu purus urna. Nam tempor "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    71
        + "tristique massa eget semper. Mauris cursus, nulla et ornare "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    72
        + "vehicula, leo dolor scelerisque metus, sit amet rutrum erat "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    73
        + "sapien quis dui. Nullam eleifend risus et velit accumsan sed "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    74
        + "suscipit felis pulvinar. Nullam faucibus suscipit gravida. "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    75
        + "Pellentesque habitant morbi tristique senectus et netus et "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    76
        + "malesuada fames ac turpis egestas. Nullam ut massa augue, "
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    77
        + "nec viverra mauris.";
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    78
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    79
    private static int getNextCount() {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    80
        return count++;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    81
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    82
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    83
    ////// end statics
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    84
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    85
    private JavaCompiler javac;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    86
    private String nameBase;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    87
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    88
    public GeneratedClassLoader() {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    89
        javac = ToolProvider.getSystemJavaCompiler();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    90
        nameBase = "TestSimpleClass";
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    91
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    92
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    93
    private long getBigValue(int which) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    94
        // > 65536 is too large to encode in the bytecode
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    95
        // so this will force us to emit a constant pool entry for this int
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    96
        return (long)which + 65537;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    97
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    98
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
    99
    private String getBigString(int which) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   100
        return bigstr + which;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   101
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   102
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   103
    private String getClassName(int count) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   104
        return nameBase + count;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   105
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   106
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   107
    private String generateSource(int count, int sizeFactor, int numClasses) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   108
        StringBuilder sb = new StringBuilder();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   109
        sb.append("public class ").append(getClassName(count)).append("{\n");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   110
        for (int j = 0; j < numClasses; ++j) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   111
            sb.append("public static class ")
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   112
              .append("Class")
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   113
              .append(j)
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   114
              .append("{\n");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   115
            for (int i = 0; i < sizeFactor; ++i) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   116
                int value = i;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   117
                sb.append("private long field")
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   118
                  .append(i).append(" = ")
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   119
                  .append(getBigValue(value++))
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   120
                  .append(";\n");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   121
                sb.append("public long method")
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   122
                  .append(i)
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   123
                  .append("() {\n");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   124
                sb.append("return ")
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   125
                  .append(getBigValue(value++))
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   126
                  .append(";");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   127
                sb.append("}\n");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   128
                sb.append("private String str").append(i)
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   129
                  .append(" = \"")
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   130
                  .append(getBigString(i))
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   131
                  .append("\";");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   132
            }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   133
            sb.append("\n}");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   134
        }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   135
        sb.append("\n}");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   136
        return sb.toString();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   137
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   138
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   139
    private GeneratedClass[] getGeneratedClass(int sizeFactor, int numClasses) throws IOException {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   140
        int uniqueCount = getNextCount();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   141
        String src = generateSource(uniqueCount, sizeFactor, numClasses);
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   142
        String className = getClassName(uniqueCount);
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   143
        File file = new File(className + ".java");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   144
        try (PrintWriter pw = new PrintWriter(new FileWriter(file))) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   145
            pw.append(src);
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   146
            pw.flush();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   147
        }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   148
        int exitcode = javac.run(null, null, null, file.getCanonicalPath());
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   149
        if (exitcode != 0) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   150
            throw new RuntimeException("javac failure when compiling: " +
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   151
                    file.getCanonicalPath());
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   152
        } else {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   153
            if (deleteFiles) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   154
                file.delete();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   155
            }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   156
        }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   157
        GeneratedClass[] gc = new GeneratedClass[numClasses];
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   158
        for (int i = 0; i < numClasses; ++i) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   159
            String name = className + "$" + "Class" + i;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   160
            File classFile = new File(name + ".class");
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   161
            byte[] bytes;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   162
            try (DataInputStream dis = new DataInputStream(new FileInputStream(classFile))) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   163
                bytes = new byte[dis.available()];
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   164
                dis.readFully(bytes);
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   165
            }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   166
            if (deleteFiles) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   167
                classFile.delete();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   168
            }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   169
            gc[i] = new GeneratedClass(bytes, name);
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   170
        }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   171
        if (deleteFiles) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   172
            new File(className + ".class").delete();
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   173
        }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   174
        return gc;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   175
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   176
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   177
    /**
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   178
     * Generate a single class, compile it and load it.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   179
     * @param sizeFactor Fuzzy measure of how large the class should be.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   180
     * @return the Class instance.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   181
     * @throws IOException
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   182
     */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   183
    public Class<?> generateClass(int sizeFactor) throws IOException {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   184
        return getGeneratedClasses(sizeFactor, 1)[0];
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   185
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   186
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   187
    /**
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   188
     * Generate several classes, compile and load them.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   189
     * @param sizeFactor Fuzzy measure of how large each class should be.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   190
     * @param numClasses The number of classes to create
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   191
     * @return an array of the Class instances.
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   192
     * @throws IOException
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   193
     */
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   194
    public Class<?>[] getGeneratedClasses(int sizeFactor, int numClasses) throws IOException {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   195
        GeneratedClass[] gc = getGeneratedClass(sizeFactor, numClasses);
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   196
        Class<?>[] classes = new Class[numClasses];
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   197
        for (int i = 0; i < numClasses; ++i) {
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   198
            classes[i] = defineClass(gc[i].name, gc[i].bytes, 0 , gc[i].bytes.length);
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   199
        }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   200
        return classes;
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   201
    }
de9b8cf69309 8009576: Test returns ClassNotFoundException
ctornqvi
parents:
diff changeset
   202
}