Issues

Select view

Select search mode

 
50 of 89

HqlLexer can cause ClassNotFound in OSGi environment

Out of Date

Description

org.hibernate.hql.ast.HqlLexer can cause a ClassNotFound exception in an OSGi environment with a stacktrace like

org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken [hql query here] at org.hibernate.hql.ast.HqlLexer.panic(HqlLexer.java:80) at antlr.CharScanner.setTokenObjectClass(CharScanner.java:338) at org.hibernate.hql.ast.HqlLexer.setTokenObjectClass(HqlLexer.java:54) ...

The HqlLexer class has antlr.CharScanner as a superclass, as evidenced by the stacktrace. The contents of the method are:

public void setTokenObjectClass(String cl) { // Ignore the token class name parameter, and use a specific token class. super.setTokenObjectClass( HqlToken.class.getName() ); }

It passes the name of the HqlToken class as a string to CharScanner, which then ultimately attempts to find HqlToken via antlr.Utils. The tokenObjectClass is a protected field. The code can be simplified by simply setting the field directly to HqlToken.class which is obviously already available to the HqlLexer class.

The reason this causes the exception in an OSGi environment is that the context class loader available to ANTLR is limited to what is explicitly declared in the ANTLR bundle. Since ANTLR cannot (and should not) have to know about all the classes that it may be passed, it does not declare an import on the org.hibernate.hql.ast package. By passing the class directly, the lookup is avoided from within the ANTLR package.

public void setTokenObjectClass(String cl) { // Ignore the token class name parameter, and use a specific token class. tokenObjectClass = HqlToken.class; }

Attachments

1
  • 28 Apr 2011, 05:15 PM

Details

Assignee

Reporter

Components

Priority

Created April 28, 2011 at 5:15 PM
Updated February 12, 2013 at 10:37 PM
Resolved February 12, 2013 at 10:37 PM

Activity

Show:

Brett Meyer February 12, 2013 at 10:36 PM

Marking out-of-date. Will be addressed in HHH-7991.

Brett Meyer February 12, 2013 at 9:49 PM
Edited

Scott, this patch might not work with OSGi class loading. I'm not sure if the runtime classloading will like it. Keep it on the radar.

Scott Marlow August 3, 2011 at 3:22 PM

I like your patch but I did mine differently in HHH-6536. One question that I have about your patch, can it be made to use an antlr api instead of referencing a protected variable directly? Is the protected tokenObjectClass documented as being safe to change (in terms of future releases of antlr including the same variable)?

In my patch, I set the thread context classloader to HqlToken.class.getClassLoader(). Would that work for your case also?