implement GA - Get Attribute Method for static method calls

This commit is contained in:
Michael Becker 2025-09-28 14:30:46 -04:00
parent bce1cf6437
commit aafd99ab94
2 changed files with 51 additions and 6 deletions

View File

@ -24,17 +24,31 @@ public class GetAttributeMethodImplementation : MethodImplementation
{ {
InstanceHandle irForClass = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Method__for__Class)); InstanceHandle irForClass = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Method__for__Class));
InstanceHandle returnsAttribute = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Get_Attribute_Method__returns__Attribute)); InstanceHandle returnsAttribute = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Get_Attribute_Method__returns__Attribute));
bool isStatic = oms.GetAttributeValue<bool>(method, oms.GetInstance(KnownAttributeGuids.Boolean.Static));
if (returnsAttribute == InstanceHandle.Empty) if (returnsAttribute == InstanceHandle.Empty)
{ {
throw new InvalidOperationException("no Work Set specified for method"); throw new InvalidOperationException("no Work Set specified for method");
} }
IInstanceReference? forInstance = (IInstanceReference?) context.GetWorkData(irForClass); IInstanceReference? forInstance = null;
if (isStatic)
{
forInstance = irForClass;
if (forInstance == null)
{
// we should never get here
// throw new NullReferenceException(String.Format("static method call without instance reference {0}", oms.GetGlobalIdentifier(irForClass)));
}
}
else
{
forInstance = (IInstanceReference?)context.GetWorkData(irForClass);
if (forInstance == null) if (forInstance == null)
{ {
throw new NullReferenceException(String.Format("non-static method call without instance reference {0}", oms.GetGlobalIdentifier(irForClass))); throw new NullReferenceException(String.Format("non-static method call without instance reference {0}", oms.GetGlobalIdentifier(irForClass)));
} }
}
if (oms.IsInstanceOf(forInstance, oms.GetInstance(KnownInstanceGuids.Classes.WorkSet))) if (oms.IsInstanceOf(forInstance, oms.GetInstance(KnownInstanceGuids.Classes.WorkSet)))
{ {

View File

@ -24,7 +24,7 @@ namespace Mocha.Core.Tests.MethodTests
{ {
[Test] [Test]
public void GetAttributeMethodTest() public void GetInstanceAttributeMethodTest()
{ {
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID); InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass); InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
@ -54,5 +54,36 @@ namespace Mocha.Core.Tests.MethodTests
Assert.That(testAttributeValue2, Is.EqualTo(testAttributeValue)); Assert.That(testAttributeValue2, Is.EqualTo(testAttributeValue));
} }
[Test]
public void GetStaticAttributeMethodTest()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestAttribute = Oms.GetInstance(TEST_ATTR_GUID);
Assert.That(irTestAttribute, Is.Not.EqualTo(InstanceHandle.Empty));
Oms.AddAttribute(irTestClass, irTestAttribute);
Oms.SetAttributeValue(irTestClass, irTestAttribute, TEST_ATTR_VALUE);
string testAttributeValue = Oms.GetAttributeValue<string>(irTestClass, irTestAttribute);
Assert.That(testAttributeValue, Is.EqualTo(TEST_ATTR_VALUE));
// here, the test attribute has a particular value.
// let's make sure that calling a GA- Get Attribute method on TestClass returns the same
OmsMethodBuilder methodBuilder = new OmsMethodBuilder(Oms);
GetAttributeMethod gaMethod = methodBuilder.CreateGetAttributeMethod(irTestClass, "get", "Test Attribute", AccessModifier.Public, true, irTestAttribute);
// Method is: `Test Class@get Test Attribute(GA)*S`
ReturnAttributeMethodBinding gaMethodRamb = methodBuilder.CreateReturnAttributeMethodBinding(gaMethod);
OmsContext context = Oms.CreateContext();
object? testAttributeValue2 = Oms.ExecuteReturningAttributeValue(context, gaMethodRamb, null);
Assert.That(testAttributeValue2, Is.TypeOf<String>());
Assert.That(testAttributeValue2, Is.EqualTo(testAttributeValue));
}
} }
} }