1/* 2 * Copyright (c) 2014, 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. 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 26package jdk.jshell; 27 28import java.util.ArrayList; 29import java.util.Arrays; 30import java.util.List; 31import java.util.Locale; 32import java.util.regex.Pattern; 33import java.util.stream.Stream; 34import java.util.stream.StreamSupport; 35import javax.lang.model.element.Name; 36 37/** 38 * Assorted shared utilities and constants. 39 */ 40class Util { 41 42 /** 43 * The package name of all wrapper classes. 44 */ 45 static final String REPL_PACKAGE = "REPL"; 46 47 /** 48 * The prefix for all wrapper class names. 49 */ 50 static final String REPL_CLASS_PREFIX = "$JShell$"; 51 52 /** 53 * The name of the invoke method. 54 */ 55 static final String DOIT_METHOD_NAME = "do_it$"; 56 57 /** 58 * A pattern matching the full or simple class name of a wrapper class. 59 */ 60 static final Pattern PREFIX_PATTERN = Pattern.compile( 61 "(" + REPL_PACKAGE + "\\.)?" 62 + "(?<class>" + Pattern.quote(REPL_CLASS_PREFIX) 63 + "\\w+" + ")" + "[\\$\\.]?"); 64 65 static final String REPL_DOESNOTMATTER_CLASS_NAME = REPL_CLASS_PREFIX + "DOESNOTMATTER"; 66 67 static final Locale PARSED_LOCALE = Locale.ROOT; 68 69 static boolean isDoIt(Name name) { 70 return isDoIt(name.toString()); 71 } 72 73 static boolean isDoIt(String sname) { 74 return sname.equals(DOIT_METHOD_NAME); 75 } 76 77 static String expunge(String s) { 78 StringBuilder sb = new StringBuilder(); 79 for (String comp : PREFIX_PATTERN.split(s)) { 80 sb.append(comp); 81 } 82 return sb.toString(); 83 } 84 85 static String asLetters(int i) { 86 if (i == 0) { 87 return ""; 88 } 89 90 char buf[] = new char[33]; 91 int charPos = 32; 92 93 i = -i; 94 while (i <= -26) { 95 buf[charPos--] = (char) ('A'-(i % 26)); 96 i = i / 26; 97 } 98 buf[charPos] = (char) ('A'-i); 99 100 return new String(buf, charPos, (33 - charPos)); 101 } 102 103 104 static String trimEnd(String s) { 105 int last = s.length() - 1; 106 int i = last; 107 while (i >= 0 && Character.isWhitespace(s.charAt(i))) { 108 --i; 109 } 110 if (i != last) { 111 return s.substring(0, i + 1); 112 } else { 113 return s; 114 } 115 } 116 117 static <T> Stream<T> stream(Iterable<T> iterable) { 118 return StreamSupport.stream(iterable.spliterator(), false); 119 } 120 121 static String[] join(String[] a1, String[] a2) { 122 List<String> result = new ArrayList<>(); 123 124 result.addAll(Arrays.asList(a1)); 125 result.addAll(Arrays.asList(a2)); 126 127 return result.toArray(new String[0]); 128 } 129 130 static class Pair<T, U> { 131 final T first; 132 final U second; 133 134 Pair(T first, U second) { 135 this.first = first; 136 this.second = second; 137 } 138 } 139} 140