1 /*
2  * Copyright (c) 2020, 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.util.HashMap;
26 import sun.hotspot.WhiteBox;
27 
28 public class UsedAllArchivedLambdasApp {
29     public static boolean isRuntime;
30     public static int NUM_CLASSES = 3;
31     public static WhiteBox wb = WhiteBox.getWhiteBox();
32     public static HashMap<Class<?>, Class<?>> inArchiveMap = new HashMap<>();
33     public static HashMap<Class<?>, Class<?>> notInArchiveMap = new HashMap<>();
34 
main(String args[])35     public static void main(String args[]) {
36         isRuntime = (args.length == 1 && args[0].equals("run")) ? true : false;
37         {Runnable run1 = UsedAllArchivedLambdasApp::myrun; run1.run();}
38         {Runnable run1 = UsedAllArchivedLambdasApp::myrun; run1.run();}
39         {Runnable run1 = UsedAllArchivedLambdasApp::myrun; run1.run();}
40         if (isRuntime) {
41             {Runnable run1 = UsedAllArchivedLambdasApp::myrun; run1.run();}
42             {Runnable run1 = UsedAllArchivedLambdasApp::myrun; run1.run();}
43             {Runnable run1 = UsedAllArchivedLambdasApp::myrun; run1.run();}
44         }
45 
46         int mapSize = 0;
47 
48         if (isRuntime) {
49             mapSize = inArchiveMap.size();
50             System.out.println("Number of lambda classes in archive: " + mapSize);
51             if (mapSize != NUM_CLASSES) {
52                 throw new RuntimeException("Expected number of lambda classes in archive is " +
53                     NUM_CLASSES + " but got " + mapSize);
54             }
55             mapSize = notInArchiveMap.size();
56             System.out.println("Number of lambda classes in archive: " + mapSize);
57             if (mapSize != NUM_CLASSES) {
58                 throw new RuntimeException("Expected number of lambda classes NOT in archive is " +
59                     NUM_CLASSES + " but got " + mapSize);
60             }
61         }
62     }
63 
myrun()64     static void myrun() {
65         Class<?> c = LambdaVerification.getCallerClass(1);
66         if (isRuntime) {
67             if (wb.isSharedClass(c)) {
68                 System.out.println(c.getName() + " is a shared class");
69                 inArchiveMap.put(c,c);
70             } else {
71                 System.out.println(c.getName() + " is NOT a shared class");
72                 notInArchiveMap.put(c,c);
73             }
74         }
75     }
76 }
77