langtools/test/tools/javac/6304921/TestLog.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 28455 41245007c074
child 42407 f3702cff2933
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     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
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
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 28455
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.util
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.io.InputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.io.OutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 11055
diff changeset
    43
import com.sun.tools.javac.tree.EndPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.tree.TreeScanner;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.util.Log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.util.Options;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
public class TestLog
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        test(false);
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        test(true);
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        Context context = new Context();
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        final String text =
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
              "public class Foo {\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            + "  public static void main(String[] args) {\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            + "    if (args.length == 0)\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            + "      System.out.println(\"no args\");\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            + "    else\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            + "      System.out.println(args.length + \" args\");\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            + "  }\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            + "}\n";
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        JavaFileObject fo = new StringJavaFileObject("Foo", text);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        log.useSource(fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
    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
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        log.setEndPosTable(fo, tree.endPositions);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        TreeScanner ts = new LogTester(log, tree.endPositions);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        ts.scan(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        check(log.nerrors, 4, "errors");
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        check(log.nwarnings, 4, "warnings");
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    private static void check(int found, int expected, String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        if (found == expected)
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            System.err.println(found + " " + name + " found, as expected.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            System.err.println("incorrect number of " + name + " found.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            System.err.println("expected: " + expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            System.err.println("   found: " + found);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            throw new IllegalStateException("test failed");
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    private static class LogTester extends TreeScanner {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 6716
diff changeset
   117
        LogTester(Log log, EndPosTable endPosTable) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            this.log = log;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 6716
diff changeset
   119
            this.endPosTable = endPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        public void visitIf(JCTree.JCIf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            JCDiagnostic.DiagnosticPosition nil = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            // generate dummy messages to exercise the log API
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            log.error("not.stmt");
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            log.error(tree.pos, "not.stmt");
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            log.error(tree.pos(), "not.stmt");
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            log.error(nil, "not.stmt");
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            log.warning("div.zero");
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            log.warning(tree.pos, "div.zero");
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            log.warning(tree.pos(), "div.zero");
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            log.warning(nil, "div.zero");
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        private Log log;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 6716
diff changeset
   137
        private EndPosTable endPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    private static class StringJavaFileObject extends SimpleJavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        StringJavaFileObject(String name, String text) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            super(URI.create(name), JavaFileObject.Kind.SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            this.text = text;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        public CharSequence getCharContent(boolean b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            return text;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        public InputStream openInputStream() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        public OutputStream openOutputStream() {
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
        private String text;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
}