src/share/classes/com/sun/tools/javac/processing/JavacMessager.java

Tue, 05 Jul 2011 16:37:24 -0700

author
darcy
date
Tue, 05 Jul 2011 16:37:24 -0700
changeset 1054
111bbf1ad913
parent 798
4868a36f6fd8
child 1258
d10db3576c08
permissions
-rw-r--r--

7025809: Provided new utility visitors supporting SourceVersion.RELEASE_8
Reviewed-by: jjg, mcimadamore

duke@1 1 /*
ohair@798 2 * Copyright (c) 2005, 2010, 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) {
duke@1 102 oldSource = log.useSource(newSource);
duke@1 103 pos = treeTop.fst.pos();
duke@1 104 }
duke@1 105 }
duke@1 106 try {
duke@1 107 switch (kind) {
duke@1 108 case ERROR:
duke@1 109 errorCount++;
duke@1 110 boolean prev = log.multipleErrors;
duke@1 111 log.multipleErrors = true;
duke@1 112 try {
duke@1 113 log.error(pos, "proc.messager", msg.toString());
duke@1 114 } finally {
duke@1 115 log.multipleErrors = prev;
duke@1 116 }
duke@1 117 break;
duke@1 118
duke@1 119 case WARNING:
jjg@614 120 warningCount++;
duke@1 121 log.warning(pos, "proc.messager", msg.toString());
duke@1 122 break;
duke@1 123
duke@1 124 case MANDATORY_WARNING:
jjg@614 125 warningCount++;
duke@1 126 log.mandatoryWarning(pos, "proc.messager", msg.toString());
duke@1 127 break;
duke@1 128
duke@1 129 default:
duke@1 130 log.note(pos, "proc.messager", msg.toString());
duke@1 131 break;
duke@1 132 }
duke@1 133 } finally {
duke@1 134 if (oldSource != null)
duke@1 135 log.useSource(oldSource);
duke@1 136 }
duke@1 137 }
duke@1 138
duke@1 139 /**
duke@1 140 * Prints an error message.
duke@1 141 * Equivalent to {@code printError(null, msg)}.
duke@1 142 * @param msg the message, or an empty string if none
duke@1 143 */
duke@1 144 public void printError(String msg) {
duke@1 145 printMessage(Diagnostic.Kind.ERROR, msg);
duke@1 146 }
duke@1 147
duke@1 148 /**
duke@1 149 * Prints a warning message.
duke@1 150 * Equivalent to {@code printWarning(null, msg)}.
duke@1 151 * @param msg the message, or an empty string if none
duke@1 152 */
duke@1 153 public void printWarning(String msg) {
duke@1 154 printMessage(Diagnostic.Kind.WARNING, msg);
duke@1 155 }
duke@1 156
duke@1 157 /**
duke@1 158 * Prints a notice.
duke@1 159 * @param msg the message, or an empty string if none
duke@1 160 */
duke@1 161 public void printNotice(String msg) {
duke@1 162 printMessage(Diagnostic.Kind.NOTE, msg);
duke@1 163 }
duke@1 164
duke@1 165 public boolean errorRaised() {
duke@1 166 return errorCount > 0;
duke@1 167 }
duke@1 168
duke@1 169 public int errorCount() {
duke@1 170 return errorCount;
duke@1 171 }
duke@1 172
jjg@614 173 public int warningCount() {
jjg@614 174 return warningCount;
jjg@614 175 }
jjg@614 176
duke@1 177 public void newRound(Context context) {
duke@1 178 log = Log.instance(context);
duke@1 179 errorCount = 0;
duke@1 180 }
duke@1 181
duke@1 182 public String toString() {
duke@1 183 return "javac Messager";
duke@1 184 }
duke@1 185 }

mercurial