mkos@450: /* mkos@567: * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. mkos@450: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mkos@450: * mkos@450: * This code is free software; you can redistribute it and/or modify it mkos@450: * under the terms of the GNU General Public License version 2 only, as mkos@450: * published by the Free Software Foundation. Oracle designates this mkos@450: * particular file as subject to the "Classpath" exception as provided mkos@450: * by Oracle in the LICENSE file that accompanied this code. mkos@450: * mkos@450: * This code is distributed in the hope that it will be useful, but WITHOUT mkos@450: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mkos@450: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mkos@450: * version 2 for more details (a copy is included in the LICENSE file that mkos@450: * accompanied this code). mkos@450: * mkos@450: * You should have received a copy of the GNU General Public License version mkos@450: * 2 along with this work; if not, write to the Free Software Foundation, mkos@450: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mkos@450: * mkos@450: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mkos@450: * or visit www.oracle.com if you need additional information or have any mkos@450: * questions. mkos@450: */ mkos@450: mkos@450: package com.sun.xml.internal.bind.v2.runtime; mkos@450: mkos@450: import com.sun.xml.internal.bind.v2.model.nav.Navigator; mkos@450: mkos@450: import java.lang.reflect.Field; mkos@450: import java.lang.reflect.InvocationTargetException; mkos@450: import java.lang.reflect.Method; mkos@450: import java.lang.reflect.Type; mkos@567: import java.security.AccessController; mkos@567: import java.security.PrivilegedAction; mkos@450: import java.util.logging.Level; mkos@450: import java.util.logging.Logger; mkos@450: mkos@450: /** mkos@450: * Utils class. mkos@721: * mkos@721: * WARNING: If you are doing any changes don't forget to change other Utils classes in different packages. mkos@721: * mkos@450: * Has *package private* access to avoid inappropriate usage. mkos@450: */ mkos@567: final class Utils { mkos@450: mkos@450: private static final Logger LOGGER = Logger.getLogger(Utils.class.getName()); mkos@450: mkos@450: /** mkos@450: * static ReflectionNavigator field to avoid usage of reflection every time we use it. mkos@450: */ mkos@567: static final Navigator REFLECTION_NAVIGATOR; mkos@450: mkos@450: static { // we statically initializing REFLECTION_NAVIGATOR property mkos@450: try { mkos@721: final Class refNav = Class.forName("com.sun.xml.internal.bind.v2.model.nav.ReflectionNavigator"); mkos@567: mkos@567: // requires accessClassInPackage privilege mkos@721: final Method getInstance = AccessController.doPrivileged( mkos@721: new PrivilegedAction() { mkos@567: @Override mkos@721: public Method run() { mkos@721: try { mkos@721: Method getInstance = refNav.getDeclaredMethod("getInstance"); mkos@721: getInstance.setAccessible(true); mkos@721: return getInstance; mkos@721: } catch (NoSuchMethodException e) { mkos@721: throw new IllegalStateException("ReflectionNavigator.getInstance can't be found"); mkos@721: } mkos@567: } mkos@567: } mkos@567: ); mkos@567: mkos@450: //noinspection unchecked mkos@450: REFLECTION_NAVIGATOR = (Navigator) getInstance.invoke(null); mkos@450: } catch (ClassNotFoundException e) { mkos@450: throw new IllegalStateException("Can't find ReflectionNavigator class"); mkos@450: } catch (InvocationTargetException e) { mkos@450: throw new IllegalStateException("ReflectionNavigator.getInstance throws the exception"); mkos@450: } catch (IllegalAccessException e) { mkos@450: throw new IllegalStateException("ReflectionNavigator.getInstance method is inaccessible"); mkos@450: } catch (SecurityException e) { mkos@450: LOGGER.log(Level.FINE, "Unable to access ReflectionNavigator.getInstance", e); mkos@450: throw e; mkos@450: } mkos@450: } mkos@450: mkos@450: /** mkos@450: * private constructor to avoid util class instantiating mkos@450: */ mkos@450: private Utils() { mkos@450: } mkos@450: }