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

Thu, 24 Jul 2008 19:06:57 +0100

author
mcimadamore
date
Thu, 24 Jul 2008 19:06:57 +0100
changeset 80
5c9cdeb740f2
parent 1
9a66ca7c79fa
child 104
5e89c4ca637c
permissions
-rw-r--r--

6717241: some diagnostic argument is prematurely converted into a String object
Summary: removed early toString() conversions applied to diagnostic arguments
Reviewed-by: jjg

duke@1 1 /*
duke@1 2 * Copyright 2005-2006 Sun Microsystems, Inc. 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
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun 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 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any 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.comp.*;
duke@1 31 import com.sun.tools.javac.tree.JCTree;
duke@1 32 import com.sun.tools.javac.tree.JCTree.*;
duke@1 33 import com.sun.tools.javac.util.Position;
duke@1 34 import javax.lang.model.element.*;
duke@1 35 import javax.tools.JavaFileObject;
duke@1 36 import javax.tools.Diagnostic;
duke@1 37 import javax.annotation.processing.*;
duke@1 38 import java.util.*;
duke@1 39
duke@1 40 /**
duke@1 41 * An implementation of the Messager built on top of log.
duke@1 42 *
duke@1 43 * <p><b>This is NOT part of any API supported by Sun Microsystems.
duke@1 44 * If you write code that depends on this, you do so at your own risk.
duke@1 45 * This code and its internal interfaces are subject to change or
duke@1 46 * deletion without notice.</b>
duke@1 47 */
duke@1 48 public class JavacMessager implements Messager {
duke@1 49 Log log;
duke@1 50 JavacProcessingEnvironment processingEnv;
duke@1 51 int errorCount = 0;
duke@1 52
duke@1 53 JavacMessager(Context context, JavacProcessingEnvironment processingEnv) {
duke@1 54 log = Log.instance(context);
duke@1 55 this.processingEnv = processingEnv;
duke@1 56 }
duke@1 57
duke@1 58 // processingEnv.getElementUtils()
duke@1 59
duke@1 60 public void printMessage(Diagnostic.Kind kind, CharSequence msg) {
duke@1 61 printMessage(kind, msg, null, null, null);
duke@1 62 }
duke@1 63
duke@1 64 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
duke@1 65 Element e) {
duke@1 66 printMessage(kind, msg, e, null, null);
duke@1 67 }
duke@1 68
duke@1 69 /**
duke@1 70 * Prints a message of the specified kind at the location of the
duke@1 71 * annotation mirror of the annotated element.
duke@1 72 *
duke@1 73 * @param kind the kind of message
duke@1 74 * @param msg the message, or an empty string if none
duke@1 75 * @param e the annotated element
duke@1 76 * @param a the annotation to use as a position hint
duke@1 77 */
duke@1 78 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
duke@1 79 Element e, AnnotationMirror a) {
duke@1 80 printMessage(kind, msg, e, a, null);
duke@1 81 }
duke@1 82
duke@1 83 /**
duke@1 84 * Prints a message of the specified kind at the location of the
duke@1 85 * annotation value inside the annotation mirror of the annotated
duke@1 86 * element.
duke@1 87 *
duke@1 88 * @param kind the kind of message
duke@1 89 * @param msg the message, or an empty string if none
duke@1 90 * @param e the annotated element
duke@1 91 * @param a the annotation containing the annotaiton value
duke@1 92 * @param v the annotation value to use as a position hint
duke@1 93 */
duke@1 94 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
duke@1 95 Element e, AnnotationMirror a, AnnotationValue v) {
duke@1 96 JavaFileObject oldSource = null;
duke@1 97 JavaFileObject newSource = null;
duke@1 98 JCDiagnostic.DiagnosticPosition pos = null;
duke@1 99 JavacElements elemUtils = processingEnv.getElementUtils();
duke@1 100 Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v);
duke@1 101 if (treeTop != null) {
duke@1 102 newSource = treeTop.snd.sourcefile;
duke@1 103 if (newSource != null) {
duke@1 104 oldSource = log.useSource(newSource);
duke@1 105 pos = treeTop.fst.pos();
duke@1 106 }
duke@1 107 }
duke@1 108 try {
duke@1 109 switch (kind) {
duke@1 110 case ERROR:
duke@1 111 errorCount++;
duke@1 112 boolean prev = log.multipleErrors;
duke@1 113 log.multipleErrors = true;
duke@1 114 try {
duke@1 115 log.error(pos, "proc.messager", msg.toString());
duke@1 116 } finally {
duke@1 117 log.multipleErrors = prev;
duke@1 118 }
duke@1 119 break;
duke@1 120
duke@1 121 case WARNING:
duke@1 122 log.warning(pos, "proc.messager", msg.toString());
duke@1 123 break;
duke@1 124
duke@1 125 case MANDATORY_WARNING:
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
duke@1 173 public void newRound(Context context) {
duke@1 174 log = Log.instance(context);
duke@1 175 errorCount = 0;
duke@1 176 }
duke@1 177
duke@1 178 public String toString() {
duke@1 179 return "javac Messager";
duke@1 180 }
duke@1 181 }

mercurial