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