Results 1 to 5 of 5
- 06-18-2010, 08:51 AM #1
Member
- Join Date
- Jun 2010
- Location
- Chur, Switzerland
- Posts
- 3
- Rep Power
- 0
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.:My Question is, if myValue is a String or a BigInteger class?Java Code:MyClass.setFoo(myValue)
I tried following code example, but didn't get the decided result ...
Any suggestions to this?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"); } }
thx in advance
Michael
- 06-18-2010, 09:13 AM #2
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
- 06-18-2010, 09:51 AM #3
please give the complete code...not understandable
Ramya:cool:
- 06-18-2010, 09:53 AM #4
Member
- Join Date
- Jun 2010
- Location
- Chur, Switzerland
- Posts
- 3
- Rep Power
- 0
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.
- 06-18-2010, 10:08 AM #5
Member
- Join Date
- Jun 2010
- Location
- Chur, Switzerland
- Posts
- 3
- Rep Power
- 0
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
-
Can a method take itself as parameter?
By bukake in forum New To JavaReplies: 10Last Post: 09-06-2008, 09:26 PM -
Input parameter of Main method
By Java Tip in forum Java TipReplies: 1Last Post: 07-12-2008, 06:24 PM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM -
Class Reflection: Finding class modifiers
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:11 PM -
Getting method names using Reflection
By Java Tip in forum Java TipReplies: 0Last Post: 01-24-2008, 03:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks