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
26package com.sun.xml.internal.bind.api;
27
28import com.sun.xml.internal.bind.v2.model.nav.Navigator;
29
30import java.lang.reflect.Field;
31import java.lang.reflect.InvocationTargetException;
32import java.lang.reflect.Method;
33import java.lang.reflect.Type;
34import java.security.AccessController;
35import java.security.PrivilegedAction;
36import java.util.logging.Level;
37import java.util.logging.Logger;
38
39/**
40 * Utils class.
41 *
42 * WARNING: If you are doing any changes don't forget to change other Utils classes in different packages.
43 *
44 * Has *package private* access to avoid inappropriate usage.
45 */
46final class Utils {
47
48    private static final Logger LOGGER = Logger.getLogger(Utils.class.getName());
49
50    /**
51     * static ReflectionNavigator field to avoid usage of reflection every time we use it.
52     */
53    static final Navigator<Type, Class, Field, Method> REFLECTION_NAVIGATOR;
54
55    static { // we statically initializing REFLECTION_NAVIGATOR property
56        try {
57            final Class refNav = Class.forName("com.sun.xml.internal.bind.v2.model.nav.ReflectionNavigator");
58
59            // requires accessClassInPackage privilege
60            final Method getInstance = AccessController.doPrivileged(
61                    new PrivilegedAction<Method>() {
62                        @Override
63                        public Method run() {
64                            try {
65                                Method getInstance = refNav.getDeclaredMethod("getInstance");
66                                getInstance.setAccessible(true);
67                                return getInstance;
68                            } catch (NoSuchMethodException e) {
69                                throw new IllegalStateException("ReflectionNavigator.getInstance can't be found");
70                            }
71                        }
72                    }
73            );
74
75            //noinspection unchecked
76            REFLECTION_NAVIGATOR = (Navigator<Type, Class, Field, Method>) getInstance.invoke(null);
77        } catch (ClassNotFoundException e) {
78            throw new IllegalStateException("Can't find ReflectionNavigator class");
79        } catch (InvocationTargetException e) {
80            throw new IllegalStateException("ReflectionNavigator.getInstance throws the exception");
81        } catch (IllegalAccessException e) {
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}
95