public static void setProperty(
     Object messageArgument, Class senderClass, Object receiver, String messageName)
     throws Throwable {
   try {
     if (receiver == null) receiver = NullObject.getNullObject();
     InvokerHelper.setProperty(receiver, messageName, messageArgument);
   } catch (GroovyRuntimeException gre) {
     throw unwrap(gre);
   }
 }
 /**
  * Provides a hook for type coercion of the given object to the required type
  *
  * @param type of object to convert the given object to
  * @param object the object to be converted
  * @return the original object or a new converted value
  * @throws Throwable if the coercion fails
  */
 public static Object asType(Object object, Class type) throws Throwable {
   if (object == null) object = NullObject.getNullObject();
   return invokeMethodN(object.getClass(), object, "asType", new Object[] {type});
 }
  public void testRestrictAgainstObject() {

    String xml = "<?xml version=\"1.0\"?>";
    xml +=
        "<inspection-result xmlns=\"http://www.metawidget.org/inspection-result\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.metawidget.org/inspection-result ../../inspector/inspection-result-1.0.xsd\" version=\"1.0\">";
    xml += "<entity type=\"ImaginaryObject\">";
    xml += "<property name=\"foo\" type=\"ImaginaryObject\"/>";
    xml += "</entity>";
    xml += "<entity type=\"" + NullObject.class.getName() + "\">";
    xml +=
        "<property name=\"nestedNullObject\" type=\"org.metawidget.inspector.xml.XmlInspectorTest$NullObject\"/>";
    xml += "</entity>";
    xml += "</inspection-result>";

    // Without restrictAgainstObject

    mInspector =
        new XmlInspector(
            new XmlInspectorConfig().setInputStream(new ByteArrayInputStream(xml.getBytes())));

    assertEquals(null, mInspector.inspect(null, "MissingObject"));
    assertTrue(null != mInspector.inspect(null, "ImaginaryObject"));
    assertTrue(null != mInspector.inspect(null, NullObject.class.getName()));

    NullObject nullObject = new NullObject();
    assertTrue(null != mInspector.inspect(nullObject, NullObject.class.getName()));
    assertTrue(
        null != mInspector.inspect(nullObject, NullObject.class.getName(), "nestedNullObject"));

    // With restrictAgainstObject

    mInspector =
        new XmlInspector(
            new XmlInspectorConfig()
                .setRestrictAgainstObject(
                    new JavaBeanPropertyStyle(
                        new JavaBeanPropertyStyleConfig().setSupportPublicFields(true)))
                .setInputStream(new ByteArrayInputStream(xml.getBytes())));
    assertTrue(null != mInspector.inspect(null, "ImaginaryObject"));
    assertTrue(null != mInspector.inspect(null, NullObject.class.getName()));
    assertTrue(null != mInspector.inspect(nullObject, NullObject.class.getName()));
    assertEquals(null, mInspector.inspect("", NullObject.class.getName()));
    assertEquals(
        mInspector.inspect(nullObject, NullObject.class.getName(), "nestedNullObject"),
        "<inspection-result xmlns=\"http://metawidget.org/inspection-result\" version=\"1.0\"><entity name=\"nestedNullObject\" type=\"org.metawidget.inspector.xml.XmlInspectorTest$NullObject\"/></inspection-result>");
    assertEquals(null, mInspector.inspect(nullObject, NullObject.class.getName(), "foo"));
    assertEquals(
        null, mInspector.inspect(null, NullObject.class.getName(), "nestedNullObject", "foo"));

    // With several levels deep

    nullObject.setNestedNullObject(new NullObject());
    assertEquals(
        mInspector.inspect(nullObject, NullObject.class.getName(), "nestedNullObject"),
        "<inspection-result xmlns=\"http://metawidget.org/inspection-result\" version=\"1.0\"><entity name=\"nestedNullObject\" type=\"org.metawidget.inspector.xml.XmlInspectorTest$NullObject\"><property name=\"nestedNullObject\" type=\"org.metawidget.inspector.xml.XmlInspectorTest$NullObject\"/></entity></inspection-result>");
    assertEquals(
        mInspector.inspect(
            nullObject, NullObject.class.getName(), "nestedNullObject", "nestedNullObject"),
        "<inspection-result xmlns=\"http://metawidget.org/inspection-result\" version=\"1.0\"><entity name=\"nestedNullObject\" type=\"org.metawidget.inspector.xml.XmlInspectorTest$NullObject\"/></inspection-result>");
    assertEquals(
        null,
        mInspector.inspect(
            nullObject,
            NullObject.class.getName(),
            "nestedNullObject",
            "nestedNullObject",
            "nestedNullObject"));

    // With recursion

    nullObject.setNestedNullObject(nullObject);
    assertTrue(null != mInspector.inspect(nullObject, NullObject.class.getName()));
    assertEquals(
        null, mInspector.inspect(nullObject, NullObject.class.getName(), "nestedNullObject"));

    if (LogUtils.getLog(XmlInspector.class).isTraceEnabled()) {
      assertEquals(
          "XmlInspector prevented infinite recursion on org.metawidget.inspector.xml.XmlInspectorTest$NullObject/nestedNullObject. Consider marking nestedNullObject as hidden='true'",
          LogUtilsTest.getLastTraceMessage());
    } else {
      assertEquals(
          "Prevented infinite recursion on {0}{1}. Consider marking {2} as hidden",
          LogUtilsTest.getLastTraceMessage());
      assertEquals(
          "org.metawidget.inspector.xml.XmlInspectorTest$NullObject",
          LogUtilsTest.getLastTraceArguments()[0]);
      assertEquals("/nestedNullObject", LogUtilsTest.getLastTraceArguments()[1]);
      assertEquals("nestedNullObject", LogUtilsTest.getLastTraceArguments()[2]);
    }
  }