BUG: NSFormatter in Java
Benjamin Riefenstahl
benny at crocodial.de
28 Jan 2000 18:54:20 +0100
Hi all,
Benjamin Riefenstahl <benny@crocodial.de> writes:
> In a nutshell we try to use custom NSFormatter subclasses as the
> "formatter" parameter to the WOTextField component. This works fine for
> the formatting part, but the parsing part seems to be severely broken.
> It looks like the Java/ObjC bridge code does not work at all.
>
> [...]
>
> - Somebody on this list comes up with some ObjC trickery to correct the
> problem without looking at the source code.
I did bite the bullet today and implemented this myself. I would like
to present the solution here for others to copy and for general
review.
I have two questions:
- I implemented the method objectValueForString: in the base class
(just throwing an exception) because otherwise I got warnings from
the compiler. Is it considered better style to do it this way or is
it better to live with the warnings and have slightly smaller code?
- I didn't implement exception handling in getObjectValue:. As I
understand the docs I should catch the Java exception
NSFormatter.ParsingException and return NO from getObjectValue:.
OTOH it's not clear to me what difference this would/should make.
If, for testing, I just return NO from my method than the
WOTextField component just ignores any change to the value silently,
leaving the old value. But I do not consider that usefull behaviour
because it basically prevents detection of the error in the
application or component.
I notice that the documentation for the ObjC version of the class
talks about catching errors in NSControl, but that doesn't apply to
a WOF application, does it? And it's not mentioned in the Java docs
either.
On the whole I think I prefer all exceptions to be passed up and the
ObjC error handling just be ignored. Am I missing anything?
Ok, enough of that, here is the code. Note: This is my first ObjC
class so please be gentle with me ;-).
>>>>>>>>>>>>> snip <<<<<<<<<<<<<<<<
/*------------------------------------------------------------------------
NSFormatterPatch.h
Fri 28-Jan-2000 Benny Created
--------------------------------------------------------------------------
$Log: .../NSFormatterPatch.h $
1 00-01-28 16:49 Benny
Patch NSFormatter class to actually work with the documented Java
interface
------------------------------------------------------------------------*/
/*------------------------------------------------------------------------
imports
------------------------------------------------------------------------*/
#import <Foundation/NSFormatter.h>
/*------------------------------------------------------------------------
class
------------------------------------------------------------------------*/
@interface NSFormatterPatch : NSFormatter
{
}
+ (void) load;
- (BOOL) getObjectValue: (id*) anObject
forString: (NSString*) string
errorDescription: (NSString**) error;
- (id) objectValueForString: (NSString*) string;
@end
/*
Local Variables: ***
mode: objc ***
End: ***
*/
/*------------------------------------------------------------------------
eof
------------------------------------------------------------------------*/
>>>>>>>>>>>>> snip <<<<<<<<<<<<<<<<
/*------------------------------------------------------------------------
NSFormatterPatch.m
Fri 28-Jan-2000 Benny Created
--------------------------------------------------------------------------
$Log: .../NSFormatterPatch.m $
1 00-01-28 16:49 Benny
Patch NSFormatter class to actually work with the documented Java
interface
------------------------------------------------------------------------*/
/*------------------------------------------------------------------------
imports
------------------------------------------------------------------------*/
#import <Foundation/NSString.h>
#import <Foundation/NSException.h>
#import "NSFormatterPatch.h"
/*------------------------------------------------------------------------
class
------------------------------------------------------------------------*/
@implementation NSFormatterPatch
+ (void) load
{
[NSFormatterPatch poseAsClass: [NSFormatter class]];
}
- (BOOL) getObjectValue: (id*) anObject
forString: (NSString*) string
errorDescription: (NSString**) error
{
*anObject = [self objectValueForString: string];
return YES;
}
- (id) objectValueForString:
(NSString*) string
{
[[NSException
exceptionWithName: @"objectValueForString not implemented"
reason: @"Must override objectValueForString in Java class"
userInfo: nil
] raise]
;
return nil;
}
@end
/*------------------------------------------------------------------------
eof
------------------------------------------------------------------------*/
>>>>>>>>>>>>> snip <<<<<<<<<<<<<<<<
so long, benny
======================================
Benjamin Riefenstahl (benny@crocodial.de)
ISION Internet AG
Ruhrstr. 61, D-22761 Hamburg, Germany