author | hseigel |
Wed, 01 Mar 2017 08:00:02 -0500 | |
changeset 46194 | 5596e6f63072 |
parent 38535 | 4a25025e0b0d |
permissions | -rw-r--r-- |
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
33362
diff
changeset
|
1 |
/* |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
package jdk.jshell; |
|
27 |
||
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
28 |
import java.util.ArrayList; |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
29 |
import java.util.Arrays; |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
30 |
import java.util.List; |
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
31 |
import java.util.Locale; |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
32 |
import java.util.regex.Pattern; |
33362 | 33 |
import java.util.stream.Stream; |
34 |
import java.util.stream.StreamSupport; |
|
35 |
import javax.lang.model.element.Name; |
|
36 |
||
37 |
/** |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
38 |
* Assorted shared utilities and constants. |
33362 | 39 |
*/ |
40 |
class Util { |
|
41 |
||
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
42 |
/** |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
43 |
* The package name of all wrapper classes. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
44 |
*/ |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
45 |
static final String REPL_PACKAGE = "REPL"; |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
46 |
|
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
47 |
/** |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
48 |
* The prefix for all wrapper class names. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
49 |
*/ |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
50 |
static final String REPL_CLASS_PREFIX = "$JShell$"; |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
51 |
|
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
52 |
/** |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
53 |
* The name of the invoke method. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
54 |
*/ |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
55 |
static final String DOIT_METHOD_NAME = "do_it$"; |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
56 |
|
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
57 |
/** |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
58 |
* A pattern matching the full or simple class name of a wrapper class. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
59 |
*/ |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
60 |
static final Pattern PREFIX_PATTERN = Pattern.compile( |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
61 |
"(" + REPL_PACKAGE + "\\.)?" |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
62 |
+ "(?<class>" + Pattern.quote(REPL_CLASS_PREFIX) |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
63 |
+ "\\w+" + ")" + "[\\$\\.]?"); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
64 |
|
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37939
diff
changeset
|
65 |
static final String REPL_DOESNOTMATTER_CLASS_NAME = REPL_CLASS_PREFIX + "DOESNOTMATTER"; |
33362 | 66 |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
67 |
static final Locale PARSED_LOCALE = Locale.ROOT; |
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
68 |
|
33362 | 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(); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
35000
diff
changeset
|
79 |
for (String comp : PREFIX_PATTERN.split(s)) { |
33362 | 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 |
} |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
120 |
|
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
121 |
static String[] join(String[] a1, String[] a2) { |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
122 |
List<String> result = new ArrayList<>(); |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
123 |
|
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
124 |
result.addAll(Arrays.asList(a1)); |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
125 |
result.addAll(Arrays.asList(a2)); |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
126 |
|
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
127 |
return result.toArray(new String[0]); |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
128 |
} |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
129 |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
130 |
static class Pair<T, U> { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
131 |
final T first; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
132 |
final U second; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
133 |
|
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
134 |
Pair(T first, U second) { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
135 |
this.first = first; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
136 |
this.second = second; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
137 |
} |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
138 |
} |
33362 | 139 |
} |