1 /*
2 * Copyright (c) 1997, 2015, 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
70 public static void cleanCache() {
71 if (tls != null) {
72 try {
73 tls.remove();
74 } catch (Exception e) {
75 logger.log(Level.WARNING, "Unable to clean Thread Local cache of classes used in Unmarshaller: {0}", e.getLocalizedMessage());
76 }
77 }
78 }
79
80 /**
81 * Creates a new instance of the class but throw exceptions without catching it.
82 */
83 public static <T> T create0( final Class<T> clazz ) throws IllegalAccessException, InvocationTargetException, InstantiationException {
84 Map<Class,WeakReference<Constructor>> m = tls.get();
85 Constructor<T> cons = null;
86 WeakReference<Constructor> consRef = m.get(clazz);
87 if(consRef!=null)
88 cons = consRef.get();
89 if(cons==null) {
90 cons = AccessController.doPrivileged(new PrivilegedAction<Constructor<T>>() {
91 @Override
92 public Constructor<T> run() {
93 try {
94 return clazz.getDeclaredConstructor(emptyClass);
95 } catch (NoSuchMethodException e) {
96 logger.log(Level.INFO,"No default constructor found on "+clazz,e);
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 }
107 }
108 });
109
110 int classMod = clazz.getModifiers();
111
112 if(!Modifier.isPublic(classMod) || !Modifier.isPublic(cons.getModifiers())) {
113 // attempt to make it work even if the constructor is not accessible
114 try {
115 cons.setAccessible(true);
116 } catch(SecurityException e) {
117 // but if we don't have a permission to do so, work gracefully.
118 logger.log(Level.FINE,"Unable to make the constructor of "+clazz+" accessible",e);
119 throw e;
120 }
121 }
122
123 m.put(clazz,new WeakReference<Constructor>(cons));
124 }
125
126 return cons.newInstance(emptyObject);
127 }
128
129 /**
130 * The same as {@link #create0} but with an error handling to make
131 * the instantiation error fatal.
132 */
133 public static <T> T create( Class<T> clazz ) {
134 try {
135 return create0(clazz);
136 } catch (InstantiationException e) {
137 logger.log(Level.INFO,"failed to create a new instance of "+clazz,e);
138 throw new InstantiationError(e.toString());
139 } catch (IllegalAccessException e) {
140 logger.log(Level.INFO,"failed to create a new instance of "+clazz,e);
141 throw new IllegalAccessError(e.toString());
142 } catch (InvocationTargetException e) {
143 Throwable target = e.getTargetException();
144
145 // most likely an error on the user's code.
146 // just let it through for the ease of debugging
147 if(target instanceof RuntimeException)
148 throw (RuntimeException)target;
|
1 /*
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.
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
70 public static void cleanCache() {
71 if (tls != null) {
72 try {
73 tls.remove();
74 } catch (Exception e) {
75 logger.log(Level.WARNING, "Unable to clean Thread Local cache of classes used in Unmarshaller: {0}", e.getLocalizedMessage());
76 }
77 }
78 }
79
80 /**
81 * Creates a new instance of the class but throw exceptions without catching it.
82 */
83 public static <T> T create0( final Class<T> clazz ) throws IllegalAccessException, InvocationTargetException, InstantiationException {
84 Map<Class,WeakReference<Constructor>> m = tls.get();
85 Constructor<T> cons = null;
86 WeakReference<Constructor> consRef = m.get(clazz);
87 if(consRef!=null)
88 cons = consRef.get();
89 if(cons==null) {
90 if (System.getSecurityManager() == null) {
91 cons = tryGetDeclaredConstructor(clazz);
92 } else {
93 cons = AccessController.doPrivileged(new PrivilegedAction<Constructor<T>>() {
94 @Override
95 public Constructor<T> run() {
96 return tryGetDeclaredConstructor(clazz);
97 }
98 });
99 }
100
101 int classMod = clazz.getModifiers();
102
103 if(!Modifier.isPublic(classMod) || !Modifier.isPublic(cons.getModifiers())) {
104 // attempt to make it work even if the constructor is not accessible
105 try {
106 cons.setAccessible(true);
107 } catch(SecurityException e) {
108 // but if we don't have a permission to do so, work gracefully.
109 logger.log(Level.FINE,"Unable to make the constructor of "+clazz+" accessible",e);
110 throw e;
111 }
112 }
113
114 m.put(clazz,new WeakReference<Constructor>(cons));
115 }
116
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 }
135 }
136
137 /**
138 * The same as {@link #create0} but with an error handling to make
139 * the instantiation error fatal.
140 */
141 public static <T> T create( Class<T> clazz ) {
142 try {
143 return create0(clazz);
144 } catch (InstantiationException e) {
145 logger.log(Level.INFO,"failed to create a new instance of "+clazz,e);
146 throw new InstantiationError(e.toString());
147 } catch (IllegalAccessException e) {
148 logger.log(Level.INFO,"failed to create a new instance of "+clazz,e);
149 throw new IllegalAccessError(e.toString());
150 } catch (InvocationTargetException e) {
151 Throwable target = e.getTargetException();
152
153 // most likely an error on the user's code.
154 // just let it through for the ease of debugging
155 if(target instanceof RuntimeException)
156 throw (RuntimeException)target;
|