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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:11:44+00:00 2026-05-25T18:11:44+00:00

I already have a solution, but I don’t like it. Also, it would be

  • 0

I already have a solution, but I don’t like it. Also, it would be nice to know your thoughts about this.

You see, elem.get gives back an Object, that is going to be a Float in the case of x and y.

This code throws the Exception described in the title:

String names[] = { "x", "y", "src" };
Class types[] = { float.class, float.class, String.class };

for (int i = 0; i < names.length; i++)
{
    if (elem.containsKey(names[i]))
    {
        par.add(types[i]);
        arg.add(types[i].cast(elem.get(names[i])));
    }
}

The code is used to define the list of parameters to instantiate an object. Both the parameters and the object’s class name are defined in an external data file (but nevermind about that).

The solution, something I’m not fond of (because it loses the flexibility I was trying to attain), has this within the for:

if(types[i] == float.class)
{
    float v = (Float)elem.get(names[i]);
    arg.add(v);
} else
{
    arg.add(types[i].cast(elem.get(names[i])));
    break;
}

Other option would be changing float.class to Float.class in types, but I won’t do that because that code is used to instantiate objects which have float parameters in their constructors.

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-05-25T18:11:44+00:00Added an answer on May 25, 2026 at 6:11 pm

    As the type of args is an ArrayList<Object>, you’re not avoiding the boxing anyway:

    // This code...
    float v = (Float)elem.get(names[i]);
    arg.add(v);
    
    // Will actually be equivalent to:
    float v = (Float)elem.get(names[i]);
    Float boxed = v;
    arg.add(boxed);
    

    If the point is to call a constructor by reflection, then you’re going to have to pass in a Float for any float parameters – that’s just how it works.

    So basically, change float.class to Float.class and it should all work fine. EDIT: Ah, except then the par list will have the wrong type. Basically you want to cast with Float.class, but add float.class to the list of parameter types.

    You probably want a map from primitive types to wrapper types, like this:

    private static final Map<Class<?>>, Class<?>> PRIMITIVE_TYPE_MAP =
        buildPrimitiveTypeMap();
    
    private static Map<Class<?>>, Class<?>> buildPrimitiveTypeMap()
    {
        Map<Class<?>>, Class<?>> map = new HashMap<Class<?>>, Class<?>>();
        map.put(float.class, Float.class);
        map.put(double.class, Double.class);
        // etc
        return map;
    }
    

    Then:

    String names[] = { "x", "y", "src" };
    Class types[] = { float.class, float.class, String.class };
    
    for (int i = 0; i < names.length; i++)
    {
        if (elem.containsKey(names[i]))
        {
            par.add(types[i]);
    
            // For primitive types, only box to the wrapper type
            Class<?> castType = PRIMITIVE_TYPE_MAP.get(types[i]);
            if (castType == null)
            {
                castType = types[i];
            }
            arg.add(castType.cast(elem.get(names[i])));
        }
    }
    

    In fact, if you trust that the values are of the right type already, I suspect you can just do:

    arg.add(elem.get(names[i]));
    

    After all, you’re just casting to a particular type and then losing that type information again… using the cast call does perform a check that the execution-time types are correct though, so you may want to keep it for that reason.

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

Sidebar

Related Questions

I'm already tossing around a solution but as I haven't done something like this
First, I have already spent the past few hours trying to find a solution
We already have things like static analysis that tells us what's wrong with our
I have already have this code to force these URLs to HTTPS: RewriteCond %{HTTPS}
I didn't find a solution for this but I think it should be doable.
Somebody must have come up with a solution for this by now. We are
I would like my @Before method to know the currently executing tests Annotations, so
Since I'm using jQuery, any solution via that would work too. Ideally, I'd like
We already have a good build server in Hudson but we want something that
This is similar to a previous question , but the answers there don't satisfy

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.