Skip to content

Commit cc2c39d

Browse files
committed
ARIES-2165: Create proxied object with objenesis
1 parent c3091f3 commit cc2c39d

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>
@@ -71,6 +72,7 @@
7172
<geronimo-j2ee-connector_1.6_spec.version>1.0</geronimo-j2ee-connector_1.6_spec.version>
7273
<javax.transaction-api.version>1.3</javax.transaction-api.version>
7374
<mockito-core.version>3.7.7</mockito-core.version>
75+
<objenesis.version>3.4</objenesis.version>
7476
<org.apache.aries.proxy.api.version>1.1.2-SNAPSHOT</org.apache.aries.proxy.api.version>
7577
</properties>
7678

@@ -81,6 +83,12 @@
8183
<version>${org.apache.aries.proxy.api.version}</version>
8284
<scope>provided</scope>
8385
</dependency>
86+
<dependency>
87+
<groupId>org.objenesis</groupId>
88+
<artifactId>objenesis</artifactId>
89+
<version>${objenesis.version}</version>
90+
<optional>true</optional>
91+
</dependency>
8492
<dependency>
8593
<groupId>org.ow2.asm</groupId>
8694
<artifactId>asm</artifactId>
@@ -148,33 +156,6 @@
148156

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

196+
<profiles>
197+
<profile>
198+
<id>java9To21</id>
199+
<activation>
200+
<jdk>(8,22)</jdk>
201+
</activation>
202+
<build>
203+
<plugins>
204+
<plugin>
205+
<groupId>org.apache.maven.plugins</groupId>
206+
<artifactId>maven-surefire-plugin</artifactId>
207+
<configuration>
208+
<!-- necessary for making method defineClass accessible -->
209+
<argLine>
210+
--add-opens java.base/java.lang=ALL-UNNAMED
211+
</argLine>
212+
</configuration>
213+
</plugin>
214+
</plugins>
215+
</build>
216+
</profile>
217+
<profile>
218+
<id>java8To17</id>
219+
<activation>
220+
<jdk>[1.8,18)</jdk>
221+
</activation>
222+
<build>
223+
<plugins>
224+
<plugin>
225+
<groupId>org.apache.maven.plugins</groupId>
226+
<artifactId>maven-antrun-plugin</artifactId>
227+
<executions>
228+
<execution>
229+
<phase>process-classes</phase>
230+
<configuration>
231+
<target>
232+
<!-- run the Synthesizer on the WovenProxy class -->
233+
<java classname="org.apache.aries.proxy.synthesizer.Synthesizer" classpathref="maven.runtime.classpath" fork="false">
234+
<arg value="${project.build.outputDirectory}/org/apache/aries/proxy/weaving/WovenProxy.class"/>
235+
</java>
236+
<delete dir="${project.build.outputDirectory}/org/apache/aries/proxy/synthesizer"/>
237+
</target>
238+
</configuration>
239+
<goals>
240+
<goal>run</goal>
241+
</goals>
242+
</execution>
243+
</executions>
244+
</plugin>
245+
</plugins>
246+
</build>
247+
</profile>
248+
<profile>
249+
<id>java21+</id>
250+
<activation>
251+
<jdk>21</jdk>
252+
</activation>
253+
<build>
254+
<plugins>
255+
<plugin>
256+
<groupId>org.apache.maven.plugins</groupId>
257+
<artifactId>maven-antrun-plugin</artifactId>
258+
<executions>
259+
<execution>
260+
<phase>process-classes</phase>
261+
<configuration>
262+
<target>
263+
<!-- run the Synthesizer on the WovenProxy class -->
264+
<!-- diff from previous java versions: fork and jvmarg -->
265+
<java classname="org.apache.aries.proxy.synthesizer.Synthesizer" classpathref="maven.runtime.classpath" fork="true">
266+
<jvmarg value="-Djava.security.manager=allow" />
267+
<arg value="${project.build.outputDirectory}/org/apache/aries/proxy/weaving/WovenProxy.class"/>
268+
</java>
269+
<delete dir="${project.build.outputDirectory}/org/apache/aries/proxy/synthesizer"/>
270+
</target>
271+
</configuration>
272+
<goals>
273+
<goal>run</goal>
274+
</goals>
275+
</execution>
276+
</executions>
277+
</plugin>
278+
</plugins>
279+
</build>
280+
</profile>
281+
</profiles>
282+
215283
</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
@@ -48,7 +48,7 @@
4848
<asm.version>9.7.1</asm.version>
4949
<depends-maven-plugin.version>1.5.0</depends-maven-plugin.version>
5050
<javax.inject.version>1</javax.inject.version>
51-
<logback.version>1.5.16</logback.version>
51+
<objenesis.version>3.4</objenesis.version>
5252
<org.apache.aries.proxy.api.version>1.1.2-SNAPSHOT</org.apache.aries.proxy.api.version>
5353
<org.apache.aries.proxy.version>1.1.15-SNAPSHOT</org.apache.aries.proxy.version>
5454
<org.apache.aries.testsupport.unit.version>2.0.0-SNAPSHOT</org.apache.aries.testsupport.unit.version>
@@ -67,11 +67,6 @@
6767
<version>${org.eclipse.osgi.version}</version>
6868
<scope>provided</scope>
6969
</dependency>
70-
<dependency>
71-
<groupId>org.osgi</groupId>
72-
<artifactId>org.osgi.compendium</artifactId>
73-
<scope>provided</scope>
74-
</dependency>
7570
<dependency>
7671
<groupId>org.apache.aries.proxy</groupId>
7772
<artifactId>org.apache.aries.proxy.api</artifactId>
@@ -81,8 +76,24 @@
8176
<dependency>
8277
<groupId>org.apache.aries.proxy</groupId>
8378
<artifactId>org.apache.aries.proxy</artifactId>
84-
<scope>test</scope>
8579
<version>${org.apache.aries.proxy.version}</version>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.objenesis</groupId>
84+
<artifactId>objenesis</artifactId>
85+
<version>${objenesis.version}</version>
86+
<scope>test</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.osgi</groupId>
90+
<artifactId>org.osgi.compendium</artifactId>
91+
<scope>provided</scope>
92+
</dependency>
93+
<dependency>
94+
<groupId>junit</groupId>
95+
<artifactId>junit</artifactId>
96+
<scope>provided</scope>
8697
</dependency>
8798
<dependency>
8899
<groupId>org.apache.aries.testsupport</groupId>
@@ -92,7 +103,7 @@
92103
</dependency>
93104

