Path: blob/master/test/hotspot/jtreg/runtime/8007320/ConstMethodTest.java
40942 views
/*1* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8007320 8014709 816323126* @summary Test all optional fields in ConstMethod27* @compile -g -parameters ConstMethodTest.java28* @run main ConstMethodTest29*/3031import java.util.*;32import java.lang.annotation.*;33import java.lang.reflect.*;34import java.io.Serializable;3536@Retention(RetentionPolicy.RUNTIME)37@interface MyAnnotation {38public String name();39public String value();40public String date() default "today";41}4243@Target(ElementType.TYPE_USE)44@Retention(RetentionPolicy.RUNTIME)45@interface TypeAnno {46String value();47}4849@Target(ElementType.TYPE_USE)50@Retention(RetentionPolicy.RUNTIME)51@interface TypeAnno2 {52String value();53}5455@Retention(RetentionPolicy.RUNTIME)56@Target(ElementType.PARAMETER)57@interface Named {58String value();59}6061@Retention(RetentionPolicy.RUNTIME)62@interface ScalarTypesWithDefault {63byte b() default 11;64short s() default 12;65int i() default 13;66long l() default 14;67char c() default 'V';68}6970// Some exception class71class OkException extends RuntimeException {};727374@MyAnnotation(name="someName", value = "Hello World")75public class ConstMethodTest {76public @TypeAnno("constructor") ConstMethodTest() { }7778public ConstMethodTest(int i) {79// needs a second unannotated constructor80}8182private static void check(boolean b) {83if (!b)84throw new RuntimeException();85}86private static void fail(String msg) {87System.err.println(msg);88throw new RuntimeException();89}90private static void equal(Object x, Object y) {91if (x == null ? y == null : x.equals(y)) {92} else {93fail(x + " not equal to " + y);94}95}96private static final String[] parameter_names = {97"parameter", "parameter2", "x"98};99100// Declare a function with everything in it.101@MyAnnotation(name="someName", value="Hello World")102static <T> void kitchenSinkFunc(@Named(value="aName") String parameter,103@Named("bName") String parameter2,104@ScalarTypesWithDefault T x)105throws @TypeAnno("RE") @TypeAnno2("RE2") RuntimeException,106NullPointerException,107@TypeAnno("AIOOBE") ArrayIndexOutOfBoundsException {108int i, j, k;109try {110System.out.println("calling kitchenSinkFunc " + parameter);111throw new OkException(); // to see stack trace with line numbers112} catch (Exception e) {113e.printStackTrace();114}115}116117private static void test1() throws Throwable {118for (Method m : ConstMethodTest.class.getDeclaredMethods()) {119if (m.getName().equals("kitchenSinkFunc")) {120Annotation[][] ann = m.getParameterAnnotations();121equal(ann.length, 3);122Annotation foo = ann[0][0];123Annotation bar = ann[1][0];124equal(foo.toString(), "@Named(\"aName\")");125equal(bar.toString(), "@Named(\"bName\")");126check(foo.equals(foo));127check(bar.equals(bar));128check(! foo.equals(bar));129// method annotations130Annotation[] ann2 = m.getAnnotations();131equal(ann2.length, 1);132Annotation mann = ann2[0];133equal(mann.toString(), "@MyAnnotation(date=\"today\", name=\"someName\", value=\"Hello World\")");134// Test Method parameter names135Parameter[] parameters = m.getParameters();136if(parameters == null)137throw new Exception("getParameters should never be null");138for(int i = 0; i < parameters.length; i++) {139Parameter p = parameters[i];140equal(parameters[i].getName(), parameter_names[i]);141}142}143}144}145146private static void testConstructor() throws Exception {147for (Constructor c : ConstMethodTest.class.getDeclaredConstructors()) {148Annotation[] aa = c.getAnnotatedReturnType().getAnnotations();149if (c.getParameterTypes().length == 1) { // should be un-annotated150check(aa.length == 0);151} else if (c.getParameterTypes().length == 0) { //should be annotated152check(aa.length == 1);153check(((TypeAnno)aa[0]).value().equals("constructor"));154} else {155//should not happen156check(false);157}158}159}160161public static void main(java.lang.String[] unused) throws Throwable {162// pass 5 so kitchenSinkFunc is instantiated with an int163kitchenSinkFunc("parameter", "param2", 5);164test1();165testConstructor();166}167};168169170171