24
24
*/
25
25
package jdk .jshell ;
26
26
27
- import java .util .ArrayList ;
28
- import java .util .Collection ;
29
- import java .util .Collections ;
30
- import java .util .List ;
31
- import java .util .Locale ;
27
+ import java .util .*;
32
28
import java .util .regex .Matcher ;
33
29
import java .util .regex .Pattern ;
34
30
import java .util .stream .Collectors ;
35
31
import javax .lang .model .element .Modifier ;
32
+ import javax .lang .model .element .Name ;
33
+
36
34
import com .sun .source .tree .ArrayTypeTree ;
37
35
import com .sun .source .tree .AssignmentTree ;
38
36
import com .sun .source .tree .ClassTree ;
49
47
import java .io .IOException ;
50
48
import java .io .StringWriter ;
51
49
import java .io .Writer ;
52
- import java .util .Arrays ;
53
- import java .util .LinkedHashSet ;
54
- import java .util .Set ;
50
+
55
51
import jdk .jshell .ExpressionToTypeInfo .ExpressionInfo ;
56
52
import jdk .jshell .ExpressionToTypeInfo .ExpressionInfo .AnonymousDescription ;
57
53
import jdk .jshell .ExpressionToTypeInfo .ExpressionInfo .AnonymousDescription .VariableDesc ;
@@ -304,6 +300,7 @@ private List<Snippet> processVariables(String userSource, List<? extends Tree> u
304
300
for (Tree unitTree : units ) {
305
301
VariableTree vt = (VariableTree ) unitTree ;
306
302
String name = vt .getName ().toString ();
303
+ // String name = userReadableName(vt.getName(), compileSource);
307
304
String typeName ;
308
305
String fullTypeName ;
309
306
String displayType ;
@@ -400,13 +397,18 @@ private List<Snippet> processVariables(String userSource, List<? extends Tree> u
400
397
winit = Wrap .simpleWrap (sinit );
401
398
subkind = SubKind .VAR_DECLARATION_SUBKIND ;
402
399
}
400
+ Wrap wname ;
403
401
int nameStart = compileSource .lastIndexOf (name , nameMax );
404
402
if (nameStart < 0 ) {
405
- throw new AssertionError ("Name '" + name + "' not found" );
403
+ // the name has been transformed (e.g. unicode).
404
+ // Use it directly
405
+ wname = Wrap .identityWrap (name );
406
+ } else {
407
+ int nameEnd = nameStart + name .length ();
408
+ Range rname = new Range (nameStart , nameEnd );
409
+ wname = new Wrap .RangeWrap (compileSource , rname );
406
410
}
407
- int nameEnd = nameStart + name .length ();
408
- Range rname = new Range (nameStart , nameEnd );
409
- Wrap guts = Wrap .varWrap (compileSource , typeWrap , sbBrackets .toString (), rname ,
411
+ Wrap guts = Wrap .varWrap (compileSource , typeWrap , sbBrackets .toString (), wname ,
410
412
winit , enhancedDesugaring , anonDeclareWrap );
411
413
DiagList modDiag = modifierDiagnostics (vt .getModifiers (), dis , true );
412
414
Snippet snip = new VarSnippet (state .keyMap .keyForVariable (name ), userSource , guts ,
@@ -417,6 +419,26 @@ private List<Snippet> processVariables(String userSource, List<? extends Tree> u
417
419
return snippets ;
418
420
}
419
421
422
+ private String userReadableName (Name nn , String compileSource ) {
423
+ String s = nn .toString ();
424
+ if (s .length () > 0 && Character .isJavaIdentifierStart (s .charAt (0 )) && compileSource .contains (s )) {
425
+ return s ;
426
+ }
427
+ String l = nameInUnicode (nn , false );
428
+ if (compileSource .contains (l )) {
429
+ return l ;
430
+ }
431
+ return nameInUnicode (nn , true );
432
+ }
433
+
434
+ private String nameInUnicode (Name nn , boolean upper ) {
435
+ return nn .codePoints ()
436
+ .mapToObj (cp -> (cp > 0x7F )
437
+ ? String .format (upper ? "\\ u%04X" : "\\ u%04x" , cp )
438
+ : "" + (char ) cp )
439
+ .collect (Collectors .joining ());
440
+ }
441
+
420
442
/**Convert anonymous classes in "init" to member classes, based
421
443
* on the additional information from ExpressionInfo.anonymousClasses.
422
444
*
@@ -680,6 +702,7 @@ private List<Snippet> processClass(String userSource, Tree unitTree, String comp
680
702
TreeDissector dis = TreeDissector .createByFirstClass (pt );
681
703
682
704
ClassTree klassTree = (ClassTree ) unitTree ;
705
+ // String name = userReadableName(klassTree.getSimpleName(), compileSource);
683
706
String name = klassTree .getSimpleName ().toString ();
684
707
DiagList modDiag = modifierDiagnostics (klassTree .getModifiers (), dis , false );
685
708
TypeDeclKey key = state .keyMap .keyForClass (name );
@@ -730,6 +753,7 @@ private List<Snippet> processMethod(String userSource, Tree unitTree, String com
730
753
final TreeDissector dis = TreeDissector .createByFirstClass (pt );
731
754
732
755
final MethodTree mt = (MethodTree ) unitTree ;
756
+ //String name = userReadableName(mt.getName(), compileSource);
733
757
final String name = mt .getName ().toString ();
734
758
if (objectMethods .contains (name )) {
735
759
// The name matches a method on Object, short of an overhaul, this
0 commit comments