1/*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License.  Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15package javassist.util.proxy;
16
17import java.lang.reflect.AccessibleObject;
18import java.lang.reflect.Constructor;
19import java.lang.reflect.Field;
20import java.lang.reflect.Method;
21import java.security.AccessController;
22import java.security.PrivilegedAction;
23import java.security.PrivilegedActionException;
24import java.security.PrivilegedExceptionAction;
25
26class SecurityActions {
27    static Method[] getDeclaredMethods(final Class clazz) {
28        if (System.getSecurityManager() == null)
29            return clazz.getDeclaredMethods();
30        else {
31            return (Method[]) AccessController
32                    .doPrivileged(new PrivilegedAction() {
33                        public Object run() {
34                            return clazz.getDeclaredMethods();
35                        }
36                    });
37        }
38    }
39
40    static Constructor[] getDeclaredConstructors(final Class clazz) {
41        if (System.getSecurityManager() == null)
42            return clazz.getDeclaredConstructors();
43        else {
44            return (Constructor[]) AccessController
45                    .doPrivileged(new PrivilegedAction() {
46                        public Object run() {
47                            return clazz.getDeclaredConstructors();
48                        }
49                    });
50        }
51    }
52
53    static Method getDeclaredMethod(final Class clazz, final String name,
54            final Class[] types) throws NoSuchMethodException {
55        if (System.getSecurityManager() == null)
56            return clazz.getDeclaredMethod(name, types);
57        else {
58            try {
59                return (Method) AccessController
60                        .doPrivileged(new PrivilegedExceptionAction() {
61                            public Object run() throws Exception {
62                                return clazz.getDeclaredMethod(name, types);
63                            }
64                        });
65            }
66            catch (PrivilegedActionException e) {
67                if (e.getCause() instanceof NoSuchMethodException)
68                    throw (NoSuchMethodException) e.getCause();
69
70                throw new RuntimeException(e.getCause());
71            }
72        }
73    }
74
75    static Constructor getDeclaredConstructor(final Class clazz,
76                                              final Class[] types)
77        throws NoSuchMethodException
78    {
79        if (System.getSecurityManager() == null)
80            return clazz.getDeclaredConstructor(types);
81        else {
82            try {
83                return (Constructor) AccessController
84                        .doPrivileged(new PrivilegedExceptionAction() {
85                            public Object run() throws Exception {
86                                return clazz.getDeclaredConstructor(types);
87                            }
88                        });
89            }
90            catch (PrivilegedActionException e) {
91                if (e.getCause() instanceof NoSuchMethodException)
92                    throw (NoSuchMethodException) e.getCause();
93
94                throw new RuntimeException(e.getCause());
95            }
96        }
97    }
98
99    static void setAccessible(final AccessibleObject ao,
100                              final boolean accessible) {
101        if (System.getSecurityManager() == null)
102            ao.setAccessible(accessible);
103        else {
104            AccessController.doPrivileged(new PrivilegedAction() {
105                public Object run() {
106                    ao.setAccessible(accessible);
107                    return null;
108                }
109            });
110        }
111    }
112
113    static void set(final Field fld, final Object target, final Object value)
114        throws IllegalAccessException
115    {
116        if (System.getSecurityManager() == null)
117            fld.set(target, value);
118        else {
119            try {
120                AccessController.doPrivileged(new PrivilegedExceptionAction() {
121                    public Object run() throws Exception {
122                        fld.set(target, value);
123                        return null;
124                    }
125                });
126            }
127            catch (PrivilegedActionException e) {
128                if (e.getCause() instanceof NoSuchMethodException)
129                    throw (IllegalAccessException) e.getCause();
130
131                throw new RuntimeException(e.getCause());
132            }
133        }
134    }
135}
136