Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Method/GenericStringTest.java
38828 views
/*1* Copyright (c) 2004, 2013, 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 5033583 6316717 6470106 800497926* @summary Check toGenericString() and toString() methods27* @author Joseph D. Darcy28*/2930import java.lang.reflect.*;31import java.lang.annotation.*;32import java.util.*;3334public class GenericStringTest {35public static void main(String argv[]) throws Exception {36int failures = 0;37List<Class<?>> classList = new LinkedList<Class<?>>();38classList.add(TestClass1.class);39classList.add(TestClass2.class);40classList.add(Roebling.class);41classList.add(TestInterface1.class);424344for(Class<?> clazz: classList)45for(Method method: clazz.getDeclaredMethods()) {46ExpectedGenericString egs = method.getAnnotation(ExpectedGenericString.class);47if (egs != null) {48String actual = method.toGenericString();49System.out.println(actual);50if (method.isBridge()) {51if (! egs.bridgeValue().equals(actual)) {52failures++;53System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",54egs.value(), actual);55}56} else {57if (! egs.value().equals(actual)) {58failures++;59System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",60egs.value(), actual);61}62}63}6465if (method.isAnnotationPresent(ExpectedString.class)) {66ExpectedString es = method.getAnnotation(ExpectedString.class);67String actual = method.toString();68if (! es.value().equals(actual)) {69failures++;70System.err.printf("ERROR: Expected ''%s''; got ''%s''.\n",71es.value(), actual);72}73}7475}7677// Bridge Test; no "volatile" methods78for(Method method: Roebling.class.getDeclaredMethods()) {79String s1 = method.toGenericString();80String s2 = method.toString();81System.out.println("Generic: " + s1);82System.out.println("Regular: " + s2);83if (s1.indexOf("volatile") != -1 ||84s2.indexOf("volatile") != -1) {85failures++;86System.err.println("ERROR: Bad string; unexpected ``volatile''");87}88}8990if (failures > 0) {91System.err.println("Test failed.");92throw new RuntimeException();93}94}95}9697class TestClass1 {98@ExpectedGenericString(99"void TestClass1.method1(int,double)")100void method1(int x, double y) {}101102@ExpectedGenericString(103"private static java.lang.String TestClass1.method2(int,int)")104private static String method2(int x, int param2) {return null;}105106@ExpectedGenericString(107"static void TestClass1.method3() throws java.lang.RuntimeException")108static void method3() throws RuntimeException {return;}109110@ExpectedGenericString(111"protected <S,T> S TestClass1.method4(S,T) throws java.lang.Exception")112protected <S, T> S method4(S s, T t) throws Exception {return null;}113}114115class TestClass2<E, F extends Exception> {116@ExpectedGenericString(117"public <T> T TestClass2.method1(E,T)")118public <T> T method1(E e, T t) {return null;}119120@ExpectedGenericString(121"public void TestClass2.method2() throws F")122public void method2() throws F {return;}123}124125class Roebling implements Comparable<Roebling> {126@ExpectedGenericString(127value="public int Roebling.compareTo(Roebling)",128bridgeValue="public int Roebling.compareTo(java.lang.Object)")129public int compareTo(Roebling r) {130throw new IllegalArgumentException();131}132133// Not a transient method, (transient var-arg overlap)134@ExpectedGenericString(135"void Roebling.varArg(java.lang.Object...)")136@ExpectedString(137"void Roebling.varArg(java.lang.Object[])")138void varArg(Object ... arg) {}139}140141interface TestInterface1 {142@ExpectedGenericString(143"public default void TestInterface1.foo()")144@ExpectedString(145"public default void TestInterface1.foo()")146public default void foo(){;}147148@ExpectedString(149"public default java.lang.Object TestInterface1.bar()")150@ExpectedGenericString(151"public default <A> A TestInterface1.bar()")152default <A> A bar(){return null;}153154@ExpectedString(155"public default strictfp double TestInterface1.quux()")156@ExpectedGenericString(157"public default strictfp double TestInterface1.quux()")158strictfp default double quux(){return 1.0;}159160}161162@Retention(RetentionPolicy.RUNTIME)163@interface ExpectedGenericString {164String value();165String bridgeValue() default "";166}167168@Retention(RetentionPolicy.RUNTIME)169@interface ExpectedString {170String value();171}172173174175