test/runtime/8007320/ConstMethodTest.java

Mon, 06 Nov 2017 16:51:47 +0800

author
aoqi
date
Mon, 06 Nov 2017 16:51:47 +0800
changeset 7997
6cbff0651f1a
parent 6876
710a3c8b516e
permissions
-rw-r--r--

[Code Reorganization] remove trailing whitespace to pass jcheck test

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8007320 8014709
aoqi@0 27 * @summary Test all optional fields in ConstMethod
aoqi@0 28 * @compile -g -parameters ConstMethodTest.java
aoqi@0 29 * @run main ConstMethodTest
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.util.*;
aoqi@0 33 import java.lang.annotation.*;
aoqi@0 34 import java.lang.reflect.*;
aoqi@0 35 import java.io.Serializable;
aoqi@0 36
aoqi@0 37 @Retention(RetentionPolicy.RUNTIME)
aoqi@0 38 @interface MyAnnotation {
aoqi@0 39 public String name();
aoqi@0 40 public String value();
aoqi@0 41 public String date() default "today";
aoqi@0 42 }
aoqi@0 43
aoqi@0 44 @Target(ElementType.TYPE_USE)
aoqi@0 45 @Retention(RetentionPolicy.RUNTIME)
aoqi@0 46 @interface TypeAnno {
aoqi@0 47 String value();
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 @Target(ElementType.TYPE_USE)
aoqi@0 51 @Retention(RetentionPolicy.RUNTIME)
aoqi@0 52 @interface TypeAnno2 {
aoqi@0 53 String value();
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 @Retention(RetentionPolicy.RUNTIME)
aoqi@0 57 @Target(ElementType.PARAMETER)
aoqi@0 58 @interface Named {
aoqi@0 59 String value();
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 @Retention(RetentionPolicy.RUNTIME)
aoqi@0 63 @interface ScalarTypesWithDefault {
aoqi@0 64 byte b() default 11;
aoqi@0 65 short s() default 12;
aoqi@0 66 int i() default 13;
aoqi@0 67 long l() default 14;
aoqi@0 68 char c() default 'V';
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 // Some exception class
aoqi@0 72 class OkException extends RuntimeException {};
aoqi@0 73
aoqi@0 74
aoqi@0 75 @MyAnnotation(name="someName", value = "Hello World")
aoqi@0 76 public class ConstMethodTest {
aoqi@0 77 public @TypeAnno("constructor") ConstMethodTest() { }
aoqi@0 78
aoqi@0 79 public ConstMethodTest(int i) {
aoqi@0 80 // needs a second unannotated constructor
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 private static void check(boolean b) {
aoqi@0 84 if (!b)
aoqi@0 85 throw new RuntimeException();
aoqi@0 86 }
aoqi@0 87 private static void fail(String msg) {
aoqi@0 88 System.err.println(msg);
aoqi@0 89 throw new RuntimeException();
aoqi@0 90 }
aoqi@0 91 private static void equal(Object x, Object y) {
aoqi@0 92 if (x == null ? y == null : x.equals(y)) {
aoqi@0 93 } else {
aoqi@0 94 fail(x + " not equal to " + y);
aoqi@0 95 }
aoqi@0 96 }
aoqi@0 97 private static final String[] parameter_names = {
aoqi@0 98 "parameter", "parameter2", "x"
aoqi@0 99 };
aoqi@0 100
aoqi@0 101 // Declare a function with everything in it.
aoqi@0 102 @MyAnnotation(name="someName", value="Hello World")
aoqi@0 103 static <T> void kitchenSinkFunc(@Named(value="aName") String parameter,
aoqi@0 104 @Named("bName") String parameter2,
aoqi@0 105 @ScalarTypesWithDefault T x)
aoqi@0 106 throws @TypeAnno("RE") @TypeAnno2("RE2") RuntimeException,
aoqi@0 107 NullPointerException,
aoqi@0 108 @TypeAnno("AIOOBE") ArrayIndexOutOfBoundsException {
aoqi@0 109 int i, j, k;
aoqi@0 110 try {
aoqi@0 111 System.out.println("calling kitchenSinkFunc " + parameter);
aoqi@0 112 throw new OkException(); // to see stack trace with line numbers
aoqi@0 113 } catch (Exception e) {
aoqi@0 114 e.printStackTrace();
aoqi@0 115 }
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 private static void test1() throws Throwable {
aoqi@0 119 for (Method m : ConstMethodTest.class.getDeclaredMethods()) {
aoqi@0 120 if (m.getName().equals("kitchenSinkFunc")) {
aoqi@0 121 Annotation[][] ann = m.getParameterAnnotations();
aoqi@0 122 equal(ann.length, 3);
aoqi@0 123 Annotation foo = ann[0][0];
aoqi@0 124 Annotation bar = ann[1][0];
aoqi@0 125 equal(foo.toString(), "@Named(value=aName)");
aoqi@0 126 equal(bar.toString(), "@Named(value=bName)");
aoqi@0 127 check(foo.equals(foo));
aoqi@0 128 check(bar.equals(bar));
aoqi@0 129 check(! foo.equals(bar));
aoqi@0 130 // method annotations
aoqi@0 131 Annotation[] ann2 = m.getAnnotations();
aoqi@0 132 equal(ann2.length, 1);
aoqi@0 133 Annotation mann = ann2[0];
aoqi@0 134 equal(mann.toString(), "@MyAnnotation(date=today, name=someName, value=Hello World)");
aoqi@0 135 // Test Method parameter names
aoqi@0 136 Parameter[] parameters = m.getParameters();
aoqi@0 137 if(parameters == null)
aoqi@0 138 throw new Exception("getParameters should never be null");
aoqi@0 139 for(int i = 0; i < parameters.length; i++) {
aoqi@0 140 Parameter p = parameters[i];
aoqi@0 141 equal(parameters[i].getName(), parameter_names[i]);
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 private static void testConstructor() throws Exception {
aoqi@0 148 for (Constructor c : ConstMethodTest.class.getDeclaredConstructors()) {
aoqi@0 149 Annotation[] aa = c.getAnnotatedReturnType().getAnnotations();
aoqi@0 150 if (c.getParameterTypes().length == 1) { // should be un-annotated
aoqi@0 151 check(aa.length == 0);
aoqi@0 152 } else if (c.getParameterTypes().length == 0) { //should be annotated
aoqi@0 153 check(aa.length == 1);
aoqi@0 154 check(((TypeAnno)aa[0]).value().equals("constructor"));
aoqi@0 155 } else {
aoqi@0 156 //should not happen
aoqi@0 157 check(false);
aoqi@0 158 }
aoqi@0 159 }
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 public static void main(java.lang.String[] unused) throws Throwable {
aoqi@0 163 // pass 5 so kitchenSinkFunc is instantiated with an int
aoqi@0 164 kitchenSinkFunc("parameter", "param2", 5);
aoqi@0 165 test1();
aoqi@0 166 testConstructor();
aoqi@0 167 }
aoqi@0 168 };
aoqi@0 169

mercurial