test/tools/javac/processing/model/util/TypesBadArg.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/model/util/TypesBadArg.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,107 @@
     1.4 +/*
     1.5 + * Copyright (c) 2006, 2010, 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     6345812
    1.30 + * @summary Validate argument kinds in Types utilities
    1.31 + * @author  Scott Seligman
    1.32 + * @library /tools/javac/lib
    1.33 + * @build   JavacTestingAbstractProcessor TypesBadArg
    1.34 + * @compile -processor TypesBadArg -proc:only TypesBadArg.java
    1.35 + */
    1.36 +
    1.37 +import java.util.Set;
    1.38 +import javax.annotation.processing.*;
    1.39 +import javax.lang.model.element.*;
    1.40 +import javax.lang.model.type.*;
    1.41 +import javax.lang.model.util.*;
    1.42 +
    1.43 +public class TypesBadArg extends JavacTestingAbstractProcessor {
    1.44 +    boolean success = true;
    1.45 +
    1.46 +    public boolean process(Set<? extends TypeElement> tes,
    1.47 +                           RoundEnvironment round) {
    1.48 +        if (round.processingOver()) return true;
    1.49 +
    1.50 +        final Elements elements = processingEnv.getElementUtils();
    1.51 +        final Types types = processingEnv.getTypeUtils();
    1.52 +
    1.53 +        final TypeMirror javaLang =
    1.54 +            elements.getPackageElement("java.lang").asType();
    1.55 +
    1.56 +        makeBadCall(new Runnable() {
    1.57 +            public void run() {
    1.58 +                types.isSubtype(javaLang, javaLang);
    1.59 +            }
    1.60 +        });
    1.61 +        makeBadCall(new Runnable() {
    1.62 +            public void run() {
    1.63 +                types.isAssignable(javaLang, javaLang);
    1.64 +            }
    1.65 +        });
    1.66 +        makeBadCall(new Runnable() {
    1.67 +            public void run() {
    1.68 +                types.contains(javaLang, javaLang);
    1.69 +            }
    1.70 +        });
    1.71 +        makeBadCall(new Runnable() {
    1.72 +            public void run() {
    1.73 +                types.directSupertypes(javaLang);
    1.74 +            }
    1.75 +        });
    1.76 +        makeBadCall(new Runnable() {
    1.77 +            public void run() {
    1.78 +                types.erasure(javaLang);
    1.79 +            }
    1.80 +        });
    1.81 +        makeBadCall(new Runnable() {
    1.82 +            public void run() {
    1.83 +                types.capture(javaLang);
    1.84 +            }
    1.85 +        });
    1.86 +        makeBadCall(new Runnable() {
    1.87 +            public void run() {
    1.88 +                types.unboxedType(javaLang);
    1.89 +            }
    1.90 +        });
    1.91 +        makeBadCall(new Runnable() {
    1.92 +            public void run() {
    1.93 +                types.unboxedType(types.getNoType(TypeKind.VOID));
    1.94 +            }
    1.95 +        });
    1.96 +        if (! success)
    1.97 +            throw new AssertionError("Some test(s) failed.");
    1.98 +        return true;
    1.99 +    }
   1.100 +
   1.101 +    private void makeBadCall(Runnable runnable) {
   1.102 +        try {
   1.103 +            runnable.run();
   1.104 +            System.out.println("Failure: IllegalArgumentException expected");
   1.105 +            success = false;
   1.106 +        } catch (IllegalArgumentException e) {
   1.107 +            System.out.println("IllegalArgumentException as expected");
   1.108 +        }
   1.109 +    }
   1.110 +}

mercurial