test/tools/javac/processing/model/type/IntersectionPropertiesTest.java

changeset 2050
09301757bb32
child 2200
7c89d200781b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/model/type/IntersectionPropertiesTest.java	Mon Sep 23 15:37:59 2013 -0400
     1.3 @@ -0,0 +1,135 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6499673
    1.30 + * @library /tools/javac/lib
    1.31 + * @build JavacTestingAbstractProcessor IntersectionPropertiesTest
    1.32 + * @run main IntersectionPropertiesTest
    1.33 + * @summary Assertion check for TypeVariable.getUpperBound() fails
    1.34 + */
    1.35 +
    1.36 +import com.sun.source.util.*;
    1.37 +import com.sun.tools.javac.api.*;
    1.38 +import com.sun.tools.javac.file.*;
    1.39 +import javax.annotation.processing.*;
    1.40 +import javax.lang.model.SourceVersion;
    1.41 +import javax.lang.model.type.*;
    1.42 +import javax.lang.model.util.ElementFilter;
    1.43 +import javax.lang.model.element.*;
    1.44 +import javax.tools.*;
    1.45 +import java.util.*;
    1.46 +import java.io.*;
    1.47 +
    1.48 +public class IntersectionPropertiesTest {
    1.49 +
    1.50 +    private int errors = 0;
    1.51 +    private static final String Intersection_name = "IntersectionTest.java";
    1.52 +    private static final String Intersection_contents =
    1.53 +        "import java.util.List;\n" +
    1.54 +        "import java.io.Serializable;\t" +
    1.55 +        "public class IntersectionTest<S extends List & Serializable> {\n" +
    1.56 +        "  void method(S s) { }\n" +
    1.57 +        "}";
    1.58 +
    1.59 +    private static final File classesdir = new File("intersectionproperties");
    1.60 +    private static final JavaCompiler comp =
    1.61 +        ToolProvider.getSystemJavaCompiler();
    1.62 +    private static final StandardJavaFileManager fm =
    1.63 +        comp.getStandardFileManager(null, null, null);
    1.64 +
    1.65 +    public void runOne(final String Test_name, final String Test_contents)
    1.66 +        throws IOException {
    1.67 +        System.err.println("Testing " + Test_name);
    1.68 +        final Iterable<? extends JavaFileObject> files =
    1.69 +            fm.getJavaFileObjectsFromFiles(Collections.singleton(writeFile(classesdir, Test_name, Test_contents)));
    1.70 +        final JavacTask ct =
    1.71 +            (JavacTask)comp.getTask(null, fm, null, null, null, files);
    1.72 +        ct.setProcessors(Collections.singleton(new TestProcessor()));
    1.73 +
    1.74 +        if (!ct.call()) {
    1.75 +            System.err.println("Compilation unexpectedly failed");
    1.76 +            errors++;
    1.77 +        }
    1.78 +    }
    1.79 +
    1.80 +    public void run() throws IOException {
    1.81 +        runOne(Intersection_name, Intersection_contents);
    1.82 +
    1.83 +        if (0 != errors)
    1.84 +            throw new RuntimeException(errors + " errors occurred");
    1.85 +    }
    1.86 +
    1.87 +    public static void main(String... args) throws IOException {
    1.88 +        new IntersectionPropertiesTest().run();
    1.89 +    }
    1.90 +
    1.91 +    private static File writeFile(File dir, String path, String body)
    1.92 +        throws IOException {
    1.93 +        File f = new File(dir, path);
    1.94 +        f.getParentFile().mkdirs();
    1.95 +        try (FileWriter out = new FileWriter(f)) {
    1.96 +            out.write(body);
    1.97 +        }
    1.98 +        return f;
    1.99 +    }
   1.100 +
   1.101 +    private class TestProcessor extends JavacTestingAbstractProcessor {
   1.102 +
   1.103 +        private Set<? extends Element> rootElements;
   1.104 +
   1.105 +        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   1.106 +            rootElements = roundEnv.getRootElements();
   1.107 +            if (!rootElements.isEmpty()) {
   1.108 +                performCheck();
   1.109 +            }
   1.110 +            return true;
   1.111 +        }
   1.112 +
   1.113 +        private void performCheck() {
   1.114 +            TypeElement typeElement = (TypeElement) rootElements.iterator().next();
   1.115 +            ExecutableElement method = ElementFilter.methodsIn(typeElement.getEnclosedElements()).get(0);
   1.116 +            TypeVariable typeVariable = (TypeVariable) method.getParameters().get(0).asType();
   1.117 +
   1.118 +            final TypeMirror upperBound = typeVariable.getUpperBound();
   1.119 +
   1.120 +            TypeParameterElement typeParameterElement = ((TypeParameterElement) typeVariable.asElement());
   1.121 +            final List<? extends TypeMirror> bounds = typeParameterElement.getBounds();
   1.122 +            final HashSet<TypeMirror> actual = new HashSet<TypeMirror>(processingEnv.getTypeUtils().directSupertypes(upperBound));
   1.123 +            final HashSet<TypeMirror> expected = new HashSet<TypeMirror>(bounds);
   1.124 +            if (!expected.equals(actual)) {
   1.125 +                System.err.println("Mismatched expected and actual bounds.");
   1.126 +                System.err.println("Expected:");
   1.127 +                for(TypeMirror tm : expected)
   1.128 +                    System.err.println("  " + tm);
   1.129 +                System.err.println("Actual:");
   1.130 +                for(TypeMirror tm : actual)
   1.131 +                    System.err.println("  " + tm);
   1.132 +                errors++;
   1.133 +            }
   1.134 +        }
   1.135 +
   1.136 +    }
   1.137 +
   1.138 +}

mercurial