Use IKVM.Reflection.Emit's new-style declarative security APIs.

This commit is contained in:
jfrijters 2009-11-09 06:39:17 +00:00
Родитель 3ea4d4aa8f
Коммит 1d979c5789
3 изменённых файлов: 80 добавлений и 15 удалений

Просмотреть файл

@ -139,7 +139,7 @@ namespace IKVM.Internal
return p1.Union(p2); return p1.Union(p2);
} }
#if !NET_4_0 #if !NET_4_0 && !IKVM_REF_EMIT
private void GetAssemblyPermissions(out PermissionSet requiredPermissions, out PermissionSet optionalPermissions, out PermissionSet refusedPermissions) private void GetAssemblyPermissions(out PermissionSet requiredPermissions, out PermissionSet optionalPermissions, out PermissionSet refusedPermissions)
{ {
requiredPermissions = null; requiredPermissions = null;
@ -190,7 +190,7 @@ namespace IKVM.Internal
name.KeyPair = new StrongNameKeyPair(keycontainer); name.KeyPair = new StrongNameKeyPair(keycontainer);
} }
name.Version = new Version(version); name.Version = new Version(version);
#if NET_4_0 #if NET_4_0 || IKVM_REF_EMIT
assemblyBuilder = assemblyBuilder =
#if IKVM_REF_EMIT #if IKVM_REF_EMIT
AssemblyBuilder AssemblyBuilder
@ -204,11 +204,7 @@ namespace IKVM.Internal
PermissionSet refusedPermissions; PermissionSet refusedPermissions;
GetAssemblyPermissions(out requiredPermissions, out optionalPermissions, out refusedPermissions); GetAssemblyPermissions(out requiredPermissions, out optionalPermissions, out refusedPermissions);
assemblyBuilder = assemblyBuilder =
#if IKVM_REF_EMIT
AssemblyBuilder
#else
AppDomain.CurrentDomain AppDomain.CurrentDomain
#endif
.DefineDynamicAssembly(name, AssemblyBuilderAccess.ReflectionOnly, assemblyDir, requiredPermissions, optionalPermissions, refusedPermissions); .DefineDynamicAssembly(name, AssemblyBuilderAccess.ReflectionOnly, assemblyDir, requiredPermissions, optionalPermissions, refusedPermissions);
#endif #endif
ModuleBuilder moduleBuilder; ModuleBuilder moduleBuilder;

Просмотреть файл

