JCoReAnnotationTools { @SuppressWarnings("unchecked") public static <T extends Annotation> T getIncludingAnnotation(JCas aJCas, Annotation focusAnnotation, Class<T> cls) { FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator(); cursor.moveToFirst(); Annotation currentAnnotation = null; while (cursor.isValid() && ((currentAnnotation = cursor.get()).getEnd() < focusAnnotation.getEnd() || !cls.isInstance(currentAnnotation))) { cursor.moveToNext(); } Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd()); Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd()); if (cursor.isValid() && cls.isInstance(currentAnnotation) && currentRange.containsRange(focusRange)) return (T) cursor.get(); return null; }  static Annotation getAnnotationByClassName(JCas aJCas, String fullAnnotationClassName); static Annotation getAnnotationAtOffset(JCas aJCas, String fullAnnotationClassName, int startOffset,
                                                   int endOffset); @SuppressWarnings("unchecked") static T getAnnotationAtMatchingOffsets(JCas aJCas, Annotation focusAnnotation,
                                                                          Class<T> cls); static Annotation getOverlappingAnnotation(JCas aJCas, String fullAnnotationClassName, int startOffset,
                                                      int endOffset); static Annotation getPartiallyOverlappingAnnotation(JCas aJCas, String fullAnnotationClassName,
                                                               int startOffset, int endOffset); @SuppressWarnings("unchecked") static T getPartiallyOverlappingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                             Class<T> cls); @SuppressWarnings("unchecked") static List<T> getIncludedAnnotations(JCas aJCas, Annotation focusAnnotation,
                                                                        Class<T> cls); @SuppressWarnings("unchecked") static T getIncludingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                  Class<T> cls); @SuppressWarnings("unchecked") static T getNearestIncludingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                         Class<T> cls); static List<T> getNearestOverlappingAnnotations(JCas aJCas, int begin, int end,
                                                                                  Class<T> cls); @SuppressWarnings("unchecked") static List<T> getNearestOverlappingAnnotations(JCas aJCas, Annotation focus,
                                                                                  Class<T> cls); @SuppressWarnings("unchecked") static T getLastOverlappingAnnotation(JCas aJCas, Annotation focusAnnotation,
                                                                        Class<T> cls);  }
@Test public void testIncludingAnnotations() throws Exception { JCas jcas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-all-types"); jcas.setDocumentText("long form includes short form"); Token t1 = new Token(jcas, 0, 4); t1.addToIndexes(); Token t2 = new Token(jcas, 5, 9); t2.addToIndexes(); Token t3 = new Token(jcas, 10, 18); t3.addToIndexes(); Token t4 = new Token(jcas, 19, 24); t4.addToIndexes(); Token t5 = new Token(jcas, 25, 29); t5.addToIndexes(); EntityMention em1 = new EntityMention(jcas, 0, 29); em1.addToIndexes(); EntityMention em2 = new EntityMention(jcas, 19, 29); em2.addToIndexes(); DocumentAnnotation docAnn = new DocumentAnnotation(jcas, 0, 29); docAnn.addToIndexes(); EntityMention includedAnnotation = JCoReAnnotationTools.getIncludingAnnotation(jcas, t4, EntityMention.class); assertNotNull(includedAnnotation); assertEquals(em1, includedAnnotation); }