33362
|
1 |
/*
|
|
2 |
* Copyright (c) 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 |
package jdk.jshell;
|
|
26 |
|
|
27 |
import jdk.jshell.Wrap.CompoundWrap;
|
|
28 |
import static jdk.jshell.Util.*;
|
|
29 |
import java.util.Locale;
|
|
30 |
import javax.tools.Diagnostic;
|
|
31 |
import javax.tools.JavaFileObject;
|
|
32 |
import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject;
|
|
33 |
|
|
34 |
/**
|
|
35 |
*
|
|
36 |
* @author Robert Field
|
|
37 |
*/
|
|
38 |
final class OuterWrap implements GeneralWrap {
|
|
39 |
|
|
40 |
private final String packageName;
|
|
41 |
private final String className;
|
|
42 |
private final String userSource;
|
|
43 |
private final GeneralWrap w;
|
|
44 |
private final Wrap guts;
|
|
45 |
|
|
46 |
public static OuterWrap wrapInClass(String packageName, String className,
|
|
47 |
String imports, String userSource, Wrap guts) {
|
|
48 |
GeneralWrap kw = new CompoundWrap(
|
|
49 |
imports
|
|
50 |
+ "class " + className + " {\n",
|
|
51 |
guts,
|
|
52 |
"}\n");
|
|
53 |
return new OuterWrap(packageName, className, userSource, kw, guts);
|
|
54 |
}
|
|
55 |
|
|
56 |
public static OuterWrap wrapImport(String userSource, Wrap guts) {
|
|
57 |
return new OuterWrap("", "", userSource, guts, guts);
|
|
58 |
}
|
|
59 |
|
|
60 |
private OuterWrap(String packageName, String className, String userSource,
|
|
61 |
GeneralWrap w, Wrap guts) {
|
|
62 |
this.packageName = packageName;
|
|
63 |
this.className = className;
|
|
64 |
this.userSource = userSource;
|
|
65 |
this.w = w;
|
|
66 |
this.guts = guts;
|
|
67 |
}
|
|
68 |
|
|
69 |
@Override
|
|
70 |
public final String wrapped() {
|
|
71 |
return w.wrapped();
|
|
72 |
}
|
|
73 |
|
|
74 |
@Override
|
|
75 |
public int snippetIndexToWrapIndex(int ui) {
|
|
76 |
return w.snippetIndexToWrapIndex(ui);
|
|
77 |
}
|
|
78 |
|
|
79 |
@Override
|
|
80 |
public int wrapIndexToSnippetIndex(int si) {
|
|
81 |
return w.wrapIndexToSnippetIndex(si);
|
|
82 |
}
|
|
83 |
|
|
84 |
@Override
|
|
85 |
public int firstSnippetIndex() {
|
|
86 |
return w.firstSnippetIndex();
|
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
|
90 |
public int lastSnippetIndex() {
|
|
91 |
return w.lastSnippetIndex();
|
|
92 |
}
|
|
93 |
|
|
94 |
@Override
|
|
95 |
public int snippetLineToWrapLine(int snline) {
|
|
96 |
return w.snippetLineToWrapLine(snline);
|
|
97 |
}
|
|
98 |
|
|
99 |
@Override
|
|
100 |
public int wrapLineToSnippetLine(int wline) {
|
|
101 |
return w.wrapLineToSnippetLine(wline);
|
|
102 |
}
|
|
103 |
|
|
104 |
@Override
|
|
105 |
public int firstSnippetLine() {
|
|
106 |
return w.firstSnippetLine();
|
|
107 |
}
|
|
108 |
|
|
109 |
@Override
|
|
110 |
public int lastSnippetLine() {
|
|
111 |
return w.lastSnippetLine();
|
|
112 |
}
|
|
113 |
|
|
114 |
public String className() {
|
|
115 |
return className;
|
|
116 |
}
|
|
117 |
|
|
118 |
public String classFullName() {
|
|
119 |
return packageName + "." + className;
|
|
120 |
}
|
|
121 |
|
|
122 |
public String getUserSource() {
|
|
123 |
return userSource;
|
|
124 |
}
|
|
125 |
|
|
126 |
Wrap guts() {
|
|
127 |
return guts;
|
|
128 |
}
|
|
129 |
|
|
130 |
Diag wrapDiag(Diagnostic<? extends JavaFileObject> d) {
|
|
131 |
return new WrappedDiagnostic(d);
|
|
132 |
}
|
|
133 |
|
|
134 |
class WrappedDiagnostic extends Diag {
|
|
135 |
|
|
136 |
private final Diagnostic<? extends JavaFileObject> diag;
|
|
137 |
|
|
138 |
WrappedDiagnostic(Diagnostic<? extends JavaFileObject> diag) {
|
|
139 |
this.diag = diag;
|
|
140 |
}
|
|
141 |
|
|
142 |
@Override
|
|
143 |
public boolean isError() {
|
|
144 |
return diag.getKind() == Diagnostic.Kind.ERROR;
|
|
145 |
}
|
|
146 |
|
|
147 |
@Override
|
|
148 |
public long getPosition() {
|
|
149 |
return wrapIndexToSnippetIndex(diag.getPosition());
|
|
150 |
}
|
|
151 |
|
|
152 |
@Override
|
|
153 |
public long getStartPosition() {
|
|
154 |
return wrapIndexToSnippetIndex(diag.getStartPosition());
|
|
155 |
}
|
|
156 |
|
|
157 |
@Override
|
|
158 |
public long getEndPosition() {
|
|
159 |
return wrapIndexToSnippetIndex(diag.getEndPosition());
|
|
160 |
}
|
|
161 |
|
|
162 |
@Override
|
|
163 |
public String getCode() {
|
|
164 |
return diag.getCode();
|
|
165 |
}
|
|
166 |
|
|
167 |
@Override
|
|
168 |
public String getMessage(Locale locale) {
|
|
169 |
return expunge(diag.getMessage(locale));
|
|
170 |
}
|
|
171 |
|
|
172 |
@Override
|
|
173 |
Unit unitOrNull() {
|
|
174 |
JavaFileObject fo = diag.getSource();
|
|
175 |
if (fo instanceof SourceMemoryJavaFileObject) {
|
|
176 |
SourceMemoryJavaFileObject sfo = (SourceMemoryJavaFileObject) fo;
|
|
177 |
if (sfo.getOrigin() instanceof Unit) {
|
|
178 |
return (Unit) sfo.getOrigin();
|
|
179 |
}
|
|
180 |
}
|
|
181 |
return null;
|
|
182 |
}
|
|
183 |
|
|
184 |
@Override
|
|
185 |
public String toString() {
|
|
186 |
return "WrappedDiagnostic(" + getMessage(null) + ":" + getPosition() + ")";
|
|
187 |
}
|
|
188 |
}
|
|
189 |
}
|