aaron-nance / as3-commons

Automatically exported from code.google.com/p/as3-commons
0 stars 0 forks source link

Metadata cannot be retrieved for private/helper classes #36

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The following program fails with this error:

  Error: A class with the name 'Test.as$0::Helper' could not be found.

The same thing happens when ByteCodeType.forInstance or the similar Type 
methods are used.

Is this expected?  Is this a limitation of AS3 or a problem in the 
as3-commons-reflect library?

It would be very useful if private/helper classes could be treated just like 
any other class with respect to reflection.

------------

// Test.as

package
{
    import org.as3commons.bytecode.reflect.ByteCodeType;

    public class Test extends Sprite
    {
        public function Test()
        {
            ByteCodeType.forClass(Helper); // ERROR!
        }
    }
}

class Helper { }

Original issue reported on code.google.com by idontneedthisacct@gmail.com on 27 Jan 2011 at 7:54

GoogleCodeExporter commented 8 years ago
Clarification...

Actually, Type/ByteCodeType.forClass() does work!  But, 
Type/ByteCodeType.forInstance() fails (which is actually what I was 
encountering in my real code).

Also, I think I see the possible cause...

The information returned by forClass shows that the fullName of Helper is 
"Test.as$0.Helper".  But, the Error thrown by forInstance complains about not 
being able to find a class named "Test.as$0::Helper".

So, the issue might be that the library is using "::" instead of "." somewhere?

Revised example:

------------

// Test.as

package
{
    import org.as3commons.bytecode.reflect.ByteCodeType;

    public class Test extends Sprite
    {
        public function Test()
        {
            var t:ByteCodeType = ByteCodeType.forClass(Helper); // OK
            ByteCodeType.forName(t.fullName); // OK
            trace(t.fullName); // "Test.as$0.Helper"
            ByteCodeType.forInstance(new Helper()); // ERROR!
        }
    }
}

class Helper { }

Original comment by idontneedthisacct@gmail.com on 27 Jan 2011 at 8:58

GoogleCodeExporter commented 8 years ago
Btw, for now, to avoid the ByteCodeType.forInstance failures on private/helper 
classes, I am using the following work-around:

    function byteCodeTypeForInstance(inst:*):ByteCodeType
    {
        var name:String = StringUtil.replace(getQualifiedClassName(inst), "::", "."); 
        return ByteCodeType.forName(name);
    }

This seems to work for instances of any class.

Original comment by idontneedthisacct@gmail.com on 27 Jan 2011 at 9:56

GoogleCodeExporter commented 8 years ago
Thanks for bringing this up, the ByteCodeType.forName() will now first 
normalize the class name before trying to retrieve it from the cache.

Original comment by ihatelivelyids on 28 Jan 2011 at 12:05

GoogleCodeExporter commented 8 years ago
What version will this fix appear in?

I am currently using...

   as3commons-reflect-1.3.4.swc
   as3commons-bytecode-0.9.7.swc

but ByteCodeType.forInstance() is still failing I as described in my previous 
comments.

Original comment by idontneedthisacct@gmail.com on 31 Jan 2011 at 11:07

GoogleCodeExporter commented 8 years ago
I made some modifications in the trunk, (which I didn't test thoroughly I 
readily admit), ByteCodeType.forClass() now checks if the clazz property is 
null after retrieving it from the cache, if its null (in the case of private 
classes) it will just assign the Class instance that was passed into the 
forClass() method the the clazz property of the specified ByteCodeType instance.
This fix will appear in the next release.

Original comment by rol...@stackandheap.com on 16 Mar 2011 at 10:01