ReferenceReplacer.cs :  » Aspect-Oriented-Frameworks » Runtime-Assembly-Instrumentation-Library » rail » Transformation » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Aspect Oriented Frameworks » Runtime Assembly Instrumentation Library 
Runtime Assembly Instrumentation Library » rail » Transformation » ReferenceReplacer.cs
/*
  The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
  you may not use this file except in compliance with the License. You may obtain a copy of the License at 
  http://www.mozilla.org/MPL/ 
  Software distributed under the License is distributed on an "AS IS" basis, 
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language 
  governing rights and limitations under the License. 

  The Original Code is RAIL(Runtime Assembly Instrumentation Library) Alpha Version.
  The Initial Developer of the Original Code is University of Coimbra,
  Computer Science Department, Dependable Systems Group. Copyright (C) University of Coimbra. 
  All Rights Reserved.  
*/
using System;
using Rail.Reflect;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections;

namespace Rail.Transformation{
  /// <summary>
  /// Replaces all occurrences of a reference to another one.
  /// <remarks>
  /// Perhaps this class should be internal and the functionality of it 
  /// exposed using a method (<code>ReplaceReference()</code>) of an utility 
  /// class?
  /// </remarks>
  /// </summary>
  public class ReferenceReplacer : EmptyVisitor
  {
    private readonly RType oldType, newType;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="oldType"></param>
    /// <param name="newType"></param>
    public ReferenceReplacer(RType oldType, RType newType)
    {
      this.oldType = oldType;
      this.newType = newType;
    }
        
