test/langtools/tools/javac/6304921/TestLog.java
author jlahoda
Fri, 15 Feb 2019 12:09:53 +0100
changeset 53773 454d54d8af1c
parent 48721 ef3557eb4306
permissions -rw-r--r--
8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round Summary: When an annotation processor reports and error, defer reporting recoverable errors from the erroneous round to the last round, to avoid reporting errors that were resolved in the erroneous round. Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     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
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1258
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1258
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1258
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 6304912
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.io.OutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 11055
diff changeset
    44
import com.sun.tools.javac.tree.EndPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.tree.TreeScanner;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.util.Log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
public class TestLog
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        test(false);
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        test(true);
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        Context context = new Context();
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        final String text =
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
              "public class Foo {\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            + "  public static void main(String[] args) {\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            + "    if (args.length == 0)\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            + "      System.out.println(\"no args\");\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            + "    else\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            + "      System.out.println(args.length + \" args\");\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            + "  }\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            + "}\n";
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        JavaFileObject fo = new StringJavaFileObject("Foo", text);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        log.useSource(fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        log.setEndPosTable(fo, tree.endPositions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        TreeScanner ts = new LogTester(log, tree.endPositions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        ts.scan(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        check(log.nerrors, 4, "errors");
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        check(log.nwarnings, 4, "warnings");
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    private static void check(int found, int expected, String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        if (found == expected)
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            System.err.println(found + " " + name + " found, as expected.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            System.err.println("incorrect number of " + name + " found.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            System.err.println("expected: " + expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            System.err.println("   found: " + found);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            throw new IllegalStateException("test failed");
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    private static class LogTester extends TreeScanner {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 6716
diff changeset
   120
        LogTester(Log log, EndPosTable endPosTable) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            this.log = log;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 6716
diff changeset
   122
            this.endPosTable = endPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        public void visitIf(JCTree.JCIf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            JCDiagnostic.DiagnosticPosition nil = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   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
06bc494ca11e Initial load
duke
parents:
diff changeset
   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
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        private Log log;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 6716
diff changeset
   140
        private EndPosTable endPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    private static class StringJavaFileObject extends SimpleJavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        StringJavaFileObject(String name, String text) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            super(URI.create(name), JavaFileObject.Kind.SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            this.text = text;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        public CharSequence getCharContent(boolean b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            return text;
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        public InputStream openInputStream() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        public OutputStream openOutputStream() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        private String text;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
}