Hi Florian,
If you use uimaFIT
http://code.google.com/p/uimafit/
you can use something more type-safe like
for (Token token : JCasUtil.select(jCas, Token.class)){
}
-Torsten
> -----Original Message-----
> From: Florian Leitner [mailto:[email protected]] On Behalf Of Florian
> Leitner
> Sent: Monday, August 06, 2012 11:51 AM
> To: [email protected]
> Subject: generics and type safety in UIMA AEs
>
> Can someone help me out on two issues I have (ab-)using generics and thus
> having type safety issues when implementing UIMA AEs? I am having a hard
> time figuring out how to implement an AE without having to resort to "black
> box" casts. It seems I am not fully seeing how generics should be applied via
> the UIMA API.
>
> 1. The first issue is the (superfluous) need to cast an annotation type from
> the annotation index. Usually, if I get an iterator, the UIMA tutorials
> instruct
> us to write:
>
> FSIterator<Annotation> annIt = jcas.getAnnotationIndex(myType).iterator();
>
> This is works for non-generic pre-Java 5 code, but because I loose the type
> information I supplied to the method fetching the index, I end up with
> something like this:
>
> MyAnnotationType ann = (MyAnnotationType) annIt.next();
>
> This is not type safe because of relying on an (both unsafe and unnecessary!)
> cast. Can somebody please point out how to create type-safe annotation
> iterators? How can I instantiate an iterator other than the (non-generic)
> FSIterator<Annotation> in UIMA? Shouldn't there be something more Java-
> 5-like in the API? E.g.:
>
> FSIterator<MyAnnotationType> =
> jcas.getAnnotationTypeIndex(MyAnnotationType.class).iterator();
>
> 2. The second type safety issue I have is with external resources. Lets
> assume I've got an interface defining a StringMapResource<T>, i.e., much
> like in the UIMA tutorials, essentially a Map<String, T> using String keys and
> some generic values. Usually, in the initialize(UimaContext ctx) method, I'd
> write:
>
> StringMapResource<MyType> smr = (StringMapResource<MyType>)
> ctx.getResourceObject(myResourceKey);
>
> Obviously, this is not an effective way to implement generics, and the
> compiler complains about this unchecked "back box" casting I am doing when
> fetching the resource. Is there a type-safe way to initialize resources I am
> not
> aware of? Currently, because of this ugly cast, I even am forced to decorate
> my initialize(UimaContext) methods with hideous
> @SuppressWarnings("unchecked") annotations and I'd really like to get rid of
> these "warts". I'd assume there should be some method to instantiate a
> type-safe, generic resource factory in the UIMA API, right?
>
> Thanks for any help!
> Florian
>
> --
> Florian Leitner, PhD <[email protected]>
>
> Structural Biology and BioComputing Programme Spanish National Cancer
> Research Centre (CNIO)
>
> Address: C/ Melchor Fernandez Almagro 3; E-28029 Madrid
> Phone: +34 91 732 8000
> Fax: +34 91 224 6980
> Internet: http://www.cnio.es