Utils.java revision 524:dcaa586ab756
1144518Sdavidxu/*
2144518Sdavidxu * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
3144518Sdavidxu * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4144518Sdavidxu *
5144518Sdavidxu * This code is free software; you can redistribute it and/or modify it
6144518Sdavidxu * under the terms of the GNU General Public License version 2 only, as
7144518Sdavidxu * published by the Free Software Foundation.  Oracle designates this
8144518Sdavidxu * particular file as subject to the "Classpath" exception as provided
9144518Sdavidxu * by Oracle in the LICENSE file that accompanied this code.
10144518Sdavidxu *
11144518Sdavidxu * This code is distributed in the hope that it will be useful, but WITHOUT
12144518Sdavidxu * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13144518Sdavidxu * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14144518Sdavidxu * version 2 for more details (a copy is included in the LICENSE file that
15144518Sdavidxu * accompanied this code).
16144518Sdavidxu *
17144518Sdavidxu * You should have received a copy of the GNU General Public License version
18144518Sdavidxu * 2 along with this work; if not, write to the Free Software Foundation,
19144518Sdavidxu * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20144518Sdavidxu *
21144518Sdavidxu * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22144518Sdavidxu * or visit www.oracle.com if you need additional information or have any
23144518Sdavidxu * questions.
24144518Sdavidxu */
25144518Sdavidxu
26144518Sdavidxupackage com.sun.xml.internal.bind.api;
27144518Sdavidxu
28144518Sdavidxuimport com.sun.xml.internal.bind.v2.model.nav.Navigator;
29144518Sdavidxu
30144518Sdavidxuimport java.lang.reflect.Field;
31144518Sdavidxuimport java.lang.reflect.InvocationTargetException;
32162061Sdavidxuimport java.lang.reflect.Method;
33144518Sdavidxuimport java.lang.reflect.Type;
34144518Sdavidxuimport java.security.AccessController;
35300043Skibimport java.security.PrivilegedAction;
36300043Skibimport java.util.logging.Level;
37300043Skibimport java.util.logging.Logger;
38300043Skib
39300043Skib/**
40212077Sdavidxu * Utils class.
41162061Sdavidxu * Has *package private* access to avoid inappropriate usage.
42233912Sdavidxu */
43179970Sdavidxufinal class Utils {
44216641Sdavidxu
45179970Sdavidxu    private static final Logger LOGGER = Logger.getLogger(Utils.class.getName());
46161680Sdavidxu
47319430Svangyzen    /**
48163334Sdavidxu     * static ReflectionNavigator field to avoid usage of reflection every time we use it.
49161680Sdavidxu     */
50161680Sdavidxu    static final Navigator<Type, Class, Field, Method> REFLECTION_NAVIGATOR;
51161680Sdavidxu
52163334Sdavidxu    static { // we statically initializing REFLECTION_NAVIGATOR property
53212077Sdavidxu        try {
54212077Sdavidxu            Class refNav = Class.forName("com.sun.xml.internal.bind.v2.model.nav.ReflectionNavigator");
55173801Sdavidxu            //noinspection unchecked
56162061Sdavidxu            final Method getInstance = refNav.getDeclaredMethod("getInstance");
57173801Sdavidxu
58178647Sdavidxu            // requires accessClassInPackage privilege
59216641Sdavidxu            AccessController.doPrivileged(
60216641Sdavidxu                    new PrivilegedAction<Object>() {
61178647Sdavidxu                        @Override
62164877Sdavidxu                        public Object run() {
63249985Sjilles                            getInstance.setAccessible(true);
64164902Sdavidxu                            return null;
65164902Sdavidxu                        }
66164902Sdavidxu                    }
67162061Sdavidxu            );
68232209Sdavidxu
69232209Sdavidxu            //noinspection unchecked
70232209Sdavidxu            REFLECTION_NAVIGATOR = (Navigator<Type, Class, Field, Method>) getInstance.invoke(null);
71232209Sdavidxu        } catch (ClassNotFoundException e) {
72177850Sdavidxu            e.printStackTrace();
73177850Sdavidxu            throw new IllegalStateException("Can't find ReflectionNavigator class");
74212076Sdavidxu        } catch (InvocationTargetException e) {
75212076Sdavidxu            e.printStackTrace();
76212076Sdavidxu            throw new IllegalStateException("ReflectionNavigator.getInstance throws the exception");
77212076Sdavidxu        } catch (NoSuchMethodException e) {
78212076Sdavidxu            e.printStackTrace();
79144518Sdavidxu            throw new IllegalStateException("ReflectionNavigator.getInstance can't be found");
80161680Sdavidxu        } catch (IllegalAccessException e) {
81161680Sdavidxu            e.printStackTrace();
82300043Skib            throw new IllegalStateException("ReflectionNavigator.getInstance method is inaccessible");
83300043Skib        } catch (SecurityException e) {
84300043Skib            LOGGER.log(Level.FINE, "Unable to access ReflectionNavigator.getInstance", e);
85300043Skib            throw e;
86300043Skib        }
87300043Skib    }
88300043Skib
89300043Skib    /**
90300043Skib     * private constructor to avoid util class instantiating
91300043Skib     */
92300043Skib    private Utils() {
93300043Skib    }
94161680Sdavidxu}
95161680Sdavidxu