Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable setUnmarshalListener on XMLBinder #2283

Open
plutext opened this issue Oct 15, 2024 · 4 comments
Open

Enable setUnmarshalListener on XMLBinder #2283

plutext opened this issue Oct 15, 2024 · 4 comments

Comments

@plutext
Copy link

plutext commented Oct 15, 2024

This has been possible in the past via reflection:

		// get xmlBinder field
		org.eclipse.persistence.jaxb.JAXBBinder jaxbBinder = (JAXBBinder)binder; 
		Field f = jaxbBinder.getClass().getDeclaredField("xmlBinder"); //NoSuchFieldException
		f.setAccessible(true);
		XMLBinder binderImpl = (XMLBinder)f.get(jaxbBinder);
		
		Field f2 = binderImpl.getClass().getDeclaredField("unmarshaller"); //NoSuchFieldException
		f2.setAccessible(true);
		org.eclipse.persistence.oxm.XMLUnmarshaller u  = (XMLUnmarshaller)f2.get(binderImpl);	
		u.setUnmarshalListener(moxyListener);
		
		// there is also SAXUnmarshaller, which wraps an unmarshaller
		Field f3 = binderImpl.getClass().getDeclaredField("saxUnmarshaller"); //NoSuchFieldException
		f3.setAccessible(true);
		org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller saxUnmarshaller  = (SAXUnmarshaller)f3.get(binderImpl);
		
		Field f4 = saxUnmarshaller.getClass().getDeclaredField("xmlUnmarshaller"); //NoSuchFieldException
		f4.setAccessible(true);
		org.eclipse.persistence.internal.oxm.Unmarshaller u2  = (org.eclipse.persistence.internal.oxm.Unmarshaller)f2.get(binderImpl);	
		u2.setUnmarshalListener(moxyListener);

but these days is more likely to fail:

java.lang.reflect.InaccessibleObjectException: Unable to make field private org.eclipse.persistence.oxm.XMLBinder org.eclipse.persistence.jaxb.JAXBBinder.xmlBinder accessible: module org.eclipse.persistence.moxy does not "opens org.eclipse.persistence.jaxb" to module org.docx4j.JAXB_MOXy
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
	at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
	at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
@Tomas-Kraus
Copy link
Member

Tomas-Kraus commented Jan 10, 2025

bug-2283.tar.gz

I made small application to run this reflection piece of code and it works fine with EclipseLink 4.0.4.

Your exception is related to Java modules. But this can be solved during Java VM startup. --add-opens parameter may help.
Also opens in module-info may do the trick, but we do not want to add this directive to org.eclipse.persistence.moxy module. There is a reason why this was disabled in Java module system.

I don't consider this as a bug.

@Tomas-Kraus
Copy link
Member

Closing.

@Tomas-Kraus Tomas-Kraus closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2025
@rfelcman rfelcman reopened this Jan 15, 2025
@rfelcman
Copy link
Contributor

rfelcman commented Jan 15, 2025

Before final close I'd like ask You in relation with
java.lang.reflect.InaccessibleObjectException: Unable to make field private org.eclipse.persistence.oxm.XMLBinder org.eclipse.persistence.jaxb.JAXBBinder.xmlBinder accessible: module org.eclipse.persistence.moxy does not "opens org.eclipse.persistence.jaxb" to module org.docx4j.JAXB_MOXy message.
Which JPMS settings are You using in Your envirnoment/application? I mean module-info.java or JPMS parameters passed to Java like mentioned --add-opens.
Another question is about used EclipseLink version and EclipseLink artifacts used in Your project e.g. <artifactId>eclipselink</artifactId> or <artifactId>moxy-standalone</artifactId> or <artifactId>org.eclipse.persistence.moxy</artifactId>?

@plutext
Copy link
Author

plutext commented Jan 15, 2025

This application is the library docx4j: https://github.com/plutext/docx4j

docx4j is an open source (Apache v2) library for creating, editing, and saving OpenXML "packages", including docx, pptx, and xslx. It uses JAXB to create the Java representation; the user can choose whether they wish to use MOXy or jaxb-ri.

To use MOXY, they include artifact https://search.maven.org/artifact/org.docx4j/docx4j-JAXB-MOXy/11.5.1/jar

You can see there the current release is using:

    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>org.eclipse.persistence.moxy</artifactId>
      <version>4.0.4</version>
    </dependency>

We have, at https://github.com/plutext/docx4j/blob/VERSION_11_5_2/docx4j-JAXB-MOXy/src/main/java/module-info.java

module org.docx4j.JAXB_MOXy {
	
	requires org.slf4j;
	requires org.docx4j.core;
	
//	requires transitive jakarta.xml.bind;
	requires transitive org.eclipse.persistence.moxy;
	requires com.sun.tools.xjc;
	requires org.eclipse.persistence.core; // required for MOXy 3.0.1; not necessary with 4.0.2, but doesn't hurt, so leave it here
	
    exports org.docx4j.jaxb.moxy;
    
    opens org.docx4j.jaxb.moxy;	
	
}

Although it is possible to workaround this issue by launching the VM with:

			--add-opens org.eclipse.persistence.moxy/org.eclipse.persistence.jaxb=org.docx4j.JAXB_MOXy
			--add-opens org.eclipse.persistence.core/org.eclipse.persistence.oxm=org.docx4j.JAXB_MOXy
			--add-opens org.eclipse.persistence.core/org.eclipse.persistence.internal.oxm.record=org.docx4j.JAXB_MOXy	

this is obviously not desirable for a library which is deployed in a variety of environments.

On a related note, please see https://stackoverflow.com/questions/79346466/classcastexception-across-modules-with-org-eclipse-persistence-oxm-xmlbinder which a user has raised regarding org.eclipse.persistence.jaxb.JAXBBinder.updateXML

I wasn't able to work around that issue using --add-opens

thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants