|
1 /* |
|
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.bind.v2.runtime.reflect; |
|
27 |
|
28 import com.sun.xml.internal.bind.v2.model.nav.Navigator; |
|
29 |
|
30 import java.lang.reflect.Field; |
|
31 import java.lang.reflect.InvocationTargetException; |
|
32 import java.lang.reflect.Method; |
|
33 import java.lang.reflect.Type; |
|
34 import java.security.AccessController; |
|
35 import java.security.PrivilegedAction; |
|
36 import java.util.logging.Level; |
|
37 import java.util.logging.Logger; |
|
38 |
|
39 /** |
|
40 * Utils class. |
|
41 * Has *package private* access to avoid inappropriate usage. |
|
42 */ |
|
43 final class Utils { |
|
44 |
|
45 private static final Logger LOGGER = Logger.getLogger(Utils.class.getName()); |
|
46 |
|
47 /** |
|
48 * static ReflectionNavigator field to avoid usage of reflection every time we use it. |
|
49 */ |
|
50 static final Navigator<Type, Class, Field, Method> REFLECTION_NAVIGATOR; |
|
51 |
|
52 static { // we statically initializing REFLECTION_NAVIGATOR property |
|
53 try { |
|
54 Class refNav = Class.forName("com.sun.xml.internal.bind.v2.model.nav.ReflectionNavigator"); |
|
55 //noinspection unchecked |
|
56 final Method getInstance = refNav.getDeclaredMethod("getInstance"); |
|
57 |
|
58 // requires accessClassInPackage privilege |
|
59 AccessController.doPrivileged( |
|
60 new PrivilegedAction<Object>() { |
|
61 @Override |
|
62 public Object run() { |
|
63 getInstance.setAccessible(true); |
|
64 return null; |
|
65 } |
|
66 } |
|
67 ); |
|
68 |
|
69 //noinspection unchecked |
|
70 REFLECTION_NAVIGATOR = (Navigator<Type, Class, Field, Method>) getInstance.invoke(null); |
|
71 } catch (ClassNotFoundException e) { |
|
72 e.printStackTrace(); |
|
73 throw new IllegalStateException("Can't find ReflectionNavigator class"); |
|
74 } catch (InvocationTargetException e) { |
|
75 e.printStackTrace(); |
|
76 throw new IllegalStateException("ReflectionNavigator.getInstance throws the exception"); |
|
77 } catch (NoSuchMethodException e) { |
|
78 e.printStackTrace(); |
|
79 throw new IllegalStateException("ReflectionNavigator.getInstance can't be found"); |
|
80 } catch (IllegalAccessException e) { |
|
81 e.printStackTrace(); |
|
82 throw new IllegalStateException("ReflectionNavigator.getInstance method is inaccessible"); |
|
83 } catch (SecurityException e) { |
|
84 LOGGER.log(Level.FINE, "Unable to access ReflectionNavigator.getInstance", e); |
|
85 throw e; |
|
86 } |
|
87 } |
|
88 |
|
89 /** |
|
90 * private constructor to avoid util class instantiating |
|
91 */ |
|
92 private Utils() { |
|
93 } |
|
94 } |