public void add(Service<ComponentType> service) { if (!(service instanceof SpringService)) { throw new IllegalArgumentException("Service type must be " + SpringService.class.getName()); } super.add(service); }
protected JavaComponentBuilder(String name, Object instance) { Class<?> clazz = instance.getClass(); String className = clazz.getName(); InjectingComponentType componentType = new InjectingComponentType(clazz); JavaImplementation implementation = new JavaImplementation(instance); implementation.setComponentType(componentType); implementation.setImplementationClass(clazz); component = new Component<>(name); component.setImplementation(implementation); }
void evaluateConstructor(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { Map<InjectionSite, Injectable> sites = componentType.getInjectionSites(); Constructor<?> constructor = componentType.getConstructor(); TypeMapping typeMapping = context.getTypeMapping(implClass); Type[] parameterTypes = constructor.getGenericParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { InjectionSite site = new ConstructorInjectionSite(constructor, i); // skip sites that have already been mapped if (sites.containsKey(site)) { continue; } Type parameterType = parameterTypes[i]; String name = helper.getSiteName(constructor, i, null); processSite(componentType, typeMapping, name, constructor, parameterType, site, context); } }
public void applyHeuristics(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { // if there is already a defined constructor then do nothing if (componentType.getConstructor() != null) { return; } Constructor<?> ctor = findConstructor(implClass, componentType, context); componentType.setConstructor(ctor); }
public void applyHeuristics(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { // if any properties, references or resources have been defined already assume that was what the user intended and return if (!(componentType.getProperties().isEmpty() && componentType.getReferences().isEmpty() && componentType.getResourceReferences().isEmpty())) { return; } evaluateConstructor(componentType, implClass, context); evaluateSetters(componentType, implClass, context); evaluateFields(componentType, implClass, context); }
public void applyHeuristics(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { // if the service contracts have not already been defined then introspect them if (componentType.getServices().isEmpty()) { // get the most specific interfaces implemented by the class Set<Class<?>> interfaces = helper.getImplementedInterfaces(implClass); // if the class does not implement any interfaces, then the class itself is the service contract // we don't have to worry about proxies because all wires to system components are optimized if (interfaces.isEmpty()) { Service<ComponentType> service = createServiceDefinition(implClass, componentType, context); componentType.add(service); } else { // otherwise, expose all of the implemented interfaces for (Class<?> serviceInterface : interfaces) { Service<ComponentType> service = createServiceDefinition(serviceInterface, componentType, context); componentType.add(service); } } } }
InjectingComponentType componentType = new InjectingComponentType(instanceClass); implementationIntrospector.introspect(componentType, context); implementation.setComponentType(componentType); Service<ComponentType> service = new Service<>(serviceName, contract); InjectingComponentType componentType = new InjectingComponentType(instanceClass); componentType.add(service);
public void generate(PhysicalJavaComponent physicalComponent, LogicalComponent<? extends JavaImplementation> component) { Component<? extends JavaImplementation> logical = component.getDefinition(); JavaImplementation implementation = logical.getImplementation(); InjectingComponentType type = implementation.getComponentType(); Scope scope = type.getScope(); // create the instance factory definition ImplementationManagerDefinition managerDefinition = new ImplementationManagerDefinition(); managerDefinition.setReinjectable(Scope.COMPOSITE == scope); managerDefinition.setConstructor(type.getConstructor()); managerDefinition.setInitMethod(type.getInitMethod()); managerDefinition.setDestroyMethod(type.getDestroyMethod()); managerDefinition.setImplementationClass(implementation.getImplementationClass()); managerDefinition.setClassLoaderUri(component.getDefinition().getContributionUri()); helper.processInjectionSites(type, managerDefinition); // create the physical component definition physicalComponent.setScope(scope); physicalComponent.setEagerInit(type.isEagerInit()); physicalComponent.setManagerDefinition(managerDefinition); physicalComponent.setManaged(type.isManaged()); physicalComponent.setManagementInfo(type.getManagementInfo()); helper.processPropertyValues(component, physicalComponent); }
public void applyHeuristics(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { // apply service heuristic serviceHeuristic.applyHeuristics(componentType, implClass, context); if (componentType.getConstructor() == null) { Constructor<?> ctor = findConstructor(implClass, componentType, context); componentType.setConstructor(ctor); } if (componentType.getProperties().isEmpty() && componentType.getReferences().isEmpty() && componentType.getResourceReferences().isEmpty()) { evaluateConstructor(componentType, implClass, context); evaluateSetters(componentType, implClass, context); evaluateFields(componentType, implClass, context); } Scope scope = componentType.getScope(); if (componentType.isManaged() && !scope.isSingleton()) { IllegalManagementAttribute warning = new IllegalManagementAttribute(implClass, componentType); context.addWarning(warning); } }
for (Map.Entry<String, Reference<ComponentType>> entry : tempType.getReferences().entrySet()) { String name = entry.getKey(); Reference<ComponentType> reference = componentType.getReferences().get(name); for (Map.Entry<String, ResourceReference> entry : tempType.getResourceReferences().entrySet()) { String name = entry.getKey(); ResourceReference definition = componentType.getResourceReferences().get(name); for (Map.Entry<String, Producer<ComponentType>> entry : tempType.getProducers().entrySet()) { String name = entry.getKey(); Producer definition = componentType.getProducers().get(name); for (Map.Entry<InjectionSite, Injectable> entry : tempType.getInjectionSites().entrySet()) { componentType.addMapping(tempType.getImplClass().getName(), entry.getKey(), entry.getValue());
public Map<AccessibleObject, Supplier<Object>> getInjectors(Class<?> clazz) { Map<AccessibleObject, Supplier<Object>> injectors = new HashMap<>(); // introspect class InjectingComponentType componentType = new InjectingComponentType(clazz); DefaultIntrospectionContext context = new DefaultIntrospectionContext(); TypeMapping typeMapping = new TypeMapping(); context.addTypeMapping(clazz, typeMapping); introspectionHelper.resolveTypeParameters(clazz, typeMapping); classVisitor.visit(componentType, clazz, context); for (PostProcessor postProcessor : postProcessors) { postProcessor.process(componentType, clazz, context); } // process injection sites for (Map.Entry<ModelObject, InjectionSite> entry : componentType.getInjectionSiteMappings().entrySet()) { ModelObject key = entry.getKey(); InjectionSite value = entry.getValue(); if (key instanceof Reference) { Reference<?> reference = (Reference) key; handleReference(reference, value, clazz, injectors); } else if (key instanceof Producer) { Producer producer = (Producer) key; throw new UnsupportedOperationException("Not yet implemented"); } else if (key instanceof Consumer) { throw new UnsupportedOperationException("Not yet implemented"); } } return injectors; }
void evaluateSetters(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { Map<InjectionSite, Injectable> sites = componentType.getInjectionSites(); TypeMapping typeMapping = context.getTypeMapping(implClass); Set<Method> setters = helper.getInjectionMethods(implClass, componentType.getServices().values()); for (Method setter : setters) { InjectionSite site = new MethodInjectionSite(setter, 0); // skip sites that have already been mapped if (sites.containsKey(site)) { continue; } String name = helper.getSiteName(setter, null); Type parameterType = setter.getGenericParameterTypes()[0]; processSite(componentType, typeMapping, name, setter, parameterType, site, context); } }
private <I> ScopedComponent createPhysicalComponent(LogicalComponent<?> logicalComponent, I instance) { URI uri = logicalComponent.getUri(); InjectingComponentType type = (InjectingComponentType) logicalComponent.getDefinition().getComponentType(); type.getInjectionSites(); return new SingletonComponent(uri, instance, type.getInjectionSites(), BOOT_CONTRIBUTION); }
public void introspect(InjectingComponentType componentType, IntrospectionContext context) { componentType.setScope(Scope.COMPOSITE); Class<?> implClass = componentType.getImplClass(); if (implClass.isInterface()) { InvalidImplementation failure = new InvalidImplementation("Implementation class is an interface", implClass, componentType); context.addError(failure); return; } TypeMapping mapping = context.getTypeMapping(implClass); if (mapping == null) { mapping = new TypeMapping(); context.addTypeMapping(implClass, mapping); helper.resolveTypeParameters(implClass, mapping); } classVisitor.visit(componentType, implClass, context); heuristic.applyHeuristics(componentType, implClass, context); } }
public void visitField(Binding annotation, Field field, Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { if (field.isAnnotationPresent(Reference.class)) { return; } String name = helper.getSiteName(field, ""); if (componentType.getReferences().containsKey(name)) { // reference already introspected, e.g. repeatable annotations return; } referenceProcessor.addDefinition(field, "", true, implClass, componentType, context); } }
private void processSite(InjectingComponentType componentType, String name, Member member, Class<?> parameterType, Class<?> declaringClass, InjectionSite site, IntrospectionContext context) { TypeMapping typeMapping = context.getTypeMapping(declaringClass); InjectableType type = helper.inferType(parameterType, typeMapping); switch (type) { case PROPERTY: addProperty(componentType, name, parameterType, declaringClass, site, context); break; case REFERENCE: addReference(componentType, name, parameterType, declaringClass, site, context); break; case CALLBACK: context.addError(new UnknownInjectionType(site, type, componentType.getImplClass().getName(), member, componentType)); break; default: context.addError(new UnknownInjectionType(site, type, componentType.getImplClass().getName(), member, componentType)); } }
protected void processReferences(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { for (Map.Entry<ModelObject, InjectionSite> entry : componentType.getInjectionSiteMappings().entrySet()) { if (!(entry.getKey() instanceof Reference)) { continue;
InjectingComponentType componentType = new InjectingComponentType(instanceClass); implementationIntrospector.introspect(componentType, context); implementation.setComponentType(componentType); Service<ComponentType> service = new Service<>(serviceName, contract); InjectingComponentType componentType = new InjectingComponentType(instanceClass); componentType.add(service);
public PhysicalComponent generate(LogicalComponent<SystemImplementation> component) throws Fabric3Exception { Component<SystemImplementation> definition = component.getDefinition(); SystemImplementation implementation = definition.getImplementation(); InjectingComponentType type = implementation.getComponentType(); ImplementationManagerDefinition managerDefinition = new ImplementationManagerDefinition(); managerDefinition.setReinjectable(true); managerDefinition.setConstructor(type.getConstructor()); managerDefinition.setInitMethod(type.getInitMethod()); managerDefinition.setDestroyMethod(type.getDestroyMethod()); managerDefinition.setImplementationClass(implementation.getImplementationClass()); managerDefinition.setClassLoaderUri(definition.getContributionUri()); helper.processInjectionSites(type, managerDefinition); // create the physical component definition PhysicalSystemComponent physical = new PhysicalSystemComponent(); physical.setEagerInit(type.isEagerInit()); physical.setManaged(type.isManaged()); physical.setManagementInfo(type.getManagementInfo()); physical.setManagerDefinition(managerDefinition); helper.processPropertyValues(component, physical); return physical; }
public void applyHeuristics(InjectingComponentType componentType, Class<?> implClass, IntrospectionContext context) { // if any services have been defined, just introspect policy if (!componentType.getServices().isEmpty()) { for (Service<ComponentType> definition : componentType.getServices().values()) { JavaServiceContract contract = (JavaServiceContract) definition.getServiceContract(); policyIntrospector.introspectPolicyOnOperations(contract, implClass, context); } return; } Set<Class<?>> interfaces = helper.getImplementedInterfaces(implClass); if (interfaces.isEmpty()) { // no interfaces, use implementation Service<ComponentType> service = createServiceDefinition(implClass, implClass, componentType, context); componentType.add(service); } else { // class implements all interfaces that are not management interfaces or in the Java package for (Class<?> interfaze : interfaces) { Package pkg = interfaze.getPackage(); if (interfaze.isAnnotationPresent(Management.class) || pkg == null || pkg.getName().startsWith("java")) { continue; } Service<ComponentType> service = createServiceDefinition(interfaze, implClass, componentType, context); componentType.add(service); } } }