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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:26:57+00:00 2026-06-08T21:26:57+00:00

I’m currently working on a small framework to collect metrics in an OSGi system.

  • 0

I’m currently working on a small framework to collect metrics in an OSGi system.
The core of it is an annotation @Metric that indicates that a given method of a service can deliver a metric (e.g. numeric value) when asked.

Those methods would look like:

@Metric
public int getQueueSize() {...}

or

@Metric
public double getAvgTaskTime() {...}

I’m using reflection to inspect the service implementation class and register the methods that are annotated with @Metric. As a sanity check, I’m checking that the method indeed delivers a numeric value. I tried this and failed:

for (Method method: metricSource.getClass().getMethods()) {
    if (method.isAnnotationPresent(Metric.class) &&  Number.class.isAssignableFrom(method.getReturnType()) { 
        // add method for later process
    }

Then, later on the processor, I’d do:

Number value = (Number) method.invoke(target, obj[]);

It turns out that for a primitive type you would get e.g. int.class and that’s not assignable to Number.class whereas the boxed type Integer.class would be. Damn. Move on.

Then I created a Map<Class<?>, Extractor> where Extractor takes a class and casts a parameter into that class:

public NumberExtractor(Class<? extends Number> clazz) {
    this.clazz = clazz;
    }       
    public double extract(Object val) {
        Number nbr = clazz.cast(val);
        return nbr.doubleValue();
    }
}

In face of the previous observation, I added an entry like this:

extractorMap.put(int.class, new NumberExtractor(int.class));

But that didn’t work either. It gave a runtime class cast exception saying that Integer.class could not be cast to int. Note also that the compiler does not complain on new NumberExtractor(int.class), letting the boundary check Class<? extends Number> pass on int.class

At the end, this combination worked:

extractorMap.put(int.class, new NumberExtractor(Integer.class));

What is going on here? Reflection says that the return type of the object (int.class) is not assignable to Number.class, but when I go on with the method invoke, I actually get a Integer.class? WhiskeyTangoFoxtrot?!

I’m left wondering whether there is another way to address this issue other than maintaining the Map of int -> Integer, Integer -> Integer, float -> Float, Float -> Float? (which is now done, so this is for the sake of learning)

The behaviour of the boxing and type information doesn’t seem coherent here.

Could anybody shine some light on this?

  • 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-08T21:26:58+00:00Added an answer on June 8, 2026 at 9:26 pm

    Reflection says that the return type of the object (int.class) is not assignable to Number.class, but when I go on with the method invoke, I actually get a Integer.class?

    Absolutely. The reflection API can’t possibly return you an actual int, so it boxes the value in an Integer. What else could it do?

    The fact that it has to box does not change the assignability from int to Number. Look at the docs for isAssignableFrom:

    If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

    So it’s behaving exactly according to the documentation.

    I’m left wondering whether there is another way to address this issue other than maintaining the Map of int -> Integer, Integer -> Integer, float -> Float, Float -> Float? (which is now done, so this is for the sake of learning)

    Yup, that sounds right to me. Or just have a Set<Class> for primitive numeric types rather than explicitly mapping.

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

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.