use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.
the class ValueObjectProvider method provide.
@Override
public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
Value annotation = annotationProvider.getAnnotation(Value.class);
if (annotation == null)
return null;
String value = annotation.value();
Object expanded = symbolSource.expandSymbols(value);
IntermediateType intermediate = annotationProvider.getAnnotation(IntermediateType.class);
if (intermediate != null)
expanded = typeCoercer.coerce(expanded, intermediate.value());
return typeCoercer.coerce(expanded, objectType);
}
use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.
the class ModuleImpl method instantiateModuleInstance.
private Object instantiateModuleInstance() {
Class moduleClass = moduleDef.getBuilderClass();
Constructor[] constructors = moduleClass.getConstructors();
if (constructors.length == 0)
throw new RuntimeException(IOCMessages.noPublicConstructors(moduleClass));
if (constructors.length > 1) {
// Sort the constructors ascending by number of parameters (descending); this is really
// just to allow the test suite to work properly across different JVMs (which will
// often order the constructors differently).
Comparator<Constructor> comparator = new Comparator<Constructor>() {
@Override
public int compare(Constructor c1, Constructor c2) {
return c2.getParameterTypes().length - c1.getParameterTypes().length;
}
};
Arrays.sort(constructors, comparator);
logger.warn(IOCMessages.tooManyPublicConstructors(moduleClass, constructors[0]));
}
Constructor constructor = constructors[0];
if (insideConstructor)
throw new RuntimeException(IOCMessages.recursiveModuleConstructor(moduleClass, constructor));
ObjectLocator locator = new ObjectLocatorImpl(registry, this);
Map<Class, Object> resourcesMap = CollectionFactory.newMap();
resourcesMap.put(Logger.class, logger);
resourcesMap.put(ObjectLocator.class, locator);
resourcesMap.put(OperationTracker.class, registry);
InjectionResources resources = new MapInjectionResources(resourcesMap);
Throwable fail = null;
try {
insideConstructor = true;
ObjectCreator[] parameterValues = InternalUtils.calculateParameters(locator, resources, constructor.getParameterTypes(), constructor.getGenericParameterTypes(), constructor.getParameterAnnotations(), registry);
Object[] realized = InternalUtils.realizeObjects(parameterValues);
Object result = constructor.newInstance(realized);
InternalUtils.injectIntoFields(result, locator, resources, registry);
return result;
} catch (InvocationTargetException ex) {
fail = ex.getTargetException();
} catch (Exception ex) {
fail = ex;
} finally {
insideConstructor = false;
}
throw new RuntimeException(IOCMessages.instantiateBuilderError(moduleClass, fail), fail);
}
use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.
the class RegistryImpl method getOrderedConfiguration.
@Override
@SuppressWarnings("unchecked")
public <T> List<T> getOrderedConfiguration(ServiceDef3 serviceDef, Class<T> objectType) {
lock.check();
String serviceId = serviceDef.getServiceId();
Logger logger = getServiceLogger(serviceId);
Orderer<T> orderer = new Orderer<T>(logger);
Map<String, OrderedConfigurationOverride<T>> overrides = CollectionFactory.newCaseInsensitiveMap();
// TAP5-2129. NOTICE: if someday an ordering between modules is added, this should be reverted
// or a notice added to the documentation.
List<Module> modules = new ArrayList<Module>(moduleToServiceDefs.keySet());
Collections.sort(modules, new ModuleComparator());
for (Module m : modules) addToOrderedConfiguration(orderer, overrides, objectType, serviceDef, m);
if (serviceId.equals("MasterObjectProvider")) {
ObjectProvider contribution = new ObjectProvider() {
@Override
public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
return findServiceByMarkerAndType(objectType, annotationProvider, null);
}
};
orderer.add("ServiceByMarker", (T) contribution);
}
for (OrderedConfigurationOverride<T> override : overrides.values()) override.apply();
final List<T> result = orderer.getOrdered();
if (!isServiceConfigurationListenerServiceDef(serviceDef)) {
serviceConfigurationListener.onOrderedConfiguration(serviceDef, result);
}
return result;
}
use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.
the class ServiceAnnotationObjectProviderTest method annotation_present.
@SuppressWarnings("unchecked")
@Test
public void annotation_present() {
Class objectType = Runnable.class;
AnnotationProvider provider = mockAnnotationProvider();
ObjectLocator locator = mockObjectLocator();
Service service = newMock(Service.class);
String serviceId = "JiffyPop";
Runnable instance = mockRunnable();
train_getAnnotation(provider, Service.class, service);
expect(service.value()).andReturn(serviceId);
train_getService(locator, serviceId, objectType, instance);
replay();
ObjectProvider objectProvider = new ServiceAnnotationObjectProvider();
assertSame(objectProvider.provide(objectType, provider, locator), instance);
verify();
}
use of org.apache.tapestry5.commons.ObjectLocator in project tapestry-5 by apache.
the class ServiceAnnotationObjectProviderTest method no_annotation.
@SuppressWarnings("unchecked")
@Test
public void no_annotation() {
Class objectType = Runnable.class;
AnnotationProvider provider = mockAnnotationProvider();
ObjectLocator locator = mockObjectLocator();
train_getAnnotation(provider, Service.class, null);
replay();
ObjectProvider objectProvider = new ServiceAnnotationObjectProvider();
assertNull(objectProvider.provide(objectType, provider, locator));
verify();
}
Aggregations