Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9269905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:14:20+00:00 2026-06-18T15:14:20+00:00

I am using JAXB to create Java objects from XSD file. I am creating

  • 0

I am using JAXB to create Java objects from XSD file. I am creating immutable wrappers to conceal objects generated by JAXB (earlier I was updating JAXB objects to implement immutable interface and return interface to client. But realised it is bad to change auto generated classes, hence using wrappers)

Currently I am returning these immutable wrappers to client app. Is there any option so that auto generated classes will be immutable and it will avoid extra work of creating immutable wrappers. Any other approach is encouraged.

  • Thanks
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:14:21+00:00Added an answer on June 18, 2026 at 3:14 pm

    You can create a Proxy for your beans just before returning them to the client. You will need javassist to create Proxies from classes (create proxies from interfaces can be done with Java SE directly).

    Then, you can throw an exception if methods starting with “set” are invoked.

    Here is a reusable class with a method that can wrap “any” POJO:

    import java.lang.reflect.Method;
    
    import javassist.util.proxy.MethodFilter;
    import javassist.util.proxy.MethodHandler;
    import javassist.util.proxy.Proxy;
    import javassist.util.proxy.ProxyFactory;
    
    public class Utils {
    
     public static <C> C createInmutableBean(Class<C> clazz, final C instance)
            throws InstantiationException, IllegalAccessException {
        if (!clazz.isAssignableFrom(instance.getClass())) {
            throw new IllegalArgumentException("given instance of class "
                    + instance.getClass() + " is not a subclass of " + clazz);
        }
        ProxyFactory f = new ProxyFactory();
        f.setSuperclass(clazz);
        f.setFilter(new MethodFilter() {
            public boolean isHandled(Method m) {
                // ignore finalize()
                return !m.getName().equals("finalize");
            }
        });
        Class c = f.createClass();
        MethodHandler mi = new MethodHandler() {
            public Object invoke(Object self, Method m, Method proceed,
                    Object[] args) throws Throwable {
                if (m.getName().startsWith("set")) {
                    throw new RuntimeException("this bean is inmutable!");
                }
    
                return m.invoke(instance, args); // execute the original method
                                                    // over the instance
            }
        };
        C proxy = (C) c.newInstance();
    
        ((Proxy) proxy).setHandler(mi);
        return (C) proxy;
     }
    }
    

    And here is an example code. Let Employee be your bean:

    public class Employee{
      private String name="John";
      private String surname="Smith";
      public String getName() {
        return name;
      }
      public void setName(String name) {
        this.name = name;
      }
      public String getSurname() {
        return surname;
      }
      public void setSurname(String surname) {
        this.surname = surname;
      }
    };
    

    And here a test case showing that you can create a proxy for a POJO, use its getters, but you can’t use its setters

    @Test
    public void testProxy() throws InstantiationException, IllegalAccessException{
        Employee aBean = new Employee();
    
        //I can modify the bean
        aBean.setName("Obi-Wan");
        aBean.setSurname("Kenobi");
    
        //create the protected java bean with the generic utility
        Employee protectedBean = Utils.createInmutableBean(Employee.class, aBean);
    
        //I can read
        System.out.println("Name: "+protectedBean.getName());
        System.out.println("Name: "+protectedBean.getSurname());
    
        //but I can't modify
        try{
            protectedBean.setName("Luke");
            protectedBean.setSurname("Skywalker");
            throw new RuntimeException("The test should not have reached this line!");
        }catch(Exception e){
            //I should be here
            System.out.println("The exception was expected! The bean should not be modified (exception message: "+e.getMessage()+")");
            assertEquals("Obi-Wan", protectedBean.getName());
            assertEquals("Kenobi", protectedBean.getSurname());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on generating Java objects from an XSD file using JAXB 2.1.
I am trying to create java objects from xml file. I am using jaxb(unmarshalling)
I am using JAXB to serialize data to XML from java objects. I do
I am trying to create objects using binding data from xml file to classes
How to create a JAXB java class out of a xml schema using a
Tech Stack: Java 1.6, JAXB, Spring 3, JAX-RS (RESTEasy), XSD Hello, I am using
I am encountering a problem while marshalling my Java objects using JAXB. When i
I'm using BeanUtils to manipulate Java objects created via JAXB, and I've run into
I'm using JAXB to generate a XML Schema from my Java classes so the
I'm reading an xml file using Jaxb, now the contents of the xml file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.