PDA

View Full Version : Problem with Spring.Objects.ObjectWrapper


darrell.pittman
09-30-2005, 10:36 PM
Hello,

I'm having a problem with Spring.Objects.ObjectWrapper.DoTypeConversionIfNec essary method.

My xml is as follows:

<object singleton="false" id="userPermissions" type="Spring.Core.IO.AssemblyResource, Spring.Core">
<constructor-arg name="resourceName" value="assembly://SpringTestConsole/SpringTestConsole.Data.Security/user-permissions.xml"/>
</object>
<object id="permissionProvider" type="SpringTestConsole.AopObjects.DefaultPermissionProv ider, SpringTestConsole">
<constructor-arg name="res" ref="userPermissions"/>
</object>

(Just thought I would use the convenience of your AssemblyResource object to load some xml)

When the ObjectWrapper attempts to convert the AssemblyResource constructor arg to the DefaultPermissionProvider it fails on the line:


if (typeConverter != null
|| requiredType != null
&& !IsAssignableFrom(newValue, requiredType))

Because of operator precedence the above statement returns true even though newValue is assignable from requiredType. I'm not sure if this is something I don't understand or if its a bug. When I change the code to:


if ((typeConverter != null
|| requiredType != null)
&& !IsAssignableFrom(newValue, requiredType))

everything works fine.

Any help would be greatly appreciated.

Rick Evans
10-01-2005, 01:30 PM
Hi Darrell

Strange. Anyway, I agree with you so far as the operator precedence issue... there should definitely be parentheses in there to express what the intent of the code is. I have gone to the offending code and changed it accordingly.

What I haven't been able to do is replicate your issue. I wrote the following test case, and it all greenlights for me... and do forgive the manual checking at the end :)

[Test]
private void Foo()
{
const string xml =
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd'>
<object id='stuff' type='Spring.Core.IO.AssemblyResource, Spring.Core'>
<constructor-arg name='resourceName' value='assembly://Spring.Core.Tests/Spring/TestResource.txt'/>
</object>
<object id='foo' type='Spring.Objects.ResourceTestObject, Spring.Core.Tests'>
<constructor-arg name='resource' ref='stuff'/>
</object>
</objects>";
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
IObjectFactory factory = new XmlObjectFactory(new InputStreamResource(stream, string.Empty));
ResourceTestObject foo = (ResourceTestObject) factory["foo"];
Console.Out.WriteLine(foo.Resource);
}

Here is a pared down version of the ResourceTestObject; it exposes a constructor that takes an IResource, just like yours (yeah?)...

namespace Spring.Objects
{
public class ResourceTestObject
{
private IResource resource;

public ResourceTestObject(IResource resource)
{
this.resource = resource;
}

public IResource Resource
{
get { return this.resource; }
set { this.resource = value; }
}
}
}

This works straight out of the box for me. I'd like to address your issue, but... well, can you post the relevant constructor snippet from DefaultPermissionProvider class?

Ciao
Rick

darrell.pittman
10-01-2005, 02:15 PM
Rick,

Thanks for the reply. Here is the code from the DefaultPermissionProvider constructor:

public DefaultPermissionProvider(IResource res)
{

XmlDocument xml=new XmlDocument();
xml.Load(new XmlTextReader(res.InputStream));
LoadPermissions(xml);
}

By the way, I am using Spring.NET 1.0 RC1.

Rick Evans
10-01-2005, 02:24 PM
Hi

Well, that's a pretty plain vanilla ordinary class and constructor there, and a whole lot like the one from the aforementioned ResourceTestObject.

And you say that the constructor argument just fails to get set... i.e its just null (or do you get an exception)? If you do get an exception, can you post the stracktrace... this is functionality that really must work straight out of the box (and does for me, just not for you), so it would be good to get to the bottom of your issue.

Mmm. I'll go download RC1 right now and give it a try with the same test case that I described in my previous post.

[Update] Mmm... works fine for me in Spring.NET RC1 too. In any case, I have refactored the code in the ObjectWrapper class inline with your suggestion (not exactly the same way), so I guess you'll get the update in Spring.NET 1.0.1 which is due out at the start of November. In the interim, you can continue using your slightly patched version... unless you can think of something else, I guess that is it. Darn.

Later
Rick

darrell.pittman
10-01-2005, 02:48 PM
Rick,

Here is the StackTrace:


at System.ComponentModel.TypeConverter.GetConvertFrom Exception(Object value)
at System.ComponentModel.TypeConverter.ConvertFrom(IT ypeDescriptorContext context, CultureInfo culture, Object value)
at Spring.Core.IO.ResourceConverter.ConvertFrom(IType DescriptorContext context, CultureInfo culture, Object value) in c:\\projects\\springtest\\trunk\\spring.core\\core \\io\\resourceconverter.cs:line 145
at System.ComponentModel.TypeConverter.ConvertFrom(Ob ject value)
at Spring.Objects.ObjectWrapper.DoTypeConversionIfNec essary(String propertyName, String fullPropertyName, Object oldValue, Object newValue, Type requiredType) in c:\\projects\\springtest\\trunk\\spring.core\\obje cts\\objectwrapper.cs:line 906

The call to typeConverter.ConvertFrom is failing. This call doesn't happen with the brackets I suggested in my original post. Without the brackets, if typeConverter is not null the line:

if (typeConverter != null
|| requiredType != null
&& !IsAssignableFrom(newValue, requiredType))

always is true and so it ends up using the typeConverter.

Hope this helps.

Rick Evans
10-01-2005, 03:10 PM
Hi

Indeed, I see that now, thanks for the stack trace... I bet that's the first time anyone has said that to you huh? :D

That still leaves the question as to why I cannot reproduce the issue. Definitely Spring.NET RC1? And your object definitions definitely look like this...

<object singleton="false" id="userPermissions" type="Spring.Core.IO.AssemblyResource, Spring.Core">
<constructor-arg name="resourceName" value="assembly://SpringTestConsole/SpringTestConsole.Data.Security/user-permissions.xml"/>
</object>
<object id="permissionProvider" type="SpringTestConsole.AopObjects.DefaultPermissionProv ider, SpringTestConsole">
<constructor-arg name="res" ref="userPermissions"/>
</object>

Yeah, not meaning to diss you there... just stumped :?

The issue is resolved for you in the current CVS head anyways. I guess that doesn't help you today though. Darn. Are we gonna call it a day there, or what? I guess you could send me your code (email address below), but if its the same as the snippets you've posted I don't suppose there's much point. Stumped.

Ciao
Rick

darrell.pittman
10-01-2005, 04:03 PM
Rick,

I didn't mean to take up alot of your time. Sorry about that. That is definitely my xml(ie: I did "Rebuild" my solution to make sure the embedded resources are up to date) . I will double check the version of the code. Thanks for looking in to it. (Curious that you can't duplicate this problem)

Have a nice rest of the weekend.

Rick Evans
10-01-2005, 04:05 PM
Hey Darrell

No worries man. Just kinda deflated that I can't get to the bottom of the issue you've raised.

Have a nice rest of the weekend.

Yeah you too dude, ciao
Rick