1 /*
   2  * Copyright 2004-2005 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package com.sun.tools.apt.mirror.apt;
  27 
  28 import javax.tools.JavaFileObject;
  29 import com.sun.mirror.apt.Messager;
  30 import com.sun.tools.apt.mirror.util.SourcePositionImpl;
  31 import com.sun.mirror.util.SourcePosition;
  32 import com.sun.tools.javac.util.Context;
  33 import com.sun.tools.javac.util.Name;
  34 import com.sun.tools.javac.util.Position;
  35 import com.sun.tools.apt.util.Bark;
  36 
  37 
  38 /**
  39  * Implementation of Messager.
  40  */
  41 @SuppressWarnings("deprecation")
  42 public class MessagerImpl implements Messager {
  43     private final Bark bark;
  44 
  45     private static final Context.Key<MessagerImpl> messagerKey =
  46             new Context.Key<MessagerImpl>();
  47 
  48     public static MessagerImpl instance(Context context) {
  49         MessagerImpl instance = context.get(messagerKey);
  50         if (instance == null) {
  51             instance = new MessagerImpl(context);
  52         }
  53         return instance;
  54     }
  55 
  56     private MessagerImpl(Context context) {
  57         context.put(messagerKey, this);
  58         bark = Bark.instance(context);
  59     }
  60 
  61 
  62     /**
  63      * {@inheritDoc}
  64      */
  65     public void printError(String msg) {
  66         bark.aptError("Messager", msg);
  67     }
  68 
  69     /**
  70      * {@inheritDoc}
  71      */
  72     public void printError(SourcePosition pos, String msg) {
  73         if (pos instanceof SourcePositionImpl) {
  74             SourcePositionImpl posImpl = (SourcePositionImpl) pos;
  75             JavaFileObject prev = bark.useSource(posImpl.getSource());
  76             bark.aptError(posImpl.getJavacPosition(), "Messager", msg);
  77             bark.useSource(prev);
  78         } else
  79             printError(msg);
  80     }
  81 
  82     /**
  83      * {@inheritDoc}
  84      */
  85     public void printWarning(String msg) {
  86         bark.aptWarning("Messager", msg);
  87     }
  88 
  89     /**
  90      * {@inheritDoc}
  91      */
  92     public void printWarning(SourcePosition pos, String msg) {
  93         if (pos instanceof SourcePositionImpl) {
  94             SourcePositionImpl posImpl = (SourcePositionImpl) pos;
  95             JavaFileObject prev = bark.useSource(posImpl.getSource());
  96             bark.aptWarning(posImpl.getJavacPosition(), "Messager", msg);
  97             bark.useSource(prev);
  98         } else
  99             printWarning(msg);
 100     }
 101 
 102     /**
 103      * {@inheritDoc}
 104      */
 105     public void printNotice(String msg) {
 106         bark.aptNote("Messager", msg);
 107     }
 108 
 109     /**
 110      * {@inheritDoc}
 111      */
 112     public void printNotice(SourcePosition pos, String msg) {
 113         if (pos instanceof SourcePositionImpl) {
 114             SourcePositionImpl posImpl = (SourcePositionImpl) pos;
 115             JavaFileObject prev = bark.useSource(posImpl.getSource());
 116             bark.aptNote(posImpl.getJavacPosition(), "Messager", msg);
 117             bark.useSource(prev);
 118         } else
 119             printNotice(msg);
 120     }
 121 }