public void IntrospectType(Type typeToIntrospect, IObjectSpecImmutable spec) { Log.InfoFormat("introspecting {0}: class-level details", typeToIntrospect.FullName); if (!TypeUtils.IsPublic(typeToIntrospect)) { throw new ReflectionException(string.Format(Resources.NakedObjects.DomainClassReflectionError, typeToIntrospect)); } introspectedType = typeToIntrospect; properties = typeToIntrospect.GetProperties(); methods = GetNonPropertyMethods(); // 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 <IObjectSpecBuilder>(); foreach (Type interfaceType in InterfacesTypes) { if (interfaceType != null && ClassStrategy.IsTypeToBeIntrospected(interfaceType)) { IObjectSpecBuilder 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); }
/// <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) { // 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(Log.LogAndReturn($"Type {propertyType.Name} is a service and cannot be used in public property {property.Name} on type {property.DeclaringType?.Name}. If the property is intended to be an injected service it should have a protected get.")); } 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) { 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 IAssociationSpecImmutable[] FindAndCreateFieldSpecs(IObjectSpecImmutable spec) { // now create fieldSpecs for value properties, for collections and for reference properties IList <PropertyInfo> collectionProperties = FacetFactorySet.FindCollectionProperties(properties, ClassStrategy).Where(pi => !FacetFactorySet.Filters(pi, ClassStrategy)).ToList(); IEnumerable <IAssociationSpecImmutable> collectionSpecs = CreateCollectionSpecs(collectionProperties, spec); // every other accessor is assumed to be a reference property. IList <PropertyInfo> allProperties = FacetFactorySet.FindProperties(properties, ClassStrategy).Where(pi => !FacetFactorySet.Filters(pi, ClassStrategy)).ToList(); IEnumerable <PropertyInfo> refProperties = allProperties.Except(collectionProperties); IEnumerable <IAssociationSpecImmutable> refSpecs = CreateRefPropertySpecs(refProperties, spec); return(collectionSpecs.Union(refSpecs).ToArray()); }