author | akulyakh |
Thu, 21 May 2015 11:41:04 -0700 | |
changeset 30730 | d3ce7619db2c |
parent 28455 | 41245007c074 |
child 42407 | f3702cff2933 |
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 |
|
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
28455
diff
changeset
|
28 |
* @modules jdk.compiler/com.sun.tools.javac.file |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
28455
diff
changeset
|
29 |
* jdk.compiler/com.sun.tools.javac.parser |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
28455
diff
changeset
|
30 |
* jdk.compiler/com.sun.tools.javac.tree |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
28455
diff
changeset
|
31 |
* jdk.compiler/com.sun.tools.javac.util |
10 | 32 |
*/ |
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
33 |
import java.lang.reflect.Field; |
10 | 34 |
import java.io.InputStream; |
35 |
import java.io.OutputStream; |
|
36 |
import java.net.URI; |
|
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
37 |
import java.util.Set; |
10 | 38 |
import javax.tools.JavaFileObject; |
39 |
import javax.tools.SimpleJavaFileObject; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
10
diff
changeset
|
40 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 41 |
import com.sun.tools.javac.parser.Parser; |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
42 |
import com.sun.tools.javac.parser.ParserFactory; |
13077 | 43 |
import com.sun.tools.javac.tree.EndPosTable; |
10 | 44 |
import com.sun.tools.javac.tree.JCTree; |
45 |
import com.sun.tools.javac.tree.TreeScanner; |
|
46 |
import com.sun.tools.javac.util.Context; |
|
47 |
import com.sun.tools.javac.util.Log; |
|
48 |
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
|
49 |
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
|
50 |
import com.sun.tools.javac.util.JCDiagnostic.Factory; |
10 | 51 |
import com.sun.tools.javac.util.Options; |
52 |
||
53 |
public class TestLog |
|
54 |
{ |
|
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
55 |
public static void main(String... args) throws Exception { |
10 | 56 |
test(false); |
57 |
test(true); |
|
58 |
} |
|
59 |
||
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
60 |
static void test(boolean genEndPos) throws Exception { |
10 | 61 |
Context context = new Context(); |
62 |
||
63 |
Options options = Options.instance(context); |
|
64 |
options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); |
|
65 |
||
66 |
Log log = Log.instance(context); |
|
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
67 |
Factory diagnosticFactory = JCDiagnostic.Factory.instance(context); |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
68 |
Field defaultErrorFlagsField = |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
69 |
JCDiagnostic.Factory.class.getDeclaredField("defaultErrorFlags"); |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
70 |
|
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
71 |
defaultErrorFlagsField.setAccessible(true); |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
72 |
|
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
73 |
Set<DiagnosticFlag> defaultErrorFlags = |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
74 |
(Set<DiagnosticFlag>) defaultErrorFlagsField.get(diagnosticFactory); |
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
75 |
|
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
13077
diff
changeset
|
76 |
defaultErrorFlags.add(DiagnosticFlag.MULTIPLE); |
10 | 77 |
|
78 |
JavacFileManager.preRegister(context); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
79 |
ParserFactory pfac = ParserFactory.instance(context); |
10 | 80 |
|
81 |
final String text = |
|
82 |
"public class Foo {\n" |
|
83 |
+ " public static void main(String[] args) {\n" |
|
84 |
+ " if (args.length == 0)\n" |
|
85 |
+ " System.out.println(\"no args\");\n" |
|
86 |
+ " else\n" |
|
87 |
+ " System.out.println(args.length + \" args\");\n" |
|
88 |
+ " }\n" |
|
89 |
+ "}\n"; |
|
90 |
JavaFileObject fo = new StringJavaFileObject("Foo", text); |
|
91 |
log.useSource(fo); |
|
92 |
||
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
93 |
CharSequence cs = fo.getCharContent(true); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
94 |
Parser parser = pfac.newParser(cs, false, genEndPos, false); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
735
diff
changeset
|
95 |
JCTree.JCCompilationUnit tree = parser.parseCompilationUnit(); |
10 | 96 |
log.setEndPosTable(fo, tree.endPositions); |
97 |
||
98 |
TreeScanner ts = new LogTester(log, tree.endPositions); |
|
99 |
ts.scan(tree); |
|
100 |
||
101 |
check(log.nerrors, 4, "errors"); |
|
102 |
check(log.nwarnings, 4, "warnings"); |
|
103 |
} |
|
104 |
||
105 |
private static void check(int found, int expected, String name) { |
|
106 |
if (found == expected) |
|
107 |
System.err.println(found + " " + name + " found, as expected."); |
|
108 |
else { |
|
109 |
System.err.println("incorrect number of " + name + " found."); |
|
110 |
System.err.println("expected: " + expected); |
|
111 |
System.err.println(" found: " + found); |
|
112 |
throw new IllegalStateException("test failed"); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
private static class LogTester extends TreeScanner { |
|
11055 | 117 |
LogTester(Log log, EndPosTable endPosTable) { |
10 | 118 |
this.log = log; |
11055 | 119 |
this.endPosTable = endPosTable; |
10 | 120 |
} |
121 |
||
122 |
public void visitIf(JCTree.JCIf tree) { |
|
123 |
JCDiagnostic.DiagnosticPosition nil = null; |
|
124 |
// generate dummy messages to exercise the log API |
|
125 |
log.error("not.stmt"); |
|
126 |
log.error(tree.pos, "not.stmt"); |
|
127 |
log.error(tree.pos(), "not.stmt"); |
|
128 |
log.error(nil, "not.stmt"); |
|
129 |
||
130 |
log.warning("div.zero"); |
|
131 |
log.warning(tree.pos, "div.zero"); |
|
132 |
log.warning(tree.pos(), "div.zero"); |
|
133 |
log.warning(nil, "div.zero"); |
|
134 |
} |
|
135 |
||
136 |
private Log log; |
|
11055 | 137 |
private EndPosTable endPosTable; |
10 | 138 |
} |
139 |
||
140 |
private static class StringJavaFileObject extends SimpleJavaFileObject { |
|
141 |
StringJavaFileObject(String name, String text) { |
|
142 |
super(URI.create(name), JavaFileObject.Kind.SOURCE); |
|
143 |
this.text = text; |
|
144 |
} |
|
145 |
public CharSequence getCharContent(boolean b) { |
|
146 |
return text; |
|
147 |
} |
|
148 |
public InputStream openInputStream() { |
|
149 |
throw new UnsupportedOperationException(); |
|
150 |
} |
|
151 |
public OutputStream openOutputStream() { |
|
152 |
throw new UnsupportedOperationException(); |
|
153 |
} |
|
154 |
private String text; |
|
155 |
} |
|
156 |
} |