jboss-javassist/javassist

In 3.3.3 (and before) compileExpression fails without add-opens on Java 17

petergeneric opened this issue · 1 comments

When Ognl.compileExpression calls ExpressionCompiler.compileExpression on Java 17 with 3.3.3 which uses the latest javassist, compile fails unless add-opens args have been added to the Java commandline.

The exception arises from this line in ExpressionCompiler:

Class clazz = pool.toClass(newClass);

I think this can be avoided - Javassist has an overload for ClassPool.toClass that lets you define a Neighbour Class, at which point it will use the new Java MethodHandles approach to define the class rather than the legacy ClassLoader.defineClass (which requires visibility changes, which is what fails without add-opens). If I instead change that line in ExpressionCompiler to:

Class clazz = pool.toClass(newClass, OgnlContext.class);

Then the expression compiles and works perfectly under Java 17.

I don't know for sure that it's safe to use OgnlContext.class as a general purpose neighbour class (don't know if MethodHandles defineClass is different in some fundamental way to the ClassLoader defineClass, and therefore how it would interact with other environments that make more extensive use of Modules), so could we have a way to opt-in to it, perhaps the ability to pass a neighbour class in through an Ognl.compileExpression overload (either a class or a flag that would have the internal code pass OgnlContext.class as the neighbour) -- or even some public static flag on ExpressionCompiler?

Sorry, raised against wrong project!