svn path=/branches/mono-1-1-9/mcs/; revision=51216
[mono.git] / mcs / class / corlib / System.Security.Permissions / ReflectionPermission.cs
index b5ee656c585d82ea3b9da1ccaa38808a631f2eb0..bb7dabcfad3c4b2f4b1f717d772c2bc86f97b213 100644 (file)
@@ -3,14 +3,11 @@
 //
 // Authors:
 //     Tim Coleman <tim@timcoleman.com>
-//     Sebastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // Copyright (C) 2002, Tim Coleman
 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
+using System.Globalization;
+using System.Runtime.InteropServices;
 
 namespace System.Security.Permissions {
 
+#if NET_2_0
+       [ComVisible (true)]
+#endif
        [Serializable]
        public sealed class ReflectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
 
-               #region Fields
+               private const int version = 1;
 
                ReflectionPermissionFlag flags;
-               PermissionState state;
-
-               #endregion // Fields
 
-               #region Constructors
 
                public ReflectionPermission (PermissionState state)
                {
-                       switch (state) {
-                               case PermissionState.None:
-                                       flags = ReflectionPermissionFlag.NoFlags;
-                                       break;
-                               case PermissionState.Unrestricted:
-                                       flags = ReflectionPermissionFlag.AllFlags;
-                                       break;
-                               default:
-                                       throw new ArgumentException ("Invalid PermissionState");
-                       }
+                       if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
+                               flags = ReflectionPermissionFlag.AllFlags;
+                       else
+                               flags = ReflectionPermissionFlag.NoFlags;
                }
 
                public ReflectionPermission (ReflectionPermissionFlag flag)
                {
-                       flags = flag;
+                       // reuse validation by the Flags property
+                       Flags = flag;
                }
 
-               #endregion // Constructors
-
-               #region Properties
 
                public ReflectionPermissionFlag Flags {
                        get { return flags; }
-                       set { flags = value; }
-               }
+                       set {
+                               if ((value & ReflectionPermissionFlag.AllFlags) != value) {
+                                       string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
+                                       throw new ArgumentException (msg, "ReflectionPermissionFlag");
+                               }
 
-               #endregion // Properties
+                               flags = value;
+                       }
+               }
 
-               #region Methods
 
                public override IPermission Copy ()
                {
@@ -87,17 +80,14 @@ namespace System.Security.Permissions {
 
                public override void FromXml (SecurityElement esd)
                {
-                       if (esd == null)
-                               throw new ArgumentNullException ("esd");
-                       if (esd.Tag != "IPermission")
-                               throw new ArgumentException ("not IPermission");
-                       if (!(esd.Attributes ["class"] as string).StartsWith ("System.Security.Permissions.ReflectionPermission"))
-                               throw new ArgumentException ("not ReflectionPermission");
-                       if ((esd.Attributes ["version"] as string) != "1")
-                               throw new ArgumentException ("wrong version");
-
-                       if ((esd.Attributes ["Unrestricted"] as string) == "true")
+                       // General validation in CodeAccessPermission
+                       CheckSecurityElement (esd, "esd", version, version);
+                       // Note: we do not (yet) care about the return value 
+                       // as we only accept version 1 (min/max values)
+
+                       if (IsUnrestricted (esd)) {
                                flags = ReflectionPermissionFlag.AllFlags;
+                       }
                        else {
                                flags = ReflectionPermissionFlag.NoFlags;
                                string xmlFlags = (esd.Attributes ["Flags"] as string);
@@ -112,45 +102,40 @@ namespace System.Security.Permissions {
 
                public override IPermission Intersect (IPermission target)
                {
-                       if (target == null)
+                       ReflectionPermission rp = Cast (target);
+                       if (rp == null)
                                return null;
-                       if (! (target is ReflectionPermission))
-                               throw new ArgumentException ("wrong type");
 
-                       ReflectionPermission o = (ReflectionPermission) target;
                        if (IsUnrestricted ()) {
-                               if (o.Flags == ReflectionPermissionFlag.NoFlags)
+                               if (rp.Flags == ReflectionPermissionFlag.NoFlags)
                                        return null;
                                else
-                                       return o.Copy ();
+                                       return rp.Copy ();
                        }
-                       if (o.IsUnrestricted ()) {
+                       if (rp.IsUnrestricted ()) {
                                if (flags == ReflectionPermissionFlag.NoFlags)
                                        return null;
                                else
                                        return Copy ();
                        }
 
-                       ReflectionPermission p = (ReflectionPermission) o.Copy ();
+                       ReflectionPermission p = (ReflectionPermission) rp.Copy ();
                        p.Flags &= flags;
                        return ((p.Flags == ReflectionPermissionFlag.NoFlags) ? null : p);
                }
 
                public override bool IsSubsetOf (IPermission target)
                {
-                       if (target == null)
+                       ReflectionPermission rp = Cast (target);
+                       if (rp == null)
                                return (flags == ReflectionPermissionFlag.NoFlags);
 
-                       if (! (target is ReflectionPermission))
-                               throw new ArgumentException ("wrong type");
-
-                       ReflectionPermission o = (ReflectionPermission) target;
                        if (IsUnrestricted ())
-                               return o.IsUnrestricted ();
-                       else if (o.IsUnrestricted ())
+                               return rp.IsUnrestricted ();
+                       else if (rp.IsUnrestricted ())
                                return true;
 
-                       return ((flags & o.Flags) == flags);
+                       return ((flags & rp.Flags) == flags);
                }
 
                public bool IsUnrestricted ()
@@ -160,7 +145,7 @@ namespace System.Security.Permissions {
 
                public override SecurityElement ToXml ()
                {
-                       SecurityElement se = Element (this, 1);
+                       SecurityElement se = Element (version);
                        if (IsUnrestricted ()) {
                                se.AddAttribute ("Unrestricted", "true");
                        }
@@ -191,16 +176,14 @@ namespace System.Security.Permissions {
 
                public override IPermission Union (IPermission other)
                {
+                       ReflectionPermission rp = Cast (other);
                        if (other == null)
                                return Copy ();
-                       if (! (other is ReflectionPermission))
-                               throw new ArgumentException ("wrong type");
 
-                       ReflectionPermission o = (ReflectionPermission) other;
-                       if (IsUnrestricted () || o.IsUnrestricted ())
+                       if (IsUnrestricted () || rp.IsUnrestricted ())
                                return new ReflectionPermission (PermissionState.Unrestricted);
 
-                       ReflectionPermission p = (ReflectionPermission) o.Copy ();
+                       ReflectionPermission p = (ReflectionPermission) rp.Copy ();
                        p.Flags |= flags;
                        return p;
                }
@@ -208,9 +191,22 @@ namespace System.Security.Permissions {
                // IBuiltInPermission
                int IBuiltInPermission.GetTokenIndex ()
                {
-                       return 4;
+                       return (int) BuiltInToken.Reflection;
                }
 
-               #endregion // Methods
+               // helpers
+
+               private ReflectionPermission Cast (IPermission target)
+               {
+                       if (target == null)
+                               return null;
+
+                       ReflectionPermission rp = (target as ReflectionPermission);
+                       if (rp == null) {
+                               ThrowInvalidPermission (target, typeof (ReflectionPermission));
+                       }
+
+                       return rp;
+               }
        }
 }