private void morph(HGPersistentHandle instanceHandle,HGAtomType oldType,HGAtomType newType){
  HGPersistentHandle[] layout=store.getLink(instanceHandle);
  Object oldInstance=rawMake(layout,oldType,instanceHandle);
  TypeUtils.releaseValue(this,oldType,layout[1]);
  indexByValue.removeEntry(layout[1],instanceHandle);
  layout[1]=TypeUtils.storeValue(this,oldInstance,newType);
  indexByValue.addEntry(layout[1],instanceHandle);
  store.store(instanceHandle,layout);
  Object newInstance=rawMake(layout,newType,instanceHandle);
  HGLiveHandle instanceLiveHandle=cache.get(instanceHandle);
  if (instanceLiveHandle != null && instanceLiveHandle.getRef() != null)   cache.atomRefresh(instanceLiveHandle,newInstance,true);
  if (oldInstance instanceof HGAtomType) {
    HGSearchResult<HGPersistentHandle> rs=null;
    try {
      rs=indexByType.find(instanceHandle);
      if (rs.hasNext() && !(newInstance instanceof HGAtomType))       throw new HGException("Cannot replace value of atom " + instanceHandle + " which was a type with something that is not a type");
      oldType=(HGAtomType)oldInstance;
      newType=(HGAtomType)newInstance;
      while (rs.hasNext()) {
        morph((HGPersistentHandle)rs.next(),oldType,newType);
      }
    }
  finally {
      if (rs != null)       rs.close();
    }
  }
}

private void morph ( HGPersistentHandle instanceHandle , HGAtomType oldType , HGAtomType newType ) { HGPersistentHandle [ ] layout = store . getLink ( instanceHandle ) ; Object oldInstance = rawMake ( layout , oldType , instanceHandle ) ; TypeUtils . releaseValue ( this , oldType , layout [ 1 ] ) ; indexByValue . removeEntry ( layout [ 1 ] , instanceHandle ) ; layout [ 1 ] = TypeUtils . storeValue ( this , oldInstance , newType ) ; indexByValue . addEntry ( layout [ 1 ] , instanceHandle ) ; store . store ( instanceHandle , layout ) ; Object newInstance = rawMake ( layout , newType , instanceHandle ) ; HGLiveHandle instanceLiveHandle = cache . get ( instanceHandle ) ; if ( instanceLiveHandle != null && instanceLiveHandle . getRef ( ) != null ) cache . atomRefresh ( instanceLiveHandle , newInstance , true ) ; if ( oldInstance instanceof HGAtomType ) { HGSearchResult < HGPersistentHandle > rs = null ; try { rs = indexByType . find ( instanceHandle ) ; if ( rs . hasNext ( ) && ! ( newInstance instanceof HGAtomType ) ) throw new HGException ( "Cannot replace value of atom " + instanceHandle + " which was a type with something that is not a type" ) ; oldType = ( HGAtomType ) oldInstance ; newType = ( HGAtomType ) newInstance ; while ( rs . hasNext ( ) ) { morph ( ( HGPersistentHandle ) rs . next ( ) , oldType , newType ) ; } } finally { if ( rs != null ) rs . close ( ) ; } } }
change the type value of an atom value . it is assumed that oldtype and newtype keep the same handle . this is used when we are replacing the value of a type atom with another value ( which must be a compatible type value ) . morphing can potentially lead to a different run - time instance of the atom . so , if the atom is " alive " , we replace the run - time instance .
