KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > JavaAccessibility


1 package org.python.core;
2 import java.lang.reflect.*;
3
4 /**
5  * Provides a means of using the Java 2
6  * {Field|Method|Constructor}.setAccessibility() methods that can be
7  * compiled with Java 1 or Java 2.
8  *
9  * When compiling with Java 2, Java2Accessibility.java must also be
10  * compiled. If compiled Java 1, or if the Java2Accessibility class cannot
11  * be found, then the methods here have no effect.
12  */

13
14 class JavaAccessibility
15 {
16     private static JavaAccessibility access = null;
17
18     static void initialize() {
19         // If we can find it, and the registry option
20
// python.security.respectJavaAccessibility is set, then we set the
21
// access object to an instance of the subclass Java2Accessibility
22
if (Options.respectJavaAccessibility)
23             return;
24         try {
25             Class JavaDoc c = Class.forName("org.python.core.Java2Accessibility");
26             Class.forName("java.lang.reflect.AccessibleObject");
27             access = (JavaAccessibility)c.newInstance();
28         }
29         catch (InstantiationException JavaDoc e) {}
30         catch (IllegalAccessException JavaDoc e) {}
31         catch (ClassNotFoundException JavaDoc e) {}
32     }
33
34     static boolean accessIsMutable() {
35         return access != null;
36     }
37
38     /**
39      * These methods get overridden in the Java2Accessibility subclass
40      */

41     void setAccess(Field field, boolean flag) throws SecurityException JavaDoc {
42     }
43
44     void setAccess(Method method, boolean flag) throws SecurityException JavaDoc {
45     }
46
47     void setAccess(Constructor constructor, boolean flag)
48         throws SecurityException JavaDoc
49     {}
50
51     public static void setAccessible(Field field, boolean flag)
52         throws SecurityException JavaDoc
53     {
54         if (access != null)
55             access.setAccess(field, flag);
56     }
57
58     public static void setAccessible(Method method, boolean flag)
59         throws SecurityException JavaDoc
60     {
61         if (access != null)
62             access.setAccess(method, flag);
63     }
64
65     public static void setAccessible(Constructor constructor, boolean flag)
66         throws SecurityException JavaDoc
67     {
68         if (access != null)
69             access.setAccess(constructor, flag);
70     }
71 }
72
Popular Tags