|
40 | 40 | import org.objectweb.asm.ClassReader;
|
41 | 41 | import org.objectweb.asm.ClassVisitor;
|
42 | 42 | import org.objectweb.asm.ClassWriter;
|
| 43 | +import org.objenesis.Objenesis; |
| 44 | +import org.objenesis.ObjenesisStd; |
43 | 45 | import org.slf4j.Logger;
|
44 | 46 | import org.slf4j.LoggerFactory;
|
45 | 47 |
|
46 |
| -import sun.reflect.ReflectionFactory; |
| 48 | +//import sun.reflect.ReflectionFactory; |
47 | 49 |
|
48 | 50 | @SuppressWarnings("restriction")
|
49 | 51 | public class ProxySubclassGenerator
|
@@ -170,31 +172,16 @@ public static Object newProxySubclassInstance(Class<?> classToProxy, ClassLoader
|
170 | 172 | try {
|
171 | 173 | Class<?> generatedProxySubclass = getProxySubclass(classToProxy, loader);
|
172 | 174 | LOGGER.debug("Getting the proxy subclass constructor");
|
173 |
| - // Because the newer JVMs throw a VerifyError if a class attempts to in a constructor other than their superclasses constructor, |
174 |
| - // and because we can't know what objects/values we need to pass into the class being proxied constructor, |
175 |
| - // we instantiate the proxy class using the ReflectionFactory.newConstructorForSerialization() method which allows us to instantiate the |
176 |
| - // proxy class without calling the proxy class' constructor. It is in fact using the java.lang.Object constructor so is in effect |
177 |
| - // doing what we were doing before. |
178 |
| - ReflectionFactory factory = ReflectionFactory.getReflectionFactory(); |
179 |
| - Constructor<?> constr = Object.class.getConstructor(); |
180 |
| - Constructor<?> subclassConstructor = factory.newConstructorForSerialization(generatedProxySubclass, constr); |
181 |
| - proxySubclassInstance = subclassConstructor.newInstance(); |
182 |
| - |
| 175 | + // Simple newInstance or constructor call cannot be used since constructor may be private |
| 176 | + // or we may not know constructor parameters |
| 177 | + Objenesis objenesis = new ObjenesisStd(); |
| 178 | + proxySubclassInstance = objenesis.newInstance(generatedProxySubclass);; |
183 | 179 | Method setIHMethod = proxySubclassInstance.getClass().getMethod("setInvocationHandler", InvocationHandler.class);
|
184 | 180 | setIHMethod.invoke(proxySubclassInstance, ih);
|
185 | 181 | LOGGER.debug("Invoked proxy subclass constructor");
|
186 |
| - } catch (NoSuchMethodException nsme) { |
187 |
| - LOGGER.debug(Constants.LOG_EXCEPTION, nsme); |
188 |
| - throw new ProxyClassInstantiationException(classToProxy, nsme); |
189 |
| - } catch (InvocationTargetException ite) { |
190 |
| - LOGGER.debug(Constants.LOG_EXCEPTION, ite); |
191 |
| - throw new ProxyClassInstantiationException(classToProxy, ite); |
192 |
| - } catch (InstantiationException ie) { |
193 |
| - LOGGER.debug(Constants.LOG_EXCEPTION, ie); |
194 |
| - throw new ProxyClassInstantiationException(classToProxy, ie); |
195 |
| - } catch (IllegalAccessException iae) { |
196 |
| - LOGGER.debug(Constants.LOG_EXCEPTION, iae); |
197 |
| - throw new ProxyClassInstantiationException(classToProxy, iae); |
| 182 | + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
| 183 | + LOGGER.debug(Constants.LOG_EXCEPTION, e); |
| 184 | + throw new ProxyClassInstantiationException(classToProxy, e); |
198 | 185 | } catch (VerifyError ve) {
|
199 | 186 | LOGGER.info(String.format("The no-argument constructor of class %s is private and therefore it may not be possible to generate a valid proxy.",
|
200 | 187 | classToProxy));
|
|
0 commit comments