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 | /* |
duke@1 | 2 | * Copyright 2005-2006 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 4813736 |
duke@1 | 27 | * @summary Additional functionality test of task and JSR 269 |
duke@1 | 28 | * @author Peter von der Ah\u00e9 |
duke@1 | 29 | * @ignore "misuse" of context breaks with 6358786 |
duke@1 | 30 | * @run main TestJavacTaskScanner TestJavacTaskScanner.java |
duke@1 | 31 | */ |
duke@1 | 32 | |
duke@1 | 33 | import com.sun.tools.javac.api.JavacTaskImpl; |
duke@1 | 34 | import com.sun.tools.javac.parser.*; // XXX |
duke@1 | 35 | import com.sun.tools.javac.util.*; // XXX |
duke@1 | 36 | import java.io.*; |
duke@1 | 37 | import java.nio.*; |
duke@1 | 38 | import javax.lang.model.element.Element; |
duke@1 | 39 | import javax.lang.model.element.TypeElement; |
duke@1 | 40 | import javax.lang.model.type.DeclaredType; |
duke@1 | 41 | import javax.lang.model.type.TypeMirror; |
duke@1 | 42 | import javax.lang.model.util.Elements; |
duke@1 | 43 | import javax.lang.model.util.Types; |
duke@1 | 44 | import javax.tools.*; |
duke@1 | 45 | import javax.tools.JavaFileManager; |
duke@1 | 46 | |
duke@1 | 47 | public class TestJavacTaskScanner implements Runnable { |
duke@1 | 48 | |
duke@1 | 49 | final JavacTaskImpl task; |
duke@1 | 50 | final Elements elements; |
duke@1 | 51 | final Types types; |
duke@1 | 52 | |
duke@1 | 53 | TestJavacTaskScanner(JavacTaskImpl task) { |
duke@1 | 54 | this.task = task; |
duke@1 | 55 | elements = task.getElements(); |
duke@1 | 56 | types = task.getTypes(); |
duke@1 | 57 | } |
duke@1 | 58 | |
duke@1 | 59 | public void run() { |
duke@1 | 60 | Iterable<? extends TypeElement> toplevels; |
duke@1 | 61 | try { |
duke@1 | 62 | toplevels = task.enter(task.parse()); |
duke@1 | 63 | } catch (IOException ex) { |
duke@1 | 64 | throw new AssertionError(ex); |
duke@1 | 65 | } |
duke@1 | 66 | for (TypeElement clazz : toplevels) { |
duke@1 | 67 | System.out.format("Testing %s:%n%n", clazz.getSimpleName()); |
duke@1 | 68 | testParseType(clazz); |
duke@1 | 69 | testGetAllMembers(clazz); |
duke@1 | 70 | System.out.println(); |
duke@1 | 71 | System.out.println(); |
duke@1 | 72 | System.out.println(); |
duke@1 | 73 | } |
duke@1 | 74 | } |
duke@1 | 75 | |
duke@1 | 76 | void testParseType(TypeElement clazz) { |
duke@1 | 77 | DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz); |
duke@1 | 78 | for (Element member : elements.getAllMembers((TypeElement)type.asElement())) { |
duke@1 | 79 | TypeMirror mt = types.asMemberOf(type, member); |
duke@1 | 80 | System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt); |
duke@1 | 81 | } |
duke@1 | 82 | } |
duke@1 | 83 | |
duke@1 | 84 | public static void main(String... args) throws IOException { |
duke@1 | 85 | JavaCompilerTool tool = ToolProvider.defaultJavaCompiler(); |
duke@1 | 86 | JavaFileManager fm = tool.getStandardFileManager(); |
duke@1 | 87 | String srcdir = System.getProperty("test.src"); |
duke@1 | 88 | File file = new File(srcdir, args[0]); |
duke@1 | 89 | JavacTaskImpl task = (JavacTaskImpl)tool.run(null, fm.getFileForInput(file.toString())); |
duke@1 | 90 | MyScanner.Factory.preRegister(task.getContext()); |
duke@1 | 91 | TestJavacTaskScanner tester = new TestJavacTaskScanner(task); |
duke@1 | 92 | tester.run(); |
duke@1 | 93 | } |
duke@1 | 94 | |
duke@1 | 95 | private void testGetAllMembers(TypeElement clazz) { |
duke@1 | 96 | for (Element member : elements.getAllMembers(clazz)) { |
duke@1 | 97 | System.out.format("%s : %s", member.getSimpleName(), member.asType()); |
duke@1 | 98 | } |
duke@1 | 99 | } |
duke@1 | 100 | } |
duke@1 | 101 | |
duke@1 | 102 | class MyScanner extends Scanner { |
duke@1 | 103 | |
duke@1 | 104 | public static class Factory extends Scanner.Factory { |
duke@1 | 105 | public static void preRegister(final Context context) { |
duke@1 | 106 | context.put(scannerFactoryKey, new Context.Factory<Scanner.Factory>() { |
duke@1 | 107 | public Factory make() { |
duke@1 | 108 | return new Factory(context); |
duke@1 | 109 | } |
duke@1 | 110 | }); |
duke@1 | 111 | } |
duke@1 | 112 | public Factory(Context context) { |
duke@1 | 113 | super(context); |
duke@1 | 114 | } |
duke@1 | 115 | |
duke@1 | 116 | @Override |
duke@1 | 117 | public Scanner newScanner(CharSequence input) { |
duke@1 | 118 | if (input instanceof CharBuffer) { |
duke@1 | 119 | return new MyScanner(this, (CharBuffer)input); |
duke@1 | 120 | } else { |
duke@1 | 121 | char[] array = input.toString().toCharArray(); |
duke@1 | 122 | return newScanner(array, array.length); |
duke@1 | 123 | } |
duke@1 | 124 | } |
duke@1 | 125 | |
duke@1 | 126 | @Override |
duke@1 | 127 | public Scanner newScanner(char[] input, int inputLength) { |
duke@1 | 128 | return new MyScanner(this, input, inputLength); |
duke@1 | 129 | } |
duke@1 | 130 | } |
duke@1 | 131 | protected MyScanner(Factory fac, CharBuffer buffer) { |
duke@1 | 132 | super(fac, buffer); |
duke@1 | 133 | } |
duke@1 | 134 | protected MyScanner(Factory fac, char[] input, int inputLength) { |
duke@1 | 135 | super(fac, input, inputLength); |
duke@1 | 136 | } |
duke@1 | 137 | |
duke@1 | 138 | public void nextToken() { |
duke@1 | 139 | super.nextToken(); |
duke@1 | 140 | System.err.format("Saw token %s (%s)%n", token(), name()); |
duke@1 | 141 | } |
duke@1 | 142 | } |