Wed, 20 Jun 2012 13:23:26 -0700
7174143: encapsulate doc comment table
Reviewed-by: ksrini, mcimadamore
duke@1 | 1 | /* |
ksrini@1258 | 2 | * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.processing; |
duke@1 | 27 | |
duke@1 | 28 | import com.sun.tools.javac.model.JavacElements; |
duke@1 | 29 | import com.sun.tools.javac.util.*; |
duke@1 | 30 | import com.sun.tools.javac.tree.JCTree; |
duke@1 | 31 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 32 | import javax.lang.model.element.*; |
duke@1 | 33 | import javax.tools.JavaFileObject; |
duke@1 | 34 | import javax.tools.Diagnostic; |
duke@1 | 35 | import javax.annotation.processing.*; |
duke@1 | 36 | |
duke@1 | 37 | /** |
duke@1 | 38 | * An implementation of the Messager built on top of log. |
duke@1 | 39 | * |
jjg@581 | 40 | * <p><b>This is NOT part of any supported API. |
duke@1 | 41 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 42 | * This code and its internal interfaces are subject to change or |
duke@1 | 43 | * deletion without notice.</b> |
duke@1 | 44 | */ |
duke@1 | 45 | public class JavacMessager implements Messager { |
duke@1 | 46 | Log log; |
duke@1 | 47 | JavacProcessingEnvironment processingEnv; |
duke@1 | 48 | int errorCount = 0; |
jjg@614 | 49 | int warningCount = 0; |
duke@1 | 50 | |
duke@1 | 51 | JavacMessager(Context context, JavacProcessingEnvironment processingEnv) { |
duke@1 | 52 | log = Log.instance(context); |
duke@1 | 53 | this.processingEnv = processingEnv; |
duke@1 | 54 | } |
duke@1 | 55 | |
duke@1 | 56 | // processingEnv.getElementUtils() |
duke@1 | 57 | |
duke@1 | 58 | public void printMessage(Diagnostic.Kind kind, CharSequence msg) { |
duke@1 | 59 | printMessage(kind, msg, null, null, null); |
duke@1 | 60 | } |
duke@1 | 61 | |
duke@1 | 62 | public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
duke@1 | 63 | Element e) { |
duke@1 | 64 | printMessage(kind, msg, e, null, null); |
duke@1 | 65 | } |
duke@1 | 66 | |
duke@1 | 67 | /** |
duke@1 | 68 | * Prints a message of the specified kind at the location of the |
duke@1 | 69 | * annotation mirror of the annotated element. |
duke@1 | 70 | * |
duke@1 | 71 | * @param kind the kind of message |
duke@1 | 72 | * @param msg the message, or an empty string if none |
duke@1 | 73 | * @param e the annotated element |
duke@1 | 74 | * @param a the annotation to use as a position hint |
duke@1 | 75 | */ |
duke@1 | 76 | public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
duke@1 | 77 | Element e, AnnotationMirror a) { |
duke@1 | 78 | printMessage(kind, msg, e, a, null); |
duke@1 | 79 | } |
duke@1 | 80 | |
duke@1 | 81 | /** |
duke@1 | 82 | * Prints a message of the specified kind at the location of the |
duke@1 | 83 | * annotation value inside the annotation mirror of the annotated |
duke@1 | 84 | * element. |
duke@1 | 85 | * |
duke@1 | 86 | * @param kind the kind of message |
duke@1 | 87 | * @param msg the message, or an empty string if none |
duke@1 | 88 | * @param e the annotated element |
duke@1 | 89 | * @param a the annotation containing the annotaiton value |
duke@1 | 90 | * @param v the annotation value to use as a position hint |
duke@1 | 91 | */ |
duke@1 | 92 | public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
duke@1 | 93 | Element e, AnnotationMirror a, AnnotationValue v) { |
duke@1 | 94 | JavaFileObject oldSource = null; |
duke@1 | 95 | JavaFileObject newSource = null; |
duke@1 | 96 | JCDiagnostic.DiagnosticPosition pos = null; |
duke@1 | 97 | JavacElements elemUtils = processingEnv.getElementUtils(); |
duke@1 | 98 | Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v); |
duke@1 | 99 | if (treeTop != null) { |
duke@1 | 100 | newSource = treeTop.snd.sourcefile; |
duke@1 | 101 | if (newSource != null) { |
ksrini@1258 | 102 | // save the old version and reinstate it later |
duke@1 | 103 | oldSource = log.useSource(newSource); |
duke@1 | 104 | pos = treeTop.fst.pos(); |
duke@1 | 105 | } |
duke@1 | 106 | } |
duke@1 | 107 | try { |
duke@1 | 108 | switch (kind) { |
duke@1 | 109 | case ERROR: |
duke@1 | 110 | errorCount++; |
duke@1 | 111 | boolean prev = log.multipleErrors; |
duke@1 | 112 | log.multipleErrors = true; |
duke@1 | 113 | try { |
duke@1 | 114 | log.error(pos, "proc.messager", msg.toString()); |
duke@1 | 115 | } finally { |
duke@1 | 116 | log.multipleErrors = prev; |
duke@1 | 117 | } |
duke@1 | 118 | break; |
duke@1 | 119 | |
duke@1 | 120 | case WARNING: |
jjg@614 | 121 | warningCount++; |
duke@1 | 122 | log.warning(pos, "proc.messager", msg.toString()); |
duke@1 | 123 | break; |
duke@1 | 124 | |
duke@1 | 125 | case MANDATORY_WARNING: |
jjg@614 | 126 | warningCount++; |
duke@1 | 127 | log.mandatoryWarning(pos, "proc.messager", msg.toString()); |
duke@1 | 128 | break; |
duke@1 | 129 | |
duke@1 | 130 | default: |
duke@1 | 131 | log.note(pos, "proc.messager", msg.toString()); |
duke@1 | 132 | break; |
duke@1 | 133 | } |
duke@1 | 134 | } finally { |
ksrini@1258 | 135 | // reinstate the saved version, only if it was saved earlier |
ksrini@1258 | 136 | if (newSource != null) |
duke@1 | 137 | log.useSource(oldSource); |
duke@1 | 138 | } |
duke@1 | 139 | } |
duke@1 | 140 | |
duke@1 | 141 | /** |
duke@1 | 142 | * Prints an error message. |
duke@1 | 143 | * Equivalent to {@code printError(null, msg)}. |
duke@1 | 144 | * @param msg the message, or an empty string if none |
duke@1 | 145 | */ |
duke@1 | 146 | public void printError(String msg) { |
duke@1 | 147 | printMessage(Diagnostic.Kind.ERROR, msg); |
duke@1 | 148 | } |
duke@1 | 149 | |
duke@1 | 150 | /** |
duke@1 | 151 | * Prints a warning message. |
duke@1 | 152 | * Equivalent to {@code printWarning(null, msg)}. |
duke@1 | 153 | * @param msg the message, or an empty string if none |
duke@1 | 154 | */ |
duke@1 | 155 | public void printWarning(String msg) { |
duke@1 | 156 | printMessage(Diagnostic.Kind.WARNING, msg); |
duke@1 | 157 | } |
duke@1 | 158 | |
duke@1 | 159 | /** |
duke@1 | 160 | * Prints a notice. |
duke@1 | 161 | * @param msg the message, or an empty string if none |
duke@1 | 162 | */ |
duke@1 | 163 | public void printNotice(String msg) { |
duke@1 | 164 | printMessage(Diagnostic.Kind.NOTE, msg); |
duke@1 | 165 | } |
duke@1 | 166 | |
duke@1 | 167 | public boolean errorRaised() { |
duke@1 | 168 | return errorCount > 0; |
duke@1 | 169 | } |
duke@1 | 170 | |
duke@1 | 171 | public int errorCount() { |
duke@1 | 172 | return errorCount; |
duke@1 | 173 | } |
duke@1 | 174 | |
jjg@614 | 175 | public int warningCount() { |
jjg@614 | 176 | return warningCount; |
jjg@614 | 177 | } |
jjg@614 | 178 | |
duke@1 | 179 | public void newRound(Context context) { |
duke@1 | 180 | log = Log.instance(context); |
duke@1 | 181 | errorCount = 0; |
duke@1 | 182 | } |
duke@1 | 183 | |
duke@1 | 184 | public String toString() { |
duke@1 | 185 | return "javac Messager"; |
duke@1 | 186 | } |
duke@1 | 187 | } |