Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Reflective constructor matching problem

Reflective constructor matching problem

Scheduled Pinned Locked Moved Java
javaregexhelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Branislav Vidovic
    wrote on last edited by
    #1

    Hello guys, I have been working on dynamic construction of object by using reflection (Java 6 SE platform). I have noticed for example that there is a method:

    Class.getConstructor(Calss[])

    You pass the argument Class objects to this method and it gives you the corresponding constructor back. This works fine in most cases. But it has one problem. It dos not support autoboxing. For example if the constructor of my class takes an int parameter and and i ask the Class object for this class for the constructor like this: Class.getConstrucort(new Class[]{Integer.class}) i will get the NoSuchMethodException indicating that there is no matching constructor. If i match the constructor argument in the class definition exactly to the parameter type given to the getCosntructor() method then i will have no problem. Did anyone had any similar problems? If any one has a solution or a workaround for this, i would need an advice. Cheers

    D 1 Reply Last reply
    0
    • B Branislav Vidovic

      Hello guys, I have been working on dynamic construction of object by using reflection (Java 6 SE platform). I have noticed for example that there is a method:

      Class.getConstructor(Calss[])

      You pass the argument Class objects to this method and it gives you the corresponding constructor back. This works fine in most cases. But it has one problem. It dos not support autoboxing. For example if the constructor of my class takes an int parameter and and i ask the Class object for this class for the constructor like this: Class.getConstrucort(new Class[]{Integer.class}) i will get the NoSuchMethodException indicating that there is no matching constructor. If i match the constructor argument in the class definition exactly to the parameter type given to the getCosntructor() method then i will have no problem. Did anyone had any similar problems? If any one has a solution or a workaround for this, i would need an advice. Cheers

      D Offline
      D Offline
      David Skelly
      wrote on last edited by
      #2

      Autoboxing involves "silently" swapping between primitives and reference objects. Integer.class and int.class are both instances of type Class, in other words they are both reference types, so no autoboxing is involved. The compiler has no knowledge that these two different references can be treated as interchangeable in this particular case. In the call to getConstructor, specify either int.class or Integer.TYPE instead of Integer.class. That will work.

      B 1 Reply Last reply
      0
      • D David Skelly

        Autoboxing involves "silently" swapping between primitives and reference objects. Integer.class and int.class are both instances of type Class, in other words they are both reference types, so no autoboxing is involved. The compiler has no knowledge that these two different references can be treated as interchangeable in this particular case. In the call to getConstructor, specify either int.class or Integer.TYPE instead of Integer.class. That will work.

        B Offline
        B Offline
        Branislav Vidovic
        wrote on last edited by
        #3

        Thanks, big part of the problem is solved. Cheers

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups