Mon, 17 Dec 2012 07:47:05 -0800
8004832: Add new doclint package
Reviewed-by: mcimadamore
mcimadamore@852 | 1 | /* |
jjg@1357 | 2 | * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@852 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@852 | 4 | * |
mcimadamore@852 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@852 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@852 | 7 | * published by the Free Software Foundation. Oracle designates this |
mcimadamore@852 | 8 | * particular file as subject to the "Classpath" exception as provided |
mcimadamore@852 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
mcimadamore@852 | 10 | * |
mcimadamore@852 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@852 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@852 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@852 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@852 | 15 | * accompanied this code). |
mcimadamore@852 | 16 | * |
mcimadamore@852 | 17 | * You should have received a copy of the GNU General Public License version |
mcimadamore@852 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@852 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@852 | 20 | * |
mcimadamore@852 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@852 | 22 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@852 | 23 | * questions. |
mcimadamore@852 | 24 | */ |
mcimadamore@852 | 25 | |
mcimadamore@852 | 26 | package com.sun.tools.javac.code; |
mcimadamore@852 | 27 | |
mcimadamore@852 | 28 | import java.util.HashMap; |
mcimadamore@852 | 29 | import java.util.Map; |
mcimadamore@852 | 30 | |
mcimadamore@852 | 31 | import com.sun.tools.javac.util.Assert; |
mcimadamore@852 | 32 | import com.sun.tools.javac.util.Context; |
jjg@1357 | 33 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
mcimadamore@852 | 34 | import com.sun.tools.javac.util.ListBuffer; |
mcimadamore@852 | 35 | |
mcimadamore@852 | 36 | /** |
mcimadamore@852 | 37 | * |
mcimadamore@852 | 38 | * <p><b>This is NOT part of any supported API. |
mcimadamore@852 | 39 | * If you write code that depends on this, you do so at your own risk. |
mcimadamore@852 | 40 | * This code and its internal interfaces are subject to change or |
mcimadamore@852 | 41 | * deletion without notice.</b> |
mcimadamore@852 | 42 | */ |
mcimadamore@852 | 43 | public class DeferredLintHandler { |
mcimadamore@852 | 44 | protected static final Context.Key<DeferredLintHandler> deferredLintHandlerKey = |
mcimadamore@852 | 45 | new Context.Key<DeferredLintHandler>(); |
mcimadamore@852 | 46 | |
mcimadamore@852 | 47 | public static DeferredLintHandler instance(Context context) { |
mcimadamore@852 | 48 | DeferredLintHandler instance = context.get(deferredLintHandlerKey); |
mcimadamore@852 | 49 | if (instance == null) |
mcimadamore@852 | 50 | instance = new DeferredLintHandler(context); |
mcimadamore@852 | 51 | return instance; |
mcimadamore@852 | 52 | } |
mcimadamore@852 | 53 | |
mcimadamore@852 | 54 | protected DeferredLintHandler(Context context) { |
mcimadamore@852 | 55 | context.put(deferredLintHandlerKey, this); |
mcimadamore@852 | 56 | } |
mcimadamore@852 | 57 | |
mcimadamore@852 | 58 | private DeferredLintHandler() {} |
mcimadamore@852 | 59 | |
mcimadamore@852 | 60 | public interface LintLogger { |
mcimadamore@852 | 61 | void report(); |
mcimadamore@852 | 62 | } |
mcimadamore@852 | 63 | |
mcimadamore@852 | 64 | private DiagnosticPosition currentPos; |
mcimadamore@852 | 65 | private Map<DiagnosticPosition, ListBuffer<LintLogger>> loggersQueue = new HashMap<DiagnosticPosition, ListBuffer<LintLogger>>(); |
mcimadamore@852 | 66 | |
mcimadamore@852 | 67 | public void report(LintLogger logger) { |
mcimadamore@852 | 68 | ListBuffer<LintLogger> loggers = loggersQueue.get(currentPos); |
mcimadamore@852 | 69 | Assert.checkNonNull(loggers); |
mcimadamore@852 | 70 | loggers.append(logger); |
mcimadamore@852 | 71 | } |
mcimadamore@852 | 72 | |
mcimadamore@852 | 73 | public void flush(DiagnosticPosition pos) { |
mcimadamore@852 | 74 | ListBuffer<LintLogger> loggers = loggersQueue.get(pos); |
mcimadamore@852 | 75 | if (loggers != null) { |
mcimadamore@852 | 76 | for (LintLogger lintLogger : loggers) { |
mcimadamore@852 | 77 | lintLogger.report(); |
mcimadamore@852 | 78 | } |
mcimadamore@852 | 79 | loggersQueue.remove(pos); |
mcimadamore@852 | 80 | } |
mcimadamore@852 | 81 | } |
mcimadamore@852 | 82 | |
mcimadamore@852 | 83 | public DeferredLintHandler setPos(DiagnosticPosition currentPos) { |
mcimadamore@852 | 84 | this.currentPos = currentPos; |
mcimadamore@852 | 85 | loggersQueue.put(currentPos, ListBuffer.<LintLogger>lb()); |
mcimadamore@852 | 86 | return this; |
mcimadamore@852 | 87 | } |
mcimadamore@852 | 88 | |
mcimadamore@852 | 89 | public static final DeferredLintHandler immediateHandler = new DeferredLintHandler() { |
mcimadamore@852 | 90 | @Override |
mcimadamore@852 | 91 | public void report(LintLogger logger) { |
mcimadamore@852 | 92 | logger.report(); |
mcimadamore@852 | 93 | } |
mcimadamore@852 | 94 | }; |
mcimadamore@852 | 95 | } |