Skip to content

Commit

Permalink
Merge pull request #1095 from sergeybykov/feature-serializer-knowntype
Browse files Browse the repository at this point in the history
Fixed CodeGen: Allow [KnownType] & [KnownAssembly] hints, fix AssemblyProcessor scanning
  • Loading branch information
jason-bragg committed Dec 2, 2015
2 parents df41250 + e4bbe7c commit 1a613de
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 105 deletions.
19 changes: 10 additions & 9 deletions src/Orleans/AssemblyLoader/AssemblyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ private static void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args)
/// <param name="assembly">The assembly to process.</param>
private static void ProcessAssembly(Assembly assembly)
{
// If the assembly is loaded for reflection only or it does not reference Orleans, avoid processing it.
if (assembly.ReflectionOnly || !TypeUtils.IsOrleansOrReferencesOrleans(assembly))
// If the assembly is loaded for reflection only avoid processing it.
if (assembly.ReflectionOnly)
{
return;
}
Expand All @@ -128,12 +128,16 @@ private static void ProcessAssembly(Assembly assembly)
}
}

// Code generation occurs in a self-contained assembly, so invoke it separately.
CodeGeneratorManager.GenerateAndCacheCodeForAssembly(assembly);
// If the assembly does not reference Orleans, avoid generating code for it.
if (TypeUtils.IsOrleansOrReferencesOrleans(assembly))
{
// Code generation occurs in a self-contained assembly, so invoke it separately.
CodeGeneratorManager.GenerateAndCacheCodeForAssembly(assembly);
}

// Process each type in the assembly.
var shouldProcessSerialization = SerializationManager.ShouldFindSerializationInfo(assembly);
Type[] assemblyTypes;
TypeInfo[] assemblyTypes;
try
{
assemblyTypes = assembly.DefinedTypes.ToArray();
Expand All @@ -147,10 +151,7 @@ private static void ProcessAssembly(Assembly assembly)
"AssemblyLoader encountered an exception loading types from assembly '{0}': {1}",
assembly.FullName,
exception);
Logger.Warn(
ErrorCode.Loader_TypeLoadError_5,
message,
exception);
Logger.Warn(ErrorCode.Loader_TypeLoadError_5, message, exception);
}

return;
Expand Down
50 changes: 50 additions & 0 deletions src/Orleans/CodeGeneration/KnownAssemblyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Project Orleans Cloud Service SDK ver. 1.0
Copyright (c) Microsoft Corporation
All rights reserved.
MIT License
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.
*/

namespace Orleans.CodeGeneration
{
using System;
using System.Reflection;

/// <summary>
/// The attribute which informs the code generator that code should be generated an assembly.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
public class KnownAssemblyAttribute : Attribute
{
public KnownAssemblyAttribute(Type type)
{
this.Assembly = type.Assembly;
}

public KnownAssemblyAttribute(string assemblyName)
{
this.Assembly = Assembly.Load(assemblyName);
}

/// <summary>
/// Gets or sets the assembly to include in code generation.
/// </summary>
public Assembly Assembly { get; set; }
}
}
41 changes: 41 additions & 0 deletions src/Orleans/CodeGeneration/KnownTypeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Project Orleans Cloud Service SDK ver. 1.0
Copyright (c) Microsoft Corporation
All rights reserved.
MIT License
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.
*/

