Skip to content

Commit c016b37

Browse files
committed
ARIES-2165: Create proxied object with objenesis
1 parent b9b246d commit c016b37

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

proxy/proxy-impl/pom.xml

Lines changed: 8 additions & 0 deletions
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

Lines changed: 7 additions & 14 deletions
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

@@ -177,24 +179,15 @@ public static Object newProxySubclassInstance(Class<?> classToProxy, ClassLoader
177179
// doing what we were doing before.
178180
// ReflectionFactory factory = ReflectionFactory.getReflectionFactory();
179181
// Constructor<?> constr = Object.class.getConstructor();
180-
Constructor<?> subclassConstructor = generatedProxySubclass.getConstructor();
181-
proxySubclassInstance = subclassConstructor.newInstance();
182+
Objenesis objenesis = new ObjenesisStd();
183+
proxySubclassInstance = objenesis.newInstance(generatedProxySubclass);;
182184

183185
Method setIHMethod = proxySubclassInstance.getClass().getMethod("setInvocationHandler", InvocationHandler.class);
184186
setIHMethod.invoke(proxySubclassInstance, ih);
185187
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);
188+
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
189+
LOGGER.debug(Constants.LOG_EXCEPTION, e);
190+
throw new ProxyClassInstantiationException(classToProxy, e);
198191
} catch (VerifyError ve) {
199192
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.",
200193
classToProxy));

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ public void testFinalClass() throws Exception
174174
* Test a private constructor
175175
*/
176176
@Test
177-
@Ignore
178177
public void testPrivateConstructor() throws Exception
179178
{
180179
Object o = ProxySubclassGenerator.newProxySubclassInstance(

proxy/proxy-itests/pom.xml

Lines changed: 7 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)