ExternalComponentRegistryImpl implements CoreComponentRegistry { @Override public AbstractComponent newInstance(ComponentTypeInfo componentTypeInfo) { Class<? extends AbstractComponent> componentClass = componentTypeInfo.getComponentClass(); LOGGER.debug("new instance called for {0}", componentClass.getClass().getName()); AbstractComponent newComponent; try { newComponent = componentClass.newInstance(); newComponent.getCapability(ComponentInitializer.class).initialize(); newComponent.setOwner(getDefaultUser()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InstantiationException ie) { throw new RuntimeException(ie); } return newComponent; } ExternalComponentRegistryImpl(); static ExternalComponentRegistryImpl getInstance(); @Override Set<ViewInfo> getViewInfos(String componentTypeId, ViewType type); Collection<ExtendedComponentTypeInfo> getComponentInfos(); @Override T getAsset(TypeInfo<?> objectType, Class<T> assetClass); void setDefaultViewProvider(ComponentProvider provider); @Override AbstractComponent newInstance(ComponentTypeInfo componentTypeInfo); @Override AbstractComponent newCollection(Collection<AbstractComponent> components); @Override T newInstance(Class<T> componentClass, AbstractComponent parent); void refreshComponents(List<ExtendedComponentProvider> providers); @Override AbstractComponent getComponent(String id); @Override boolean isCreatable(Class<?> clazz); @Override AbstractComponent newInstance(String componentType); static final byte USER_DEFINED_GROUP_PERMISSION; }
@Test public void testNewInstance() throws Exception { ComponentTypeInfo info = new ComponentTypeInfo("displayName", "desc", TestBaseComponent.class); ExtendedComponentProvider provider = createProvider(Collections.singletonList(info), null); registry.refreshComponents(Collections.singletonList(provider)); TestBaseComponent newComponent = registry.newInstance(TestBaseComponent.class,null); Assert.assertTrue(newComponent.getClass().equals(TestBaseComponent.class)); AbstractComponent parentComponent = new MockComponent() { @Override public synchronized List<AbstractComponent> getComponents() { return Collections.<AbstractComponent> emptyList(); } }; TestBaseComponent newComponent2 = registry.newInstance(TestBaseComponent.class,parentComponent); Assert.assertTrue(newComponent2.getClass().equals(TestBaseComponent.class)); }