Path: blob/master/test/jdk/java/lang/annotation/ParameterAnnotations.java
41152 views
/*1* Copyright (c) 2008, 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 6761678 816281726* @summary Check properties of Annotations returned from27* getParameterAnnotations, including freedom from security28* exceptions.29* @run main/othervm -Djava.security.manager=allow ParameterAnnotations30* @author Martin Buchholz31*/3233import java.lang.annotation.Annotation;34import java.lang.annotation.ElementType;35import java.lang.annotation.Retention;36import java.lang.annotation.RetentionPolicy;37import java.lang.annotation.Target;38import java.lang.reflect.Method;39import java.security.Permission;40import java.security.Policy;41import java.security.ProtectionDomain;4243@Retention(RetentionPolicy.RUNTIME)44@Target({ ElementType.FIELD, ElementType.PARAMETER })45@interface Named {46String value();47}4849public class ParameterAnnotations {5051// A security policy that differs from the default only in that it52// allows a security manager to be uninstalled.53static class MyPolicy extends Policy {54final Policy defaultPolicy;55MyPolicy(Policy defaultPolicy) {56this.defaultPolicy = defaultPolicy;57}58public boolean implies(ProtectionDomain pd, Permission p) {59return p.getName().equals("setSecurityManager") ||60defaultPolicy.implies(pd, p);61}62}6364public void nop(@Named("foo") Object foo,65@Named("bar") Object bar) {66}6768void test(String[] args) throws Throwable {69// Test without a security manager70test1();7172// Test with a security manager73Policy defaultPolicy = Policy.getPolicy();74Policy.setPolicy(new MyPolicy(defaultPolicy));75System.setSecurityManager(new SecurityManager());76try {77test1();78} finally {79System.setSecurityManager(null);80Policy.setPolicy(defaultPolicy);81}82}8384void test1() throws Throwable {85for (Method m : thisClass.getMethods()) {86if (m.getName().equals("nop")) {87Annotation[][] ann = m.getParameterAnnotations();88equal(ann.length, 2);89Annotation foo = ann[0][0];90Annotation bar = ann[1][0];91equal(foo.toString(), "@Named(\"foo\")");92equal(bar.toString(), "@Named(\"bar\")");93check(foo.equals(foo));94check(! foo.equals(bar));95}96}97}9899//--------------------- Infrastructure ---------------------------100volatile int passed = 0, failed = 0;101void pass() {passed++;}102void fail() {failed++; Thread.dumpStack();}103void fail(String msg) {System.err.println(msg); fail();}104void unexpected(Throwable t) {failed++; t.printStackTrace();}105void check(boolean cond) {if (cond) pass(); else fail();}106void equal(Object x, Object y) {107if (x == null ? y == null : x.equals(y)) pass();108else fail(x + " not equal to " + y);}109static Class<?> thisClass = new Object(){}.getClass().getEnclosingClass();110public static void main(String[] args) throws Throwable {111try {thisClass.getMethod("instanceMain",String[].class)112.invoke(thisClass.newInstance(), (Object) args);}113catch (Throwable e) {throw e.getCause();}}114public void instanceMain(String[] args) throws Throwable {115try {test(args);} catch (Throwable t) {unexpected(t);}116System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);117if (failed > 0) throw new AssertionError("Some tests failed");}118}119120121