Results 1 to 5 of 5
  1. #1
    bazong is offline Member
    Join Date
    Jun 2010
    Location
    Chur, Switzerland
    Posts
    3
    Rep Power
    0

    Default Reflection - How to determine the method parameter class

    Hi,

    I need to write a String value into set method with different parameter objects. For this i need to know the parameter class.
    e.g.:
    Java Code:
    MyClass.setFoo(myValue)
    My Question is, if myValue is a String or a BigInteger class?

    I tried following code example, but didn't get the decided result ...

    Java Code:
    Method m = iRecord.getClass().getDeclaredMethods()[j];
    Class params[] = m.getParameterTypes();
    if(params.length == 1){
      if(params[0] instanceof String){
        System.out.println("String recogniZed");
      }
      if(params[0] instanceof BigInteger){
        System.out.println("BigInteger recogniZed");
      }
    }
    Any suggestions to this?

    thx in advance
    Michael

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    it sounds you need to overload your method setFoo, example

    public void setFoo(String s) {}
    public void setFoo(int i) {}
    and so on

    when you now call setFoo("myString") then the method with the string parameter will be called. more on this topi here

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    please give the complete code...not understandable
    Ramya:cool:

  4. #4
    bazong is offline Member
    Join Date
    Jun 2010
    Location
    Chur, Switzerland
    Posts
    3
    Rep Power
    0

    Default

    My problem is, the MyClass in my example is a auto-generated JAXB class which i won't change and i'm trying to create a generic method to fill in different JAXB classes. Theses classes have exactly one set method according to an XML element. So i need to now the arguments type.

  5. #5
    bazong is offline Member
    Join Date
    Jun 2010
    Location
    Chur, Switzerland
    Posts
    3
    Rep Power
    0

    Default

    here my complete code:

    Java Code:
    public DTSalesTurnover setIFRecord(String filename) throws InvalidContentException {
    		// record field information
    		int idx = 0;
    		public Vector<DataField> record = new Vector<DataField>();
    		record.add(new DataField(COMPANY_CODE_QUALIFIER, "", idx++, 6));
    		record.add(new DataField(PERIOD_DATE_QUALIFIER, "", idx++, 6));
    		// XML record element
    		DTSalesTurnover iRecord = oFactory.createDTSalesTurnover();
    		int i = 0;
    		try {
    			for (; i < record.size(); i++) {
    				// loop all record fields
    				try {
    					for (int j = 0; j < iRecord.getClass().getDeclaredMethods().length; j++) {
    						// loop all XML object methods ...
    						if (iRecord.getClass().getDeclaredMethods()[j].toString().contains("." + SETTER_PREFIX)) {
    							// ... setter
    							Method m = iRecord.getClass().getDeclaredMethods()[j];
    							if (m.getName().contains(
    									SETTER_PREFIX + record.get(i).name.substring(0, 1).toUpperCase() + record.get(i).name.substring(1))) {
    								System.out.println("--> " + m.getName());
    							
    //-------------------------------------------------------------------------------------
    // Here i'd like to determine the class type of the set method
    // all my field values are in a String format ....
    
    								Object iParams[] = new Object[1];
    								if ( m.getParameterTypes()[0].getClass().isInstance(BigInteger.class) ) {
    									iParams[0] = new BigInteger(record.get(i).value);
    								}
    								else if (m.getParameterTypes()[0].getClass().isInstance(Float.class)){
    									iParams[0] = new Float(record.get(i).value);
    								}
    								else if (m.getParameterTypes()[0].getClass().isInstance(String.class)){
    									iParams[0] = record.get(i).value;
    								}
    								else{
    									System.out.println("???\t"+record.get(i).value);
    								}
    								iRecord.getClass().getMethod(m.getName(), params).invoke(m, iParams);
    
    //-------------------------------------------------------------------------------------
    							}
    						}
    					}// for
    				} catch (IllegalArgumentException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (SecurityException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IllegalAccessException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (InvocationTargetException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (NoSuchMethodException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}// for
    		} catch (NumberFormatException e) {
    			throw new InvalidContentException(e.getClass().getSimpleName() + ": Element <" + record.get(i).name + ">" + e.getMessage());
    		}
    		return iRecord;
    	}

Similar Threads

  1. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 10:26 PM
  2. Input parameter of Main method
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-12-2008, 07:24 PM
  3. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM
  4. Class Reflection: Finding class modifiers
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:11 PM
  5. Getting method names using Reflection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 04:18 PM

Tags for this Thread

Posting Permissions

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