uima-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tommaso Teofili <tommaso.teof...@gmail.com>
Subject Re: Get annotation class from Type
Date Tue, 01 Dec 2009 08:46:12 GMT
2009/11/29 Steven Bethard <steven.bethard@gmail.com>

> On Sat, Nov 28, 2009 at 1:12 AM, Fabien POULARD <grdscarabe@gmail.com>
> wrote:
> > On Fri, Nov 27, 2009 at 4:56 PM, Thilo Goetz <twgoetz@gmx.de> wrote:
> >> I guess this will work, as long as there even is a JCas type
> >> for the type you're looking for.  There may not always be one,
> >> and we have this whole meta-programming kind of API for this
> >> kind of situation.  If you tell us what you need the class for,
> >> we may be able to suggest alternative ways of handling this.
> >
> > My need is quite simple, a component I develop work on some types
> > passed to the component in parameter. I retrieve the type from its
> > name passed in parameter with a simple :
> >
> > Type mMonType  = cas.getTypeSystem().getType(theParameterTypeStr);
> >
> > ... and I check it is not null before using it.
> >
> > Now I use some generic classes (java generics) for my processing and I
> > need to "configure" them with the class of the annotation
> > corresponding to this type. That's where I'm lost...
>
> If you know you're loading a Java class for a type, why not have your
> "theParameterTypeStr" be the fully qualified Java class name instead
> of the UIMA type? Then you can use the usual Java Class.forName()
> directly.
>

Using reflection is a simple quick way I also use sometimes for this
purpose.
Just beware, if you want to instantiate your Annotations automatically, that
JCasGen generated types usually have more than one constructor so you should
choose the one you deserve (i.e.: the constructor with the JCas parameter,
that is usually the first one).

ex.:
Type mMonType  = cas.getTypeSystem().getType(
theParameterTypeStr);

Class typeClass = Class.forName(mMonType.getName());
Constructor jcasConstructor = getProperConstructor(typeClass);
FeatureStructure fs = (FeatureStructure) jcasConstructor.newInstance(aJCas);
...

private Constructor getProperConstructor(Class typeClass) {
....
//FIXME : dummy sample
return typeClass.getConstructors()[1];
..
}

Tommaso


>
> Steve
> --
> Where did you get that preposterous hypothesis?
> Did Steve tell you that?
>        --- The Hiphopopotamus
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message