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 8607653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:24:41+00:00 2026-06-12T03:24:41+00:00

I have the following Java object hierarchy: public interface Function { public void calculate(long

  • 0

I have the following Java object hierarchy:

public interface Function {
    public void calculate(long t);
}

public class ConstantFunction implements Function {
    private double constant;

    @Override
    public void calculate(long t) {
        // ...
    }
}

public class LinearFunction implements Function {
    private double slope;

    private double yIntercept;

    @Override
    public void calculate(long t) {
        // ...
    }
}

Users can create ConstantFunction and LinearFunction instances by defining them inside XML like so:

<myapp>
    <function type="ConstantFunction>
        <!-- ... -->
    </function>
    <function type="LinearFunction>
        <!-- ... -->
    </function>
</myapp>

I am using XStream to OX-map the user-defined XML into Java POJOs. I’m currently trying to configure the XStream mapper with aliases so that it knows what Java class to bind to the function element:

XStream oxmapper = new XStream();
oxmapper.alias("myapp", MyApp.class);
oxmapper.alias("function", ???);

The problem is, I need to configure XStream with logic that says: *if function/type is ConstantFunction, then use oxmapper.alias("function", ConstantFunction.class); but if its value is LinearFunction, then use oxmapper.alias("function", LinearFunction.class).

The problem is, I don’t think XStream API gives provides a way to inspect the XML the way I need it to in order to implement this logic. If I am incorrect, please point me in the right direction!

If I am correct, then the only solution I can think of would be to have a really nasty “hodgepodge” class that forms a union of all the Function concretions like so:

public class FunctionFactory implements Function {
    private double constant;
    private double slope;
    private double yIntercept;

    private Class<? extends Function> concreteClass;

    @Override
    public void calculate(long t) {
        // Do nothing. This class is a workaround to limitations with XStream.
        return;
    }
}

In the OX-mapper config:

oxampper.alias("function", FunctionFactory.class);
oxmapper.aliasField("function", "type", "concreteClass");

Now, every time I read an XML instance into a MyApp instance, I need to correct the conversion:

XStream oxmapper = getConfiguredMapper();
MyApp app = oxmapper.fromXml("<myapp>...</myapp>");

FunctionFactory factory = app.getFunction();
Function concretion = factory.getConcreteClass();

app.setFunction(concretion);

This is the only workaround I can cook up, but it feels really nasty, and I have to believe there’s a better way to do this. Thanks in advance!

  • 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-12T03:24:43+00:00Added an answer on June 12, 2026 at 3:24 am

    When you want to have custom behaviors in XStream, you should use Converters as described in Converter tutorial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Java Spring WebApp) I have: public class PersonValidator implements Validator { private PersonDAO d;
I have the following code: import java.util.*; public class SellTransaction extends Transaction { private
Say suppose I have the following Java code. public class Example { public static
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
Say I have the following simple java bean: class MyBean { private Date startDate;
I have the following java model class in App Engine: public class Xyz ...
I have following class in Java code: public class CHRTreeCreator extends IndexCreator { ...
I have the following code: public class Tests { public static void main(String[] args)
I have a the following java script object function eventTypeObj() { allEventTypes = [];
I have the following Java interfaces, public interface JavascriptInterface { public MyIterator getItems(); }

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.