src/share/classes/com/sun/tools/javac/code/DeferredLintHandler.java

changeset 2028
4ce8148ffc4f
parent 1357
c75be5bc5283
child 2047
5f915a0c9615
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/DeferredLintHandler.java	Sat Sep 14 19:04:47 2013 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/DeferredLintHandler.java	Mon Sep 16 14:13:44 2013 +0200
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -28,6 +28,8 @@
    1.11  import java.util.HashMap;
    1.12  import java.util.Map;
    1.13  
    1.14 +import com.sun.tools.javac.tree.EndPosTable;
    1.15 +import com.sun.tools.javac.tree.JCTree;
    1.16  import com.sun.tools.javac.util.Assert;
    1.17  import com.sun.tools.javac.util.Context;
    1.18  import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    1.19 @@ -53,10 +55,13 @@
    1.20  
    1.21      protected DeferredLintHandler(Context context) {
    1.22          context.put(deferredLintHandlerKey, this);
    1.23 +        this.currentPos = IMMEDIATE_POSITION;
    1.24      }
    1.25  
    1.26 -    private DeferredLintHandler() {}
    1.27 -
    1.28 +    /**An interface for deferred lint reporting - loggers passed to
    1.29 +     * {@link #report(LintLogger) } will be called when
    1.30 +     * {@link #flush(DiagnosticPosition) } is invoked.
    1.31 +     */
    1.32      public interface LintLogger {
    1.33          void report();
    1.34      }
    1.35 @@ -64,12 +69,26 @@
    1.36      private DiagnosticPosition currentPos;
    1.37      private Map<DiagnosticPosition, ListBuffer<LintLogger>> loggersQueue = new HashMap<DiagnosticPosition, ListBuffer<LintLogger>>();
    1.38  
    1.39 +    /**Associate the given logger with the current position as set by {@link #setPos(DiagnosticPosition) }.
    1.40 +     * Will be invoked when {@link #flush(DiagnosticPosition) } will be invoked with the same position.
    1.41 +     * <br>
    1.42 +     * Will invoke the logger synchronously if {@link #immediate() } was called
    1.43 +     * instead of {@link #setPos(DiagnosticPosition) }.
    1.44 +     */
    1.45      public void report(LintLogger logger) {
    1.46 -        ListBuffer<LintLogger> loggers = loggersQueue.get(currentPos);
    1.47 -        Assert.checkNonNull(loggers);
    1.48 -        loggers.append(logger);
    1.49 +        if (currentPos == IMMEDIATE_POSITION) {
    1.50 +            logger.report();
    1.51 +        } else {
    1.52 +            ListBuffer<LintLogger> loggers = loggersQueue.get(currentPos);
    1.53 +            if (loggers == null) {
    1.54 +                loggersQueue.put(currentPos, loggers = ListBuffer.<LintLogger>lb());
    1.55 +            }
    1.56 +            loggers.append(logger);
    1.57 +        }
    1.58      }
    1.59  
    1.60 +    /**Invoke all {@link LintLogger}s that were associated with the provided {@code pos}.
    1.61 +     */
    1.62      public void flush(DiagnosticPosition pos) {
    1.63          ListBuffer<LintLogger> loggers = loggersQueue.get(pos);
    1.64          if (loggers != null) {
    1.65 @@ -80,16 +99,46 @@
    1.66          }
    1.67      }
    1.68  
    1.69 -    public DeferredLintHandler setPos(DiagnosticPosition currentPos) {
    1.70 +    /**Sets the current position to the provided {@code currentPos}. {@link LintLogger}s
    1.71 +     * passed to subsequent invocations of {@link #report(LintLogger) } will be associated
    1.72 +     * with the given position.
    1.73 +     */
    1.74 +    public DiagnosticPosition setPos(DiagnosticPosition currentPos) {
    1.75 +        DiagnosticPosition prevPosition = this.currentPos;
    1.76          this.currentPos = currentPos;
    1.77 -        loggersQueue.put(currentPos, ListBuffer.<LintLogger>lb());
    1.78 -        return this;
    1.79 +        return prevPosition;
    1.80      }
    1.81  
    1.82 -    public static final DeferredLintHandler immediateHandler = new DeferredLintHandler() {
    1.83 +    /**{@link LintLogger}s passed to subsequent invocations of
    1.84 +     * {@link #report(LintLogger) } will be invoked immediately.
    1.85 +     */
    1.86 +    public DiagnosticPosition immediate() {
    1.87 +        return setPos(IMMEDIATE_POSITION);
    1.88 +    }
    1.89 +
    1.90 +    private static final DiagnosticPosition IMMEDIATE_POSITION = new DiagnosticPosition() {
    1.91          @Override
    1.92 -        public void report(LintLogger logger) {
    1.93 -            logger.report();
    1.94 +        public JCTree getTree() {
    1.95 +            Assert.error();
    1.96 +            return null;
    1.97 +        }
    1.98 +
    1.99 +        @Override
   1.100 +        public int getStartPosition() {
   1.101 +            Assert.error();
   1.102 +            return -1;
   1.103 +        }
   1.104 +
   1.105 +        @Override
   1.106 +        public int getPreferredPosition() {
   1.107 +            Assert.error();
   1.108 +            return -1;
   1.109 +        }
   1.110 +
   1.111 +        @Override
   1.112 +        public int getEndPosition(EndPosTable endPosTable) {
   1.113 +            Assert.error();
   1.114 +            return -1;
   1.115          }
   1.116      };
   1.117  }

mercurial