1 /*
  2  * Copyright (c) 2016, 2019, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 /**
 27  * This application is meant to be run to create a classlist file representing
 28  * common use.
 29  *
 30  * The classlist is produced by adding -XX:DumpLoadedClassList=classlist
 31  */
 32 package build.tools.classlist;
 33 
 34 import java.lang.invoke.MethodHandle;
 35 import java.lang.invoke.MethodHandles;
 36 import java.lang.invoke.MethodType;
 37 import java.net.InetAddress;
 38 import java.nio.file.FileSystems;
 39 import java.time.LocalDateTime;
 40 import java.time.ZoneId;
 41 import java.time.format.DateTimeFormatter;
 42 import java.util.stream.Stream;
 43 import java.util.logging.*;
 44 import java.util.Arrays;
 45 import java.util.Date;
 46 import java.util.List;
 47 import java.util.Locale;
 48 import java.text.DateFormat;
 49 
 50 import static java.util.stream.Collectors.*;
 51 
 52 /**
 53  * This class is used to generate a classlist during build. Intent
 54  * is to touch a reasonable amount of JDK classes that are commonly
 55  * loaded and used early.
 56  */
 57 public class HelloClasslist {
 58 
 59     private static final Logger LOGGER = Logger.getLogger("Hello");
 60 
 61     public static void main(String ... args) throws Throwable {
 62 
 63         FileSystems.getDefault();
 64 
 65         List<String> strings = Arrays.asList("Hello", "World!", "From: ",
 66                 InetAddress.getLoopbackAddress().toString());
 67 
 68         String helloWorld = strings.parallelStream()
 69                 .map(s -> s.toLowerCase(Locale.ROOT))
 70                 .collect(joining(","));
 71 
 72         Stream.of(helloWorld.split("([,x-z]{1,3})([\\s]*)"))
 73                 .map(String::toString)
 74                 .forEach(System.out::println);
 75 
 76         // Common concatenation patterns
 77         int i = args.length;
 78         String s = String.valueOf(i);
 79 
 80         String SS     = s + s;
 81         String CS     = "string" + s;
 82         String SC     = s + "string";
 83         String SCS    = s + "string" + s;
 84         String CSS    = "string" + s + s;
 85         String CSC    = "string" + s + "string";
 86         String SSC    = s + s + "string";
 87         String CSCS   = "string" + s + "string" + s;
 88         String SCSC   = s + "string" + s + "string";
 89         String CSCSC  = "string" + s + "string" + s + "string";
 90         String SCSCS  = s + "string" + s + "string" + s;
 91         String SSCSS  = s + s + "string" + s + s;
 92         String SSSSS  = s + s + s + s + s;
 93 
 94         String CI     = "string" + i;
 95         String IC     = i + "string";
 96         String SI     = s + i;
 97         String IS     = i + s;
 98         String CIS    = "string" + i + s;
 99         String CSCI   = "string" + s + "string" + i;
100         String CIC    = "string" + i + "string";
101         String CICI   = "string" + i + "string" + i;
102 
103         long l = System.currentTimeMillis();
104         String CJ     = "string" + l;
105         String JC     = l + "string";
106         String CJC    = "string" + l + "string";
107         String CJCJ   = "string" + l + "string" + l;
108         String CJCJC  = "string" + l + "string" + l + "string";
109         double d = i / 2.0;
110         String CD     = "string" + d;
111 
112         String newDate = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(
113                 LocalDateTime.now(ZoneId.of("GMT")));
114 
115         String oldDate = String.format("%s%n",
116                 DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.ROOT)
117                         .format(new Date()));
118 
119         // A selection of trivial and relatively common MH operations
120         invoke(MethodHandles.identity(double.class), 1.0);
121         invoke(MethodHandles.identity(int.class), 1);
122         invoke(MethodHandles.identity(String.class), "x");
123 
124         invoke(handle("staticMethod_V", MethodType.methodType(void.class)));
125 
126         LOGGER.log(Level.FINE, "New Date: " + newDate + " - old: " + oldDate);
127     }
128 
129     public static void staticMethod_V() {}
130 
131     private static MethodHandle handle(String name, MethodType type) throws Throwable {
132         return MethodHandles.lookup().findStatic(HelloClasslist.class, name, type);
133     }
134 
135     private static Object invoke(MethodHandle mh, Object ... args) throws Throwable {
136         try {
137             for (Object o : args) {
138                 mh = MethodHandles.insertArguments(mh, 0, o);
139             }
140             return mh.invoke();
141         } catch (Throwable t) {
142             LOGGER.warning("Failed to find, link and/or invoke " + mh.toString() + ": " + t.getMessage());
143             throw t;
144         }
145     }
146 }