From c2230278b30d9449c4b41476dd955aaff0556060 Mon Sep 17 00:00:00 2001 From: Mike Kestner Date: Fri, 7 Sep 2007 14:40:46 +0000 Subject: [PATCH] 2007-09-06 Mike Kestner * AssemblyInfo.cs.in : add IgnoreClassInitializers attr to all. * generator/ObjectGen.cs : add custom-attr generation for objects. * glib/ClassInitializerAttribute.cs : obsolete * glib/IgnoreClassInitializersAttribute.cs : new assembly attr to avoid a blind GetMethods reflection. * glib/Makefile.am : add files * glib/TypeInitializerAttribute.cs : new attr to specify init method to be run at type registration. * gtk/Widget.custom : remove the ClassInitializerAttr. * gtk/Gtk.metadata : add a custom-attr node to GtkWidget. * sample/Subclass.cs : use the IgnoreClassInitializers attr. svn path=/trunk/gtk-sharp/; revision=85480 --- AssemblyInfo.cs.in | 1 + ChangeLog | 14 +++++++ generator/ObjectGen.cs | 7 ++++ glib/ClassInitializerAttribute.cs | 1 + glib/IgnoreClassInitializersAttribute.cs | 31 +++++++++++++++ glib/Makefile.am | 2 + glib/Object.cs | 25 +++++++++--- glib/TypeInitializerAttribute.cs | 48 ++++++++++++++++++++++++ gtk/Gtk.metadata | 1 + gtk/Widget.custom | 1 - sample/Subclass.cs | 2 + 11 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 glib/IgnoreClassInitializersAttribute.cs create mode 100644 glib/TypeInitializerAttribute.cs diff --git a/AssemblyInfo.cs.in b/AssemblyInfo.cs.in index 727ae19f7..1ff6d5e00 100644 --- a/AssemblyInfo.cs.in +++ b/AssemblyInfo.cs.in @@ -4,3 +4,4 @@ using System.Runtime.CompilerServices; [assembly:AssemblyVersion("@API_VERSION@")] [assembly:AssemblyDelaySign(false)] [assembly:AssemblyKeyFile("gtk-sharp.snk")] +[assembly:GLib.IgnoreClassInitializers] diff --git a/ChangeLog b/ChangeLog index 9d0cf06a8..a183dbacf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2007-09-06 Mike Kestner + + * AssemblyInfo.cs.in : add IgnoreClassInitializers attr to all. + * generator/ObjectGen.cs : add custom-attr generation for objects. + * glib/ClassInitializerAttribute.cs : obsolete + * glib/IgnoreClassInitializersAttribute.cs : new assembly attr + to avoid a blind GetMethods reflection. + * glib/Makefile.am : add files + * glib/TypeInitializerAttribute.cs : new attr to specify init + method to be run at type registration. + * gtk/Widget.custom : remove the ClassInitializerAttr. + * gtk/Gtk.metadata : add a custom-attr node to GtkWidget. + * sample/Subclass.cs : use the IgnoreClassInitializers attr. + 2007-08-14 Mike Kestner * gtk/Gtk.metadata : kill a few "new" warnings in FileChooser diff --git a/generator/ObjectGen.cs b/generator/ObjectGen.cs index 35fee903e..8ecb85a55 100644 --- a/generator/ObjectGen.cs +++ b/generator/ObjectGen.cs @@ -30,6 +30,7 @@ namespace GtkSharp.Generation { public class ObjectGen : ObjectBase { + private ArrayList custom_attrs = new ArrayList(); private ArrayList strings = new ArrayList(); private ArrayList vm_nodes = new ArrayList(); private Hashtable childprops = new Hashtable(); @@ -48,6 +49,10 @@ namespace GtkSharp.Generation { Statistics.IgnoreCount++; break; + case "custom-attribute": + custom_attrs.Add (member.InnerXml); + break; + case "virtual_method": Statistics.IgnoreCount++; break; @@ -150,6 +155,8 @@ namespace GtkSharp.Generation { sw.WriteLine ("#region Autogenerated code"); if (IsDeprecated) sw.WriteLine ("\t[Obsolete]"); + foreach (string attr in custom_attrs) + sw.WriteLine ("\t" + attr); sw.Write ("\tpublic {0} class " + Name, IsAbstract ? "abstract" : ""); string cs_parent = table.GetCSType(Elem.GetAttribute("parent")); if (cs_parent != "") { diff --git a/glib/ClassInitializerAttribute.cs b/glib/ClassInitializerAttribute.cs index 15d3dd699..d38ec83ee 100644 --- a/glib/ClassInitializerAttribute.cs +++ b/glib/ClassInitializerAttribute.cs @@ -23,6 +23,7 @@ namespace GLib { using System; + [Obsolete ("Replaced by TypeInitializerAttribute")] public sealed class ClassInitializerAttribute : Attribute { public ClassInitializerAttribute () {} diff --git a/glib/IgnoreClassInitializersAttribute.cs b/glib/IgnoreClassInitializersAttribute.cs new file mode 100644 index 000000000..1dada10f4 --- /dev/null +++ b/glib/IgnoreClassInitializersAttribute.cs @@ -0,0 +1,31 @@ +// IgnoreClassInitializersAttribute.cs +// +// Author: Mike Kestner +// +// Copyright (c) 2007 Novell, Inc. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 2 of the Lesser GNU General +// Public License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + + +namespace GLib { + + using System; + + [AttributeUsage (AttributeTargets.Assembly)] + public sealed class IgnoreClassInitializersAttribute : Attribute + { + public IgnoreClassInitializersAttribute () {} + } +} diff --git a/glib/Makefile.am b/glib/Makefile.am index eac05d5e5..58087ecbe 100644 --- a/glib/Makefile.am +++ b/glib/Makefile.am @@ -32,6 +32,7 @@ sources = \ GType.cs \ GTypeAttribute.cs \ Idle.cs \ + IgnoreClassInitializersAttribute.cs \ InitiallyUnowned.cs \ IWrapper.cs \ ListBase.cs \ @@ -59,6 +60,7 @@ sources = \ ToggleRef.cs \ TypeConverter.cs \ TypeFundamentals.cs \ + TypeInitializerAttribute.cs \ UnwrappedObject.cs \ ValueArray.cs \ Value.cs \ diff --git a/glib/Object.cs b/glib/Object.cs index bcda5101d..6e6cbff49 100644 --- a/glib/Object.cs +++ b/glib/Object.cs @@ -163,16 +163,29 @@ namespace GLib { private static void InvokeClassInitializers (GType gtype, System.Type t) { object[] parms = {gtype, t}; - BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy; - foreach (MethodInfo minfo in t.GetMethods(flags)) - if (minfo.IsDefined (typeof (ClassInitializerAttribute), true)) - minfo.Invoke (null, parms); - } + + BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic; + + foreach (TypeInitializerAttribute tia in t.GetCustomAttributes (typeof (TypeInitializerAttribute), true)) { + MethodInfo m = tia.Type.GetMethod (tia.MethodName, flags); + if (m != null) + m.Invoke (null, parms); + } + + for (Type curr = t; curr != typeof(GLib.Object); curr = curr.BaseType) { + + if (curr.Assembly.IsDefined (typeof (IgnoreClassInitializersAttribute), false)) + continue; + + foreach (MethodInfo minfo in curr.GetMethods(flags)) + if (minfo.IsDefined (typeof (ClassInitializerAttribute), true)) + minfo.Invoke (null, parms); + } + } [DllImport("glibsharpglue-2")] static extern IntPtr gtksharp_register_type (IntPtr name, IntPtr parent_type); - static int type_uid; static string BuildEscapedName (System.Type t) { diff --git a/glib/TypeInitializerAttribute.cs b/glib/TypeInitializerAttribute.cs new file mode 100644 index 000000000..400b59058 --- /dev/null +++ b/glib/TypeInitializerAttribute.cs @@ -0,0 +1,48 @@ +// TypeInitializerAttribute.cs +// +// Author: Mike Kestner +// +// Copyright (c) 2007 Novell, Inc. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 2 of the Lesser GNU General +// Public License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + + +namespace GLib { + + using System; + + [AttributeUsage (AttributeTargets.Class)] + public sealed class TypeInitializerAttribute : Attribute + { + string method_name; + Type type; + + public TypeInitializerAttribute (Type type, string method_name) + { + this.type = type; + this.method_name = method_name; + } + + public string MethodName { + get { return method_name; } + set { method_name = value; } + } + + public Type Type { + get { return type; } + set { type = value; } + } + } +} diff --git a/gtk/Gtk.metadata b/gtk/Gtk.metadata index 3a583aa11..3aadbb5c3 100644 --- a/gtk/Gtk.metadata +++ b/gtk/Gtk.metadata @@ -637,6 +637,7 @@ 1 ScrollAdjustmentsSet 1 + [GLib.TypeInitializer (typeof (Gtk.Widget), "ClassInit")] 1 1 1 diff --git a/gtk/Widget.custom b/gtk/Widget.custom index 97602dd07..d595e5502 100644 --- a/gtk/Widget.custom +++ b/gtk/Widget.custom @@ -293,7 +293,6 @@ static extern void gtksharp_widget_add_binding_signal (IntPtr gvalue, IntPtr nam [DllImport ("gtksharpglue-2")] static extern void gtksharp_widget_register_binding (IntPtr gvalue, IntPtr name, uint key, int mod, IntPtr data); -[GLib.ClassInitializer] static void ClassInit (GLib.GType gtype, Type t) { object[] attrs = t.GetCustomAttributes (typeof (BindingAttribute), true); diff --git a/sample/Subclass.cs b/sample/Subclass.cs index 7ffa85221..6e403b930 100755 --- a/sample/Subclass.cs +++ b/sample/Subclass.cs @@ -4,6 +4,8 @@ // // (c) 2001-2003 Mike Kestner, Novell, Inc. +[assembly:GLib.IgnoreClassInitializers] + namespace GtkSamples { using Gtk;