aspose file tools*
The moose likes I/O and Streams and the fly likes Test to determine if field or method exists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Test to determine if field or method exists" Watch "Test to determine if field or method exists" New topic
Author

Test to determine if field or method exists

mike rich
Greenhorn

Joined: Jan 30, 2002
Posts: 2
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

Joined: Jan 30, 2002
Posts: 7
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

Joined: Jan 30, 2002
Posts: 7
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Test to determine if field or method exists