jaxws/src/java.xml.bind/share/classes/com/sun/xml/internal/bind/v2/ClassFactory.java
author mkos
Fri, 30 Oct 2015 10:34:46 +0100
changeset 33547 e4c76ac38b12
parent 29953 d5894097fe65
child 36523 116e5d5cdade
permissions -rw-r--r--
8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT) Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     1
/*
33547
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     4
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    10
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    15
 * accompanied this code).
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    16
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    20
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    23
 * questions.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    24
 */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    25
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    26
package com.sun.xml.internal.bind.v2;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    27
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    28
import java.lang.reflect.Constructor;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    29
import java.lang.reflect.InvocationTargetException;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    30
import java.lang.reflect.Method;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    31
import java.lang.reflect.Modifier;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    32
import java.lang.ref.WeakReference;
29953
d5894097fe65 8078057: Reapply fixes for 8073361, 8073374, 8073696
aefimov
parents: 29839
diff changeset
    33
import java.security.AccessController;
d5894097fe65 8078057: Reapply fixes for 8073361, 8073374, 8073696
aefimov
parents: 29839
diff changeset
    34
import java.security.PrivilegedAction;
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    35
import java.util.Map;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    36
import java.util.WeakHashMap;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    37
import java.util.logging.Level;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    38
import java.util.logging.Logger;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    39
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    40
import com.sun.xml.internal.bind.Util;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    41
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    42
/**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    43
 * Creates new instances of classes.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    44
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    45
 * <p>
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    46
 * This code handles the case where the class is not public or the constructor is
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    47
 * not public.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    48
 *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    49
 * @since 2.0
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    50
 * @author Kohsuke Kawaguchi
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    51
 */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    52
