2 // RemoveSerialization.cs
5 // Jb Evain (jbevain@novell.com)
7 // (C) 2007 Novell, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System
.Collections
;
33 using Mono
.Linker
.Steps
;
37 namespace Mono
.Tuner
{
39 public class RemoveSerialization
: BaseStep
{
41 static readonly string _Serialization
= "System.Runtime.Serialization";
42 static readonly string _ISerializable
= Concat (_Serialization
, "ISerializable");
43 static readonly string _IDeserializationCallback
= Concat (_Serialization
, "IDeserializationCallback");
44 static readonly string _SerializationInfo
= Concat (_Serialization
, "SerializationInfo");
45 static readonly string _StreamingContext
= Concat (_Serialization
, "StreamingContext");
47 static readonly string _GetObjectData
= "GetObjectData";
48 static readonly string _OnDeserialization
= "OnDeserialization";
50 static string Concat (string lhs
, string rhs
)
52 return string.Concat (lhs
, ".", rhs
);
55 protected override void ProcessAssembly (AssemblyDefinition assembly
)
57 if (assembly
.Name
.Name
== Constants
.Corlib
)
60 if (Annotations
.GetAction (assembly
) != AssemblyAction
.Link
)
63 foreach (ModuleDefinition module
in assembly
.Modules
)
64 foreach (TypeDefinition type
in module
.Types
)
68 static void RemoveInterface (TypeDefinition type
, string name
)
70 for (int i
= 0; i
< type
.Interfaces
.Count
; i
++) {
71 TypeReference iface
= type
.Interfaces
[i
];
72 if (iface
.FullName
== name
) {
73 type
.Interfaces
.RemoveAt (i
);
79 static void RemoveSerializableFlag (TypeDefinition type
)
81 type
.Attributes
&= ~TypeAttributes
.Serializable
;
84 static void ProcessType (TypeDefinition type
)
86 RemoveSerializableFlag (type
);
88 RemoveInterface (type
, _ISerializable
);
89 RemoveConstructor (type
, _SerializationInfo
, _StreamingContext
);
90 RemoveInterfaceMethod (type
, _ISerializable
, _GetObjectData
, _SerializationInfo
, _StreamingContext
);
92 RemoveInterface (type
, _IDeserializationCallback
);
93 RemoveInterfaceMethod (type
, _IDeserializationCallback
, _OnDeserialization
, Constants
.Object
);
98 static void RemoveField (TypeDefinition type
)
100 for (int i
= 0; i
< type
.Fields
.Count
; i
++) {
101 FieldDefinition field
= type
.Fields
[i
];
102 if (field
.FieldType
.FullName
== _SerializationInfo
) {
103 type
.Fields
.RemoveAt (i
);
109 static bool ParametersMatch (IMethodSignature meth
, string [] parameters
)
111 for (int i
= 0; i
< parameters
.Length
; i
++) {
112 ParameterDefinition param
= meth
.Parameters
[i
];
113 if (param
.ParameterType
.FullName
!= parameters
[i
])
120 static void RemoveInterfaceMethod (TypeDefinition type
, string iface
, string method
, params string [] parameters
)
122 RemoveMethod (type
, method
, parameters
);
123 RemoveMethod (type
, Concat (iface
, method
), parameters
);
126 static void RemoveMethod (TypeDefinition type
, string name
, params string [] parameters
)
128 RemoveMethod (type
.Methods
, name
, parameters
);
131 static void RemoveConstructor (TypeDefinition type
, params string [] parameters
)
133 RemoveMethod (type
.Constructors
, MethodDefinition
.Ctor
, parameters
);
136 static void RemoveMethod (IList container
, string name
, params string [] parameters
)
138 for (int i
= 0; i
< container
.Count
; i
++) {
139 MethodDefinition method
= (MethodDefinition
) container
[i
];
140 if (method
.Name
!= name
)
143 if (method
.Parameters
.Count
!= parameters
.Length
)
146 if (!ParametersMatch (method
, parameters
))
149 container
.RemoveAt (i
);