public static Stella_Object coerceToInstanceInModule(Stella_Object self, Module module, boolean localP, Stella_Object original) {
    if (self == null) {
      if (original != null) {
        System.out.println("Can't find the entity referenced as: `" + original + "'.");
      }
      return (null);
    }
    { Surrogate testValue000 = Stella_Object.safePrimaryType(self);

      if (Surrogate.subtypeOfP(testValue000, Logic.SGT_LOGIC_LOGIC_OBJECT)) {
        { LogicObject self000 = ((LogicObject)(self));

          return (self000);
        }
      }
      else if (Surrogate.subtypeOfStringP(testValue000)) {
        { StringWrapper self000 = ((StringWrapper)(self));

          { String name = self000.wrapperValue;
            Module modulefromstring = null;

            if (Stella.qualifiedStellaNameP(name)) {
              { Object [] caller_MV_returnarray = new Object[1];

                modulefromstring = Stella.computeModuleAndBareName(name, caller_MV_returnarray);
                name = ((String)(((StringWrapper)(caller_MV_returnarray[0])).wrapperValue));
              }
            }
            if (module == null) {
              if (modulefromstring != null) {
                module = modulefromstring;
              }
              else {
                module = ((Module)(Stella.$MODULE$.get()));
              }
            }
            { Surrogate surrogate = Surrogate.lookupSurrogateInModule(name, module, localP);

              if ((surrogate == null) ||
                  (surrogate.surrogateValue == null)) {
                if ((surrogate != null) &&
                    (Logic.homeObjectStore(surrogate) != null)) {
                  Logic.homeObjectStore(surrogate).fetchInstance(surrogate);
                }
                else if ((surrogate == null) &&
                    (((ObjectStore)(KeyValueList.dynamicSlotValue(module.dynamicSlots, Logic.SYM_LOGIC_OBJECT_STORE, null))) != null)) {
                  ((ObjectStore)(KeyValueList.dynamicSlotValue(module.dynamicSlots, Logic.SYM_LOGIC_OBJECT_STORE, null))).fetchInstance(Surrogate.internSurrogateInModule(name, module, localP));
                }
              }
              if ((surrogate != null) &&
                  (surrogate.surrogateValue != null)) {
                return (Logic.coerceToInstanceInModule(surrogate, module, true, original));
              }
              else {
                return (null);
              }
            }
          }
        }
      }
      else if (Surrogate.subtypeOfSymbolP(testValue000)) {
        { Symbol self000 = ((Symbol)(self));

          { Module startingmodule = ((module != null) ? module : ((Module)(self000.homeContext)));
            Stella_Object value = Logic.coerceToInstanceInModule(Surrogate.lookupSurrogateInModule(self000.symbolName, startingmodule, localP), null, localP, null);

            if (value != null) {
              return (value);
            }
            else if (!(module == ((Module)(Stella.$MODULE$.get())))) {
              return (Logic.coerceToInstanceInModule(Surrogate.lookupSurrogateInModule(self000.symbolName, ((Module)(Stella.$MODULE$.get())), localP), ((Module)(Stella.$MODULE$.get())), localP, original));
            }
            else if (original != null) {
              return (Logic.coerceToInstanceInModule(null, null, false, original));
            }
            else {
              return (null);
            }
          }
        }
      }
      else if (Surrogate.subtypeOfSurrogateP(testValue000)) {
        { Surrogate self000 = ((Surrogate)(self));

          { Stella_Object value = self000.surrogateValue;

            if (value != null) {
              return (Logic.coerceToInstanceInModule(value, null, true, original));
            }
            else {
              return (Logic.coerceToInstanceInModule(StringWrapper.wrapString(self000.symbolName), ((Module)(self000.homeContext)), localP, original));
            }
          }
        }
      }
      else if (Surrogate.subtypeOfP(testValue000, Logic.SGT_STELLA_RELATION)) {
        { Relation self000 = ((Relation)(self));

          return (Logic.getDescription(self000));
        }
      }
      else if (Surrogate.subtypeOfKeywordP(testValue000)) {
        { Keyword self000 = ((Keyword)(self));

          return (Logic.coerceToInstanceInModule(StringWrapper.wrapString(self000.symbolName), module, localP, original));
        }
      }
      else if (Surrogate.subtypeOfP(testValue000, Logic.SGT_STELLA_THING)) {
        { Thing self000 = ((Thing)(self));

          return (self000);
        }
      }
      else if (Surrogate.subtypeOfP(testValue000, Logic.SGT_LOGIC_PROPOSITION)) {
        { Proposition self000 = ((Proposition)(self));

          return (self000);
        }
      }
      else {
        return (Logic.coerceToInstanceInModule(null, null, false, original));
      }
    }
  }