@ -1435,12 +1435,16 @@ namespace IKVM.Internal
} }
if (type.IsSubclassOf(JVM.Import(typeof(SecurityAttribute)))) if (type.IsSubclassOf(JVM.Import(typeof(SecurityAttribute))))
{ {
#if IKVM_REF_EMIT
tb.__AddDeclarativeSecurity(MakeCustomAttributeBuilder(loader, annotation));
#else
SecurityAction action; SecurityAction action;
PermissionSet permSet; PermissionSet permSet;
if (MakeDeclSecurity(type, annotation, out action, out permSet)) if (MakeDeclSecurity(type, annotation, out action, out permSet))
{ {
tb.AddDeclarativeSecurity(action, permSet); tb.AddDeclarativeSecurity(action, permSet);
} }
#endif
} }
else else
{ {
@ -1452,12 +1456,16 @@ namespace IKVM.Internal
{ {
if (type.IsSubclassOf(JVM.Import(typeof(SecurityAttribute)))) if (type.IsSubclassOf(JVM.Import(typeof(SecurityAttribute))))
{ {
#if IKVM_REF_EMIT
cb.__AddDeclarativeSecurity(MakeCustomAttributeBuilder(loader, annotation));
#else
SecurityAction action; SecurityAction action;
PermissionSet permSet; PermissionSet permSet;
if (MakeDeclSecurity(type, annotation, out action, out permSet)) if (MakeDeclSecurity(type, annotation, out action, out permSet))
{ {
cb.AddDeclarativeSecurity(action, permSet); cb.AddDeclarativeSecurity(action, permSet);
} }
#endif
} }
else else
{ {
@ -1469,12 +1477,16 @@ namespace IKVM.Internal
{ {
if (type.IsSubclassOf(JVM.Import(typeof(SecurityAttribute)))) if (type.IsSubclassOf(JVM.Import(typeof(SecurityAttribute))))
{ {
#if IKVM_REF_EMIT
mb.__AddDeclarativeSecurity(MakeCustomAttributeBuilder(loader, annotation));
#else
SecurityAction action; SecurityAction action;
PermissionSet permSet; PermissionSet permSet;
if (MakeDeclSecurity(type, annotation, out action, out permSet)) if (MakeDeclSecurity(type, annotation, out action, out permSet))
{ {
mb.AddDeclarativeSecurity(action, permSet); mb.AddDeclarativeSecurity(action, permSet);
} }
#endif
} }
else else
{ {
@ -1519,7 +1531,9 @@ namespace IKVM.Internal
{ {
if (type.IsSubclassOf(JVM.Import(typeof(SecurityAttribute)))) if (type.IsSubclassOf(JVM.Import(typeof(SecurityAttribute))))
{ {
// you can only add declarative security to an assembly when defining the assembly #if IKVM_REF_EMIT
ab.__AddDeclarativeSecurity(MakeCustomAttributeBuilder(loader, annotation));
#endif
} }
else else
{ {

Просмотреть файл

@ -68,6 +68,7 @@ namespace IKVM.Internal
private static Type typeofModifiers = JVM.LoadType(typeof(Modifiers)); private static Type typeofModifiers = JVM.LoadType(typeof(Modifiers));
private static Type typeofSourceFileAttribute = JVM.LoadType(typeof(SourceFileAttribute)); private static Type typeofSourceFileAttribute = JVM.LoadType(typeof(SourceFileAttribute));
private static Type typeofLineNumberTableAttribute = JVM.LoadType(typeof(LineNumberTableAttribute)); private static Type typeofLineNumberTableAttribute = JVM.LoadType(typeof(LineNumberTableAttribute));
private static Type typeofSecurityAttribute = JVM.Import(typeof(SecurityAttribute));
#endif // STATIC_COMPILER #endif // STATIC_COMPILER
private static Type typeofRemappedClassAttribute = JVM.LoadType(typeof(RemappedClassAttribute)); private static Type typeofRemappedClassAttribute = JVM.LoadType(typeof(RemappedClassAttribute));
private static Type typeofRemappedTypeAttribute = JVM.LoadType(typeof(RemappedTypeAttribute)); private static Type typeofRemappedTypeAttribute = JVM.LoadType(typeof(RemappedTypeAttribute));
@ -158,6 +159,7 @@ namespace IKVM.Internal
} }
} }
#if !IKVM_REF_EMIT
private static void SetPropertiesAndFields(ClassLoaderWrapper loader, Attribute attrib, IKVM.Internal.MapXml.Attribute attr) private static void SetPropertiesAndFields(ClassLoaderWrapper loader, Attribute attrib, IKVM.Internal.MapXml.Attribute attr)
{ {
Type t = attrib.GetType(); Type t = attrib.GetType();
@ -186,7 +188,7 @@ namespace IKVM.Internal
if(attr.Type != null) if(attr.Type != null)
{ {
Type t = StaticCompiler.GetType(attr.Type); Type t = StaticCompiler.GetType(attr.Type);
if(typeof(SecurityAttribute).IsAssignableFrom(t)) if(typeofSecurityAttribute.IsAssignableFrom(t))
{ {
Type[] argTypes; Type[] argTypes;
object[] args; object[] args;
@ -202,9 +204,11 @@ namespace IKVM.Internal
} }
return false; return false;
} }
#endif
internal static void SetCustomAttribute(ClassLoaderWrapper loader, TypeBuilder tb, IKVM.Internal.MapXml.Attribute attr) internal static void SetCustomAttribute(ClassLoaderWrapper loader, TypeBuilder tb, IKVM.Internal.MapXml.Attribute attr)
{ {
#if !IKVM_REF_EMIT
SecurityAction action; SecurityAction action;
PermissionSet pset; PermissionSet pset;
if(IsDeclarativeSecurityAttribute(loader, attr, out action, out pset)) if(IsDeclarativeSecurityAttribute(loader, attr, out action, out pset))
@ -212,8 +216,22 @@ namespace IKVM.Internal
tb.AddDeclarativeSecurity(action, pset); tb.AddDeclarativeSecurity(action, pset);
} }
else else
#endif
{ {
tb.SetCustomAttribute(CreateCustomAttribute(loader, attr)); bool declarativeSecurity;
CustomAttributeBuilder cab = CreateCustomAttribute(loader, attr, out declarativeSecurity);
if (declarativeSecurity)
{
#if IKVM_REF_EMIT
tb.__AddDeclarativeSecurity(cab);
#else
throw new InvalidOperationException();
#endif
}
else
{
tb.SetCustomAttribute(cab);
}
} }
} }
@ -229,6 +247,7 @@ namespace IKVM.Internal
internal static void SetCustomAttribute(ClassLoaderWrapper loader, MethodBuilder mb, IKVM.Internal.MapXml.Attribute attr) internal static void SetCustomAttribute(ClassLoaderWrapper loader, MethodBuilder mb, IKVM.Internal.MapXml.Attribute attr)
{ {
#if !IKVM_REF_EMIT
SecurityAction action; SecurityAction action;
PermissionSet pset; PermissionSet pset;
if(IsDeclarativeSecurityAttribute(loader, attr, out action, out pset)) if(IsDeclarativeSecurityAttribute(loader, attr, out action, out pset))
@ -236,13 +255,28 @@ namespace IKVM.Internal
mb.AddDeclarativeSecurity(action, pset); mb.AddDeclarativeSecurity(action, pset);
} }
else else
#endif
{
bool declarativeSecurity;
CustomAttributeBuilder cab = CreateCustomAttribute(loader, attr, out declarativeSecurity);
if (declarativeSecurity)
{
#if IKVM_REF_EMIT
mb.__AddDeclarativeSecurity(cab);
#else
throw new InvalidOperationException();
#endif
}
else
{ {
mb.SetCustomAttribute(CreateCustomAttribute(loader, attr)); mb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
} }
} }
}
internal static void SetCustomAttribute(ClassLoaderWrapper loader, ConstructorBuilder cb, IKVM.Internal.MapXml.Attribute attr) internal static void SetCustomAttribute(ClassLoaderWrapper loader, ConstructorBuilder cb, IKVM.Internal.MapXml.Attribute attr)
{ {
#if !IKVM_REF_EMIT
SecurityAction action; SecurityAction action;
PermissionSet pset; PermissionSet pset;
if(IsDeclarativeSecurityAttribute(loader, attr, out action, out pset)) if(IsDeclarativeSecurityAttribute(loader, attr, out action, out pset))
@ -250,10 +284,24 @@ namespace IKVM.Internal
cb.AddDeclarativeSecurity(action, pset); cb.AddDeclarativeSecurity(action, pset);
} }
else else
#endif
{
bool declarativeSecurity;
CustomAttributeBuilder cab = CreateCustomAttribute(loader, attr, out declarativeSecurity);
if (declarativeSecurity)
{
#if IKVM_REF_EMIT
cb.__AddDeclarativeSecurity(cab);
#else
throw new InvalidOperationException();
#endif
}
else
{ {
cb.SetCustomAttribute(CreateCustomAttribute(loader, attr)); cb.SetCustomAttribute(CreateCustomAttribute(loader, attr));
} }
} }
}
internal static void SetCustomAttribute(ClassLoaderWrapper loader, PropertyBuilder pb, IKVM.Internal.MapXml.Attribute attr) internal static void SetCustomAttribute(ClassLoaderWrapper loader, PropertyBuilder pb, IKVM.Internal.MapXml.Attribute attr)
{ {
@ -296,6 +344,12 @@ namespace IKVM.Internal
} }
private static CustomAttributeBuilder CreateCustomAttribute(ClassLoaderWrapper loader, IKVM.Internal.MapXml.Attribute attr) private static CustomAttributeBuilder CreateCustomAttribute(ClassLoaderWrapper loader, IKVM.Internal.MapXml.Attribute attr)
{
bool ignore;
return CreateCustomAttribute(loader, attr, out ignore);
}
private static CustomAttributeBuilder CreateCustomAttribute(ClassLoaderWrapper loader, IKVM.Internal.MapXml.Attribute attr, out bool isDeclarativeSecurity)
{ {
// TODO add error handling // TODO add error handling
Type[] argTypes; Type[] argTypes;
@ -304,10 +358,7 @@ namespace IKVM.Internal
if(attr.Type != null) if(attr.Type != null)
{ {
Type t = StaticCompiler.GetType(attr.Type); Type t = StaticCompiler.GetType(attr.Type);
if(typeof(SecurityAttribute).IsAssignableFrom(t)) isDeclarativeSecurity = t.IsSubclassOf(typeofSecurityAttribute);
{
throw new NotImplementedException("Declarative SecurityAttribute support not implemented");
}
ConstructorInfo ci = t.GetConstructor(argTypes); ConstructorInfo ci = t.GetConstructor(argTypes);
if(ci == null) if(ci == null)
{ {
@ -356,6 +407,7 @@ namespace IKVM.Internal
throw new NotImplementedException("Setting property values on Java attributes is not implemented"); throw new NotImplementedException("Setting property values on Java attributes is not implemented");
} }
TypeWrapper t = loader.LoadClassByDottedName(attr.Class); TypeWrapper t = loader.LoadClassByDottedName(attr.Class);
isDeclarativeSecurity = t.TypeAsBaseType.IsSubclassOf(typeofSecurityAttribute);
MethodWrapper mw = t.GetMethodWrapper("<init>", attr.Sig, false); MethodWrapper mw = t.GetMethodWrapper("<init>", attr.Sig, false);
mw.Link(); mw.Link();
ConstructorInfo ci = (ConstructorInfo)mw.GetMethod(); ConstructorInfo ci = (ConstructorInfo)mw.GetMethod();
@ -1843,11 +1895,13 @@ namespace IKVM.Internal
} }
} }
#if !IKVM_REF_EMIT
internal static bool MakeDeclSecurity(Type type, object annotation, out SecurityAction action, out PermissionSet permSet) internal static bool MakeDeclSecurity(Type type, object annotation, out SecurityAction action, out PermissionSet permSet)
{ {
ConstructorInfo ci = type.GetConstructor(new Type[] { typeof(SecurityAction) }); ConstructorInfo ci = type.GetConstructor(new Type[] { typeof(SecurityAction) });
if (ci == null) if (ci == null)
{ {
// TODO should we support HostProtectionAttribute? (which has a no-arg constructor)
// TODO issue message? // TODO issue message?
action = 0; action = 0;
permSet = null; permSet = null;
@ -1884,6 +1938,7 @@ namespace IKVM.Internal
permSet.AddPermission(attr.CreatePermission()); permSet.AddPermission(attr.CreatePermission());
return true; return true;
} }
#endif
internal static bool HasRetentionPolicyRuntime(object[] annotations) internal static bool HasRetentionPolicyRuntime(object[] annotations)
{ {