1 /* |
|
2 * Copyright (c) 2004, 2011, 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 com.sun.tools.apt.util; |
|
27 |
|
28 import com.sun.tools.javac.util.Context; |
|
29 import com.sun.tools.javac.util.JCDiagnostic; |
|
30 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; |
|
31 import com.sun.tools.javac.util.Log; |
|
32 import com.sun.tools.javac.util.JavacMessages; |
|
33 import com.sun.tools.javac.util.Position; |
|
34 |
|
35 /** A subtype of Log for use in APT. |
|
36 * |
|
37 * <p><b>This is NOT part of any supported API. |
|
38 * If you write code that depends on this, you do so at your own risk. |
|
39 * This code and its internal interfaces are subject to change or |
|
40 * deletion without notice.</b> |
|
41 */ |
|
42 public class Bark extends Log { |
|
43 /** The context key for the bark. */ |
|
44 protected static final Context.Key<Bark> barkKey = |
|
45 new Context.Key<Bark>(); |
|
46 |
|
47 /** |
|
48 * Preregisters factories to create and use a Bark object for use as |
|
49 * both a Log and a Bark. |
|
50 */ |
|
51 public static void preRegister(Context context) { |
|
52 context.put(barkKey, new Context.Factory<Bark>() { |
|
53 public Bark make(Context c) { |
|
54 return new Bark(c); |
|
55 } |
|
56 }); |
|
57 context.put(Log.logKey, new Context.Factory<Log>() { |
|
58 public Log make(Context c) { |
|
59 return Bark.instance(c); |
|
60 } |
|
61 }); |
|
62 } |
|
63 |
|
64 /** Get the Bark instance for this context. */ |
|
65 public static Bark instance(Context context) { |
|
66 Bark instance = context.get(barkKey); |
|
67 if (instance == null) |
|
68 instance = new Bark(context); |
|
69 return instance; |
|
70 } |
|
71 |
|
72 /** Specifies whether or not to ignore any diagnostics that are reported. |
|
73 */ |
|
74 private boolean ignoreDiagnostics; |
|
75 |
|
76 /** |
|
77 * Factory for APT-specific diagnostics. |
|
78 */ |
|
79 private JCDiagnostic.Factory aptDiags; |
|
80 |
|
81 |
|
82 /** |
|
83 * Creates a Bark. |
|
84 */ |
|
85 protected Bark(Context context) { |
|
86 super(context); // will register this object in context with Log.logKey |
|
87 context.put(barkKey, this); |
|
88 |
|
89 // register additional resource bundle for APT messages. |
|
90 JavacMessages aptMessages = JavacMessages.instance(context); |
|
91 aptMessages.add("com.sun.tools.apt.resources.apt"); |
|
92 aptDiags = new JCDiagnostic.Factory(aptMessages, "apt"); |
|
93 |
|
94 multipleErrors = true; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Sets a flag indicating whether or not to ignore all diagnostics. |
|
99 * When ignored, they are not reported to the output writers, not are they |
|
100 * counted in the various counters. |
|
101 * @param b If true, subsequent diagnostics will be ignored. |
|
102 * @return the previous state of the flag |
|
103 */ |
|
104 public boolean setDiagnosticsIgnored(boolean b) { |
|
105 boolean prev = ignoreDiagnostics; |
|
106 ignoreDiagnostics = b; |
|
107 return prev; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Report a diagnostic if they are not currently being ignored. |
|
112 */ |
|
113 @Override |
|
114 public void report(JCDiagnostic diagnostic) { |
|
115 if (ignoreDiagnostics) |
|
116 return; |
|
117 |
|
118 super.report(diagnostic); |
|
119 } |
|
120 |
|
121 /** Report an error. |
|
122 * @param key The key for the localized error message. |
|
123 * @param args Fields of the error message. |
|
124 */ |
|
125 public void aptError(String key, Object... args) { |
|
126 aptError(Position.NOPOS, key, args); |
|
127 } |
|
128 |
|
129 /** Report an error, unless another error was already reported at same |
|
130 * source position. |
|
131 * @param pos The source position at which to report the error. |
|
132 * @param key The key for the localized error message. |
|
133 * @param args Fields of the error message. |
|
134 */ |
|
135 public void aptError(int pos, String key, Object ... args) { |
|
136 report(aptDiags.error(source, new SimpleDiagnosticPosition(pos), key, args)); |
|
137 } |
|
138 |
|
139 /** Report a warning, unless suppressed by the -nowarn option or the |
|
140 * maximum number of warnings has been reached. |
|
141 * @param key The key for the localized warning message. |
|
142 * @param args Fields of the warning message. |
|
143 */ |
|
144 public void aptWarning(String key, Object... args) { |
|
145 aptWarning(Position.NOPOS, key, args); |
|
146 } |
|
147 |
|
148 /** Report a warning, unless suppressed by the -nowarn option or the |
|
149 * maximum number of warnings has been reached. |
|
150 * @param pos The source position at which to report the warning. |
|
151 * @param key The key for the localized warning message. |
|
152 * @param args Fields of the warning message. |
|
153 */ |
|
154 public void aptWarning(int pos, String key, Object ... args) { |
|
155 report(aptDiags.warning(source, new SimpleDiagnosticPosition(pos), key, args)); |
|
156 } |
|
157 |
|
158 /** Report a note, unless suppressed by the -nowarn option. |
|
159 * @param key The key for the localized note message. |
|
160 * @param args Fields of the note message. |
|
161 */ |
|
162 public void aptNote(String key, Object... args) { |
|
163 aptNote(Position.NOPOS, key, args); |
|
164 } |
|
165 |
|
166 /** Report a note, unless suppressed by the -nowarn option. |
|
167 * @param pos The source position at which to report the note. |
|
168 * @param key The key for the localized note message. |
|
169 * @param args Fields of the note message. |
|
170 */ |
|
171 public void aptNote(int pos, String key, Object ... args) { |
|
172 report(aptDiags.note(source, new SimpleDiagnosticPosition(pos), key, args)); |
|
173 } |
|
174 } |
|