1 /*
   2  * Copyright (c) 2015, 2017, 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  *
  23  */
  24 
  25 import java.io.File;
  26 import java.net.URL;
  27 import java.net.URLClassLoader;
  28 import sun.hotspot.WhiteBox;
  29 
  30 public class SameNameUnrelatedLoaders {
  31     public static void main(String args[]) throws Exception {
  32         String path = args[0];
  33         String testCase = args[1];
  34         URL url = new File(path).toURI().toURL();
  35         URL[] urls = new URL[] {url};
  36 
  37         ClassLoader appLoader = SameNameUnrelatedLoaders.class.getClassLoader();
  38         URLClassLoader ldr01 = null;
  39         URLClassLoader ldr02 = null;
  40 
  41         switch (testCase) {
  42             case "FpBoth":
  43                 ldr01 = new URLClassLoader(urls);
  44                 ldr02 = new URLClassLoader(urls);
  45             break;
  46 
  47             default:
  48                 throw new IllegalArgumentException("Invalid testCase ID");
  49         }
  50 
  51 
  52         Class class01 = ldr01.loadClass("CustomLoadee");
  53         Class class02  = ldr02.loadClass("CustomLoadee");
  54 
  55         System.out.println("class01 = " + class01);
  56         System.out.println("class02 = " + class02);
  57 
  58         if (class01.getClassLoader() != ldr01) {
  59             throw new RuntimeException("class01 loaded by wrong loader");
  60         }
  61         if (class02.getClassLoader() != ldr02) {
  62             throw new RuntimeException("class02 loaded by wrong loader");
  63         }
  64 
  65         if (true) {
  66             if (class01.isAssignableFrom(class02)) {
  67                 throw new RuntimeException("assignable condition failed");
  68             }
  69 
  70             Object obj01 = class01.newInstance();
  71             Object obj02 = class02.newInstance();
  72 
  73             if (class01.isInstance(obj02)) {
  74                 throw new RuntimeException("instance relationship condition 01 failed");
  75             }
  76             if (class02.isInstance(obj01)) {
  77                 throw new RuntimeException("instance relationship condition 02 failed");
  78             }
  79         }
  80 
  81         WhiteBox wb = WhiteBox.getWhiteBox();
  82         if (wb.isSharedClass(SameNameUnrelatedLoaders.class)) {
  83             boolean class1Shared = wb.isSharedClass(class01);
  84             boolean class2Shared = wb.isSharedClass(class02);
  85 
  86             if (testCase.equals("FpBoth")) {
  87                 if (!class1Shared) {
  88                     throw new RuntimeException("first class is not shared");
  89                 }
  90 
  91                 if (class2Shared) {
  92                     throw new RuntimeException("second class is shared, " +
  93                     "and it should not be - first come first serve violation");
  94                 }
  95             } else {
  96                 if (! (class1Shared && class2Shared) )
  97                     throw new RuntimeException("both classes expected to be shared, but are not");
  98             }
  99         }
 100     }
 101 }