Diag.java revision 3062:15bdc18525ff
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
26package jdk.jshell;
27
28import java.util.Locale;
29import javax.tools.Diagnostic;
30
31/**
32 * Diagnostic information for a Snippet.
33 * @see jdk.jshell.JShell#diagnostics(jdk.jshell.Snippet)
34 */
35public abstract class Diag {
36    // Simplified view on compiler Diagnostic.
37
38    /**
39     * Used to signal that no position is available.
40     */
41    public final static long NOPOS = Diagnostic.NOPOS;
42
43    /**
44     * Is this diagnostic and error (as opposed to a warning or note)
45     * @return true if this diagnostic is an error
46     */
47    public abstract boolean isError();
48
49    /**
50     * Returns a character offset from the beginning of the source object
51     * associated with this diagnostic that indicates the location of
52     * the problem.  In addition, the following must be true:
53     *
54     * <p>{@code getStartPostion() <= getPosition()}
55     * <p>{@code getPosition() <= getEndPosition()}
56     *
57     * @return character offset from beginning of source; {@link
58     * #NOPOS} if {@link #getSource()} would return {@code null} or if
59     * no location is suitable
60     */
61    public abstract long getPosition();
62
63    /**
64     * Returns the character offset from the beginning of the file
65     * associated with this diagnostic that indicates the start of the
66     * problem.
67     *
68     * @return offset from beginning of file; {@link #NOPOS} if and
69     * only if {@link #getPosition()} returns {@link #NOPOS}
70     */
71    public abstract long getStartPosition();
72
73    /**
74     * Returns the character offset from the beginning of the file
75     * associated with this diagnostic that indicates the end of the
76     * problem.
77     *
78     * @return offset from beginning of file; {@link #NOPOS} if and
79     * only if {@link #getPosition()} returns {@link #NOPOS}
80     */
81    public abstract long getEndPosition();
82
83    /**
84     * Returns a diagnostic code indicating the type of diagnostic.  The
85     * code is implementation-dependent and might be {@code null}.
86     *
87     * @return a diagnostic code
88     */
89    public abstract String getCode();
90
91    /**
92     * Returns a localized message for the given locale.  The actual
93     * message is implementation-dependent.  If the locale is {@code
94     * null} use the default locale.
95     *
96     * @param locale a locale; might be {@code null}
97     * @return a localized message
98     */
99    public abstract String getMessage(Locale locale);
100
101    // *** Internal support ***
102
103    /**
104     * Internal: If this is from a compile, extract the compilation Unit.
105     * Otherwise null.
106     */
107    abstract Unit unitOrNull();
108
109    /**
110     * This is an unreachable-statement error
111     */
112    boolean isUnreachableError() {
113        return getCode().equals("compiler.err.unreachable.stmt");
114    }
115
116    /**
117     * This is a not-a-statement error
118     */
119    boolean isNotAStatementError() {
120        return getCode().equals("compiler.err.not.stmt");
121    }
122
123    /**
124     * This is a resolution error.
125     */
126    boolean isResolutionError() {
127        //TODO: try javac RESOLVE_ERROR flag
128        return getCode().startsWith("compiler.err.cant.resolve");
129    }
130}
131