Results 1 to 3 of 3
  1. #1
    baizba is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default Reflective constructor matching problem

    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:
    Java Code:
    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
    Java Code:
    int
    parameter and and i ask the Class object for this class for the constructor like this:
    Java Code:
    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
    Java Code:
    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

  2. #2
    ranma173 is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    When indicating types, not values, there's no autoboxing. Autoboxing is only for values. I mean, (int)5 can be boxed to (Integer)5 but "int" primitive type can't be "boxed" to "Integer" class.

    So, you have to indicate the type "int". You can do it this way:
    Java Code:
    clazz.getConstructor(Integer.TYPE);
    By the way, you don't need to create an array to call getConstructor() because it takes a "vararg".

    Regards
    Last edited by ranma173; 09-24-2010 at 01:10 PM. Reason: mistake

  3. #3
    baizba is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Constructor Problem
    By Aggy in forum New To Java
    Replies: 9
    Last Post: 01-19-2010, 11:42 PM
  2. [SOLVED] Constructor problem
    By McChill in forum New To Java
    Replies: 3
    Last Post: 03-09-2009, 07:43 PM
  3. Problem with Constructor
    By ToastyBainey in forum New To Java
    Replies: 3
    Last Post: 03-09-2009, 03:36 AM
  4. [SOLVED] Constructor problem
    By sfe23 in forum New To Java
    Replies: 10
    Last Post: 02-21-2009, 09:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •