Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A How can I set a private class instance variable to the File object used by an unmarshaller?

I've found a way to set the private field without exposing it or giving it a setter: Reflection. Using external event listeners, I can get ahold of the File object. Then, inside the beforeUnmarsha...

posted 1mo ago by aura-lsprog-86‭  ·  edited 1mo ago by aura-lsprog-86‭

Answer
#2: Post edited by user avatar aura-lsprog-86‭ · 2025-02-13T05:38:23Z (about 1 month ago)
#1: Initial revision by user avatar aura-lsprog-86‭ · 2025-02-13T05:37:15Z (about 1 month ago)
I've found a way to set the private field without exposing it or giving it a setter: [Reflection](http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html).

Using external event listeners, I can get ahold of the `File` object. Then, inside the `beforeUnmarshal` method, and after checking that I got the correct object, I use reflection to get the private field, and with the `setAccessible` method, I can now control when I get access to the field _using reflection only_.

After lifting the access checks, it's only a matter of editing the value via `set`ting it, and reinstating the checks after that.

# Relevant changes

The following snippet includes the relevant changes:

    unmarshallerObj.setListener(new Unmarshaller.Listener() {
        @Override
        public void beforeUnmarshal(Object target, Object parent) {
            if (!(target instanceof MarshalMe))
                return;

            MarshalMe me = (MarshalMe) target;
            try {
                Field meBackingFile = MarshalMe.class.getDeclaredField("backingFile");
                meBackingFile.setAccessible(true);
                meBackingFile.set(me, xml);
                meBackingFile.setAccessible(false);
            } catch (NoSuchFieldException | IllegalAccessException ex) {
                // Intentionally left blank.
            }
        }
    });

# Including the edit in the sample program
Edit the file `JAXBDemo.java` by adding the following code:

    // Add to the import section
    import java.lang.reflect.Field;

    // Under this function
    public static MarshalMe readXML(File xml) {
        MarshalMe me = null;

        try {
            JAXBContext contextObj = JAXBContext.newInstance(MarshalMe.class);
            Unmarshaller unmarshallerObj = contextObj.createUnmarshaller();

            /* Add this code vvv */
            unmarshallerObj.setListener(new Unmarshaller.Listener() {
                @Override
                public void beforeUnmarshal(Object target, Object parent) {
                    if (!(target instanceof MarshalMe))
                        return;

                    MarshalMe me = (MarshalMe) target;
                    try {
                        Field meBackingFile = MarshalMe.class.getDeclaredField("backingFile");
                        meBackingFile.setAccessible(true);
                        meBackingFile.set(me, xml);
                        meBackingFile.setAccessible(false);
                    } catch (NoSuchFieldException | IllegalAccessException ex) {
                        // Intentionally left blank.
                    }
                }
            });
            /* Add this code ^^^ */

            me = (MarshalMe) unmarshallerObj.unmarshal(xml);
        } catch (JAXBException jaxbe) {
            jaxbe.printStackTrace();
        }

        return me;
    }

After adding the `import` and the code between the `/* Add this code */` lines, running the program again now outputs:

    dummy.hai
    me.xml

as expected.