-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #454 from nunit/jnm2/replace_cecil
Replaced Mono.Cecil
- Loading branch information
Showing
22 changed files
with
517 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/NUnitTestAdapter/Metadata/DirectReflectionMetadataProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// *********************************************************************** | ||
// Copyright (c) 2018 Charlie Poole, Terje Sandstrom | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining | ||
// a copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to | ||
// permit persons to whom the Software is furnished to do so, subject to | ||
// the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// *********************************************************************** | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using NUnit.VisualStudio.TestAdapter.Internal; | ||
|
||
#if NETCOREAPP1_0 | ||
using System.Runtime.Loader; | ||
#endif | ||
|
||
namespace NUnit.VisualStudio.TestAdapter.Metadata | ||
{ | ||
internal sealed class DirectReflectionMetadataProvider : IMetadataProvider | ||
{ | ||
public TypeInfo? GetDeclaringType(string assemblyPath, string reflectedTypeName, string methodName) | ||
{ | ||
var type = TryGetSingleMethod(assemblyPath, reflectedTypeName, methodName)?.DeclaringType; | ||
if (type == null) return null; | ||
|
||
#if NET35 | ||
if (type.IsGenericType) | ||
#else | ||
if (type.IsConstructedGenericType) | ||
#endif | ||
{ | ||
type = type.GetGenericTypeDefinition(); | ||
} | ||
|
||
return new TypeInfo(type); | ||
} | ||
|
||
public TypeInfo? GetStateMachineType(string assemblyPath, string reflectedTypeName, string methodName) | ||
{ | ||
var method = TryGetSingleMethod(assemblyPath, reflectedTypeName, methodName); | ||
if (method == null) return null; | ||
|
||
var candidate = (Type)null; | ||
|
||
foreach (var attributeData in CustomAttributeData.GetCustomAttributes(method)) | ||
{ | ||
for (var current = attributeData.Constructor.DeclaringType; current != null; current = current.GetTypeInfo().BaseType) | ||
{ | ||
if (current.FullName != "System.Runtime.CompilerServices.StateMachineAttribute") continue; | ||
|
||
var parameters = attributeData.Constructor.GetParameters(); | ||
for (var i = 0; i < parameters.Length; i++) | ||
{ | ||
if (parameters[i].Name != "stateMachineType") continue; | ||
var argument = attributeData.ConstructorArguments[i].Value as Type; | ||
if (argument != null) | ||
{ | ||
if (candidate != null) return null; | ||
candidate = argument; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (candidate == null) return null; | ||
return new TypeInfo(candidate); | ||
} | ||
|
||
private static MethodInfo TryGetSingleMethod(string assemblyPath, string reflectedTypeName, string methodName) | ||
{ | ||
try | ||
{ | ||
#if NETCOREAPP1_0 | ||
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath); | ||
#else | ||
var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath); | ||
#endif | ||
|
||
var type = assembly.GetType(reflectedTypeName, throwOnError: false); | ||
if (type == null) return null; | ||
|
||
var methods = type.GetMethods().Where(m => m.Name == methodName).Take(2).ToList(); | ||
return methods.Count == 1 ? methods[0] : null; | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// *********************************************************************** | ||
// Copyright (c) 2018 Charlie Poole, Terje Sandstrom | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining | ||
// a copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to | ||
// permit persons to whom the Software is furnished to do so, subject to | ||
// the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// *********************************************************************** | ||
|
||
using System; | ||
|
||
namespace NUnit.VisualStudio.TestAdapter.Metadata | ||
{ | ||
internal interface IMetadataProvider : IDisposable | ||
{ | ||
TypeInfo? GetDeclaringType(string assemblyPath, string reflectedTypeName, string methodName); | ||
TypeInfo? GetStateMachineType(string assemblyPath, string reflectedTypeName, string methodName); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/NUnitTestAdapter/Metadata/ReflectionAppDomainMetadataProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// *********************************************************************** | ||
// Copyright (c) 2018 Charlie Poole, Terje Sandstrom | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining | ||
// a copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to | ||
// permit persons to whom the Software is furnished to do so, subject to | ||
// the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// *********************************************************************** | ||
|
||
#if !NETCOREAPP1_0 | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace NUnit.VisualStudio.TestAdapter.Metadata | ||
{ | ||
internal sealed partial class ReflectionAppDomainMetadataProvider : IMetadataProvider | ||
{ | ||
private AppDomain _appDomain; | ||
private AppDomainHelper _helper; | ||
|
||
private AppDomainHelper GetHelper() | ||
{ | ||
if (_helper == null) | ||
{ | ||
var adapterAssembly = typeof(ReflectionAppDomainMetadataProvider).Assembly; | ||
var adapterAssemblyPath = adapterAssembly.ManifestModule.FullyQualifiedName; | ||
|
||
_appDomain = AppDomain.CreateDomain( | ||
friendlyName: typeof(ReflectionAppDomainMetadataProvider).FullName, | ||
securityInfo: null, | ||
info: new AppDomainSetup { ApplicationBase = Path.GetDirectoryName(adapterAssemblyPath) }); | ||
|
||
_appDomain.ReflectionOnlyAssemblyResolve += AppDomainReflectionOnlyAssemblyResolve; | ||
|
||
_helper = (AppDomainHelper)_appDomain.CreateInstanceAndUnwrap(adapterAssembly.FullName, typeof(AppDomainHelper).FullName); | ||
} | ||
|
||
return _helper; | ||
} | ||
|
||
private static Assembly AppDomainReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args) | ||
{ | ||
return Assembly.ReflectionOnlyLoad(args.Name); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_appDomain != null) | ||
AppDomain.Unload(_appDomain); | ||
} | ||
|
||
public TypeInfo? GetDeclaringType(string assemblyPath, string reflectedTypeName, string methodName) | ||
{ | ||
return GetHelper().GetDeclaringType(assemblyPath, reflectedTypeName, methodName); | ||
} | ||
|
||
public TypeInfo? GetStateMachineType(string assemblyPath, string reflectedTypeName, string methodName) | ||
{ | ||
return GetHelper().GetStateMachineType(assemblyPath, reflectedTypeName, methodName); | ||
} | ||
|
||
private sealed class AppDomainHelper : MarshalByRefObject | ||
{ | ||
private readonly DirectReflectionMetadataProvider provider = new DirectReflectionMetadataProvider(); | ||
|
||
public TypeInfo? GetDeclaringType(string assemblyPath, string reflectedTypeName, string methodName) | ||
{ | ||
return provider.GetDeclaringType(assemblyPath, reflectedTypeName, methodName); | ||
} | ||
|
||
public TypeInfo? GetStateMachineType(string assemblyPath, string reflectedTypeName, string methodName) | ||
{ | ||
return provider.GetStateMachineType(assemblyPath, reflectedTypeName, methodName); | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// *********************************************************************** | ||
// Copyright (c) 2018 Charlie Poole, Terje Sandstrom | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining | ||
// a copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to | ||
// permit persons to whom the Software is furnished to do so, subject to | ||
// the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// *********************************************************************** | ||
|
||
using System; | ||
using System.Reflection; | ||
using NUnit.VisualStudio.TestAdapter.Internal; | ||
|
||
namespace NUnit.VisualStudio.TestAdapter.Metadata | ||
{ | ||
#if !NETCOREAPP1_0 | ||
[Serializable] | ||
#endif | ||
public struct TypeInfo | ||
{ | ||
public TypeInfo(Type type) | ||
{ | ||
AssemblyPath = type.GetTypeInfo().Assembly.Location; | ||
FullName = type.FullName; | ||
} | ||
|
||
public TypeInfo(string assemblyPath, string fullName) | ||
{ | ||
AssemblyPath = assemblyPath; | ||
FullName = fullName; | ||
} | ||
|
||
public string AssemblyPath { get; } | ||
public string FullName { get; } | ||
} | ||
} |
Oops, something went wrong.