1 /* |
1 /* |
2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
26 package com.sun.tools.javac.code; |
26 package com.sun.tools.javac.code; |
27 |
27 |
28 import java.util.HashMap; |
28 import java.util.HashMap; |
29 import java.util.Map; |
29 import java.util.Map; |
30 |
30 |
|
31 import com.sun.tools.javac.tree.EndPosTable; |
|
32 import com.sun.tools.javac.tree.JCTree; |
31 import com.sun.tools.javac.util.Assert; |
33 import com.sun.tools.javac.util.Assert; |
32 import com.sun.tools.javac.util.Context; |
34 import com.sun.tools.javac.util.Context; |
33 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
35 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
34 import com.sun.tools.javac.util.ListBuffer; |
36 import com.sun.tools.javac.util.ListBuffer; |
35 |
37 |
51 return instance; |
53 return instance; |
52 } |
54 } |
53 |
55 |
54 protected DeferredLintHandler(Context context) { |
56 protected DeferredLintHandler(Context context) { |
55 context.put(deferredLintHandlerKey, this); |
57 context.put(deferredLintHandlerKey, this); |
|
58 this.currentPos = IMMEDIATE_POSITION; |
56 } |
59 } |
57 |
60 |
58 private DeferredLintHandler() {} |
61 /**An interface for deferred lint reporting - loggers passed to |
59 |
62 * {@link #report(LintLogger) } will be called when |
|
63 * {@link #flush(DiagnosticPosition) } is invoked. |
|
64 */ |
60 public interface LintLogger { |
65 public interface LintLogger { |
61 void report(); |
66 void report(); |
62 } |
67 } |
63 |
68 |
64 private DiagnosticPosition currentPos; |
69 private DiagnosticPosition currentPos; |
65 private Map<DiagnosticPosition, ListBuffer<LintLogger>> loggersQueue = new HashMap<DiagnosticPosition, ListBuffer<LintLogger>>(); |
70 private Map<DiagnosticPosition, ListBuffer<LintLogger>> loggersQueue = new HashMap<DiagnosticPosition, ListBuffer<LintLogger>>(); |
66 |
71 |
|
72 /**Associate the given logger with the current position as set by {@link #setPos(DiagnosticPosition) }. |
|
73 * Will be invoked when {@link #flush(DiagnosticPosition) } will be invoked with the same position. |
|
74 * <br> |
|
75 * Will invoke the logger synchronously if {@link #immediate() } was called |
|
76 * instead of {@link #setPos(DiagnosticPosition) }. |
|
77 */ |
67 public void report(LintLogger logger) { |
78 public void report(LintLogger logger) { |
68 ListBuffer<LintLogger> loggers = loggersQueue.get(currentPos); |
79 if (currentPos == IMMEDIATE_POSITION) { |
69 Assert.checkNonNull(loggers); |
80 logger.report(); |
70 loggers.append(logger); |
81 } else { |
|
82 ListBuffer<LintLogger> loggers = loggersQueue.get(currentPos); |
|
83 if (loggers == null) { |
|
84 loggersQueue.put(currentPos, loggers = ListBuffer.<LintLogger>lb()); |
|
85 } |
|
86 loggers.append(logger); |
|
87 } |
71 } |
88 } |
72 |
89 |
|
90 /**Invoke all {@link LintLogger}s that were associated with the provided {@code pos}. |
|
91 */ |
73 public void flush(DiagnosticPosition pos) { |
92 public void flush(DiagnosticPosition pos) { |
74 ListBuffer<LintLogger> loggers = loggersQueue.get(pos); |
93 ListBuffer<LintLogger> loggers = loggersQueue.get(pos); |
75 if (loggers != null) { |
94 if (loggers != null) { |
76 for (LintLogger lintLogger : loggers) { |
95 for (LintLogger lintLogger : loggers) { |
77 lintLogger.report(); |
96 lintLogger.report(); |
78 } |
97 } |
79 loggersQueue.remove(pos); |
98 loggersQueue.remove(pos); |
80 } |
99 } |
81 } |
100 } |
82 |
101 |
83 public DeferredLintHandler setPos(DiagnosticPosition currentPos) { |
102 /**Sets the current position to the provided {@code currentPos}. {@link LintLogger}s |
|
103 * passed to subsequent invocations of {@link #report(LintLogger) } will be associated |
|
104 * with the given position. |
|
105 */ |
|
106 public DiagnosticPosition setPos(DiagnosticPosition currentPos) { |
|
107 DiagnosticPosition prevPosition = this.currentPos; |
84 this.currentPos = currentPos; |
108 this.currentPos = currentPos; |
85 loggersQueue.put(currentPos, ListBuffer.<LintLogger>lb()); |
109 return prevPosition; |
86 return this; |
|
87 } |
110 } |
88 |
111 |
89 public static final DeferredLintHandler immediateHandler = new DeferredLintHandler() { |
112 /**{@link LintLogger}s passed to subsequent invocations of |
|
113 * {@link #report(LintLogger) } will be invoked immediately. |
|
114 */ |
|
115 public DiagnosticPosition immediate() { |
|
116 return setPos(IMMEDIATE_POSITION); |
|
117 } |
|
118 |
|
119 private static final DiagnosticPosition IMMEDIATE_POSITION = new DiagnosticPosition() { |
90 @Override |
120 @Override |
91 public void report(LintLogger logger) { |
121 public JCTree getTree() { |
92 logger.report(); |
122 Assert.error(); |
|
123 return null; |
|
124 } |
|
125 |
|
126 @Override |
|
127 public int getStartPosition() { |
|
128 Assert.error(); |
|
129 return -1; |
|
130 } |
|
131 |
|
132 @Override |
|
133 public int getPreferredPosition() { |
|
134 Assert.error(); |
|
135 return -1; |
|
136 } |
|
137 |
|
138 @Override |
|
139 public int getEndPosition(EndPosTable endPosTable) { |
|
140 Assert.error(); |
|
141 return -1; |
93 } |
142 } |
94 }; |
143 }; |
95 } |
144 } |