BundleClassesFinder { Set<Class<?>> readOsgiServicesForClass(Class<?> clazz) { Set<Class<?>> osgiClasses = new HashSet<Class<?>>(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(OsgiService.class)) { Class<?> fieldClass = field.getType(); osgiClasses.add(fieldClass); } } Constructor<?>[] constructors = clazz.getConstructors(); for (Constructor<?> constructor : constructors) { Class<?>[] parameterTypes = constructor.getParameterTypes(); Annotation[][] annotations = constructor.getParameterAnnotations(); int j = 0; for (int i = (parameterTypes.length - annotations.length); i < parameterTypes.length; i++) { for (Annotation annotation : annotations[j]) { if (annotation.annotationType().equals(OsgiService.class)) { osgiClasses.add(parameterTypes[i]); break; } } j++; } } return osgiClasses; } BundleClassesFinder(String basePackage); Collection<Class<?>> getClasses(final Bundle bundle); Collection<Class<?>> getClasses(final Collection<Bundle> bundles); void addFilter(ClassFilter classFilter); Collection<Class<?>> traverseBundlesForOsgiServices(final Collection<Bundle> bundles);  }
@Test public void testReadOsgiServicesForClassWithConstructorServices() { Collection<Class<?>> classes = classFinder .readOsgiServicesForClass(ClassTestConstructorService.class); Assert.assertEquals(2, classes.size()); Assert.assertTrue(classes.contains(Double.class)); Assert.assertTrue(classes.contains(Long.class)); }
@Test public void testReadOsgiServicesForClassInnerClass() { Collection<Class<?>> classes = classFinder .readOsgiServicesForClass(InnerClassTestService.InnerClass.class); Assert.assertEquals(1, classes.size()); Assert.assertTrue(classes.contains(Integer.class)); }
@Test public void testReadOsgiServicesForClassWithInnerClass() { Collection<Class<?>> classes = classFinder.readOsgiServicesForClass(InnerClassTestService.class); Assert.assertEquals(0, classes.size()); }
@Test public void testReadOsgiServicesForClass() { Collection<Class<?>> classes = classFinder.readOsgiServicesForClass(ClassTestService.class); Assert.assertEquals(2, classes.size()); Assert.assertTrue(classes.contains(String.class)); Assert.assertTrue(classes.contains(Long.class)); }