public final class ClassFactory {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    53
    private static final Class[] emptyClass = new Class[0];
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    54
    private static final Object[] emptyObject = new Object[0];
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    55
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    56
    private static final Logger logger = Util.getClassLogger();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    57
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    58
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    59
     * Cache from a class to its default constructor.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    60
     *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    61
     * To avoid synchronization among threads, we use {@link ThreadLocal}.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    62
     */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    63
    private static final ThreadLocal<Map<Class, WeakReference<Constructor>>> tls = new ThreadLocal<Map<Class,WeakReference<Constructor>>>() {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    64
        @Override
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    65
        public Map<Class,WeakReference<Constructor>> initialValue() {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    66
            return new WeakHashMap<Class,WeakReference<Constructor>>();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    67
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    68
    };
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    69
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    70
    public static void cleanCache() {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    71
        if (tls != null) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    72
            try {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    73
                tls.remove();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    74
            } catch (Exception e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    75
                logger.log(Level.WARNING, "Unable to clean Thread Local cache of classes used in Unmarshaller: {0}", e.getLocalizedMessage());
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    76
            }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    77
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    78
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    79
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    80
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    81
     * Creates a new instance of the class but throw exceptions without catching it.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    82
     */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    83
    public static <T> T create0( final Class<T> clazz ) throws IllegalAccessException, InvocationTargetException, InstantiationException {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    84
        Map<Class,WeakReference<Constructor>> m = tls.get();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    85
        Constructor<T> cons = null;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    86
        WeakReference<Constructor> consRef = m.get(clazz);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    87
        if(consRef!=null)
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    88
            cons = consRef.get();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
    89
        if(cons==null) {
33547
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
    90
            if (System.getSecurityManager() == null) {
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
    91
                cons = tryGetDeclaredConstructor(clazz);
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
    92
            } else {
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
    93
                cons = AccessController.doPrivileged(new PrivilegedAction<Constructor<T>>() {
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
    94
                    @Override
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
    95
                    public Constructor<T> run() {
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
    96
                        return tryGetDeclaredConstructor(clazz);
29953
d5894097fe65 8078057: Reapply fixes for 8073361, 8073374, 8073696
aefimov
parents: 29839
diff changeset
    97
                    }
33547
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
    98
                });
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
    99
            }
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   100
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   101
            int classMod = clazz.getModifiers();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   102
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   103
            if(!Modifier.isPublic(classMod) || !Modifier.isPublic(cons.getModifiers())) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   104
                // attempt to make it work even if the constructor is not accessible
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   105
                try {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   106
                    cons.setAccessible(true);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   107
                } catch(SecurityException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   108
                    // but if we don't have a permission to do so, work gracefully.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   109
                    logger.log(Level.FINE,"Unable to make the constructor of "+clazz+" accessible",e);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   110
                    throw e;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   111
                }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   112
            }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   113
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   114
            m.put(clazz,new WeakReference<Constructor>(cons));
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   115
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   116
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   117
        return cons.newInstance(emptyObject);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   118
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   119
33547
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   120
    private static <T> Constructor<T> tryGetDeclaredConstructor(Class<T> clazz) {
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   121
        try {
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   122
            return clazz.getDeclaredConstructor((Class<T>[])emptyClass);
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   123
        } catch (NoSuchMethodException e) {
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   124
            logger.log(Level.INFO,"No default constructor found on "+clazz,e);
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   125
            NoSuchMethodError exp;
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   126
            if(clazz.getDeclaringClass()!=null && !Modifier.isStatic(clazz.getModifiers())) {
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   127
                exp = new NoSuchMethodError(Messages.NO_DEFAULT_CONSTRUCTOR_IN_INNER_CLASS
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   128
                                                    .format(clazz.getName()));
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   129
            } else {
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   130
                exp = new NoSuchMethodError(e.getMessage());
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   131
            }
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   132
            exp.initCause(e);
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   133
            throw exp;
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   134
        }
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   135
    }
e4c76ac38b12 8139743: Update JAX-WS RI integration to latest version (2.3.0-SNAPSHOT)
mkos
parents: 29953
diff changeset
   136
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   137
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   138
     * The same as {@link #create0} but with an error handling to make
16791
fe5141eabb0e 8010393: Update JAX-WS RI to 2.2.9-b12941
alanb
parents: 12009
diff changeset
   139
     * the instantiation error fatal.
12009
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   140
     */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   141
    public static <T> T create( Class<T> clazz ) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   142
        try {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   143
            return create0(clazz);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   144
        } catch (InstantiationException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   145
            logger.log(Level.INFO,"failed to create a new instance of "+clazz,e);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   146
            throw new InstantiationError(e.toString());
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   147
        } catch (IllegalAccessException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   148
            logger.log(Level.INFO,"failed to create a new instance of "+clazz,e);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   149
            throw new IllegalAccessError(e.toString());
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   150
        } catch (InvocationTargetException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   151
            Throwable target = e.getTargetException();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   152
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   153
            // most likely an error on the user's code.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   154
            // just let it through for the ease of debugging
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   155
            if(target instanceof RuntimeException)
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   156
                throw (RuntimeException)target;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   157
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   158
            // error. just forward it for the ease of debugging
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   159
            if(target instanceof Error)
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   160
                throw (Error)target;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   161
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   162
            // a checked exception.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   163
            // not sure how we should report this error,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   164
            // but for now we just forward it by wrapping it into a runtime exception
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   165
            throw new IllegalStateException(target);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   166
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   167
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   168
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   169
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   170
     *  Call a method in the factory class to get the object.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   171
     */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   172
    public static Object create(Method method) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   173
        Throwable errorMsg;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   174
        try {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   175
            return method.invoke(null, emptyObject);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   176
        } catch (InvocationTargetException ive) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   177
            Throwable target = ive.getTargetException();
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   178
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   179
            if(target instanceof RuntimeException)
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   180
                throw (RuntimeException)target;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   181
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   182
            if(target instanceof Error)
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   183
                throw (Error)target;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   184
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   185
            throw new IllegalStateException(target);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   186
        } catch (IllegalAccessException e) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   187
            logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),e);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   188
            throw new IllegalAccessError(e.toString());
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   189
        } catch (IllegalArgumentException iae){
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   190
            logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),iae);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   191
            errorMsg = iae;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   192
        } catch (NullPointerException npe){
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   193
            logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),npe);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   194
            errorMsg = npe;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   195
        } catch (ExceptionInInitializerError eie){
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   196
            logger.log(Level.INFO,"failed to create a new instance of "+method.getReturnType().getName(),eie);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   197
            errorMsg = eie;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   198
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   199
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   200
        NoSuchMethodError exp;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   201
        exp = new NoSuchMethodError(errorMsg.getMessage());
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   202
        exp.initCause(errorMsg);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   203
        throw exp;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   204
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   205
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   206
    /**
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   207
     * Infers the instanciable implementation class that can be assigned to the given field type.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   208
     *
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   209
     * @return null
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   210
     *      if inference fails.
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   211
     */
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   212
    public static <T> Class<? extends T> inferImplClass(Class<T> fieldType, Class[] knownImplClasses) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   213
        if(!fieldType.isInterface())
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   214
            return fieldType;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   215
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   216
        for( Class<?> impl : knownImplClasses ) {
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   217
            if(fieldType.isAssignableFrom(impl))
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   218
                return impl.asSubclass(fieldType);
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   219
        }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   220
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   221
        // if we can't find an implementation class,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   222
        // let's just hope that we will never need to create a new object,
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   223
        // and returns null
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   224
        return null;
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   225
    }
4abb694f273a 7150322: Stop using drop source bundles in jaxws
ohair
parents:
diff changeset
   226
}