94105
<!-- pax exam -->
95-
<dependency>
106+
<dependency>
96107
<groupId>org.ops4j.pax.exam</groupId>
97108
<artifactId>pax-exam</artifactId>
98109
<version>${pax-exam.version}</version>
@@ -127,11 +138,6 @@
127138
<artifactId>tinybundles</artifactId>
128139
<version>${pax-tinybundles.version}</version>
129140
</dependency>
130-
<dependency>
131-
<groupId>junit</groupId>
132-
<artifactId>junit</artifactId>
133-
<scope>test</scope>
134-
</dependency>
135141

136142
<dependency>
137143
<groupId>org.ow2.asm</groupId>
@@ -194,4 +200,24 @@
194200
</plugin>
195201
</plugins>
196202
</build>
203+
204+
<profiles>
205+
<profile>
206+
<id>java9To21</id>
207+
<activation>
208+
<jdk>(8,22)</jdk>
209+
</activation>
210+
<build>
211+
<plugins>
212+
<plugin>
213+
<groupId>org.apache.maven.plugins</groupId>
214+
<artifactId>maven-surefire-plugin</artifactId>
215+
<configuration>
216+
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
217+
</configuration>
218+
</plugin>
219+
</plugins>
220+
</build>
221+
</profile>
222+
</profiles>
197223
</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)