use of org.apache.felix.ipojo.Nullable in project felix by apache.
the class TemporalDependency method start.
/**
* Start method. Initializes the nullable object.
* @see org.apache.felix.ipojo.util.DependencyModel#start()
*/
public void start() {
super.start();
switch(m_policy) {
case TemporalHandler.NULL:
m_nullableObject = null;
break;
case TemporalHandler.NULLABLE:
// access to the service specification.
try {
m_nullableObject = Proxy.newProxyInstance(m_handler.getInstanceManager().getClazz().getClassLoader(), new Class[] { getSpecification(), Nullable.class }, // NOPMD
new NullableObject());
if (isAggregate()) {
if (m_collection) {
List list = new ArrayList(1);
list.add(m_nullableObject);
m_nullableObject = list;
} else {
Object[] array = (Object[]) Array.newInstance(getSpecification(), 1);
array[0] = m_nullableObject;
m_nullableObject = array;
}
}
} catch (NoClassDefFoundError e) {
// It generally comes from a missing import.
throw new IllegalStateException("Cannot create the Nullable object, a referenced class cannot be loaded: " + e.getMessage());
}
break;
case TemporalHandler.DEFAULT_IMPLEMENTATION:
// Create the default-implementation object.
try {
Class clazz = m_handler.getInstanceManager().getContext().getBundle().loadClass(m_di);
m_nullableObject = clazz.newInstance();
} catch (IllegalAccessException e) {
throw new IllegalStateException("Cannot load the default-implementation " + m_di + " : " + e.getMessage());
} catch (InstantiationException e) {
throw new IllegalStateException("Cannot load the default-implementation " + m_di + " : " + e.getMessage());
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot load the default-implementation " + m_di + " : " + e.getMessage());
}
if (isAggregate()) {
if (m_collection) {
List list = new ArrayList(1);
list.add(m_nullableObject);
m_nullableObject = list;
} else {
Object[] array = (Object[]) Array.newInstance(getSpecification(), 1);
array[0] = m_nullableObject;
m_nullableObject = array;
}
}
break;
case TemporalHandler.EMPTY:
if (!m_collection) {
m_nullableObject = Array.newInstance(getSpecification(), 0);
} else {
// Empty collection
m_nullableObject = new ArrayList(0);
}
break;
default:
// Cannot occurs
break;
}
}
use of org.apache.felix.ipojo.Nullable in project felix by apache.
the class NullableTest method testOnGet_whenNullableEnabled_returnsAnInstanceOfNullableAndSpecification.
public void testOnGet_whenNullableEnabled_returnsAnInstanceOfNullableAndSpecification() {
Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
BundleContext context = Mockito.mock(BundleContext.class);
InstanceManager im = Mockito.mock(InstanceManager.class);
Mockito.when(im.getClazz()).thenReturn(ComponentTestWithSuperClass.class);
DependencyHandler handler = Mockito.mock(DependencyHandler.class);
Mockito.when(handler.getInstanceManager()).thenReturn(im);
Dependency dependency = new Dependency(handler, "a_field", TestSpecification.class, null, true, false, true, false, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
dependency.start();
Object service = dependency.onGet(null, null, null);
Assert.assertTrue(service instanceof Nullable);
Assert.assertTrue(service instanceof TestSpecification);
}
use of org.apache.felix.ipojo.Nullable in project felix by apache.
the class FilterCheckSubscriber method getProps.
public Properties getProps() {
Properties props = new Properties();
props.put("Bind", new Integer(bound));
props.put("Nullable", new Boolean(m_foo instanceof Nullable));
return props;
}
Aggregations