Skip to content

Commit 1d27942

Browse files
committed
ARIES-2165: Create proxied object with objenesis
1 parent 05bfb7f commit 1d27942

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

proxy/proxy-impl/pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
org.apache.aries.proxy*;provide:=true,
5959
org.objectweb.asm*;resolution:=optional;version="[9,10)",
6060
sun.reflect*;resolution:=optional,
61+
org.objenesis*;resolution:=optional,
6162
*
6263
</aries.osgi.import.pkg>
6364
<aries.osgi.private.pkg>
@@ -70,6 +71,7 @@
7071
<geronimo-j2ee-connector_1.6_spec.version>1.0</geronimo-j2ee-connector_1.6_spec.version>
7172
<javax.transaction-api.version>1.3</javax.transaction-api.version>
7273
<mockito-core.version>3.7.7</mockito-core.version>
74+
<objenesis.version>3.4</objenesis.version>
7375
<org.apache.aries.proxy.api.version>1.1.2-SNAPSHOT</org.apache.aries.proxy.api.version>
7476
</properties>
7577

@@ -80,6 +82,12 @@
8082
<version>${org.apache.aries.proxy.api.version}</version>
8183
<scope>provided</scope>
8284
</dependency>
85+
<dependency>
86+
<groupId>org.objenesis</groupId>
87+
<artifactId>objenesis</artifactId>
88+
<version>${objenesis.version}</version>
89+
<optional>true</optional>
90+
</dependency>
8391
<dependency>
8492
<groupId>org.ow2.asm</groupId>
8593
<artifactId>asm</artifactId>

proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/gen/ProxySubclassGenerator.java

+9-22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.objectweb.asm.ClassReader;
4141
import org.objectweb.asm.ClassVisitor;
4242
import org.objectweb.asm.ClassWriter;
43+
import org.objenesis.Objenesis;
44+
import org.objenesis.ObjenesisStd;
4345
import org.slf4j.Logger;
4446
import org.slf4j.LoggerFactory;
4547

@@ -170,31 +172,16 @@ public static Object newProxySubclassInstance(Class<?> classToProxy, ClassLoader
170172
try {
171173
Class<?> generatedProxySubclass = getProxySubclass(classToProxy, loader);
172174
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 = generatedProxySubclass.getConstructor();
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);;
183179
Method setIHMethod = proxySubclassInstance.getClass().getMethod("setInvocationHandler", InvocationHandler.class);
184180
setIHMethod.invoke(proxySubclassInstance, ih);
185181
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);
198185
} catch (VerifyError ve) {
199186
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.",
200187
classToProxy));

proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxySubclassGeneratorTest.java

-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.apache.aries.proxy.impl.gen.ProxySubclassMethodHashSet;
5151
import org.apache.commons.io.IOUtils;
5252
import org.junit.Before;
53-
import org.junit.Ignore;
5453
import org.junit.Test;
5554

5655
/**
@@ -174,7 +173,6 @@ public void testFinalClass() throws Exception
174173
* Test a private constructor
175174
*/
176175
@Test
177-
@Ignore
178176
public void testPrivateConstructor() throws Exception
179177
{
180178
Object o = ProxySubclassGenerator.newProxySubclassInstance(

proxy/proxy-itests/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<org.eclipse.osgi.version>3.22.0</org.eclipse.osgi.version>
5353
<tinybundles.version>2.0.0</tinybundles.version>
5454
<url.version>2.6.16</url.version>
55+
<objenesis.version>3.4</objenesis.version>
5556
</properties>
5657

5758
<dependencies>
@@ -72,6 +73,12 @@
7273
<scope>test</scope>
7374
<version>${org.apache.aries.proxy.version}</version>
7475
</dependency>
76+
<dependency>
77+
<groupId>org.objenesis</groupId>
78+
<artifactId>objenesis</artifactId>
79+
<version>${objenesis.version}</version>
80+
<scope>test</scope>
81+
</dependency>
7582
<dependency>
7683
<groupId>org.osgi</groupId>
7784
<artifactId>org.osgi.compendium</artifactId>

proxy/proxy-itests/src/test/java/org/apache/aries/proxy/itests/AbstractProxyTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ protected Option proxyOptions() {
301301
mavenBundle("org.ow2.asm", "asm").versionAsInProject(),
302302
mavenBundle("org.ow2.asm", "asm-tree").versionAsInProject(),
303303
mavenBundle("org.ow2.asm", "asm-analysis").versionAsInProject(),
304+
mavenBundle("org.objenesis", "objenesis").versionAsInProject(),
304305
mavenBundle("org.ops4j.pax.logging", "pax-logging-api").versionAsInProject(),
305306
mavenBundle("org.ops4j.pax.logging", "pax-logging-service").versionAsInProject(),
306307
mavenBundle("org.apache.aries.proxy", "org.apache.aries.proxy.api").versionAsInProject(),

0 commit comments

Comments
 (0)