1 /*
2  * Copyright (c) 2015-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.Locale;
29 import javax.tools.Diagnostic;
30 import javax.tools.JavaFileObject;
31 import static jdk.jshell.Util.PARSED_LOCALE;
32 import static jdk.jshell.Util.REPL_CLASS_PREFIX;
33 import static jdk.jshell.Util.REPL_DOESNOTMATTER_CLASS_NAME;
34 import static jdk.jshell.Util.REPL_PACKAGE;
35 import static jdk.jshell.Util.expunge;
36 
37 /**
38  *
39  * @author Robert Field
40  */
41 class OuterWrap implements GeneralWrap {
42 
43     protected final Wrap w;
44 
OuterWrap(Wrap wrap)45     OuterWrap(Wrap wrap) {
46         this.w = wrap;
47     }
48 
49     @Override
wrapped()50     public final String wrapped() {
51         return w.wrapped();
52     }
53 
54     @Override
snippetIndexToWrapIndex(int ui)55     public int snippetIndexToWrapIndex(int ui) {
56         return w.snippetIndexToWrapIndex(ui);
57     }
58 
59     @Override
wrapIndexToSnippetIndex(int si)60     public int wrapIndexToSnippetIndex(int si) {
61         return w.wrapIndexToSnippetIndex(si);
62     }
63 
64     @Override
firstSnippetIndex()65     public int firstSnippetIndex() {
66         return w.firstSnippetIndex();
67     }
68 
69     @Override
lastSnippetIndex()70     public int lastSnippetIndex() {
71         return w.lastSnippetIndex();
72     }
73 
74     @Override
snippetLineToWrapLine(int snline)75     public int snippetLineToWrapLine(int snline) {
76         return w.snippetLineToWrapLine(snline);
77     }
78 
79     @Override
wrapLineToSnippetLine(int wline)80     public int wrapLineToSnippetLine(int wline) {
81         return w.wrapLineToSnippetLine(wline);
82     }
83 
84     @Override
firstSnippetLine()85     public int firstSnippetLine() {
86         return w.firstSnippetLine();
87     }
88 
89     @Override
lastSnippetLine()90     public int lastSnippetLine() {
91         return w.lastSnippetLine();
92     }
93 
className()94     public String className() {
95         return REPL_DOESNOTMATTER_CLASS_NAME;
96     }
97 
classFullName()98     public String classFullName() {
99         return REPL_PACKAGE + "." + className();
100     }
101 
102     @Override
hashCode()103     public int hashCode() {
104         return className().hashCode();
105     }
106 
107     @Override
equals(Object o)108     public boolean equals(Object o) {
109         return (o instanceof OuterWrap)
110                 ? className().equals(((OuterWrap) o).className())
111                 : false;
112     }
113 
114     @Override
toString()115     public String toString() {
116         return "OW(" + w + ")";
117     }
118 
wrapDiag(Diagnostic<? extends JavaFileObject> d)119     Diag wrapDiag(Diagnostic<? extends JavaFileObject> d) {
120         return new WrappedDiagnostic(d);
121     }
122 
123     class WrappedDiagnostic extends Diag {
124 
125         final Diagnostic<? extends JavaFileObject> diag;
126 
WrappedDiagnostic(Diagnostic<? extends JavaFileObject> diag)127         WrappedDiagnostic(Diagnostic<? extends JavaFileObject> diag) {
128             this.diag = diag;
129         }
130 
131         @Override
isError()132         public boolean isError() {
133             return diag.getKind() == Diagnostic.Kind.ERROR;
134         }
135 
136         @Override
getPosition()137         public long getPosition() {
138             return wrapIndexToSnippetIndex(diag.getPosition());
139         }
140 
141         @Override
getStartPosition()142         public long getStartPosition() {
143             return wrapIndexToSnippetIndex(diag.getStartPosition());
144         }
145 
146         @Override
getEndPosition()147         public long getEndPosition() {
148             return wrapIndexToSnippetIndex(diag.getEndPosition());
149         }
150 
151         @Override
getCode()152         public String getCode() {
153             return diag.getCode();
154         }
155 
156         @Override
getMessage(Locale locale)157         public String getMessage(Locale locale) {
158             return expunge(diag.getMessage(locale));
159         }
160 
161         @Override
isResolutionError()162         boolean isResolutionError() {
163             if (!super.isResolutionError()) {
164                 return false;
165             }
166             for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) {
167                 if (line.trim().startsWith("location:")) {
168                     if (!line.contains(REPL_CLASS_PREFIX)) {
169                         // Resolution error must occur within a REPL class or it is not resolvable
170                         return false;
171                     }
172                 }
173             }
174             return true;
175         }
176 
177         @Override
toString()178         public String toString() {
179             return "WrappedDiagnostic(" + getMessage(null) + ":" + getPosition() + ")";
180         }
181     }
182 }
183