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