1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package myloaders;
24
25import java.io.*;
26import java.lang.module.ModuleReference;
27
28// Declare a MySameClassLoader class to be used to map modules to the same
29// class loader.  This class loader will also be used to load classes
30// within modules.
31public class MySameClassLoader extends ClassLoader
32{
33    public static MySameClassLoader loader1 = new MySameClassLoader();
34
35    public Class loadClass(String name) throws ClassNotFoundException {
36        // override all classloaders
37        if (!name.equals("p1.c1") &&
38            !name.equals("p1.c1ReadEdge") &&
39            !name.equals("p1.c1Loose") &&
40            !name.equals("p2.c2") &&
41            !name.equals("p3.c3") &&
42            !name.equals("p3.c3ReadEdge") &&
43            !name.equals("c4") &&
44            !name.equals("c5") &&
45            !name.equals("p6.c6")) {
46            return super.loadClass(name);
47        }
48        byte[] data = getClassData(name);
49        return defineClass(name, data, 0, data.length);
50    }
51    byte[] getClassData(String name) {
52        try {
53           String TempName = name.replaceAll("\\.", "/");
54           String currentDir = System.getProperty("test.classes");
55           String filename = currentDir + File.separator + TempName + ".class";
56           FileInputStream fis = new FileInputStream(filename);
57           byte[] b = new byte[5000];
58           int cnt = fis.read(b, 0, 5000);
59           byte[] c = new byte[cnt];
60           for (int i=0; i<cnt; i++) c[i] = b[i];
61              return c;
62        } catch (IOException e) {
63           return null;
64        }
65    }
66
67    public void register(ModuleReference mref) { }
68}
69