From 01ab36f9a6e0cf6645b2e5b86db4d1ecdbbb335b Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Wed, 20 Oct 2010 17:46:36 +0200 Subject: [PATCH] Implement JavaGet and JavaSet. - Omit 'special' get_ and set_ methods in DLL produced for getter/setters - Allow properties to be marked read/write only and honour when generating auto java --- .../src/cs2j/CLR/TranslationTemplate.cs | 57 +++++++++++++++++-- .../antlr3/src/cs2jTemplateGen/Main.cs | 6 ++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs index 1ef8acc..224169b 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/TranslationTemplate.cs @@ -154,7 +154,7 @@ namespace RusticiSoftware.Translator.CLR } // The Java translation for this C# entity - private string _java = null; + protected string _java = null; public string Java { get { if (_java == null) { @@ -624,12 +624,56 @@ namespace RusticiSoftware.Translator.CLR // guessing that normally imports will be the same for both) public class PropRepTemplate : FieldRepTemplate, IEquatable { - public string JavaSet { get; set; } + private string _javaGet = null; public string JavaGet { - get { return _javaGet ?? Java; } + get { + if (!CanRead) return null; + if (_javaGet == null) { + if (_java == null) { + return (CanRead ? "${this}.get" + Name + "()" : null); + } + else { + return _java; + } + } + else { + return _javaGet; + } + } set { _javaGet = value; } } + + private string _javaSet = null; + public string JavaSet { + get { + if (_javaSet == null) { + return (CanWrite ? "${this}.set" + Name + "(${value})" : null); + } + else { + return _javaSet; + } + } + set { _javaSet = value; } + } + + // canRead? + private bool _canRead = true; + [XmlAttribute("read")] + [System.ComponentModel.DefaultValueAttribute(true)] + public bool CanRead { + get { return _canRead; } + set { _canRead = value; } + } + + // canWrite? + private bool _canWrite = true; + [XmlAttribute("write")] + [System.ComponentModel.DefaultValueAttribute(true)] + public bool CanWrite { + get { return _canWrite; } + set { _canWrite = value; } + } public PropRepTemplate () : base() { @@ -644,7 +688,12 @@ namespace RusticiSoftware.Translator.CLR public PropRepTemplate (string fType, string fName) : this(fType, fName, null, null, null) { } - + + public override string mkJava () + { + // favour JavaGet + return null; + } #region Equality public bool Equals (PropRepTemplate other) diff --git a/CSharpTranslator/antlr3/src/cs2jTemplateGen/Main.cs b/CSharpTranslator/antlr3/src/cs2jTemplateGen/Main.cs index ada38f2..b69d666 100644 --- a/CSharpTranslator/antlr3/src/cs2jTemplateGen/Main.cs +++ b/CSharpTranslator/antlr3/src/cs2jTemplateGen/Main.cs @@ -99,6 +99,10 @@ namespace cs2j.Template.Utils // Grab Methods foreach (MethodInfo m in t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)) { + if (m.IsSpecialName) { + // e.g., a property's getter / setter method + continue; + } MethodRepTemplate methRep = new MethodRepTemplate(); methRep.Name = m.Name; methRep.Return = TypeHelper.buildTypeName(m.ReturnType); @@ -122,6 +126,8 @@ namespace cs2j.Template.Utils PropRepTemplate propRep = new PropRepTemplate(); propRep.Name = p.Name; propRep.Type = TypeHelper.buildTypeName(p.PropertyType); + propRep.CanRead = p.CanRead; + propRep.CanWrite = p.CanWrite; iface.Properties.Add(propRep); }