namespace Orleans.CodeGeneration
{
using System;

/// <summary>
/// The attribute which informs the code generator that code should be generated a type.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
public class KnownTypeAttribute : Attribute
{
public KnownTypeAttribute(Type type)
{
this.Type = type;
}

public Type Type { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/Orleans/Orleans.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@
<Compile Include="CodeGeneration\IGrainMethodInvoker.cs" />
<Compile Include="CodeGeneration\IRuntimeCodeGenerator.cs" />
<Compile Include="CodeGeneration\ISourceCodeGenerator.cs" />
<Compile Include="CodeGeneration\KnownAssemblyAttribute.cs" />
<Compile Include="CodeGeneration\Language.cs" />
<Compile Include="CodeGeneration\OrleansCodeGenerationTargetAttribute.cs" />
<Compile Include="CodeGeneration\KnownTypeAttribute.cs" />
<Compile Include="CodeGeneration\SkipCodeGenerationAttribute.cs" />
<Compile Include="Providers\DefaultServiceProvider.cs" />
<Compile Include="Serialization\IExternalSerializer.cs" />
Expand Down
6 changes: 3 additions & 3 deletions src/Orleans/Serialization/TypeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ public static string GetLocationSafe(this Assembly a)
}
}

public static bool IsTypeIsInaccessibleForSerialization(Type t, Module fromModule, Assembly fromAssembly)
public static bool IsTypeIsInaccessibleForSerialization(Type type, Module fromModule, Assembly fromAssembly)
{
var typeInfo = t.GetTypeInfo();
var typeInfo = type.GetTypeInfo();

if (!typeInfo.IsVisible && typeInfo.IsConstructedGenericType)
{
Expand Down Expand Up @@ -265,7 +265,7 @@ public static bool IsTypeIsInaccessibleForSerialization(Type t, Module fromModul
return IsTypeIsInaccessibleForSerialization(typeInfo.GetElementType(), fromModule, fromAssembly);
}

var result = typeInfo.IsNestedPrivate || typeInfo.IsNestedFamily;
var result = typeInfo.IsNestedPrivate || typeInfo.IsNestedFamily || type.IsPointer;

return result;
}
Expand Down
6 changes: 4 additions & 2 deletions src/OrleansCodeGenerator/GrainReferenceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,10 @@ private static MemberDeclarationSyntax GenerateInterfaceNameProperty(Type grainT
private static MethodDeclarationSyntax GenerateGetMethodNameMethod(Type grainType)
{
// Get the method with the correct type.
var method = typeof(GrainReference).GetMethods(
BindingFlags.NonPublic | BindingFlags.Instance).Where(m=>m.Name == "GetMethodName").FirstOrDefault();
var method =
typeof(GrainReference)
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault(m => m.Name == "GetMethodName");

var methodDeclaration =
method.GetDeclarationSyntax()
Expand Down
94 changes: 51 additions & 43 deletions src/OrleansCodeGenerator/RoslynCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,36 +317,31 @@ private static GeneratedSyntax GenerateForAssemblies(List<Assembly> assemblies,

var members = new List<MemberDeclarationSyntax>();

// If any KnownAssemblies have been specified, include them during code generation.
var knownAssemblies =
assemblies.SelectMany(_ => _.GetCustomAttributes<KnownAssemblyAttribute>())
.Select(_ => _.Assembly)
.Distinct()
.ToSet();
if (knownAssemblies.Count > 0)
{
knownAssemblies.IntersectWith(assemblies);
assemblies = knownAssemblies.ToList();
}

// Get types from assemblies which reference Orleans and are not generated assemblies.
var includedTypes = new HashSet<Type>();
foreach (var type in assemblies.SelectMany(_ => _.DefinedTypes))
for (var i = 0; i < assemblies.Count; i++)
{
// The module containing the serializer.
var module = runtime ? null : type.Module;
var typeInfo = type.GetTypeInfo();

// Every type which is encountered must be considered for serialization.
if (!typeInfo.IsNested && !typeInfo.IsGenericParameter && typeInfo.IsSerializable)
var assembly = assemblies[i];
foreach (var attribute in assembly.GetCustomAttributes<KnownTypeAttribute>())
{
// If a type was encountered which can be accessed, process it for serialization.
var isAccessibleForSerialization =
!TypeUtilities.IsTypeIsInaccessibleForSerialization(type, module, targetAssembly);
if (isAccessibleForSerialization)
{
includedTypes.Add(type);
SerializerGenerationManager.RecordTypeToGenerate(type);
}
ConsiderType(attribute.Type, runtime, targetAssembly, includedTypes);
}

// Collect the types which require code generation.
if (GrainInterfaceData.IsGrainInterface(type))
foreach (var type in assembly.DefinedTypes)
{
if (Logger.IsVerbose2)
{
Logger.Verbose2("Will generate code for: {0}", type.GetParseableName());
}

includedTypes.Add(type);
ConsiderType(type, runtime, targetAssembly, includedTypes);
}
}

Expand All @@ -365,12 +360,7 @@ private static GeneratedSyntax GenerateForAssemblies(List<Assembly> assemblies,
Action<Type> onEncounteredType = encounteredType =>
{
// If a type was encountered which can be accessed, process it for serialization.
var isAccessibleForSerialization =
!TypeUtilities.IsTypeIsInaccessibleForSerialization(encounteredType, module, targetAssembly);
if (isAccessibleForSerialization)
{
SerializerGenerationManager.RecordTypeToGenerate(encounteredType);
}
SerializerGenerationManager.RecordTypeToGenerate(encounteredType, module, targetAssembly);
};

if (Logger.IsVerbose2)
Expand Down Expand Up @@ -398,20 +388,6 @@ private static GeneratedSyntax GenerateForAssemblies(List<Assembly> assemblies,
Type toGen;
while (SerializerGenerationManager.GetNextTypeToProcess(out toGen))
{
// Filter types which are inaccessible by the serialzation module/assembly.
var skipSerialzerGeneration =
toGen.GetAllFields()
.Any(
field =>
TypeUtilities.IsTypeIsInaccessibleForSerialization(
field.FieldType,
module,
targetAssembly));
if (skipSerialzerGeneration)
{
continue;
}

if (!runtime)
{
if (first)
Expand Down Expand Up @@ -462,6 +438,38 @@ private static GeneratedSyntax GenerateForAssemblies(List<Assembly> assemblies,
};
}

private static void ConsiderType(
Type type,
bool runtime,
Assembly targetAssembly,
ISet<Type> includedTypes)
{
// The module containing the serializer.
var module = runtime ? null : type.Module;
var typeInfo = type.GetTypeInfo();

// Every type which is encountered must be considered for serialization.
if (!typeInfo.IsNested && !typeInfo.IsGenericParameter && typeInfo.IsSerializable)
{
// If a type was encountered which can be accessed, process it for serialization.
if (SerializerGenerationManager.RecordTypeToGenerate(type, module, targetAssembly))
{
includedTypes.Add(type);
}
}

// Collect the types which require code generation.
if (GrainInterfaceData.IsGrainInterface(type))
{
if (Logger.IsVerbose2)
{
Logger.Verbose2("Will generate code for: {0}", type.GetParseableName());
}

includedTypes.Add(type);
}
}

/// <summary>
/// Returns a value indicating whether or not code should be generated for the provided assembly.
/// </summary>
Expand Down
Loading

0 comments on commit 1a613de

Please sign in to comment.