/// <summary> /// Creates a list of Association fields for all the properties that use NakedObjects. /// </summary> private IEnumerable <IAssociationSpecImmutable> CreateRefPropertySpecs(IEnumerable <PropertyInfo> foundProperties, IObjectSpecImmutable spec) { var specs = new List <IAssociationSpecImmutable>(); foreach (PropertyInfo property in foundProperties) { Log.DebugFormat("Identified 1-1 association method {0}", property); Log.DebugFormat("One-to-One association {0} -> {1}", property.Name, property); // create a reference property spec var identifier = new IdentifierImpl(FullName, property.Name); Type propertyType = property.PropertyType; var propertySpec = reflector.LoadSpecification(propertyType); if (propertySpec is IServiceSpecImmutable) { throw new ReflectionException(string.Format( "Type {0} is a service and cannot be used in public property {1} on type {2}." + " If the property is intended to be an injected service it should have a protected get.", propertyType.Name, property.Name, property.DeclaringType.Name )); } var referenceProperty = ImmutableSpecFactory.CreateOneToOneAssociationSpecImmutable(identifier, spec, propertySpec as IObjectSpecImmutable); // Process facets for the property FacetFactorySet.Process(reflector, property, new IntrospectorMethodRemover(methods), referenceProperty, FeatureType.Properties); specs.Add(referenceProperty); } return(specs); }
private IActionSpecImmutable[] FindActionMethods(ITypeSpecImmutable spec) { Log.Debug("Looking for action methods"); var actionSpecs = new List <IActionSpecImmutable>(); var actions = FacetFactorySet.FindActions(methods.Where(m => m != null).ToArray(), reflector.ClassStrategy).Where(a => !FacetFactorySet.Filters(a, reflector.ClassStrategy)).ToArray(); methods = methods.Except(actions).ToArray(); // ReSharper disable once ForCanBeConvertedToForeach // kepp for look as actions are nulled out within loop for (int i = 0; i < actions.Length; i++) { MethodInfo actionMethod = actions[i]; // actions are potentially being nulled within this loop if (actionMethod != null) { string fullMethodName = actionMethod.Name; Type[] parameterTypes = actionMethod.GetParameters().Select(parameterInfo => parameterInfo.ParameterType).ToArray(); // build action & its parameters if (actionMethod.ReturnType != typeof(void)) { reflector.LoadSpecification(actionMethod.ReturnType); } IIdentifier identifier = new IdentifierImpl(FullName, fullMethodName, actionMethod.GetParameters().ToArray()); IActionParameterSpecImmutable[] actionParams = parameterTypes.Select(pt => ImmutableSpecFactory.CreateActionParameterSpecImmutable(reflector.LoadSpecification <IObjectSpecImmutable>(pt), identifier)).ToArray(); var action = ImmutableSpecFactory.CreateActionSpecImmutable(identifier, spec, actionParams); // Process facets on the action & parameters FacetFactorySet.Process(reflector, actionMethod, new IntrospectorMethodRemover(actions), action, FeatureType.Actions); for (int l = 0; l < actionParams.Length; l++) { FacetFactorySet.ProcessParams(reflector, actionMethod, l, actionParams[l]); } actionSpecs.Add(action); } } return(actionSpecs.ToArray()); }
private IActionSpecImmutable[] FindActionMethods(IObjectSpecImmutable spec) { Log.Debug("Looking for action methods"); var actionSpecs = new List <IActionSpecImmutable>(); for (int i = 0; i < methods.Length; i++) { // careful in here - methods are being nulled out within the methods array as we iterate. if (methods[i] != null) { MethodInfo actionMethod = methods[i]; string fullMethodName = actionMethod.Name; if (!FacetFactorySet.Filters(actionMethod, reflector.ClassStrategy)) { Log.DebugFormat("Identified action {0}", actionMethod); methods[i] = null; Type[] parameterTypes = actionMethod.GetParameters().Select(parameterInfo => parameterInfo.ParameterType).ToArray(); // build action & its parameters IActionParameterSpecImmutable[] actionParams = parameterTypes.Select(pt => new ActionParameterSpecImmutable(GetSpecification(pt))).Cast <IActionParameterSpecImmutable>().ToArray(); IIdentifier identifier = new IdentifierImpl(metamodel, FullName, fullMethodName, actionMethod.GetParameters().ToArray()); var action = new ActionSpecImmutable(identifier, spec, actionParams); // Process facets on the action & parameters FacetFactorySet.Process(reflector, actionMethod, new IntrospectorMethodRemover(methods), action, FeatureType.Action); for (int l = 0; l < actionParams.Length; l++) { FacetFactorySet.ProcessParams(reflector, actionMethod, l, actionParams[l]); } if (actionMethod.ReturnType != typeof(void)) { reflector.LoadSpecification(actionMethod.ReturnType); } actionSpecs.Add(action); } } } return(actionSpecs.ToArray()); }
public void IntrospectType(Type typeToIntrospect, ITypeSpecImmutable spec) { if (!TypeUtils.IsPublic(typeToIntrospect)) { throw new ReflectionException(logger.LogAndReturn(string.Format(Resources.NakedObjects.DomainClassReflectionError, typeToIntrospect))); } IntrospectedType = typeToIntrospect; SpecificationType = GetSpecificationType(typeToIntrospect); properties = typeToIntrospect.GetProperties(); methods = GetNonPropertyMethods(); Identifier = new IdentifierImpl(FullName); // Process facets at object level // this will also remove some methods, such as the superclass methods. var methodRemover = new IntrospectorMethodRemover(methods); FacetFactorySet.Process(reflector, IntrospectedType, methodRemover, spec); if (SuperclassType != null && ClassStrategy.IsTypeToBeIntrospected(SuperclassType)) { Superclass = reflector.LoadSpecification(SuperclassType); } AddAsSubclass(spec); var interfaces = new List <ITypeSpecBuilder>(); foreach (var interfaceType in InterfacesTypes) { if (interfaceType != null && ClassStrategy.IsTypeToBeIntrospected(interfaceType)) { var interfaceSpec = reflector.LoadSpecification(interfaceType); interfaceSpec.AddSubclass(spec); interfaces.Add(interfaceSpec); } } Interfaces = interfaces.ToArray(); IntrospectPropertiesAndCollections(spec); IntrospectActions(spec); }
private IEnumerable <IAssociationSpecImmutable> CreateCollectionSpecs(IEnumerable <PropertyInfo> collectionProperties, IObjectSpecImmutable spec) { var specs = new List <IAssociationSpecImmutable>(); foreach (PropertyInfo property in collectionProperties) { IIdentifier identifier = new IdentifierImpl(FullName, property.Name); // create a collection property spec Type returnType = property.PropertyType; var returnSpec = reflector.LoadSpecification <IObjectSpecImmutable>(returnType); Type defaultType = typeof(object); var defaultSpec = reflector.LoadSpecification <IObjectSpecImmutable>(defaultType); var collection = ImmutableSpecFactory.CreateOneToManyAssociationSpecImmutable(identifier, spec, returnSpec, defaultSpec); FacetFactorySet.Process(reflector, property, new IntrospectorMethodRemover(methods), collection, FeatureType.Collections); specs.Add(collection); } return(specs); }
/// <summary> /// Creates a list of Association fields for all the properties that use NakedObjects. /// </summary> private IEnumerable <IAssociationSpecImmutable> CreateRefPropertySpecs(IEnumerable <PropertyInfo> foundProperties, IObjectSpecImmutable spec) { var specs = new List <IAssociationSpecImmutable>(); foreach (PropertyInfo property in foundProperties) { Log.DebugFormat("Identified 1-1 association method {0}", property); Log.DebugFormat("One-to-One association {0} -> {1}", property.Name, property); // create a reference property spec var identifier = new IdentifierImpl(metamodel, FullName, property.Name); Type propertyType = property.PropertyType; IObjectSpecBuilder propertySpec = reflector.LoadSpecification(propertyType); var referenceProperty = new OneToOneAssociationSpecImmutable(identifier, spec, propertySpec); // Process facets for the property FacetFactorySet.Process(reflector, property, new IntrospectorMethodRemover(methods), referenceProperty, FeatureType.Property); specs.Add(referenceProperty); } return(specs); }
private IEnumerable <IAssociationSpecImmutable> CreateCollectionSpecs(IEnumerable <PropertyInfo> collectionProperties, IObjectSpecImmutable spec) { var specs = new List <IAssociationSpecImmutable>(); foreach (PropertyInfo property in collectionProperties) { Log.DebugFormat("Identified one-many association method {0}", property); IIdentifier identifier = new IdentifierImpl(metamodel, FullName, property.Name); // create a collection property spec Type returnType = property.PropertyType; IObjectSpecBuilder returnSpec = reflector.LoadSpecification(returnType); Type defaultType = typeof(object); IObjectSpecBuilder defaultSpec = reflector.LoadSpecification(defaultType); var collection = new OneToManyAssociationSpecImmutable(identifier, spec, returnSpec, defaultSpec); FacetFactorySet.Process(reflector, property, new IntrospectorMethodRemover(methods), collection, FeatureType.Collections); specs.Add(collection); } return(specs); }