Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Test to determine if field or method exists

 
mike rich
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read in 2 data files into 2 'Record' object arrays. I need to loop through the values in each to determine combinations based on a set of logic. I have created a new object called Merge that takes in an array of each of the Record objects (Record1 [] and Record2 []). The issue that I have is that in order to process the logic rules I must determine if each of the Record2 objects has 2 fields/variables or has 5 fields/variables. Once I know this I can determine the logic rules to process. How can I determine if a field/variable exists or not in a object? I feel like my use of 2 distinct constuctors (one for 2 fields and one for 5 fields) when I read from the text file is what the problem is. I was thinking maybe I should always expect 5 fields and then force in a null to the remaining 3 when there are only two fields to read in.
Please help - I am at the end of my rope. I am new at this and now that I have most of the heavy lifting of reading a file into an array of objects done I feel like I can't reach a conclusion...... So close, yet still so far!!!
Mike
 
Melynda Coxx
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I haven't done exactly what you are describing, but recently I did something similar -- looking to see if a method (rather than a field) was available.
I don't know whether this is the best way to do this. I figured it out myself, so it is probably not the most efficient way. I am curious myself to see how others might reply to your question.
Let me first explain what I was trying to do and then show my code snippet:
There were two versions of a class I was using called ProxyPort. The later version of ProxyPort had changed its disconnect() method from private to public. I wanted to call disconnect() if it was available, so I did a check to see if it was available using the File.getDeclaredMethod() method. (You may be able to use the File.getDeclaredField() method in a similar fashion.)
// a class field:
private ProxyPort proxyPort;
...
// the method:
private void invokeDisconnectIfAvailable()
{
Class proxyPortClass = proxyPort.getClass();
java.lang.reflect.Method disconnectMethod = null;
try
{
disconnectMethod = proxyPortClass.getDeclaredMethod("disconnect",new Class[0]);
}
catch(NoSuchMethodException e)
{
return; // nothing we can do
}
int modifiers = disconnectMethod.getModifiers();
if (! java.lang.reflect.Modifier.isPublic(modifiers))
return; // can't call it
try
{
disconnectMethod.invoke(proxyPort,new Object[0]);
}
catch(IllegalAccessException e)
{} // oh well we tried
catch(java.lang.reflect.InvocationTargetException e)
{} // oh well we tried
} // end invokeDisconnectIfAvailable()
Hope this helps,
Mel
[ January 30, 2002: Message edited by: Melynda Coxx ]
 
Melynda Coxx
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See, I AM a greenhorn!
My code above lost its spacing. (Sorry it looks terrible.)
I just now noticed the CODE button down below...
Mel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic