Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Parameter/WithoutParameters.java
38828 views
/*1* Copyright (c) 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 800472926* @summary javac should generate method parameters correctly.27*/2829import java.lang.*;30import java.lang.reflect.*;31import java.lang.annotation.*;32import java.util.List;33import java.util.Objects;3435import static java.lang.annotation.ElementType.*;3637public class WithoutParameters {38int errors = 0;3940private WithoutParameters() {}4142public static void main(String argv[]) throws Exception {43WithoutParameters wp = new WithoutParameters();44wp.runTests(Foo.class.getMethods());45wp.runTests(Foo.Inner.class.getConstructors());46wp.checkForErrors();47}4849void runTests(Method[] methods) throws Exception {50for(Method m : methods) {runTest(m);}51}5253void runTests(Constructor[] constructors) throws Exception {54for(Constructor c : constructors) {runTest(c);}55}5657void runTest(Executable e) throws Exception {58System.err.println("Inspecting executable " + e);59Parameter[] parameters = e.getParameters();60Objects.requireNonNull(parameters, "getParameters should never be null");6162ExpectedParameterInfo epi = e.getAnnotation(ExpectedParameterInfo.class);63if (epi != null) {64abortIfTrue(epi.parameterCount() != e.getParameterCount(), "Bad parameter count for "+ e);65abortIfTrue(epi.isVarArgs() != e.isVarArgs(),"Bad varargs value for "+ e);66}67abortIfTrue(e.getParameterCount() != parameters.length, "Mismatched of parameter counts.");6869for(int i = 0; i < parameters.length; i++) {70Parameter p = parameters[i];71errorIfTrue(p.isNamePresent(), p + ".isNamePresent == true");72errorIfTrue(!p.getDeclaringExecutable().equals(e), p + ".getDeclaringExecutable != " + e);73Objects.requireNonNull(p.getType(), "getType() should not be null");74Objects.requireNonNull(p.getParameterizedType(), "getParameterizedType() should not be null");7576if (epi != null) {77Class<?> expectedParameterType = epi.parameterTypes()[i];78errorIfTrue(!p.getType().equals(expectedParameterType),79"Wrong parameter type for " + p + ": expected " + expectedParameterType +80", but got " + p.getType());8182ParameterizedInfo[] expectedParameterizedTypes = epi.parameterizedTypes();83if (expectedParameterizedTypes.length > 0) {84Type parameterizedType = p.getParameterizedType();85Class<? extends Type> expectedParameterziedTypeType = expectedParameterizedTypes[i].value();86errorIfTrue(!expectedParameterziedTypeType.isAssignableFrom(parameterizedType.getClass()),87"Wrong class of parameteried type of " + p + ": expected " + expectedParameterziedTypeType +88", but got " + parameterizedType.getClass());8990if (expectedParameterziedTypeType.equals(Class.class)) {91errorIfTrue(!parameterizedType.equals(expectedParameterType),92"Wrong parameteried type for " + p + ": expected " + expectedParameterType +93", but got " + parameterizedType);94} else {95if (expectedParameterziedTypeType.equals(ParameterizedType.class)) {96ParameterizedType ptype = (ParameterizedType)parameterizedType;97errorIfTrue(!ptype.getRawType().equals(expectedParameterType),98"Wrong raw type for " + p + ": expected " + expectedParameterType +99", but got " + ptype.getRawType());100}101102// Check string representation103String expectedStringOfType = epi.parameterizedTypes()[i].string();104errorIfTrue(!expectedStringOfType.equals(parameterizedType.toString()),105"Bad type string" + p + ": expected " + expectedStringOfType +106", but got " + parameterizedType.toString());107}108}109}110}111}112113private void checkForErrors() {114if (errors > 0)115throw new RuntimeException("Failed " + errors + " tests");116}117118private void errorIfTrue(boolean predicate, String errMessage) {119if (predicate) {120errors++;121System.err.println(errMessage);122}123}124125private void abortIfTrue(boolean predicate, String errMessage) {126if (predicate) {127throw new RuntimeException(errMessage);128}129}130131@Retention(RetentionPolicy.RUNTIME)132@Target({METHOD, CONSTRUCTOR})133@interface ExpectedParameterInfo {134int parameterCount() default 0;135Class<?>[] parameterTypes() default {};136ParameterizedInfo[] parameterizedTypes() default {};137boolean isVarArgs() default false;138}139140@Target({})141@interface ParameterizedInfo {142Class<? extends Type> value() default Class.class;143String string() default "";144}145146public class Foo {147int thing;148@ExpectedParameterInfo(parameterCount = 6,149parameterTypes =150{int.class, Foo.class,151List.class, List.class,152List.class, String[].class},153parameterizedTypes =154{@ParameterizedInfo(Class.class),155@ParameterizedInfo(Class.class),156@ParameterizedInfo(value=ParameterizedType.class, string="java.util.List<?>"),157@ParameterizedInfo(value=ParameterizedType.class, string="java.util.List<WithoutParameters$Foo>"),158@ParameterizedInfo(value=ParameterizedType.class, string="java.util.List<? extends WithoutParameters$Foo>"),159@ParameterizedInfo(Class.class)},160isVarArgs = true)161public void qux(int quux, Foo quuux,162List<?> l, List<Foo> l2,163List<? extends Foo> l3,164String... rest) {}165public class Inner {166int thang;167@ExpectedParameterInfo(parameterCount = 2,168parameterTypes = {Foo.class, int.class})169public Inner(int theng) {170thang = theng + thing;171}172}173}174}175176177