|
1 /* |
|
2 * Copyright (c) 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 |
|
26 package jdk.jshell; |
|
27 |
|
28 import java.util.List; |
|
29 import com.sun.source.tree.ArrayTypeTree; |
|
30 import com.sun.source.tree.ClassTree; |
|
31 import com.sun.source.tree.ExpressionTree; |
|
32 import com.sun.source.tree.MethodTree; |
|
33 import com.sun.source.tree.Tree; |
|
34 import com.sun.source.tree.VariableTree; |
|
35 import jdk.jshell.Wrap.Range; |
|
36 import static java.util.stream.Collectors.toList; |
|
37 |
|
38 /** |
|
39 * Produce a corralled version of the Wrap for a snippet. |
|
40 * |
|
41 * @author Robert Field |
|
42 */ |
|
43 class Corraller { |
|
44 |
|
45 private final int index; |
|
46 private final String compileSource; |
|
47 private final TreeDissector dis; |
|
48 |
|
49 Corraller(int index, String compileSource, TreeDissector dis) { |
|
50 this.index = index; |
|
51 this.compileSource = compileSource; |
|
52 this.dis = dis; |
|
53 } |
|
54 |
|
55 Wrap corralTree(Tree tree, String enclosingType, int indent) { |
|
56 switch (tree.getKind()) { |
|
57 case VARIABLE: |
|
58 return corralVariable((VariableTree) tree, indent); |
|
59 case CLASS: |
|
60 case ENUM: |
|
61 case ANNOTATION_TYPE: |
|
62 case INTERFACE: |
|
63 return corralType((ClassTree) tree, indent); |
|
64 case METHOD: |
|
65 return corralMethod((MethodTree) tree, enclosingType, indent); |
|
66 default: |
|
67 return null; |
|
68 } |
|
69 } |
|
70 |
|
71 Wrap corralMethod(MethodTree mt) { |
|
72 return corralMethod(mt, null, 1); |
|
73 } |
|
74 |
|
75 Wrap corralMethod(MethodTree mt, String enclosingType, int indent) { |
|
76 Range modRange = dis.treeToRange(mt.getModifiers()); |
|
77 Range tpRange = dis.treeListToRange(mt.getTypeParameters()); |
|
78 Range typeRange = dis.treeToRange(mt.getReturnType()); |
|
79 String name = mt.getName().toString(); |
|
80 if ("<init>".equals(name)) { |
|
81 name = enclosingType; |
|
82 } |
|
83 Range paramRange = dis.treeListToRange(mt.getParameters()); |
|
84 Range throwsRange = dis.treeListToRange(mt.getThrows()); |
|
85 return Wrap.corralledMethod(compileSource, |
|
86 modRange, tpRange, typeRange, name, paramRange, throwsRange, index, indent); |
|
87 } |
|
88 |
|
89 Wrap corralVariable(VariableTree vt, int indent) { |
|
90 String name = vt.getName().toString(); |
|
91 Range modRange = dis.treeToRange(vt.getModifiers()); |
|
92 Tree baseType = vt.getType(); |
|
93 StringBuilder sbBrackets = new StringBuilder(); |
|
94 while (baseType instanceof ArrayTypeTree) { |
|
95 //TODO handle annotations too |
|
96 baseType = ((ArrayTypeTree) baseType).getType(); |
|
97 sbBrackets.append("[]"); |
|
98 } |
|
99 Range rtype = dis.treeToRange(baseType); |
|
100 Range runit = dis.treeToRange(vt); |
|
101 runit = new Range(runit.begin, runit.end - 1); |
|
102 ExpressionTree it = vt.getInitializer(); |
|
103 int nameMax; |
|
104 if (it != null) { |
|
105 Range rinit = dis.treeToRange(it); |
|
106 nameMax = rinit.begin - 1; |
|
107 } else { |
|
108 nameMax = runit.end - 1; |
|
109 } |
|
110 int nameStart = compileSource.lastIndexOf(name, nameMax); |
|
111 if (nameStart < 0) { |
|
112 throw new AssertionError("Name '" + name + "' not found"); |
|
113 } |
|
114 int nameEnd = nameStart + name.length(); |
|
115 Range rname = new Range(nameStart, nameEnd); |
|
116 return Wrap.corralledVar(compileSource, modRange, rtype, sbBrackets.toString(), rname, indent); |
|
117 } |
|
118 |
|
119 Wrap corralType(ClassTree ct, int indent) { |
|
120 boolean isClass; |
|
121 switch (ct.getKind()) { |
|
122 case CLASS: |
|
123 isClass = true; |
|
124 break; |
|
125 case INTERFACE: |
|
126 isClass = false; |
|
127 break; |
|
128 default: |
|
129 return null; |
|
130 } |
|
131 Range modRange = dis.treeToRange(ct.getModifiers()); |
|
132 String name = ct.getSimpleName().toString(); |
|
133 Range tpRange = dis.treeListToRange(ct.getTypeParameters()); |
|
134 Range extendsRange = dis.treeToRange(ct.getExtendsClause()); |
|
135 List<Range> implementsRanges = ct.getImplementsClause().stream() |
|
136 .map(ic -> dis.treeToRange(ic)) |
|
137 .collect(toList()); |
|
138 List<Wrap> members = ct.getMembers().stream() |
|
139 .map(t -> corralTree(t, name, indent + 1)) |
|
140 .filter(w -> w != null) |
|
141 .collect(toList()); |
|
142 boolean hasConstructor = ct.getMembers().stream() |
|
143 .anyMatch(t -> t.getKind() == Tree.Kind.METHOD && ((MethodTree) t).getName().toString().equals("<init>")); |
|
144 Wrap wrap = Wrap.corralledType(compileSource, modRange, ct.getKind(), name, tpRange, |
|
145 extendsRange, implementsRanges, members, isClass && !hasConstructor, index, indent); |
|
146 return wrap; |
|
147 } |
|
148 } |