mcimadamore@852: /*
jlahoda@2028: * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
mcimadamore@852: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@852: *
mcimadamore@852: * This code is free software; you can redistribute it and/or modify it
mcimadamore@852: * under the terms of the GNU General Public License version 2 only, as
mcimadamore@852: * published by the Free Software Foundation. Oracle designates this
mcimadamore@852: * particular file as subject to the "Classpath" exception as provided
mcimadamore@852: * by Oracle in the LICENSE file that accompanied this code.
mcimadamore@852: *
mcimadamore@852: * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@852: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@852: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@852: * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@852: * accompanied this code).
mcimadamore@852: *
mcimadamore@852: * You should have received a copy of the GNU General Public License version
mcimadamore@852: * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@852: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@852: *
mcimadamore@852: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@852: * or visit www.oracle.com if you need additional information or have any
mcimadamore@852: * questions.
mcimadamore@852: */
mcimadamore@852:
mcimadamore@852: package com.sun.tools.javac.code;
mcimadamore@852:
mcimadamore@852: import java.util.HashMap;
mcimadamore@852: import java.util.Map;
mcimadamore@852:
jlahoda@2028: import com.sun.tools.javac.tree.EndPosTable;
jlahoda@2028: import com.sun.tools.javac.tree.JCTree;
mcimadamore@852: import com.sun.tools.javac.util.Assert;
mcimadamore@852: import com.sun.tools.javac.util.Context;
jjg@1357: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
mcimadamore@852: import com.sun.tools.javac.util.ListBuffer;
mcimadamore@852:
mcimadamore@852: /**
mcimadamore@852: *
mcimadamore@852: *
This is NOT part of any supported API.
mcimadamore@852: * If you write code that depends on this, you do so at your own risk.
mcimadamore@852: * This code and its internal interfaces are subject to change or
mcimadamore@852: * deletion without notice.
mcimadamore@852: */
mcimadamore@852: public class DeferredLintHandler {
mcimadamore@852: protected static final Context.Key deferredLintHandlerKey =
mcimadamore@852: new Context.Key();
mcimadamore@852:
mcimadamore@852: public static DeferredLintHandler instance(Context context) {
mcimadamore@852: DeferredLintHandler instance = context.get(deferredLintHandlerKey);
mcimadamore@852: if (instance == null)
mcimadamore@852: instance = new DeferredLintHandler(context);
mcimadamore@852: return instance;
mcimadamore@852: }
mcimadamore@852:
mcimadamore@852: protected DeferredLintHandler(Context context) {
mcimadamore@852: context.put(deferredLintHandlerKey, this);
jlahoda@2028: this.currentPos = IMMEDIATE_POSITION;
mcimadamore@852: }
mcimadamore@852:
jlahoda@2028: /**An interface for deferred lint reporting - loggers passed to
jlahoda@2028: * {@link #report(LintLogger) } will be called when
jlahoda@2028: * {@link #flush(DiagnosticPosition) } is invoked.
jlahoda@2028: */
mcimadamore@852: public interface LintLogger {
mcimadamore@852: void report();
mcimadamore@852: }
mcimadamore@852:
mcimadamore@852: private DiagnosticPosition currentPos;
mcimadamore@852: private Map> loggersQueue = new HashMap>();
mcimadamore@852:
jlahoda@2028: /**Associate the given logger with the current position as set by {@link #setPos(DiagnosticPosition) }.
jlahoda@2028: * Will be invoked when {@link #flush(DiagnosticPosition) } will be invoked with the same position.
jlahoda@2028: *
jlahoda@2028: * Will invoke the logger synchronously if {@link #immediate() } was called
jlahoda@2028: * instead of {@link #setPos(DiagnosticPosition) }.
jlahoda@2028: */
mcimadamore@852: public void report(LintLogger logger) {
jlahoda@2028: if (currentPos == IMMEDIATE_POSITION) {
jlahoda@2028: logger.report();
jlahoda@2028: } else {
jlahoda@2028: ListBuffer loggers = loggersQueue.get(currentPos);
jlahoda@2028: if (loggers == null) {
alundblad@2047: loggersQueue.put(currentPos, loggers = new ListBuffer<>());
jlahoda@2028: }
jlahoda@2028: loggers.append(logger);
jlahoda@2028: }
mcimadamore@852: }
mcimadamore@852:
jlahoda@2028: /**Invoke all {@link LintLogger}s that were associated with the provided {@code pos}.
jlahoda@2028: */
mcimadamore@852: public void flush(DiagnosticPosition pos) {
mcimadamore@852: ListBuffer loggers = loggersQueue.get(pos);
mcimadamore@852: if (loggers != null) {
mcimadamore@852: for (LintLogger lintLogger : loggers) {
mcimadamore@852: lintLogger.report();
mcimadamore@852: }
mcimadamore@852: loggersQueue.remove(pos);
mcimadamore@852: }
mcimadamore@852: }
mcimadamore@852:
jlahoda@2028: /**Sets the current position to the provided {@code currentPos}. {@link LintLogger}s
jlahoda@2028: * passed to subsequent invocations of {@link #report(LintLogger) } will be associated
jlahoda@2028: * with the given position.
jlahoda@2028: */
jlahoda@2028: public DiagnosticPosition setPos(DiagnosticPosition currentPos) {
jlahoda@2028: DiagnosticPosition prevPosition = this.currentPos;
mcimadamore@852: this.currentPos = currentPos;
jlahoda@2028: return prevPosition;
mcimadamore@852: }
mcimadamore@852:
jlahoda@2028: /**{@link LintLogger}s passed to subsequent invocations of
jlahoda@2028: * {@link #report(LintLogger) } will be invoked immediately.
jlahoda@2028: */
jlahoda@2028: public DiagnosticPosition immediate() {
jlahoda@2028: return setPos(IMMEDIATE_POSITION);
jlahoda@2028: }
jlahoda@2028:
jlahoda@2028: private static final DiagnosticPosition IMMEDIATE_POSITION = new DiagnosticPosition() {
mcimadamore@852: @Override
jlahoda@2028: public JCTree getTree() {
jlahoda@2028: Assert.error();
jlahoda@2028: return null;
jlahoda@2028: }
jlahoda@2028:
jlahoda@2028: @Override
jlahoda@2028: public int getStartPosition() {
jlahoda@2028: Assert.error();
jlahoda@2028: return -1;
jlahoda@2028: }
jlahoda@2028:
jlahoda@2028: @Override
jlahoda@2028: public int getPreferredPosition() {
jlahoda@2028: Assert.error();
jlahoda@2028: return -1;
jlahoda@2028: }
jlahoda@2028:
jlahoda@2028: @Override
jlahoda@2028: public int getEndPosition(EndPosTable endPosTable) {
jlahoda@2028: Assert.error();
jlahoda@2028: return -1;
mcimadamore@852: }
mcimadamore@852: };
mcimadamore@852: }