1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

Add delegates

This commit is contained in:
Kevin Glynn 2010-10-21 16:31:09 +02:00
parent 948f702ad1
commit 916eca5eac
3 changed files with 62 additions and 22 deletions

View File

@ -1,12 +1,18 @@
using System;
namespace cs2jTest.Various.Features
{
// delegate declaration
public delegate string MyDelegate(int i);
public class Various
{
public Various ()
{
}
private int myInt;
public string TestRWProperty {get; set;}
public string get_TestRWProperty() {

View File

@ -214,6 +214,21 @@ namespace RusticiSoftware.Translator.CLR
Java = java;
}
protected string mkJavaParams(IList<ParamRepTemplate> pars) {
StringBuilder parStr = new StringBuilder();
parStr.Append("(");
foreach (ParamRepTemplate p in pars) {
parStr.Append("${"+p.Name+"},");
}
if (parStr[parStr.Length-1] == ',') {
parStr.Remove(parStr.Length-1,1);
}
parStr.Append(")");
return parStr.ToString();
}
#region Equality
public bool Equals (TranslationBase other)
@ -281,25 +296,12 @@ namespace RusticiSoftware.Translator.CLR
}
}
protected string mkJavaParams() {
StringBuilder parStr = new StringBuilder();
parStr.Append("(");
foreach (ParamRepTemplate p in Params) {
parStr.Append("${"+p.Name+"},");
}
if (parStr[parStr.Length-1] == ',') {
parStr.Remove(parStr.Length-1,1);
}
parStr.Append(")");
return parStr.ToString();
}
public override string mkJava() {
string constructorName = "CONSTRUCTOR";
if (SurroundingTypeName != null) {
constructorName = SurroundingTypeName.Substring(SurroundingTypeName.LastIndexOf('.') + 1);
}
return "new " + constructorName + mkJavaParams();
return "new " + constructorName + mkJavaParams(Params);
}
public override string[] mkImports() {
@ -434,7 +436,7 @@ namespace RusticiSoftware.Translator.CLR
methStr.Append("${this}.");
}
methStr.Append(Name);
return methStr.ToString() + mkJavaParams();
return methStr.ToString() + mkJavaParams(Params);
}
#region Equality
@ -1104,6 +1106,10 @@ namespace RusticiSoftware.Translator.CLR
_params = args;
}
public override string mkJava() {
return "${delegate}.Invoke" + mkJavaParams(Params);
}
public override TypeRep mkEmptyRep ()
{
return new DelegateRep ();

View File

@ -65,12 +65,12 @@ namespace cs2j.Template.Utils
}
private void buildParameters(ConstructorRepTemplate c, MethodBase m) {
private void buildParameters(IList<ParamRepTemplate> ps, MethodBase m) {
foreach (ParameterInfo p in m.GetParameters()) {
ParamRepTemplate paramRep = new ParamRepTemplate();
paramRep.Type = TypeHelper.buildTypeName(p.ParameterType);
paramRep.Name = p.Name;
c.Params.Add(paramRep);
ps.Add(paramRep);
}
}
@ -113,7 +113,7 @@ namespace cs2j.Template.Utils
}
methRep.TypeParams = tParams;
}
buildParameters(methRep, m);
buildParameters(methRep.Params, m);
if (m.IsStatic) {
methRep.IsStatic = true;
}
@ -146,7 +146,7 @@ namespace cs2j.Template.Utils
// Grab Constructors
foreach (ConstructorInfo c in t.GetConstructors()) {
ConstructorRepTemplate consRep = new ConstructorRepTemplate();
buildParameters(consRep, c);
buildParameters(consRep.Params, c);
consRep.SurroundingTypeName = klass.TypeName;
klass.Constructors.Add(consRep);
}
@ -170,6 +170,27 @@ namespace cs2j.Template.Utils
}
private void buildDelegate(DelegateRepTemplate d, Type t) {
if (t.IsGenericType) {
d.TypeName = t.GetGenericTypeDefinition().FullName;
string[] tParams = new string[t.GetGenericArguments().Length];
for (int i = 0; i < t.GetGenericArguments().Length; i++) {
tParams[i] = t.GetGenericArguments()[i].Name;
}
d.TypeParams = tParams;
}
else {
d.TypeName = t.FullName;
}
MethodInfo invoker = t.GetMethod("Invoke");
if (invoker == null)
throw new Exception("Unexpected: class " + t.FullName + " inherits from System.Delegate but doesn't have an Invoke method");
buildParameters(d.Params, invoker);
d.Return = TypeHelper.buildTypeName(invoker.ReturnType);
}
private IList<TypeRepTemplate> mkTemplates(string typeName) {
List<TypeRepTemplate> rets = new List<TypeRepTemplate>();
@ -181,9 +202,16 @@ namespace cs2j.Template.Utils
}
TypeRepTemplate retRep = null;
if (t.IsClass) {
ClassRepTemplate classRep = new ClassRepTemplate();
buildClass(classRep, t);
retRep = classRep;
if (t.IsSubclassOf(typeof(System.Delegate))) {
DelegateRepTemplate delRep = new DelegateRepTemplate();
buildDelegate(delRep, t);
retRep = delRep;
}
else {
ClassRepTemplate classRep = new ClassRepTemplate();
buildClass(classRep, t);
retRep = classRep;
}
}
else if (t.IsInterface) {
InterfaceRepTemplate intRep = new InterfaceRepTemplate();