jaxws/src/java.xml.bind/share/classes/com/sun/xml/internal/bind/v2/ClassFactory.java
changeset 33547 e4c76ac38b12
parent 29953 d5894097fe65
child 36523 116e5d5cdade
equal deleted inserted replaced
33390:d131f4b8433a 33547:e4c76ac38b12
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    85         Constructor<T> cons = null;
    85         Constructor<T> cons = null;
    86         WeakReference<Constructor> consRef = m.get(clazz);
    86         WeakReference<Constructor> consRef = m.get(clazz);
    87         if(consRef!=null)
    87         if(consRef!=null)
    88             cons = consRef.get();
    88             cons = consRef.get();
    89         if(cons==null) {
    89         if(cons==null) {
    90             cons = AccessController.doPrivileged(new PrivilegedAction<Constructor<T>>() {
    90             if (System.getSecurityManager() == null) {
    91                 @Override
    91                 cons = tryGetDeclaredConstructor(clazz);
    92                 public Constructor<T> run() {
    92             } else {
    93                     try {
    93                 cons = AccessController.doPrivileged(new PrivilegedAction<Constructor<T>>() {
    94                         return clazz.getDeclaredConstructor(emptyClass);
    94                     @Override
    95                     } catch (NoSuchMethodException e) {
    95                     public Constructor<T> run() {
    96                         logger.log(Level.INFO,"No default constructor found on "+clazz,e);
    96                         return tryGetDeclaredConstructor(clazz);
    97                         NoSuchMethodError exp;
       
    98                         if(clazz.getDeclaringClass()!=null && !Modifier.isStatic(clazz.getModifiers())) {
       
    99                             exp = new NoSuchMethodError(Messages.NO_DEFAULT_CONSTRUCTOR_IN_INNER_CLASS
       
   100                                                                 .format(clazz.getName()));
       
   101                         } else {
       
   102                             exp = new NoSuchMethodError(e.getMessage());
       
   103                         }
       
   104                         exp.initCause(e);
       
   105                         throw exp;
       
   106                     }
    97                     }
   107                 }
    98                 });
   108             });
    99             }
   109 
   100 
   110             int classMod = clazz.getModifiers();
   101             int classMod = clazz.getModifiers();
   111 
   102 
   112             if(!Modifier.isPublic(classMod) || !Modifier.isPublic(cons.getModifiers())) {
   103             if(!Modifier.isPublic(classMod) || !Modifier.isPublic(cons.getModifiers())) {
   113                 // attempt to make it work even if the constructor is not accessible
   104                 // attempt to make it work even if the constructor is not accessible
   122 
   113 
   123             m.put(clazz,new WeakReference<Constructor>(cons));
   114             m.put(clazz,new WeakReference<Constructor>(cons));
   124         }
   115         }
   125 
   116 
   126         return cons.newInstance(emptyObject);
   117         return cons.newInstance(emptyObject);
       
   118     }
       
   119 
       
   120     private static <T> Constructor<T> tryGetDeclaredConstructor(Class<T> clazz) {
       
   121         try {
       
   122             return clazz.getDeclaredConstructor((Class<T>[])emptyClass);
       
   123         } catch (NoSuchMethodException e) {
       
   124             logger.log(Level.INFO,"No default constructor found on "+clazz,e);
       
   125             NoSuchMethodError exp;
       
   126             if(clazz.getDeclaringClass()!=null && !Modifier.isStatic(clazz.getModifiers())) {
       
   127                 exp = new NoSuchMethodError(Messages.NO_DEFAULT_CONSTRUCTOR_IN_INNER_CLASS
       
   128                                                     .format(clazz.getName()));
       
   129             } else {
       
   130                 exp = new NoSuchMethodError(e.getMessage());
       
   131             }
       
   132             exp.initCause(e);
       
   133             throw exp;
       
   134         }
   127     }
   135     }
   128 
   136 
   129     /**
   137     /**
   130      * The same as {@link #create0} but with an error handling to make
   138      * The same as {@link #create0} but with an error handling to make
   131      * the instantiation error fatal.
   139      * the instantiation error fatal.