IntersectionPropertiesTest.java revision 2199:7c89d200781b
1/* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 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 * 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. 22 */ 23 24/* 25 * @test 26 * @bug 6499673 27 * @library /tools/javac/lib 28 * @build JavacTestingAbstractProcessor IntersectionPropertiesTest 29 * @run main IntersectionPropertiesTest 30 * @summary Assertion check for TypeVariable.getUpperBound() fails 31 */ 32 33import com.sun.source.util.*; 34import com.sun.tools.javac.util.Assert; 35import javax.annotation.processing.*; 36import javax.lang.model.type.*; 37import javax.lang.model.util.ElementFilter; 38import javax.lang.model.element.*; 39import javax.tools.*; 40import java.util.*; 41import java.io.*; 42import javax.lang.model.util.Types; 43 44public class IntersectionPropertiesTest { 45 46 private int errors = 0; 47 private static final String Intersection_name = "IntersectionTest.java"; 48 private static final String Intersection_contents = 49 "import java.util.AbstractList;\n" + 50 "import java.util.List;\n" + 51 "import java.io.Serializable;\t" + 52 "public class IntersectionTest<S extends List & Serializable, One extends AbstractList & Runnable & Serializable, Two extends AbstractList & Serializable & Runnable> {\n" + 53 " void method(S s, One o, Two t) { }\n" + 54 " public static abstract class SubType extends AbstractList implements Runnable, Serializable { } \n" + 55 "}"; 56 57 private static final File classesdir = new File("intersectionproperties"); 58 private static final JavaCompiler comp = 59 ToolProvider.getSystemJavaCompiler(); 60 private static final StandardJavaFileManager fm = 61 comp.getStandardFileManager(null, null, null); 62 63 public void runOne(final String Test_name, final String Test_contents) 64 throws IOException { 65 System.err.println("Testing " + Test_name); 66 final Iterable<? extends JavaFileObject> files = 67 fm.getJavaFileObjectsFromFiles(Collections.singleton(writeFile(classesdir, Test_name, Test_contents))); 68 final JavacTask ct = 69 (JavacTask)comp.getTask(null, fm, null, null, null, files); 70 ct.setProcessors(Collections.singleton(new TestProcessor())); 71 72 if (!ct.call()) { 73 System.err.println("Compilation unexpectedly failed"); 74 errors++; 75 } 76 } 77 78 public void run() throws IOException { 79 runOne(Intersection_name, Intersection_contents); 80 81 if (0 != errors) 82 throw new RuntimeException(errors + " errors occurred"); 83 } 84 85 public static void main(String... args) throws IOException { 86 new IntersectionPropertiesTest().run(); 87 } 88 89 private static File writeFile(File dir, String path, String body) 90 throws IOException { 91 File f = new File(dir, path); 92 f.getParentFile().mkdirs(); 93 try (FileWriter out = new FileWriter(f)) { 94 out.write(body); 95 } 96 return f; 97 } 98 99 private class TestProcessor extends JavacTestingAbstractProcessor { 100 101 private Set<? extends Element> rootElements; 102 103 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 104 rootElements = roundEnv.getRootElements(); 105 if (!rootElements.isEmpty()) { 106 performCheck(); 107 } 108 return true; 109 } 110 111 private void performCheck() { 112 TypeElement typeElement = (TypeElement) rootElements.iterator().next(); 113 ExecutableElement method = ElementFilter.methodsIn(typeElement.getEnclosedElements()).get(0); 114 TypeVariable typeVariable = (TypeVariable) method.getParameters().get(0).asType(); 115 116 final TypeMirror upperBound = typeVariable.getUpperBound(); 117 118 TypeParameterElement typeParameterElement = ((TypeParameterElement) typeVariable.asElement()); 119 final List<? extends TypeMirror> bounds = typeParameterElement.getBounds(); 120 Types types = processingEnv.getTypeUtils(); 121 final HashSet<TypeMirror> actual = new HashSet<TypeMirror>(types.directSupertypes(upperBound)); 122 final HashSet<TypeMirror> expected = new HashSet<TypeMirror>(bounds); 123 if (!expected.equals(actual)) { 124 System.err.println("Mismatched expected and actual bounds."); 125 System.err.println("Expected:"); 126 for(TypeMirror tm : expected) 127 System.err.println(" " + tm); 128 System.err.println("Actual:"); 129 for(TypeMirror tm : actual) 130 System.err.println(" " + tm); 131 errors++; 132 } 133 134 TypeVariable oneTypeVariable = (TypeVariable) method.getParameters().get(1).asType(); 135 TypeMirror oneUpperBound = oneTypeVariable.getUpperBound(); 136 TypeVariable twoTypeVariable = (TypeVariable) method.getParameters().get(2).asType(); 137 TypeMirror twoUpperBound = twoTypeVariable.getUpperBound(); 138 TypeElement oneUpperBoundElement = (TypeElement) types.asElement(oneUpperBound); 139 140 Assert.checkNonNull(oneUpperBoundElement); 141 142 Assert.check("java.util.AbstractList".equals(oneUpperBoundElement.getSuperclass().toString()), 143 oneUpperBoundElement.getSuperclass().toString()); 144 145 List<String> superInterfaces = new java.util.ArrayList<>(); 146 147 for (TypeMirror tm : oneUpperBoundElement.getInterfaces()) { 148 superInterfaces.add(tm.toString()); 149 } 150 151 Assert.check(java.util.Arrays.asList("java.lang.Runnable", 152 "java.io.Serializable").equals(superInterfaces), 153 superInterfaces); 154 155 Assert.check(types.isSameType(upperBound, types.capture(upperBound))); 156 Assert.check(types.isSameType(types.erasure(typeVariable), types.erasure(upperBound))); 157 158 TypeElement subTypeClass = processingEnv.getElementUtils().getTypeElement("IntersectionTest.SubType"); 159 160 Assert.checkNonNull(subTypeClass); 161 162 Assert.check(types.isAssignable(subTypeClass.asType(), oneUpperBound)); 163 Assert.check(types.isSameType(oneUpperBound, twoUpperBound)); 164 Assert.check(!types.isSameType(upperBound, twoUpperBound)); 165 Assert.check(types.isSubtype(subTypeClass.asType(), oneUpperBound)); 166 Assert.check(types.isSubtype(oneUpperBound, upperBound)); 167 } 168 169 } 170 171} 172