Skip to content

Commit f44ad3b

Browse files
committed
ARIES-2165: Create proxied object with objenesis
1 parent 46b1ca7 commit f44ad3b

File tree

6 files changed

+153
-63
lines changed

6 files changed

+153
-63
lines changed

blueprint/itests/blueprint-itests/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<commons-jexl.version>2.1.1</commons-jexl.version>
5757
<depends-maven-plugin.version>1.5.0</depends-maven-plugin.version>
5858
<maven-surefire-plugin.version>3.5.2</maven-surefire-plugin.version>
59+
<objenesis.version>3.4</objenesis.version>
5960
<org.apache.aries.proxy.version>1.1.15-SNAPSHOT</org.apache.aries.proxy.version>
6061
<org.apache.aries.quiesce.api.version>1.0.1-SNAPSHOT</org.apache.aries.quiesce.api.version>
6162
<org.apache.aries.testsupport.unit.version>2.0.0-SNAPSHOT</org.apache.aries.testsupport.unit.version>
@@ -107,6 +108,12 @@
107108
<artifactId>org.apache.aries.proxy</artifactId>
108109
<version>${org.apache.aries.proxy.version}</version>
109110
</dependency>
111+
<dependency>
112+
<groupId>org.objenesis</groupId>
113+
<artifactId>objenesis</artifactId>
114+
<version>${objenesis.version}</version>
115+
<scope>test</scope>
116+
</dependency>
110117
<dependency>
111118
<groupId>org.apache.servicemix.bundles</groupId>
112119
<artifactId>org.apache.servicemix.bundles.spring-context</artifactId>

blueprint/itests/blueprint-itests/src/test/java/org/apache/aries/blueprint/itests/Helper.java

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static Option blueprintBundles(boolean startBlueprint) {
6565
mvnBundle("org.apache.felix", "org.apache.felix.configadmin"),
6666
mvnBundle("org.ops4j.pax.url", "pax-url-aether"),
6767
mvnBundle("org.apache.aries.testsupport", "org.apache.aries.testsupport.unit"),
68+
mvnBundle("org.objenesis", "objenesis"),
6869
mvnBundle("org.apache.aries.proxy", "org.apache.aries.proxy"),
6970
mvnBundle("org.apache.commons", "commons-jexl"),
7071
mvnBundle("org.apache.aries.blueprint", "org.apache.aries.blueprint.jexl.evaluator"),

proxy/proxy-impl/pom.xml

+95-27
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>
@@ -147,33 +155,6 @@
147155

148156
<build>
149157
<plugins>
150-
<plugin>
151-
<groupId>org.apache.maven.plugins</groupId>
152-
<artifactId>maven-compiler-plugin</artifactId>
153-
</plugin>
154-
<plugin>
155-
<groupId>org.apache.maven.plugins</groupId>
156-
<artifactId>maven-antrun-plugin</artifactId>
157-
<executions>
158-
<execution>
159-
<phase>process-classes</phase>
160-
<configuration>
161-
<target>
162-
<!-- run the Synthesizer on the WovenProxy class -->
163-
<java classname="org.apache.aries.proxy.synthesizer.Synthesizer" classpathref="maven.runtime.classpath" fork="false">
164-
<arg value="${project.build.outputDirectory}/org/apache/aries/proxy/weaving/WovenProxy.class"/>
165-
</java>
166-
167-
<delete dir="${project.build.outputDirectory}/org/apache/aries/proxy/synthesizer"/>
168-
169-
</target>
170-
</configuration>
171-
<goals>
172-
<goal>run</goal>
173-
</goals>
174-
</execution>
175-
</executions>
176-
</plugin>
177158
<plugin>
178159
<groupId>org.apache.aries.versioning</groupId>
179160
<artifactId>org.apache.aries.versioning.plugin</artifactId>
@@ -211,4 +192,91 @@
211192
</plugins>
212193
</build>
213194

195+
<profiles>
196+
<profile>
197+
<id>java9To21</id>
198+
<activation>
199+
<jdk>(8,22)</jdk>
200+
</activation>
201+
<build>
202+
<plugins>
203+
<plugin>
204+
<groupId>org.apache.maven.plugins</groupId>
205+
<artifactId>maven-surefire-plugin</artifactId>
206+
<configuration>
207+
<!-- necessary for making method defineClass accessible -->
208+
<argLine>
209+
--add-opens java.base/java.lang=ALL-UNNAMED
210+
</argLine>
211+
</configuration>
212+
</plugin>
213+
</plugins>
214+
</build>
215+
</profile>
216+
<profile>
217+
<id>java8To17</id>
218+
<activation>
219+
<jdk>[1.8,18)</jdk>
220+
</activation>
221+
<build>
222+
<plugins>
223+
<plugin>
224+
<groupId>org.apache.maven.plugins</groupId>
225+
<artifactId>maven-antrun-plugin</artifactId>
226+
<executions>
227+
<execution>
228+
<phase>process-classes</phase>
229+
<configuration>
230+
<target>
231+
<!-- run the Synthesizer on the WovenProxy class -->
232+
<java classname="org.apache.aries.proxy.synthesizer.Synthesizer" classpathref="maven.runtime.classpath" fork="false">
233+
<arg value="${project.build.outputDirectory}/org/apache/aries/proxy/weaving/WovenProxy.class"/>
234+
</java>
235+
<delete dir="${project.build.outputDirectory}/org/apache/aries/proxy/synthesizer"/>
236+
</target>
237+
</configuration>
238+
<goals>
239+
<goal>run</goal>
240+
</goals>
241+
</execution>
242+
</executions>
243+
</plugin>
244+
</plugins>
245+
</build>
246+
</profile>
247+
<profile>
248+
<id>java21+</id>
249+
<activation>
250+
<jdk>21</jdk>
251+
</activation>
252+
<build>
253+
<plugins>
254+
<plugin>
255+
<groupId>org.apache.maven.plugins</groupId>
256+
<artifactId>maven-antrun-plugin</artifactId>
257+
<executions>
258+
<execution>
259+
<phase>process-classes</phase>
260+
<configuration>
261+
<target>
262+
<!-- run the Synthesizer on the WovenProxy class -->
263+
<!-- diff from previous java versions: fork and jvmarg -->
264+
<java classname="org.apache.aries.proxy.synthesizer.Synthesizer" classpathref="maven.runtime.classpath" fork="true">
265+
<jvmarg value="-Djava.security.manager=allow" />
266+
<arg value="${project.build.outputDirectory}/org/apache/aries/proxy/weaving/WovenProxy.class"/>
267+
</java>
268+
<delete dir="${project.build.outputDirectory}/org/apache/aries/proxy/synthesizer"/>
269+
</target>
270+
</configuration>
271+
<goals>
272+
<goal>run</goal>
273+
</goals>
274+
</execution>
275+
</executions>
276+
</plugin>
277+
</plugins>
278+
</build>
279+
</profile>
280+
</profiles>
281+
214282
</project>

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

+10-23
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
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

46-
import sun.reflect.ReflectionFactory;
48+
//import sun.reflect.ReflectionFactory;
4749

4850
@SuppressWarnings("restriction")
4951
public class ProxySubclassGenerator
@@ -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 = 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);;
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-itests/pom.xml

