File: IAE_Loader2.java

package info (click to toggle)
openjdk-11 11.0.29%2B7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,596 kB
  • sloc: java: 5,209,028; xml: 1,192,267; cpp: 1,143,585; ansic: 462,354; javascript: 162,416; sh: 16,740; objc: 13,730; python: 4,757; asm: 3,570; makefile: 2,969; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (166 lines) | stat: -rw-r--r-- 6,314 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (c) 2018 by SAP AG, Walldorf, Germany.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

package test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.SecureClassLoader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.Collectors;

/**
 * This is a class loader which can load the same classes as another class loader.
 * <p>
 * This is mainly useful for tests when you want to load a class, but do it with a class
 * loader you can dispose. The clone loader just asks the loader to be cloned to get
 * the bytecodes, but defines the class itself.
 * <p>
 * Additionally you can specify a set of classes the loader should not be able to load.
 */
public class IAE_Loader2 extends SecureClassLoader {

    /**
     * The class loaded to clone.
     */
    private final ClassLoader toClone;

    /**
     * The strings we cannot load.
     */
    private final HashSet<String> notLoadable;

    /**
     * The strings we just delegate.
     */
    private final HashSet<String> simpleDelegate;

    /**
     * Creates a class loader which can load the same classes as the loader which
     * loaded the <code>IAE_Loader2</code> class itself.
     * <p>
     * Only the classes which are loadable by the 'parent' loader are delegated to that
     * loader (to make it possible mix classes).
     *
     * @param name the name of the class loader.
     * @param parent the parent loader which is first asked to load a class.
     * @param toClone the class loader to mimic. The clone class loader will be able to
     *                load the same classes as the 'toClone' loader.
     * @param notLoadable The classes we should not be able to load via this loader.
     * @param simpleDelegate The names of the classes for which we simply delegate.
     */
    public IAE_Loader2(String name, ClassLoader parent, ClassLoader toClone,
                       String[] notLoadable, String[] simpleDelegate) {
        super(name, parent);

        this.toClone = toClone;
        this.notLoadable    = Arrays.stream(notLoadable).collect(Collectors.toCollection(HashSet<String>::new));
        this.simpleDelegate = Arrays.stream(simpleDelegate).collect(Collectors.toCollection(HashSet<String>::new));
    }

    /**
     * @see java.lang.ClassLoader#findClass(java.lang.String)
     */
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        if (notLoadable.contains(name)) {
            throw new ClassNotFoundException("The clone class loader explicitely " +
                    "didn't found the class");
        }

        if (simpleDelegate.contains(name)) {
            return toClone.loadClass(name);
        }

        // We just ask the wrapper class loader to find the resource for us
        URL res = toClone.getResource(name.replace('.', '/') + ".class");

        if (res == null) {
            throw new ClassNotFoundException(name);
        }

        try {
            InputStream is = res.openStream();
            byte[] code = readStreamIntoBuffer(is, 8192);
            is.close();
            return defineClass(name, code, 0, code.length);
        } catch (IOException e) {
            throw new ClassNotFoundException(name, e);
        }
    }

    /**
     * Reads all data of a stream into a byte array. The method allocates as
     * much memory as necessary to put the whole data into that byte
     * array. The data is read in chunks of <code>chunkSize</code>
     * chunks.<br><br>
     * <b>Implementation Note: </b> The data is read in chunks of
     * <code>chunkSize</code> bytes. The data is copied to the result
     * array. The memory consumption at the end of the reading is
     * <code>2 x [size of resulting array] + chunkSize</code>.
     *
     * @param is the stream to read the data from
     * @param chunkSize the size of the chunks the data should be read in
     * @return the <b>whole</b> data of the stream read into an byte array
     * @throws IllegalArgumentException if chunkSize <= 0
     * @throws NullPointerException if is == null
     * @throws IOException thrown if the provided stream encounters IO problems
     */
    public static byte[] readStreamIntoBuffer(InputStream is, int chunkSize)
            throws IOException {

        // Check preconditions.
        if (chunkSize <= 0) {
            throw new IllegalArgumentException("chunkSize <= 0");
        }
        else if (is == null) {
            throw new NullPointerException("is is null");
        }

        // Temporary buffer for read operations and result buffer.
        byte[] tempBuffer = new byte[chunkSize];
        byte[] buffer     = new byte[0];

        int bytesRead = 0;  // bytes actual read
        int oldSize   = 0;  // size of the resulting buffer

        while ((bytesRead = is.read(tempBuffer)) > 0) {

            // Temporary reference to the buffer for the copy operation.
            byte[] oldBuffer = buffer;

            // Create a new buffer with the size needed and copy data.
            buffer = new byte[oldSize + bytesRead];
            System.arraycopy(oldBuffer,  0, buffer, 0,       oldBuffer.length);

            // Copy the new data.
            System.arraycopy(tempBuffer, 0, buffer, oldSize, bytesRead);
            oldSize += bytesRead;
        }

        return buffer;
    }
}