Thu, 23 Oct 2008 17:59:43 +0100
6711619: javac doesn't allow access to protected members in intersection types
Summary: Accordingly to new accessibility rules all members of intersection types (but private ones) should be accessible
Reviewed-by: jjg
duke@1 | 1 | /* |
xdono@54 | 2 | * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 18 | * |
duke@1 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 21 | * have any questions. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | /* |
duke@1 | 25 | * @test |
duke@1 | 26 | * @bug 6304912 |
duke@1 | 27 | * @summary unit test for Log |
duke@1 | 28 | */ |
duke@1 | 29 | import java.io.InputStream; |
duke@1 | 30 | import java.io.IOException; |
duke@1 | 31 | import java.io.OutputStream; |
duke@1 | 32 | import java.net.URI; |
duke@1 | 33 | import javax.tools.JavaFileObject; |
duke@1 | 34 | import javax.tools.SimpleJavaFileObject; |
jjg@50 | 35 | import com.sun.tools.javac.file.JavacFileManager; |
duke@1 | 36 | import com.sun.tools.javac.parser.Parser; |
jjg@111 | 37 | import com.sun.tools.javac.parser.ParserFactory; |
duke@1 | 38 | import com.sun.tools.javac.parser.Scanner; |
duke@1 | 39 | import com.sun.tools.javac.tree.JCTree; |
duke@1 | 40 | import com.sun.tools.javac.tree.TreeScanner; |
duke@1 | 41 | import com.sun.tools.javac.util.Context; |
duke@1 | 42 | import com.sun.tools.javac.util.Log; |
duke@1 | 43 | import com.sun.tools.javac.util.JCDiagnostic; |
duke@1 | 44 | import com.sun.tools.javac.util.Options; |
duke@1 | 45 | |
duke@1 | 46 | public class TestLog |
duke@1 | 47 | { |
duke@1 | 48 | public static void main(String... args) throws IOException { |
duke@1 | 49 | test(false); |
duke@1 | 50 | test(true); |
duke@1 | 51 | } |
duke@1 | 52 | |
duke@1 | 53 | static void test(boolean genEndPos) throws IOException { |
duke@1 | 54 | Context context = new Context(); |
duke@1 | 55 | |
duke@1 | 56 | Options options = Options.instance(context); |
duke@1 | 57 | options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); |
duke@1 | 58 | |
duke@1 | 59 | Log log = Log.instance(context); |
duke@1 | 60 | log.multipleErrors = true; |
duke@1 | 61 | |
duke@1 | 62 | JavacFileManager.preRegister(context); |
duke@1 | 63 | Scanner.Factory sfac = Scanner.Factory.instance(context); |
jjg@111 | 64 | ParserFactory pfac = ParserFactory.instance(context); |
duke@1 | 65 | |
duke@1 | 66 | final String text = |
duke@1 | 67 | "public class Foo {\n" |
duke@1 | 68 | + " public static void main(String[] args) {\n" |
duke@1 | 69 | + " if (args.length == 0)\n" |
duke@1 | 70 | + " System.out.println(\"no args\");\n" |
duke@1 | 71 | + " else\n" |
duke@1 | 72 | + " System.out.println(args.length + \" args\");\n" |
duke@1 | 73 | + " }\n" |
duke@1 | 74 | + "}\n"; |
duke@1 | 75 | JavaFileObject fo = new StringJavaFileObject("Foo", text); |
duke@1 | 76 | log.useSource(fo); |
duke@1 | 77 | |
jjg@111 | 78 | CharSequence cs = fo.getCharContent(true); |
jjg@111 | 79 | Parser parser = pfac.newParser(cs, false, genEndPos, false); |
jjg@111 | 80 | JCTree.JCCompilationUnit tree = parser.parseCompilationUnit(); |
duke@1 | 81 | log.setEndPosTable(fo, tree.endPositions); |
duke@1 | 82 | |
duke@1 | 83 | TreeScanner ts = new LogTester(log, tree.endPositions); |
duke@1 | 84 | ts.scan(tree); |
duke@1 | 85 | |
duke@1 | 86 | check(log.nerrors, 4, "errors"); |
duke@1 | 87 | check(log.nwarnings, 4, "warnings"); |
duke@1 | 88 | } |
duke@1 | 89 | |
duke@1 | 90 | private static void check(int found, int expected, String name) { |
duke@1 | 91 | if (found == expected) |
duke@1 | 92 | System.err.println(found + " " + name + " found, as expected."); |
duke@1 | 93 | else { |
duke@1 | 94 | System.err.println("incorrect number of " + name + " found."); |
duke@1 | 95 | System.err.println("expected: " + expected); |
duke@1 | 96 | System.err.println(" found: " + found); |
duke@1 | 97 | throw new IllegalStateException("test failed"); |
duke@1 | 98 | } |
duke@1 | 99 | } |
duke@1 | 100 | |
duke@1 | 101 | private static class LogTester extends TreeScanner { |
duke@1 | 102 | LogTester(Log log, java.util.Map<JCTree, Integer> endPositions) { |
duke@1 | 103 | this.log = log; |
duke@1 | 104 | this.endPositions = endPositions; |
duke@1 | 105 | } |
duke@1 | 106 | |
duke@1 | 107 | public void visitIf(JCTree.JCIf tree) { |
duke@1 | 108 | JCDiagnostic.DiagnosticPosition nil = null; |
duke@1 | 109 | // generate dummy messages to exercise the log API |
duke@1 | 110 | log.error("not.stmt"); |
duke@1 | 111 | log.error(tree.pos, "not.stmt"); |
duke@1 | 112 | log.error(tree.pos(), "not.stmt"); |
duke@1 | 113 | log.error(nil, "not.stmt"); |
duke@1 | 114 | |
duke@1 | 115 | log.warning("div.zero"); |
duke@1 | 116 | log.warning(tree.pos, "div.zero"); |
duke@1 | 117 | log.warning(tree.pos(), "div.zero"); |
duke@1 | 118 | log.warning(nil, "div.zero"); |
duke@1 | 119 | } |
duke@1 | 120 | |
duke@1 | 121 | private Log log; |
duke@1 | 122 | private java.util.Map<JCTree, Integer> endPositions; |
duke@1 | 123 | } |
duke@1 | 124 | |
duke@1 | 125 | private static class StringJavaFileObject extends SimpleJavaFileObject { |
duke@1 | 126 | StringJavaFileObject(String name, String text) { |
duke@1 | 127 | super(URI.create(name), JavaFileObject.Kind.SOURCE); |
duke@1 | 128 | this.text = text; |
duke@1 | 129 | } |
duke@1 | 130 | public CharSequence getCharContent(boolean b) { |
duke@1 | 131 | return text; |
duke@1 | 132 | } |
duke@1 | 133 | public InputStream openInputStream() { |
duke@1 | 134 | throw new UnsupportedOperationException(); |
duke@1 | 135 | } |
duke@1 | 136 | public OutputStream openOutputStream() { |
duke@1 | 137 | throw new UnsupportedOperationException(); |
duke@1 | 138 | } |
duke@1 | 139 | private String text; |
duke@1 | 140 | } |
duke@1 | 141 | } |