+39-13
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<asm.version>9.7.1</asm.version>
4747
<depends-maven-plugin.version>1.5.0</depends-maven-plugin.version>
4848
<javax.inject.version>1</javax.inject.version>
49-
<logback.version>1.5.16</logback.version>
49+
<objenesis.version>3.4</objenesis.version>
5050
<org.apache.aries.proxy.api.version>1.1.2-SNAPSHOT</org.apache.aries.proxy.api.version>
5151
<org.apache.aries.proxy.version>1.1.15-SNAPSHOT</org.apache.aries.proxy.version>
5252
<org.apache.aries.testsupport.unit.version>2.0.0-SNAPSHOT</org.apache.aries.testsupport.unit.version>
@@ -65,11 +65,6 @@
6565
<version>${org.eclipse.osgi.version}</version>
6666
<scope>provided</scope>
6767
</dependency>
68-
<dependency>
69-
<groupId>org.osgi</groupId>
70-
<artifactId>org.osgi.compendium</artifactId>
71-
<scope>provided</scope>
72-
</dependency>
7368
<dependency>
7469
<groupId>org.apache.aries.proxy</groupId>
7570
<artifactId>org.apache.aries.proxy.api</artifactId>
@@ -79,8 +74,24 @@
7974
<dependency>
8075
<groupId>org.apache.aries.proxy</groupId>
8176
<artifactId>org.apache.aries.proxy</artifactId>
82-
<scope>test</scope>
8377
<version>${org.apache.aries.proxy.version}</version>
78+
<scope>test</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.objenesis</groupId>
82+
<artifactId>objenesis</artifactId>
83+
<version>${objenesis.version}</version>
84+
<scope>test</scope>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.osgi</groupId>
88+
<artifactId>org.osgi.compendium</artifactId>
89+
<scope>provided</scope>
90+
</dependency>
91+
<dependency>
92+
<groupId>junit</groupId>
93+
<artifactId>junit</artifactId>
94+
<scope>provided</scope>
8495
</dependency>
8596
<dependency>
8697
<groupId>org.apache.aries.testsupport</groupId>
@@ -90,7 +101,7 @@
90101
</dependency>
91102

92103
<!-- pax exam -->
93-
<dependency>
104+
<dependency>
94105
<groupId>org.ops4j.pax.exam</groupId>
95106
<artifactId>pax-exam</artifactId>
96107
<version>${pax-exam.version}</version>
@@ -125,11 +136,6 @@
125136
<artifactId>tinybundles</artifactId>
126137
<version>${pax-tinybundles.version}</version>
127138
</dependency>
128-
<dependency>
129-
<groupId>junit</groupId>
130-
<artifactId>junit</artifactId>
131-
<scope>test</scope>
132-
</dependency>
133139

134140
<dependency>
135141
<groupId>org.ow2.asm</groupId>
@@ -192,4 +198,24 @@
192198
</plugin>
193199
</plugins>
194200
</build>
201+
202+
<profiles>
203+
<profile>
204+
<id>java9To21</id>
205+
<activation>
206+
<jdk>(8,22)</jdk>
207+
</activation>
208+
<build>
209+
<plugins>
210+
<plugin>
211+
<groupId>org.apache.maven.plugins</groupId>
212+
<artifactId>maven-surefire-plugin</artifactId>
213+
<configuration>
214+
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
215+
</configuration>
216+
</plugin>
217+
</plugins>
218+
</build>
219+
</profile>
220+
</profiles>
195221
</project>

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

+1
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ protected Option proxyOptions() {
294294
setPaxExamLogLevel("INFO"),
295295
configurePaxUrlLocalMavenRepoIfNeeded(),
296296
addAsmBundles(),
297+
mavenBundle("org.objenesis", "objenesis").versionAsInProject(),
297298
mavenBundle("org.apache.aries.testsupport", "org.apache.aries.testsupport.unit").versionAsInProject(),
298299
mavenBundle("org.apache.aries.proxy", "org.apache.aries.proxy.api").versionAsInProject(),
299300
mavenBundle("org.apache.aries.proxy", "org.apache.aries.proxy").versionAsInProject()

0 commit comments

Comments
 (0)