    /// <summary>
    /// 
    /// </summary>
    /// <param name="field"></param>
    public override void VisitField(RFieldDef field) 
    {
      // Verify the field type  
      if (field.FieldType.FullName==this.oldType.FullName)
        field.FieldType=this.newType;
      //TODO: How to deal with Custom Attributes???
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="method"></param>
    private void VisitMethodBody(RMethodBase method)
    {
      #region Changes the body of the method
      
      MSIL.MethodBody mb = null;
      if (method is RConstructor)
        mb = ((RConstructorDef)method).MethodBody;
      else if (method is RMethod)
        mb = ((RMethodDef)method).MethodBody;

      if (mb!=null)
        foreach (MSIL.Instruction i in mb.GetCode())
        {
          if (i is MSIL.ILArgument)
          {
            MSIL.ILArgument a = (MSIL.ILArgument)i;
            if (a!=null)
              if (a.Parameter!=null)
                if (a.Parameter.ParameterType.FullName==this.oldType.FullName)
                  ((MSIL.ILArgument)i).Parameter.ParameterType = this.newType;
          }
          else if (i is MSIL.ILField)
          {
            MSIL.ILField b = (MSIL.ILField)i;
            if (b.Field.DeclaringType.FullName==method.DeclaringType.FullName &&
              b.Field.FieldType.FullName==this.oldType.FullName)
            {
              ((MSIL.ILField)i).Field.FieldType=this.newType;
            }
            else if (b.Field.FieldType.FullName==this.oldType.FullName &&
              b.Field.DeclaringType.FullName==this.oldType.FullName)
            {
              ((MSIL.ILField)i).Field= this.newType.GetField(b.Field.Name);
            }
            else if (b.Field.DeclaringType.FullName==this.oldType.FullName)
            {
              ((MSIL.ILField)i).Field = this.newType.GetField(b.Field.Name);
            }
            if (((MSIL.ILField)i).Field==null)
              throw new NullReferenceException();
          }
          else if (i is MSIL.ILLocalVariable)
          {
            MSIL.ILLocalVariable c = (MSIL.ILLocalVariable)i;
            if (c.Local.Type.FullName==this.oldType.FullName)
              ((MSIL.ILLocalVariable)i).Local.Type = this.newType;
          }
          else if (i is MSIL.ILMethod)
          {
            MSIL.ILMethod d = (MSIL.ILMethod)i;
            if (d.Method.DeclaringType.FullName==this.oldType.FullName)
            {
              if (d.Method is RMethod)
                ((MSIL.ILMethod)i).Method = this.newType.GetMethod(
                  d.Method.Name,
                  ((RMethod)d.Method).ReturnType.ReturnType,
                  d.Method.GetParameters());
              else if (d.Method is RConstructor)
                ((MSIL.ILMethod)i).Method = this.newType.GetConstructor(
                  d.Method.GetParameters());
              if (((MSIL.ILMethod)i).Method==null)
                throw new NullReferenceException();

            }
          }
          else if (i is MSIL.ILToken)
          {
            MSIL.ILToken e = (MSIL.ILToken)i;
            if (e.Token is RField)
            {
              RField rf = e.Token as RField;
              if (rf.DeclaringType.FullName==method.DeclaringType.FullName &&
                rf.FieldType.FullName==this.oldType.FullName)
              {
                ((RField)((MSIL.ILToken)i).Token).FieldType=this.newType;
              }
              if (rf.FieldType.FullName==this.oldType.FullName &&
                rf.DeclaringType.FullName==this.oldType.FullName)
              {
                ((MSIL.ILToken)i).Token=this.newType.GetField(rf.Name);
              }
              else if (rf.DeclaringType.FullName==this.oldType.FullName)
              {
                ((MSIL.ILToken)i).Token = this.newType.GetField(rf.Name);
                if (((MSIL.ILToken)i).Token ==null)
                  throw new NullReferenceException();
              }
            }
            else if (e.Token is RMethod)
            {
              RMethod rm = e.Token as RMethod;
              if (rm.DeclaringType.FullName==this.oldType.FullName)
              {
                ((MSIL.ILToken)i).Token = this.newType.GetMethod(
                  rm.Name,
                  ((RMethod)rm).ReturnType.ReturnType,
                  rm.GetParameters());
                if (((MSIL.ILToken)i).Token==null)
                  throw new NullReferenceException();
              }
            }
            else if (e.Token is RType)
            {
              RType rt = e.Token as RType;
              if (rt.FullName==this.oldType.FullName)
                ((MSIL.ILToken)i).Token=this.newType;
            }
            else 
              throw new InvalidOperationException();
          }
          else if (i is MSIL.ILType)
          {
            MSIL.ILType f = (MSIL.ILType)i;
            if (f.Type.FullName==this.oldType.FullName)
              ((MSIL.ILType)i).Type=this.newType;
          }
        }
      
      #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="method"></param>
    public override void VisitMethod(RMethodDef method) 
    {
      // Verify: return value, arguments and references in the code
      if (method.ReturnType.ReturnType.FullName==this.oldType.FullName)
        method.ReturnType.ReturnType=this.newType;
      RParameter [] parames = method.GetParameters();
      if (parames!=null)
        for (int i = 0; i <parames.Length;i++)
        {
          if (parames[i].ParameterType.FullName==this.oldType.FullName)
            parames[i].ParameterType=this.newType;
        }
      VisitMethodBody((RMethodBase)method);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="property"></param>
    public override void VisitProperty(RPropertyDef property) 
    {
      // Verify: property type and references in the code of the accessors.
      // TODO: Probably the "value" must also be handled in some way.
      if (property.PropertyType.FullName==this.oldType.FullName)
        property.PropertyType = this.newType;
      RMethodDef rM = (RMethodDef)property.GetMethod;
      if (rM!=null)
        this.VisitMethod(rM);
      rM = (RMethodDef)property.SetMethod;
      if (rM!=null)
        this.VisitMethod(rM);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="method"></param>
    public override void VisitConstructor(RConstructorDef method) 
    {
      // Verifiy: arguments and references in code
      RParameter [] parames = method.GetParameters();
      if (parames!=null)
        for (int i = 0; i <parames.Length;i++)
        {
          if (parames[i].ParameterType.FullName==this.oldType.FullName)
            parames[i].ParameterType=this.newType;
        }
      VisitMethodBody((RMethodBase)method);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="evt"></param>
    public override void VisitEvent(REventDef evt) 
    {
      // Verify: event type, eventArgsType and associated methods. 
      // (add, remove and raise)
      RMethodDef rM = (RMethodDef)evt.AddOnMethod;
      if (rM!=null)
        this.VisitMethod(rM);
      if (evt.EventArgsType.FullName==this.oldType.FullName)
        evt.EventArgsType=this.newType;
      if (evt.EventHandlerType.FullName==this.oldType.FullName)
        evt.EventHandlerType=this.newType;
      if (evt.EventType.FullName==this.oldType.FullName)
        evt.EventType=this.newType;
      rM = (RMethodDef)evt.RaiseMethod;
      if (rM!=null)
        this.VisitMethod(rM);
      rM = (RMethodDef)evt.RemoveOnMethod;
      if (rM!=null)
        this.VisitMethod(rM);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="type"></param>
    public override void VisitType(RTypeDef type) 
    {
      // Verify: implemented interfaces and classes
      if (type.ElementType!=null)
        if (type.ElementType.FullName==this.oldType.FullName)
          type.ElementType=this.newType;
      RType t = type.GetInterface(this.oldType.FullName);
      if (t!=null)
      {
        type.RemoveInterface(t);
        type.AddInterface(this.newType);
      }
    }
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.