684N/A/*
3261N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
684N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
684N/A *
684N/A * This code is free software; you can redistribute it and/or modify it
684N/A * under the terms of the GNU General Public License version 2 only, as
684N/A * published by the Free Software Foundation.
684N/A *
684N/A * This code is distributed in the hope that it will be useful, but WITHOUT
684N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
684N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
684N/A * version 2 for more details (a copy is included in the LICENSE file that
684N/A * accompanied this code).
684N/A *
684N/A * You should have received a copy of the GNU General Public License version
684N/A * 2 along with this work; if not, write to the Free Software Foundation,
684N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
684N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
684N/A */
684N/A
684N/A/*
684N/A * @test
684N/A * @bug 6761678
684N/A * @summary Check properties of Annotations returned from
684N/A * getParameterAnnotations, including freedom from security
684N/A * exceptions.
2483N/A * @run main/othervm ParameterAnnotations
684N/A * @author Martin Buchholz
684N/A */
684N/A
684N/Aimport java.lang.annotation.Annotation;
684N/Aimport java.lang.annotation.ElementType;
684N/Aimport java.lang.annotation.Retention;
684N/Aimport java.lang.annotation.RetentionPolicy;
684N/Aimport java.lang.annotation.Target;
684N/Aimport java.lang.reflect.Method;
684N/Aimport java.security.Permission;
684N/Aimport java.security.Policy;
684N/Aimport java.security.ProtectionDomain;
684N/A
684N/A@Retention(RetentionPolicy.RUNTIME)
684N/A@Target({ ElementType.FIELD, ElementType.PARAMETER })
684N/A@interface Named {
684N/A String value();
684N/A}
684N/A
684N/Apublic class ParameterAnnotations {
684N/A
684N/A // A security policy that differs from the default only in that it
684N/A // allows a security manager to be uninstalled.
684N/A static class MyPolicy extends Policy {
684N/A final Policy defaultPolicy;
684N/A MyPolicy(Policy defaultPolicy) {
684N/A this.defaultPolicy = defaultPolicy;
684N/A }
684N/A public boolean implies(ProtectionDomain pd, Permission p) {
684N/A return p.getName().equals("setSecurityManager") ||
684N/A defaultPolicy.implies(pd, p);
684N/A }
684N/A }
684N/A
684N/A public void nop(@Named("foo") Object foo,
684N/A @Named("bar") Object bar) {
684N/A }
684N/A
684N/A void test(String[] args) throws Throwable {
684N/A // Test without a security manager
684N/A test1();
684N/A
684N/A // Test with a security manager
684N/A Policy defaultPolicy = Policy.getPolicy();
684N/A Policy.setPolicy(new MyPolicy(defaultPolicy));
684N/A System.setSecurityManager(new SecurityManager());
684N/A try {
684N/A test1();
684N/A } finally {
684N/A System.setSecurityManager(null);
684N/A Policy.setPolicy(defaultPolicy);
684N/A }
684N/A }
684N/A
684N/A void test1() throws Throwable {
684N/A for (Method m : thisClass.getMethods()) {
684N/A if (m.getName().equals("nop")) {
684N/A Annotation[][] ann = m.getParameterAnnotations();
684N/A equal(ann.length, 2);
684N/A Annotation foo = ann[0][0];
684N/A Annotation bar = ann[1][0];
684N/A equal(foo.toString(), "@Named(value=foo)");
684N/A equal(bar.toString(), "@Named(value=bar)");
684N/A check(foo.equals(foo));
684N/A check(! foo.equals(bar));
684N/A }
684N/A }
684N/A }
684N/A
684N/A //--------------------- Infrastructure ---------------------------
684N/A volatile int passed = 0, failed = 0;
684N/A void pass() {passed++;}
684N/A void fail() {failed++; Thread.dumpStack();}
684N/A void fail(String msg) {System.err.println(msg); fail();}
684N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
684N/A void check(boolean cond) {if (cond) pass(); else fail();}
684N/A void equal(Object x, Object y) {
684N/A if (x == null ? y == null : x.equals(y)) pass();
684N/A else fail(x + " not equal to " + y);}
684N/A static Class<?> thisClass = new Object(){}.getClass().getEnclosingClass();
684N/A public static void main(String[] args) throws Throwable {
684N/A try {thisClass.getMethod("instanceMain",String[].class)
684N/A .invoke(thisClass.newInstance(), (Object) args);}
684N/A catch (Throwable e) {throw e.getCause();}}
684N/A public void instanceMain(String[] args) throws Throwable {
684N/A try {test(args);} catch (Throwable t) {unexpected(t);}
684N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
684N/A if (failed > 0) throw new AssertionError("Some tests failed");}
684N/A}