/// <summary> /// Create proxy for interface /// </summary> protected void GenerateProxy(CodeNamespace nameSpace) { var tgtType = new CodeTypeDeclaration("__" + type.Name); SetCurrentType(type.CLRNamespaceExt + ".__" + type.Name, type.CLRNamespace + "." + type.Name, type.CLRNamespaceExt + ".__" + type.Name, type.CLRNamespaceExt + "." + type.Name + "_"); AddTypeCLR(CurrentType.BaseType); nameSpace.Types.Add(tgtType); tgtType.TypeAttributes = TypeAttributes.NotPublic | TypeAttributes.Sealed; Utils.AddAttribute(tgtType, "net.sf.jni4net.attributes.JavaProxyAttribute", RealType, StaticType); tgtType.BaseTypes.Add(Repository.javaLangObject.CLRReference); if (type.IsInterface) { tgtType.BaseTypes.Add(type.CLRReference); } tgtType.IsPartial = true; GenerateTypeOfInit(tgtType, true); GenerateWrapperInitJ2C(); if (type.Registration == null || !type.Registration.NoMethods) { if (type.IsInterface || type.IsDelegate) { GenerateProxyMethodsC2J(tgtType); } GenerateWrapperMethodsJ2C(tgtType); } GenerateConstructionHelper(tgtType); CreateEnvConstructor(tgtType, "net.sf.jni4net.jni.JNIEnv", false, false, true); tgtType.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, cdc)); tgtType.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, cdc)); }
private void GenerateInterface(CodeNamespace nameSpace) { var tgtType = new CodeTypeDeclaration(type.Name); SetCurrentType(type.JVMNamespace + "." + type.Name); AddTypeCLR(CurrentType.BaseType); nameSpace.Types.Add(tgtType); tgtType.TypeAttributes = TypeAttributes.Public; tgtType.IsInterface = true; tgtType.IsPartial = true; Utils.AddAttribute(tgtType, "net.sf.jni4net.attributes.JavaInterfaceAttribute", type.JVMFullName); foreach (GType ifc in type.Interfaces) { if (ifc.IsCLRGenerate || ifc.IsCLRType) { tgtType.BaseTypes.Add(ifc.CLRReference); } } if (type.Registration == null || !type.Registration.NoMethods) { // dump java side methods // CLR methods should be in partial file foreach (GMethod method in type.Methods) { if (method.IsJVMMethod && !method.IsField) { CreateMethodSignature(tgtType, method, false); } } } tgtType.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, cdc)); tgtType.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, cdc)); }
protected CodeMemberMethod CreateMethodSignature(GMethod method, bool skipSignature) { CodeMemberMethod tgtMethod; if (method.IsConstructor) { tgtMethod = new CodeConstructor(); } else { tgtMethod = new CodeMemberMethod(); tgtMethod.Name = method.JVMName; tgtMethod.ReturnType = method.ReturnType.JVMReference; } if (!config.SkipSignatures && !skipSignature) { if (method.IsConstructor) { Utils.AddAttribute(tgtMethod, "net.sf.jni4net.attributes.ClrConstructor", method.CLRSignature); } else { Utils.AddAttribute(tgtMethod, "net.sf.jni4net.attributes.ClrMethod", method.CLRSignature); } } tgtMethod.Attributes = method.Attributes; GenerateParameters(method, tgtMethod); return(tgtMethod); }
/// <summary> /// Create proxy for interface /// </summary> protected void GenerateProxy(CodeNamespace nameSpace) { var tgtType = new CodeTypeDeclaration("__" + type.Name); SetCurrentType(type.JVMNamespaceExt + ".__" + type.Name, type.JVMNamespace + "." + type.Name, type.JVMNamespaceExt + ".__" + type.Name, type.CLRNamespaceExt + "." + type.Name + "_"); AddTypeJVM(CurrentType.BaseType); nameSpace.Types.Add(tgtType); tgtType.TypeAttributes = TypeAttributes.NotPublic; Utils.AddAttribute(tgtType, "net.sf.jni4net.attributes.ClrProxy"); if (!type.IsDelegate) { tgtType.BaseTypes.Add(Repository.systemObject.JVMReference); } tgtType.BaseTypes.Add(type.JVMReference); CreateEnvConstructor(tgtType, "net.sf.jni4net.inj.INJEnv", false, true, false); if (type.Registration == null || !type.Registration.NoMethods) { foreach (GMethod method in type.MethodsWithInterfaces) { if (!method.IsStatic || !type.IsInterface) { CodeMemberMethod tgtMethod = CreateMethodSignature(method, false); ChangeNativeAttributes(tgtMethod); tgtType.Members.Add(tgtMethod); } } } tgtType.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, sMagicProxy)); tgtType.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, eMagicProxy)); }
protected void GenerateWrapperMethodsJ2C(CodeTypeDeclaration tgtType) { Utils.AddAttribute(tgtType, "net.sf.jni4net.attributes.ClrWrapperAttribute", RealType, StaticType); tgtType.Members.Add(initMethod); int m = 0; foreach (GMethod method in type.MethodsWithInterfaces) { string uName = (method.CLRName + m); if (!method.IsStatic || !type.IsInterface) { GenerateMethodJ2C(method, tgtType, uName); GenerateMethodRegistrationJ2C(method, uName); } m++; } m = 0; foreach (GMethod method in type.Constructors) { string uName = "__ctor" + type.Name + m; GenerateMethodJ2C(method, tgtType, uName); GenerateMethodRegistrationJ2C(method, uName); m++; } initMethod.Statements.Add( new CodeMethodReturnStatement(new CodeVariableReferenceExpression("methods"))); }
private void GenerateClass(CodeNamespace nameSpace) { var tgtType = new CodeTypeDeclaration(type.Name); SetCurrentType(type.CLRNamespace + "." + type.Name); AddTypeCLR(CurrentType.BaseType); nameSpace.Types.Add(tgtType); tgtType.TypeAttributes = type.Attributes & TypeAttributes.VisibilityMask; tgtType.IsPartial = true; Utils.AddAttribute(tgtType, "net.sf.jni4net.attributes.JavaClassAttribute"); tgtType.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, cdc)); tgtType.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, cdc)); if (type.Base != null) { tgtType.BaseTypes.Add(type.Base.CLRReference); } foreach (GType ifc in type.Interfaces) { if (ifc.IsCLRGenerate || ifc.IsCLRType) { tgtType.BaseTypes.Add(ifc.CLRReference); } } // fields with type information GenerateStaticFields(tgtType); GenerateTypeOfInit(tgtType, false); GenerateMethodsC2J(tgtType); GenerateConstructionHelper(tgtType); CreateEnvConstructor(tgtType, "net.sf.jni4net.jni.JNIEnv", type == Repository.javaLangClass || type == Repository.javaLangString, false, false); if (type.IsException) { if (type.IsRootType) { return; } Utils.AddAttribute(tgtType, "System.SerializableAttribute"); var sc = new CodeConstructor(); sc.Attributes = MemberAttributes.Family; sc.Parameters.Add(new CodeParameterDeclarationExpression(TypeReference(typeof(SerializationInfo)), "info")); sc.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("info")); sc.Parameters.Add(new CodeParameterDeclarationExpression(TypeReference(typeof(StreamingContext)), "context")); sc.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("context")); tgtType.Members.Add(sc); } }
private void GenerateStatic(CodeNamespace nameSpace) { // static helper type var tgtType = new CodeTypeDeclaration(type.Name + "_"); SetCurrentType(type.JVMNamespaceExt + "." + type.Name + "_", type.JVMNamespace + "." + type.Name, type.JVMNamespaceExt + ".__" + type.Name, type.JVMNamespaceExt + "." + type.Name + "_"); AddTypeJVM(CurrentType.BaseType); nameSpace.Types.Add(tgtType); tgtType.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed; Utils.AddAttribute(tgtType, "net.sf.jni4net.attributes.ClrTypeInfo"); // fields with type information GenerateTypeOfInit(tgtType); WrapMethodsMagic(tgtType, sMagicStatic, eMagicStatic); }
/*private void GenerateMethods(CodeTypeDeclaration tgtType) * { * foreach (GMethod method in type.Methods) * { * CreateMethodSignature(tgtType, method, false); * } * }*/ private CodeMemberMethod CreateConstructorHelper(GMethod constructor, string uName) { var tgtMethod = new CodeMemberMethod(); tgtMethod.Name = uName; tgtMethod.Attributes = MemberAttributes.Static | MemberAttributes.Private | MemberAttributes.New; Utils.AddAttribute(tgtMethod, "net.sf.jni4net.attributes.ClrMethod", constructor.CLRSignature); // inject thiz parameter var tgtParameter = new CodeParameterDeclarationExpression(); tgtParameter.Name = "thiz"; tgtParameter.Type = constructor.Type.CLRReference; tgtMethod.Parameters.Add(tgtParameter); GenerateParameters(constructor, tgtMethod); return(tgtMethod); }
protected override void Generate(CodeNamespace nameSpace) { var tgtType = new CodeTypeDeclaration(type.Name); SetCurrentType(type.JVMNamespace + "." + type.Name); AddTypeJVM(CurrentType.BaseType); nameSpace.Types.Add(tgtType); tgtType.TypeAttributes = type.Attributes & TypeAttributes.VisibilityMask; /*if (type.IsAbstract) * { * tgtType.TypeAttributes |= TypeAttributes.Abstract; * }*/ Utils.AddAttribute(tgtType, "net.sf.jni4net.attributes.ClrType"); if (type.Base != null) { tgtType.BaseTypes.Add(type.Base.JVMReference); } foreach (GType ifc in type.Interfaces) { if (ifc.IsJVMGenerate || ifc.IsJVMType) { tgtType.BaseTypes.Add(ifc.JVMReference); } } // fields with type information CreateEnvConstructor(tgtType, "net.sf.jni4net.inj.INJEnv", false, true, false); GenerateConstructors(tgtType); GenerateMethods(tgtType); GenerateTypeOfInit(tgtType); WrapMethodsMagic(tgtType, sMagicProxy, eMagicProxy); if (type.IsDelegate) { tgtType.TypeAttributes |= TypeAttributes.Abstract; GenerateProxy(nameSpace); } }
private void GenerateInterface(CodeNamespace nameSpace) { var tgtType = new CodeTypeDeclaration(type.Name); SetCurrentType(type.JVMNamespace + "." + type.Name); AddTypeJVM(CurrentType.BaseType); nameSpace.Types.Add(tgtType); tgtType.TypeAttributes = TypeAttributes.Public; tgtType.IsInterface = true; Utils.AddAttribute(tgtType, "net.sf.jni4net.attributes.ClrInterface"); foreach (GType ifc in type.Interfaces) { if (ifc.IsJVMGenerate || ifc.IsJVMType) { tgtType.BaseTypes.Add(ifc.JVMReference); } } CodeMemberMethod first = null; CodeMemberMethod last = null; if (type.Registration == null || !type.Registration.NoMethods) { // dump java side methods first, they will be replaced by oldFile eventually foreach (GMethod method in type.Methods) { if (method.IsJVMMethod) { last = CreateMethodSignature(method, true); tgtType.Members.Add(last); } } // now dump methods which are found on CLR side foreach (GMethod method in type.Methods) { if (method.IsCLRMethod) { last = CreateMethodSignature(method, false); tgtType.Members.Add(last); if (first == null) { first = last; } } } } // if there is at leat one, put magic block around if (first != null) { first.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, sMagicIface)); last.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, eMagicIface)); } else if (last != null) { last.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, sMagicIface)); last.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, eMagicIface)); } }
protected CodeStatementCollection CreateMethodSignature(CodeTypeDeclaration tgtType, GMethod method, bool isProxy) { bool add = true; CodeStatementCollection tgtStatements; CodeTypeMember tgtMember; CodeMemberMethod tgtMethod = null; CodeMemberPropertyEx tgtProperty; CodeMemberPropertyEx tgtEvent; if (method.IsConstructor) { var tgtConstructor = new CodeConstructor(); tgtMethod = tgtConstructor; tgtMember = tgtMethod; tgtStatements = tgtMethod.Statements; if (!type.IsRootType) { tgtConstructor.BaseConstructorArgs.Add( new CodeCastExpression(TypeReference("net.sf.jni4net.jni.JNIEnv"), new CodePrimitiveExpression(null))); } } else if (method.IsField) { var p = new CodeMemberProperty(); tgtMember = p; tgtStatements = p.GetStatements; p.Name = method.CLRName; p.Type = method.ReturnType.CLRReference; } else if (method.IsEvent) { tgtEvent = new CodeMemberPropertyEx(); tgtEvent.Getter = method.CLRPropertyAdd; tgtEvent.Setter = method.CLRPropertyRemove; tgtEvent.Name = method.CLRName; if (method.UseExplicitInterface) { tgtEvent.PrivateImplementationType = method.DeclaringType.CLRReference; } foreach (CodeTypeMember m in tgtType.Members) { var member = m as CodeMemberPropertyEx; if (member != null) { if (member.Getter == method || member.Setter == method) { tgtEvent = member; add = false; break; } } } int count = method.Parameters.Count - 1; tgtEvent.Type = method.Parameters[count].CLRReference; if (add) { for (int i = 0; i < count; i++) { var tgtParameter = new CodeParameterDeclarationExpression(); tgtParameter.Name = method.ParameterNames[i]; tgtParameter.Type = method.Parameters[i].CLRReference; tgtEvent.Parameters.Add(tgtParameter); } tgtEvent.Comments.Add(new CodeCommentStatement("__event__")); } tgtMember = tgtEvent; if (method.IsCLRPropertyAdd) { tgtEvent.GetStatements.Add(new CodeCommentStatement("__add__")); tgtStatements = tgtEvent.GetStatements; } else { tgtEvent.SetStatements.Add(new CodeCommentStatement("__remove__")); tgtStatements = tgtEvent.SetStatements; } } else if (method.IsProperty) { tgtProperty = new CodeMemberPropertyEx(); tgtProperty.Setter = method.CLRPropertySetter; tgtProperty.Getter = method.CLRPropertyGetter; tgtProperty.Name = method.CLRName; if (method.UseExplicitInterface) { tgtProperty.PrivateImplementationType = method.DeclaringType.CLRReference; } foreach (CodeTypeMember m in tgtType.Members) { var member = m as CodeMemberPropertyEx; if (member != null) { if (member.Getter == method || member.Setter == method) { tgtProperty = member; add = false; break; } } } int count = method.Parameters.Count; if (!method.IsCLRPropertyGetter) { count--; } if (add) { for (int i = 0; i < count; i++) { var tgtParameter = new CodeParameterDeclarationExpression(); tgtParameter.Name = method.ParameterNames[i]; tgtParameter.Type = method.Parameters[i].CLRReference; tgtProperty.Parameters.Add(tgtParameter); } } if (method.IsCLRPropertyGetter) { tgtProperty.Type = method.ReturnType.CLRReference; tgtStatements = tgtProperty.GetStatements; } else { tgtProperty.Type = method.Parameters[count].CLRReference; tgtStatements = tgtProperty.SetStatements; } tgtMember = tgtProperty; } else { tgtMethod = new CodeMemberMethod(); tgtMethod.Name = method.CLRName; tgtMethod.ReturnType = method.ReturnType.CLRReference; tgtMember = tgtMethod; tgtStatements = tgtMethod.Statements; if (method.UseExplicitInterface) { tgtMethod.PrivateImplementationType = method.DeclaringType.CLRReference; } } if (!config.SkipSignatures && !isProxy) { Utils.AddAttribute(tgtMember, "net.sf.jni4net.attributes.JavaMethodAttribute", method.JVMSignature); } tgtMember.Attributes = method.Attributes; if (isProxy) { tgtMember.Attributes |= MemberAttributes.Final; } if (tgtMethod != null) { GenerateParameters(method, tgtMethod); } if (add) { tgtType.Members.Add(tgtMember); } return(tgtStatements); }