/*
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>
/// Useful to redirect field access between to fields. This visitor implementes
/// <code>VisitMethod</code> and <code>VisitConstructor</code> methods
/// </summary>
/// <remarks>
/// The fields must be of the same type. They both have to be instance fields
/// or static fields, you cannot use a static field to replace an instance
/// field or vice-versa.
/// If they are instance fields both have to be declared in the same type.
/// </remarks>
public class FieldAccessReplacer : EmptyVisitor
{
/// <summary>
///
/// </summary>
private readonly RField oldField, newField;
/// <summary>
/// Public constructor
/// </summary>
/// <param name="oldField">The RField to be replaced</param>
/// <param name="newField">The replacement RField</param>
public FieldAccessReplacer(RField oldField, RField newField)
{
this.oldField = oldField;
this.newField = newField;
}
/// <summary>
/// This method redirects the access of one field to the other inside
/// the method body of methods and constructors
/// </summary>
/// <param name="method">The RMethodBase whoch method body is to be instrumented</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;
MSIL.Code code = null;
if (mb!=null)
code = mb.GetCode();
if (code!=null)
foreach (MSIL.Instruction i in code)
{
if (i is MSIL.ILField)
{
MSIL.ILField d = (MSIL.ILField)i;
if (d.Field.Equals(this.oldField))
{
((MSIL.ILField)i).Field = this.newField;
if (((MSIL.ILField)i).Field==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.Equals(this.oldField))
{
((MSIL.ILToken)i).Token = this.newField;
if (((MSIL.ILToken)i).Token==null)
throw new NullReferenceException();
}
}
}
}
#endregion
}
/// <summary>
///
/// </summary>
/// <param name="method"></param>
public override void VisitMethod(RMethodDef method)
{
//check the method body
VisitMethodBody((RMethodBase)method);
}
/// <summary>
///
/// </summary>
/// <param name="method"></param>
public override void VisitConstructor(RConstructorDef method)
{
//Check the method body
VisitMethodBody((RMethodBase)method);
}
}
/// <summary>
/// Useful to redirect field access between a field and a property. This visitor
/// implementes <code>VisitMethod</code> and <code>VisitConstructor</code> methods
/// </summary>
/// <remarks>
/// The field and the property must be of the same type.
/// They both have to be instance or static, you cannot use a static property to replace
/// an instance field or an instance property to replace a satic field.
/// If they are instance fields both have to be declared in the same type.
/// </remarks>
public class FieldToPropAccessReplacer : EmptyVisitor
{
/// <summary>
///
/// </summary>
private readonly RField origField;
/// <summary>
///
/// </summary>
private readonly RProperty newProp;
/// <summary>
/// The constructor
/// </summary>
/// <param name="origField">The field to replace</param>
/// <param name="newProp">The replacement property</param>
public FieldToPropAccessReplacer(RField origField, RProperty newProp)
{
this.origField = origField;
this.newProp = newProp;
}
/// <summary>
/// This method redirects the accesses to a field to a property inside
/// RMethodDef and RConstructorDef method body
/// </summary>
/// <param name="method">The RMethodBase of the method or of the constructor</param>
private void VisitMethodBody(RMethodBase method)
{
#region Changes the body of the method
bool changed = false;
MSIL.MethodBody mb = null;
if (method is RConstructor)
mb = ((RConstructorDef)method).MethodBody;
else if (method is RMethod)
mb = ((RMethodDef)method).MethodBody;
MSIL.Code code = null;
if (mb!=null)
code = mb.GetCode();
if (code!=null)
{
code.updateInstructionIndex();
foreach (MSIL.Instruction i in code)
{
if (i is MSIL.ILField)
{
MSIL.ILField d = (MSIL.ILField)i;
if (d.Field.Equals(this.origField))
{
if (d.OpCode.Value==OpCodes.Ldsfld.Value ||
d.OpCode.Value==OpCodes.Ldsflda.Value ||
d.OpCode.Value==OpCodes.Ldfld.Value ||
d.OpCode.Value==OpCodes.Ldflda.Value)
{
ReplaceInstruction(code,this.newProp.GetMethod,d.index);
}
else
{
ReplaceInstruction(code,this.newProp.SetMethod,d.index);
}
changed = true;
}
}
if (changed)
{
VisitMethodBody(method);
break;
}
}
}
#endregion
}
/// <summary>
/// The method used to replace a field access instruction with a property access method
/// in the method body code
/// </summary>
/// <param name="code">The code of the method to instrument</param>
/// <param name="method">The property access method</param>
/// <param name="pos">The position on the code where to replace the instruction</param>
private void ReplaceInstruction(MSIL.Code code, RMethod method,int pos)
{
MSIL.ILMethod ilm = new MSIL.ILMethod(OpCodes.Call,method);
code.Remove(pos);
code.Insert(pos,ilm);
code.updateInstructionIndex();
}
/// <summary>
///
/// </summary>
/// <param name="method"></param>
public override void VisitMethod(RMethodDef method)
{
//check the method body
VisitMethodBody((RMethodBase)method);
}
/// <summary>
///
/// </summary>
/// <param name="method"></param>
public override void VisitConstructor(RConstructorDef method)
{
//Check the method body
VisitMethodBody((RMethodBase)method);
}
}
/// <summary>
/// Useful to change field read access to a method
/// </summary>
/// <remarks>
/// Both field and method must be static or they both have to be instance.
/// If the are instance they have to be defined on the same type
/// </remarks>
public class ReplaceFieldReadAccess : EmptyVisitor
{
/// <summary>
///
/// </summary>
private readonly RField origField;
/// <summary>
///
/// </summary>
private readonly RMethod newMethod;
/// <summary>
/// Constructor
/// </summary>
/// <param name="origField">The field which read access is to be replaced</param>
/// <param name="newMethod">The method to call instead of the field</param>
public ReplaceFieldReadAccess(RField origField, RMethod newMethod)
{
this.origField = origField;
this.newMethod = newMethod;
}
/// <summary>
/// This method is used to instrument the code in <code>RMEthodDef</code> and <code>RConstrucorDef</code>
/// ojects
/// </summary>
/// <param name="method">the <code>RMethodBase</code> object to be instrumented</param>
private void VisitMethodBody(RMethodBase method)
{
#region Changes the body of the method
bool changed = false;
MSIL.MethodBody mb = null;
if (method is RConstructor)
mb = ((RConstructorDef)method).MethodBody;
else if (method is RMethod)
mb = ((RMethodDef)method).MethodBody;
MSIL.Code code = null;
if (mb!=null)
code = mb.GetCode();
if (code!=null)
{
code.updateInstructionIndex();
foreach (MSIL.Instruction i in code)
{
if (i is MSIL.ILField)
{
MSIL.ILField d = (MSIL.ILField)i;
if (d.Field.Equals(this.origField))
{
if (d.OpCode.Value==OpCodes.Ldsfld.Value ||
d.OpCode.Value==OpCodes.Ldsflda.Value ||
d.OpCode.Value==OpCodes.Ldfld.Value ||
d.OpCode.Value==OpCodes.Ldflda.Value)
{
ReplaceInstruction(code,this.newMethod,d.index);
changed = true;
}
}
}
if (changed)
{
VisitMethodBody(method);
break;
}
}
}
#endregion
}
/// <summary>
/// Replace a field read access instruction with a method call instruction
/// </summary>
/// <param name="code">The Code of the method to be instrumented</param>
/// <param name="method">The method to call in replacement of the field access</param>
/// <param name="pos">The position in the code where to perform the change</param>
/// <remarks>
/// If the Field is static the the method must be static, if the field is instance
/// then the method must be defined on the same type
/// </remarks>
private void ReplaceInstruction(MSIL.Code code, RMethod method,int pos)
{
MSIL.ILField d = (MSIL.ILField)code[pos];
MSIL.ILMethod ilm = new MSIL.ILMethod(OpCodes.Call,method);
if ((d.OpCode.Value==OpCodes.Ldsfld.Value ||
d.OpCode.Value==OpCodes.Ldsflda.Value) &&
(method.Attributes & MethodAttributes.Static) !=0)
{
code.Remove(pos);
code.Insert(pos,ilm);
}
else
{
code.Remove(pos);
code.Insert(pos,ilm);
}
code.updateInstructionIndex();
}
/// <summary>
///
/// </summary>
/// <param name="method"></param>
public override void VisitMethod(RMethodDef method)
{
//check the method body
VisitMethodBody((RMethodBase)method);
}
/// <summary>
///
/// </summary>
/// <param name="method"></param>
public override void VisitConstructor(RConstructorDef method)
{
//Check the method body
VisitMethodBody((RMethodBase)method);
}
}
/// <summary>
/// Useful to change field write access to a method
/// </summary>
/// <remarks>
/// Both field and method must be static or they both have to be instance.
/// If the are instance they have to be defined on the same type
/// </remarks>
public class ReplaceFieldWriteAccess : EmptyVisitor
{
/// <summary>
///
/// </summary>
private readonly RField origField;
/// <summary>
///
/// </summary>
private readonly RMethod newMethod;
/// <summary>
/// Constructor
/// </summary>
/// <param name="origField">The field which write access is to be replaced</param>
/// <param name="newMethod">The method to call instead of the field</param>
public ReplaceFieldWriteAccess(RField origField, RMethod newMethod)
{
this.origField = origField;
this.newMethod = newMethod;
}
/// <summary>
/// This method is used to instrument the code in <code>RMethodDef</code> and <code>RConstrucorDef</code>
/// ojects
/// </summary>
/// <param name="method">the <code>RMethodBase</code> object to be instrumented</param>
private void VisitMethodBody(RMethodBase method)
{
#region Changes the body of the method
bool changed = false;
MSIL.MethodBody mb = null;
if (method is RConstructor)
mb = ((RConstructorDef)method).MethodBody;
else if (method is RMethod)
mb = ((RMethodDef)method).MethodBody;
MSIL.Code code = null;
if (mb!=null)
code = mb.GetCode();
if (code!=null)
{
code.updateInstructionIndex();
foreach (MSIL.Instruction i in code)
{
if (i is MSIL.ILField)
{
MSIL.ILField d = (MSIL.ILField)i;
if (d.Field.Equals(this.origField))
{
if (d.OpCode.Value==OpCodes.Stfld.Value ||
d.OpCode.Value==OpCodes.Stsfld.Value)
{
ReplaceInstruction(code,this.newMethod,d.index);
changed = true;
}
}
}
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.Equals(this.origField))
{
//Check this
throw new NotImplementedException();
}
}
}
if (changed)
{
VisitMethodBody(method);
break;
}
}
}
#endregion
}
/// <summary>
/// Replace a field write access instruction with a method call instruction
/// </summary>
/// <param name="code">The Code of the method to be instrumented</param>
/// <param name="method">The method to call in replacement of the field access</param>
/// <param name="pos">The position in the code where to perform the change</param>
/// <remarks>
/// If the Field is static the the method must be static, if the field is instance
/// then the method must be defined on the same type
/// </remarks>
private void ReplaceInstruction(MSIL.Code code, RMethod method,int pos)
{
MSIL.ILField d = (MSIL.ILField)code[pos];
MSIL.ILMethod ilm = new MSIL.ILMethod(OpCodes.Call,method);
if (d.OpCode.Value==OpCodes.Stsfld.Value &&
(method.Attributes & MethodAttributes.Static) !=0)
{
code.Remove(pos);
code.Insert(pos,ilm);
}
else
{
code.Remove(pos);
code.Insert(pos,ilm);
}
code.updateInstructionIndex();
}
/// <summary>
///
/// </summary>
/// <param name="method"></param>
public override void VisitMethod(RMethodDef method)
{
//check the method body
VisitMethodBody((RMethodBase)method);
}
/// <summary>
///
/// </summary>
/// <param name="method"></param>
public override void VisitConstructor(RConstructorDef method)
{
//Check the method body
VisitMethodBody((RMethodBase)method);
}
}
}
|