author | jlahoda |
Thu, 05 May 2016 12:55:21 +0200 | |
changeset 37939 | 3eb8c2a89b77 |
parent 37644 | 33cf53901cac |
child 38535 | 4a25025e0b0d |
permissions | -rw-r--r-- |
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
33362
diff
changeset
|
1 |
/* |
33362 | 2 |
* Copyright (c) 2014, 2015, 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 |
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; |
33362 | 32 |
import java.util.stream.Stream; |
33 |
import java.util.stream.StreamSupport; |
|
34 |
import javax.lang.model.element.Name; |
|
35 |
import static jdk.internal.jshell.remote.RemoteCodes.DOIT_METHOD_NAME; |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
35000
diff
changeset
|
36 |
import static jdk.internal.jshell.remote.RemoteCodes.PREFIX_PATTERN; |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
35000
diff
changeset
|
37 |
import static jdk.internal.jshell.remote.RemoteCodes.REPL_CLASS_PREFIX; |
33362 | 38 |
|
39 |
/** |
|
40 |
* Assorted shared utilities. |
|
41 |
* @author Robert Field |
|
42 |
*/ |
|
43 |
class Util { |
|
44 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
35000
diff
changeset
|
45 |
static final String REPL_DOESNOTMATTER_CLASS_NAME = REPL_CLASS_PREFIX+"DOESNOTMATTER"; |
33362 | 46 |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
47 |
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
|
48 |
|
33362 | 49 |
static boolean isDoIt(Name name) { |
50 |
return isDoIt(name.toString()); |
|
51 |
} |
|
52 |
||
53 |
static boolean isDoIt(String sname) { |
|
54 |
return sname.equals(DOIT_METHOD_NAME); |
|
55 |
} |
|
56 |
||
57 |
static String expunge(String s) { |
|
58 |
StringBuilder sb = new StringBuilder(); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
35000
diff
changeset
|
59 |
for (String comp : PREFIX_PATTERN.split(s)) { |
33362 | 60 |
sb.append(comp); |
61 |
} |
|
62 |
return sb.toString(); |
|
63 |
} |
|
64 |
||
65 |
static String asLetters(int i) { |
|
66 |
if (i == 0) { |
|
67 |
return ""; |
|
68 |
} |
|
69 |
||
70 |
char buf[] = new char[33]; |
|
71 |
int charPos = 32; |
|
72 |
||
73 |
i = -i; |
|
74 |
while (i <= -26) { |
|
75 |
buf[charPos--] = (char) ('A'-(i % 26)); |
|
76 |
i = i / 26; |
|
77 |
} |
|
78 |
buf[charPos] = (char) ('A'-i); |
|
79 |
||
80 |
return new String(buf, charPos, (33 - charPos)); |
|
81 |
} |
|
82 |
||
83 |
||
84 |
static String trimEnd(String s) { |
|
85 |
int last = s.length() - 1; |
|
86 |
int i = last; |
|
87 |
while (i >= 0 && Character.isWhitespace(s.charAt(i))) { |
|
88 |
--i; |
|
89 |
} |
|
90 |
if (i != last) { |
|
91 |
return s.substring(0, i + 1); |
|
92 |
} else { |
|
93 |
return s; |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
static <T> Stream<T> stream(Iterable<T> iterable) { |
|
98 |
return StreamSupport.stream(iterable.spliterator(), false); |
|
99 |
} |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
100 |
|
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
101 |
static String[] join(String[] a1, String[] a2) { |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
102 |
List<String> result = new ArrayList<>(); |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
103 |
|
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
104 |
result.addAll(Arrays.asList(a1)); |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
105 |
result.addAll(Arrays.asList(a2)); |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
106 |
|
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
107 |
return result.toArray(new String[0]); |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
108 |
} |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
109 |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
110 |
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
|
111 |
final T first; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
112 |
final U second; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
113 |
|
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
114 |
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
|
115 |
this.first = first; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
116 |
this.second = second; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
117 |
} |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
118 |
} |
33362 | 119 |
} |