SingleTypeExtensionFactory implements ExtensionFactory { @SuppressWarnings({"unchecked"}) @Override public final E getExtension(Object object, Class<?> extensionType) { if (this.extensionType.isAssignableFrom(extensionType)) { try { return getExtensionImpl((T) object, (Class<E>) extensionType); } catch (Throwable throwable) { throw new RuntimeException(throwable); } } return null; } SingleTypeExtensionFactory(Class<E> extensionType);  SingleTypeExtensionFactory(Class<E> extensionType, Class<? extends E> extensionSubType); final Class<E> getExtensionType(); final Class<? extends E> getExtensionSubType(); @SuppressWarnings({"unchecked"}) @Override final E getExtension(Object object, Class<?> extensionType); @Override final Class<?>[] getExtensionTypes();  }
@Test public void testFactory() { Object extension; SingleTypeExtensionFactory<Object, JButton> f1 = new SingleTypeExtensionFactory<Object, JButton>(JButton.class); extension = f1.getExtension(OBJECT_INSTANCE, JButton.class); assertTrue(extension instanceof AbstractButton); extension = f1.getExtension(OBJECT_INSTANCE, AbstractButton.class); assertNull(extension); }
@Test public void testFactoryWithSubClass() { Object extension; SingleTypeExtensionFactory<Object, JComponent> f2 = new SingleTypeExtensionFactory<Object, JComponent>(JComponent.class, JButton.class); extension = f2.getExtension(OBJECT_INSTANCE, AbstractButton.class); assertTrue(extension instanceof AbstractButton); extension = f2.getExtension(OBJECT_INSTANCE, JButton.class); assertTrue(extension instanceof AbstractButton); extension = f2.getExtension(OBJECT_INSTANCE, AbstractButton.class); assertTrue(extension instanceof AbstractButton); extension = f2.getExtension(OBJECT_INSTANCE, JComponent.class); assertTrue(extension instanceof JComponent); extension = f2.getExtension(OBJECT_INSTANCE, Component.class); assertNull(extension); }
@Test public void testFactoryWith1ArgConstructor() { Object extension; SingleTypeExtensionFactory<Object, ExtensionClass> f2 = new SingleTypeExtensionFactory<Object, ExtensionClass>(ExtensionClass.class); extension = f2.getExtension(OBJECT_INSTANCE, ExtensionClass.class); assertTrue(extension instanceof ExtensionClass); assertSame(OBJECT_INSTANCE, ((ExtensionClass) extension).arg); }
@Test public void testFactoryWithSubClassWith1ArgConstructor() { Object extension; SingleTypeExtensionFactory<Object, ExtensionClass> f2 = new SingleTypeExtensionFactory<Object, ExtensionClass>(ExtensionClass.class, ExtensionClassSubClass.class); extension = f2.getExtension(OBJECT_INSTANCE, ExtensionClass.class); assertTrue(extension instanceof ExtensionClassSubClass); assertSame(OBJECT_INSTANCE, ((ExtensionClassSubClass) extension).arg); }