author | jlahoda |
Tue, 13 Jan 2015 19:13:42 +0100 | |
changeset 28455 | 41245007c074 |
parent 13077 | 16fb753bb5dc |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
2 |
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. |
10 | 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. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5520 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
10 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6304912 |
|
27 |
* @summary unit test for Log |
|
28 |
*/ |
|
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
29 |
import java.lang.reflect.Field; |
10 | 30 |
import java.io.InputStream; |
31 |
import java.io.OutputStream; |
|
32 |
import java.net.URI; |
|
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
33 |
import java.util.Set; |
10 | 34 |
import javax.tools.JavaFileObject; |
35 |
import javax.tools.SimpleJavaFileObject; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
10
diff
changeset
|
36 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 37 |
import com.sun.tools.javac.parser.Parser; |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
38 |
import com.sun.tools.javac.parser.ParserFactory; |
13077 | 39 |
import com.sun.tools.javac.tree.EndPosTable; |
10 | 40 |
import com.sun.tools.javac.tree.JCTree; |
41 |
import com.sun.tools.javac.tree.TreeScanner; |
|
42 |
import com.sun.tools.javac.util.Context; |
|
43 |
import com.sun.tools.javac.util.Log; |
|
44 |
import com.sun.tools.javac.util.JCDiagnostic; |
|
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
45 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag; |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
46 |
import com.sun.tools.javac.util.JCDiagnostic.Factory; |
10 | 47 |
import com.sun.tools.javac.util.Options; |
48 |
||
49 |
public class TestLog |
|
50 |
{ |
|
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
51 |
public static void main(String... args) throws Exception { |
10 | 52 |
test(false); |
53 |
test(true); |
|
54 |
} |
|
55 |
||
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
56 |
static void test(boolean genEndPos) throws Exception { |
10 | 57 |
Context context = new Context(); |
58 |
||
59 |
Options options = Options.instance(context); |
|
60 |
options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); |
|
61 |
||
62 |
Log log = Log.instance(context); |
|
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
63 |
Factory diagnosticFactory = JCDiagnostic.Factory.instance(context); |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
64 |
Field defaultErrorFlagsField = |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
65 |
JCDiagnostic.Factory.class.getDeclaredField("defaultErrorFlags"); |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
66 |
|
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
67 |
defaultErrorFlagsField.setAccessible(true); |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
68 |
|
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
69 |
Set<DiagnosticFlag> defaultErrorFlags = |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
70 |
(Set<DiagnosticFlag>) defaultErrorFlagsField.get(diagnosticFactory); |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
71 |
|
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
72 |
defaultErrorFlags.add(DiagnosticFlag.MULTIPLE); |
10 | 73 |
|
74 |
JavacFileManager.preRegister(context); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
75 |
ParserFactory pfac = ParserFactory.instance(context); |
10 | 76 |
|
77 |
final String text = |
|
78 |
"public class Foo {\n" |
|
79 |
+ " public static void main(String[] args) {\n" |
|
80 |
+ " if (args.length == 0)\n" |
|
81 |
+ " System.out.println(\"no args\");\n" |
|
82 |
+ " else\n" |
|
83 |
+ " System.out.println(args.length + \" args\");\n" |
|
84 |
+ " }\n" |
|
85 |
+ "}\n"; |
|
86 |
JavaFileObject fo = new StringJavaFileObject("Foo", text); |
|
87 |
log.useSource(fo); |
|
88 |
||
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
89 |
CharSequence cs = fo.getCharContent(true); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
90 |
Parser parser = pfac.newParser(cs, false, genEndPos, false); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
91 |
JCTree.JCCompilationUnit tree = parser.parseCompilationUnit(); |
10 | 92 |
log.setEndPosTable(fo, tree.endPositions); |
93 |
||
94 |
TreeScanner ts = new LogTester(log, tree.endPositions); |
|
95 |
ts.scan(tree); |
|
96 |
||
97 |
check(log.nerrors, 4, "errors"); |
|
98 |
check(log.nwarnings, 4, "warnings"); |
|
99 |
} |
|
100 |
||
101 |
private static void check(int found, int expected, String name) { |
|
102 |
if (found == expected) |
|
103 |
System.err.println(found + " " + name + " found, as expected."); |
|
104 |
else { |
|
105 |
System.err.println("incorrect number of " + name + " found."); |
|
106 |
System.err.println("expected: " + expected); |
|
107 |
System.err.println(" found: " + found); |
|
108 |
throw new IllegalStateException("test failed"); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
private static class LogTester extends TreeScanner { |
|
11055 | 113 |
LogTester(Log log, EndPosTable endPosTable) { |
10 | 114 |
this.log = log; |
11055 | 115 |
this.endPosTable = endPosTable; |
10 | 116 |
} |
117 |
||
118 |
public void visitIf(JCTree.JCIf tree) { |
|
119 |
JCDiagnostic.DiagnosticPosition nil = null; |
|
120 |
// generate dummy messages to exercise the log API |
|
121 |
log.error("not.stmt"); |
|
122 |
log.error(tree.pos, "not.stmt"); |
|
123 |
log.error(tree.pos(), "not.stmt"); |
|
124 |
log.error(nil, "not.stmt"); |
|
125 |
||
126 |
log.warning("div.zero"); |
|
127 |
log.warning(tree.pos, "div.zero"); |
|
128 |
log.warning(tree.pos(), "div.zero"); |
|
129 |
log.warning(nil, "div.zero"); |
|
130 |
} |
|
131 |
||
132 |
private Log log; |
|
11055 | 133 |
private EndPosTable endPosTable; |
10 | 134 |
} |
135 |
||
136 |
private static class StringJavaFileObject extends SimpleJavaFileObject { |
|
137 |
StringJavaFileObject(String name, String text) { |
|
138 |
super(URI.create(name), JavaFileObject.Kind.SOURCE); |
|
139 |
this.text = text; |
|
140 |
} |
|
141 |
public CharSequence getCharContent(boolean b) { |
|
142 |
return text; |
|
143 |
} |
|
144 |
public InputStream openInputStream() { |
|
145 |
throw new UnsupportedOperationException(); |
|
146 |
} |
|
147 |
public OutputStream openOutputStream() { |
|
148 |
throw new UnsupportedOperationException(); |
|
149 |
} |
|
150 |
private String text; |
|
151 |
} |
|
152 |
} |