static void Main() { DataCheckerBot DB = new DataCheckerBot(); MemberBox tbox = new MemberBox(); Console.WriteLine(tbox.Name); Console.ReadKey(); }
public MemberBox MemberBoxFull() { MemberBox MBox = new MemberBox(); //MBox.Email = button; //MBox.MemberType = button; //MBox.Mobile = button; //MBox.Name = button; //MBox.NUSID = button.ToUpper(); return(MBox); }
private static MemberBox ExtractSetMethod(Type type, MemberBox[] methods, bool isStatic) { // // Note: it may be preferable to allow NativeJavaMethod.findFunction() // to find the appropriate setter; unfortunately, it requires an // instance of the target arg to determine that. // // Make two passes: one to find a method with direct type assignment, // and one to find a widening conversion. for (int pass = 1; pass <= 2; ++pass) { foreach (MemberBox method in methods) { if (!isStatic || method.IsStatic()) { Type[] @params = method.argTypes; if (@params.Length == 1) { if (pass == 1) { if (@params[0] == type) { return method; } } else { if (pass != 2) { Kit.CodeBug(); } if (@params[0].IsAssignableFrom(type)) { return method; } } } } } } return null; }
private static MemberBox ExtractGetMethod(MemberBox[] methods, bool isStatic) { // Inspect the list of all MemberBox for the only one having no // parameters foreach (MemberBox method in methods) { // Does getter method have an empty parameter list with a return // value (eg. a getSomething() or isSomething())? if (method.argTypes.Length == 0 && (!isStatic || method.IsStatic())) { Type type = method.Method().ReturnType; if (type != typeof(void)) { return method; } break; } } return null; }
private void Reflect(Scriptable scope, bool includeProtected, bool includePrivate) { // We reflect methods first, because we want overloaded field/method // names to be allocated to the NativeJavaMethod before the field // gets in the way. MethodInfo[] methods = DiscoverAccessibleMethods(cl, includeProtected, includePrivate); foreach (MethodInfo method in methods) { int mods = method.Attributes; bool isStatic = Modifier.IsStatic(mods); IDictionary<string, object> ht = isStatic ? staticMembers : members; string name = method.Name; object value = ht.Get(name); if (value == null) { ht.Put(name, method); } else { ObjArray overloadedMethods; if (value is ObjArray) { overloadedMethods = (ObjArray)value; } else { if (!(value is MethodInfo)) { Kit.CodeBug(); } // value should be instance of Method as at this stage // staticMembers and members can only contain methods overloadedMethods = new ObjArray(); overloadedMethods.Add(value); ht.Put(name, overloadedMethods); } overloadedMethods.Add(method); } } // replace Method instances by wrapped NativeJavaMethod objects // first in staticMembers and then in members for (int tableCursor = 0; tableCursor != 2; ++tableCursor) { bool isStatic = (tableCursor == 0); IDictionary<string, object> ht = isStatic ? staticMembers : members; foreach (KeyValuePair<string, object> entry in ht.EntrySet()) { MemberBox[] methodBoxes; object value = entry.Value; if (value is MethodInfo) { methodBoxes = new MemberBox[1]; methodBoxes[0] = new MemberBox((MethodInfo)value); } else { ObjArray overloadedMethods = (ObjArray)value; int N = overloadedMethods.Size(); if (N < 2) { Kit.CodeBug(); } methodBoxes = new MemberBox[N]; for (int i = 0; i != N; ++i) { MethodInfo method_1 = (MethodInfo)overloadedMethods.Get(i); methodBoxes[i] = new MemberBox(method_1); } } NativeJavaMethod fun = new NativeJavaMethod(methodBoxes); if (scope != null) { ScriptRuntime.SetFunctionProtoAndParent(fun, scope); } ht.Put(entry.Key, fun); } } // Reflect fields. FieldInfo[] fields = GetAccessibleFields(includeProtected, includePrivate); foreach (FieldInfo field in fields) { string name = field.Name; int mods = field.Attributes; try { bool isStatic = Modifier.IsStatic(mods); IDictionary<string, object> ht = isStatic ? staticMembers : members; object member = ht.Get(name); if (member == null) { ht.Put(name, field); } else { if (member is NativeJavaMethod) { NativeJavaMethod method_1 = (NativeJavaMethod)member; FieldAndMethods fam = new FieldAndMethods(scope, method_1.methods, field); IDictionary<string, FieldAndMethods> fmht = isStatic ? staticFieldAndMethods : fieldAndMethods; if (fmht == null) { fmht = new Dictionary<string, FieldAndMethods>(); if (isStatic) { staticFieldAndMethods = fmht; } else { fieldAndMethods = fmht; } } fmht.Put(name, fam); ht.Put(name, fam); } else { if (member is FieldInfo) { FieldInfo oldField = (FieldInfo)member; // If this newly reflected field shadows an inherited field, // then replace it. Otherwise, since access to the field // would be ambiguous from Java, no field should be // reflected. // For now, the first field found wins, unless another field // explicitly shadows it. if (oldField.DeclaringType.IsAssignableFrom(field.DeclaringType)) { ht.Put(name, field); } } else { // "unknown member type" Kit.CodeBug(); } } } } catch (SecurityException) { // skip this field Context.ReportWarning("Could not access field " + name + " of class " + cl.FullName + " due to lack of privileges."); } } // Create bean properties from corresponding get/set methods first for // static members and then for instance members for (int tableCursor_1 = 0; tableCursor_1 != 2; ++tableCursor_1) { bool isStatic = (tableCursor_1 == 0); IDictionary<string, object> ht = isStatic ? staticMembers : members; IDictionary<string, BeanProperty> toAdd = new Dictionary<string, BeanProperty>(); // Now, For each member, make "bean" properties. foreach (string name in ht.Keys) { // Is this a getter? bool memberIsGetMethod = name.StartsWith("get"); bool memberIsSetMethod = name.StartsWith("set"); bool memberIsIsMethod = name.StartsWith("is"); if (memberIsGetMethod || memberIsIsMethod || memberIsSetMethod) { // Double check name component. string nameComponent = Sharpen.Runtime.Substring(name, memberIsIsMethod ? 2 : 3); if (nameComponent.Length == 0) { continue; } // Make the bean property name. string beanPropertyName = nameComponent; char ch0 = nameComponent[0]; if (System.Char.IsUpper(ch0)) { if (nameComponent.Length == 1) { beanPropertyName = nameComponent.ToLower(); } else { char ch1 = nameComponent[1]; if (!System.Char.IsUpper(ch1)) { beanPropertyName = System.Char.ToLower(ch0) + Sharpen.Runtime.Substring(nameComponent, 1); } } } // If we already have a member by this name, don't do this // property. if (toAdd.ContainsKey(beanPropertyName)) { continue; } object v = ht.Get(beanPropertyName); if (v != null) { // A private field shouldn't mask a public getter/setter if (!includePrivate || !(v is MemberInfo) || !Modifier.IsPrivate(((MemberInfo)v).Attributes)) { continue; } } // Find the getter method, or if there is none, the is- // method. MemberBox getter = null; getter = FindGetter(isStatic, ht, "get", nameComponent); // If there was no valid getter, check for an is- method. if (getter == null) { getter = FindGetter(isStatic, ht, "is", nameComponent); } // setter MemberBox setter = null; NativeJavaMethod setters = null; string setterName = System.String.Concat("set", nameComponent); if (ht.ContainsKey(setterName)) { // Is this value a method? object member = ht.Get(setterName); if (member is NativeJavaMethod) { NativeJavaMethod njmSet = (NativeJavaMethod)member; if (getter != null) { // We have a getter. Now, do we have a matching // setter? Type type = getter.Method().ReturnType; setter = ExtractSetMethod(type, njmSet.methods, isStatic); } else { // No getter, find any set method setter = ExtractSetMethod(njmSet.methods, isStatic); } if (njmSet.methods.Length > 1) { setters = njmSet; } } } // Make the property. BeanProperty bp = new BeanProperty(getter, setter, setters); toAdd.Put(beanPropertyName, bp); } } // Add the new bean properties. foreach (string key in toAdd.Keys) { object value = toAdd.Get(key); ht.Put(key, value); } } // Reflect constructors ConstructorInfo<object>[] constructors = GetAccessibleConstructors(includePrivate); MemberBox[] ctorMembers = new MemberBox[constructors.Length]; for (int i_1 = 0; i_1 != constructors.Length; ++i_1) { ctorMembers[i_1] = new MemberBox(constructors[i_1]); } ctors = new NativeJavaMethod(ctorMembers, cl.Name); }
internal FieldAndMethods(Scriptable scope, MemberBox[] methods, FieldInfo field) : base(methods) { this.field = field; SetParentScope(scope); SetPrototype(ScriptableObject.GetFunctionPrototype(scope)); }
private static void PrintDebug(string msg, MemberBox member, object[] args) { }
/// <summary>Create a JavaScript function object from a Java method.</summary> /// <remarks> /// Create a JavaScript function object from a Java method. /// <p>The <code>member</code> argument must be either a java.lang.reflect.Method /// or a java.lang.reflect.Constructor and must match one of two forms.<p> /// The first form is a member with zero or more parameters /// of the following types: Object, String, boolean, Scriptable, /// int, or double. The Long type is not supported /// because the double representation of a long (which is the /// EMCA-mandated storage type for Numbers) may lose precision. /// If the member is a Method, the return value must be void or one /// of the types allowed for parameters.<p> /// The runtime will perform appropriate conversions based /// upon the type of the parameter. A parameter type of /// Object specifies that no conversions are to be done. A parameter /// of type String will use Context.toString to convert arguments. /// Similarly, parameters of type double, boolean, and Scriptable /// will cause Context.toNumber, Context.toBoolean, and /// Context.toObject, respectively, to be called.<p> /// If the method is not static, the Java 'this' value will /// correspond to the JavaScript 'this' value. Any attempt /// to call the function with a 'this' value that is not /// of the right Java type will result in an error.<p> /// The second form is the variable arguments (or "varargs") /// form. If the FunctionObject will be used as a constructor, /// the member must have the following parameters /// <pre> /// (Context cx, Object[] args, Function ctorObj, /// boolean inNewExpr)</pre> /// and if it is a Method, be static and return an Object result.<p> /// Otherwise, if the FunctionObject will <i>not</i> be used to define a /// constructor, the member must be a static Method with parameters /// <pre> /// (Context cx, Scriptable thisObj, Object[] args, /// Function funObj) </pre> /// and an Object result.<p> /// When the function varargs form is called as part of a function call, /// the <code>args</code> parameter contains the /// arguments, with <code>thisObj</code> /// set to the JavaScript 'this' value. <code>funObj</code> /// is the function object for the invoked function.<p> /// When the constructor varargs form is called or invoked while evaluating /// a <code>new</code> expression, <code>args</code> contains the /// arguments, <code>ctorObj</code> refers to this FunctionObject, and /// <code>inNewExpr</code> is true if and only if a <code>new</code> /// expression caused the call. This supports defining a function that /// has different behavior when called as a constructor than when /// invoked as a normal function call. (For example, the Boolean /// constructor, when called as a function, /// will convert to boolean rather than creating a new object.)<p> /// </remarks> /// <param name="name">the name of the function</param> /// <param name="methodOrConstructor"> /// a java.lang.reflect.Method or a java.lang.reflect.Constructor /// that defines the object /// </param> /// <param name="scope">enclosing scope of function</param> /// <seealso cref="Scriptable">Scriptable</seealso> public FunctionObject(string name, MemberInfo methodOrConstructor, Scriptable scope) { // API class if (methodOrConstructor is ConstructorInfo) { member = new MemberBox((ConstructorInfo<object>)methodOrConstructor); isStatic = true; } else { // well, doesn't take a 'this' member = new MemberBox((MethodInfo)methodOrConstructor); isStatic = member.IsStatic(); } string methodName = member.GetName(); this.functionName = name; Type[] types = member.argTypes; int arity = types.Length; if (arity == 4 && (types[1].IsArray || types[2].IsArray)) { // Either variable args or an error. if (types[1].IsArray) { if (!isStatic || types[0] != ScriptRuntime.ContextClass || types[1].GetElementType() != ScriptRuntime.ObjectClass || types[2] != ScriptRuntime.FunctionClass || types[3] != typeof(bool)) { throw Context.ReportRuntimeError1("msg.varargs.ctor", methodName); } parmsLength = VARARGS_CTOR; } else { if (!isStatic || types[0] != ScriptRuntime.ContextClass || types[1] != ScriptRuntime.ScriptableClass || types[2].GetElementType() != ScriptRuntime.ObjectClass || types[3] != ScriptRuntime.FunctionClass) { throw Context.ReportRuntimeError1("msg.varargs.fun", methodName); } parmsLength = VARARGS_METHOD; } } else { parmsLength = arity; if (arity > 0) { typeTags = new byte[arity]; for (int i = 0; i != arity; ++i) { int tag = GetTypeTag(types[i]); if (tag == JAVA_UNSUPPORTED_TYPE) { throw Context.ReportRuntimeError2("msg.bad.parms", types[i].FullName, methodName); } typeTags[i] = unchecked((byte)tag); } } } if (member.IsMethod()) { MethodInfo method = member.Method(); Type returnType = method.ReturnType; if (returnType == typeof(void)) { hasVoidReturn = true; } else { returnTypeTag = GetTypeTag(returnType); } } else { Type ctorType = member.GetDeclaringClass(); if (!ScriptRuntime.ScriptableClass.IsAssignableFrom(ctorType)) { throw Context.ReportRuntimeError1("msg.bad.ctor.return", ctorType.FullName); } } ScriptRuntime.SetFunctionProtoAndParent(this, scope); }
internal NativeJavaMethod(MemberBox[] methods, string name) { this.functionName = name; this.methods = methods; }
internal NativeJavaMethod(MemberBox[] methods) { this.functionName = methods[0].GetName(); this.methods = methods; }
/// <summary> /// Find the index of the correct function to call given the set of methods /// or constructors and the arguments. /// </summary> /// <remarks> /// Find the index of the correct function to call given the set of methods /// or constructors and the arguments. /// If no function can be found to call, return -1. /// </remarks> internal static int FindFunction(Context cx, MemberBox[] methodsOrCtors, object[] args) { if (methodsOrCtors.Length == 0) { return -1; } else { if (methodsOrCtors.Length == 1) { MemberBox member = methodsOrCtors[0]; Type[] argTypes = member.argTypes; int alength = argTypes.Length; if (member.vararg) { alength--; if (alength > args.Length) { return -1; } } else { if (alength != args.Length) { return -1; } } for (int j = 0; j != alength; ++j) { if (!NativeJavaObject.CanConvert(args[j], argTypes[j])) { return -1; } } return 0; } } int firstBestFit = -1; int[] extraBestFits = null; int extraBestFitsCount = 0; for (int i = 0; i < methodsOrCtors.Length; i++) { MemberBox member = methodsOrCtors[i]; Type[] argTypes = member.argTypes; int alength = argTypes.Length; if (member.vararg) { alength--; if (alength > args.Length) { goto search_continue; } } else { if (alength != args.Length) { goto search_continue; } } for (int j = 0; j < alength; j++) { if (!NativeJavaObject.CanConvert(args[j], argTypes[j])) { goto search_continue; } } if (firstBestFit < 0) { firstBestFit = i; } else { // Compare with all currently fit methods. // The loop starts from -1 denoting firstBestFit and proceed // until extraBestFitsCount to avoid extraBestFits allocation // in the most common case of no ambiguity int betterCount = 0; // number of times member was prefered over // best fits int worseCount = 0; // number of times best fits were prefered // over member for (int j_1 = -1; j_1 != extraBestFitsCount; ++j_1) { int bestFitIndex; if (j_1 == -1) { bestFitIndex = firstBestFit; } else { bestFitIndex = extraBestFits[j_1]; } MemberBox bestFit = methodsOrCtors[bestFitIndex]; if (cx.HasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS) && (bestFit.Member().Attributes & Modifier.PUBLIC) != (member.Member().Attributes & Modifier.PUBLIC)) { // When FEATURE_ENHANCED_JAVA_ACCESS gives us access // to non-public members, continue to prefer public // methods in overloading if ((bestFit.Member().Attributes & Modifier.PUBLIC) == 0) { ++betterCount; } else { ++worseCount; } } else { int preference = PreferSignature(args, argTypes, member.vararg, bestFit.argTypes, bestFit.vararg); if (preference == PREFERENCE_AMBIGUOUS) { break; } else { if (preference == PREFERENCE_FIRST_ARG) { ++betterCount; } else { if (preference == PREFERENCE_SECOND_ARG) { ++worseCount; } else { if (preference != PREFERENCE_EQUAL) { Kit.CodeBug(); } // This should not happen in theory // but on some JVMs, Class.getMethods will return all // static methods of the class hierarchy, even if // a derived class's parameters match exactly. // We want to call the derived class's method. if (bestFit.IsStatic() && bestFit.GetDeclaringClass().IsAssignableFrom(member.GetDeclaringClass())) { // On some JVMs, Class.getMethods will return all // static methods of the class hierarchy, even if // a derived class's parameters match exactly. // We want to call the derived class's method. if (j_1 == -1) { firstBestFit = i; } else { extraBestFits[j_1] = i; } } goto search_continue; } } } } } if (betterCount == 1 + extraBestFitsCount) { // member was prefered over all best fits firstBestFit = i; extraBestFitsCount = 0; } else { if (worseCount == 1 + extraBestFitsCount) { } else { // all best fits were prefered over member, ignore it // some ambiguity was present, add member to best fit set if (extraBestFits == null) { // Allocate maximum possible array extraBestFits = new int[methodsOrCtors.Length - 1]; } extraBestFits[extraBestFitsCount] = i; ++extraBestFitsCount; } } } search_continue: ; } search_break: ; if (firstBestFit < 0) { // Nothing was found return -1; } else { if (extraBestFitsCount == 0) { // single best fit return firstBestFit; } } // report remaining ambiguity StringBuilder buf = new StringBuilder(); for (int j_2 = -1; j_2 != extraBestFitsCount; ++j_2) { int bestFitIndex; if (j_2 == -1) { bestFitIndex = firstBestFit; } else { bestFitIndex = extraBestFits[j_2]; } buf.Append("\n "); buf.Append(methodsOrCtors[bestFitIndex].ToJavaDeclaration()); } MemberBox firstFitMember = methodsOrCtors[firstBestFit]; string memberName = firstFitMember.GetName(); string memberClass = firstFitMember.GetDeclaringClass().FullName; if (methodsOrCtors[0].IsCtor()) { throw Context.ReportRuntimeError3("msg.constructor.ambiguous", memberName, ScriptSignature(args), buf.ToString()); } else { throw Context.ReportRuntimeError4("msg.method.ambiguous", memberClass, memberName, ScriptSignature(args), buf.ToString()); } }
internal static object ConstructInternal(object[] args, MemberBox ctor) { Type[] argTypes = ctor.argTypes; if (ctor.vararg) { // marshall the explicit parameter object[] newArgs = new object[argTypes.Length]; for (int i = 0; i < argTypes.Length - 1; i++) { newArgs[i] = Context.JsToJava(args[i], argTypes[i]); } object varArgs; // Handle special situation where a single variable parameter // is given and it is a Java or ECMA array. if (args.Length == argTypes.Length && (args[args.Length - 1] == null || args[args.Length - 1] is NativeArray || args[args.Length - 1] is NativeJavaArray)) { // convert the ECMA array into a native array varArgs = Context.JsToJava(args[args.Length - 1], argTypes[argTypes.Length - 1]); } else { // marshall the variable parameter Type componentType = argTypes[argTypes.Length - 1].GetElementType(); varArgs = System.Array.CreateInstance(componentType, args.Length - argTypes.Length + 1); for (int i_1 = 0; i_1 < Sharpen.Runtime.GetArrayLength(varArgs); i_1++) { object value = Context.JsToJava(args[argTypes.Length - 1 + i_1], componentType); Sharpen.Runtime.SetArrayValue(varArgs, i_1, value); } } // add varargs newArgs[argTypes.Length - 1] = varArgs; // replace the original args with the new one args = newArgs; } else { object[] origArgs = args; for (int i = 0; i < args.Length; i++) { object arg = args[i]; object x = Context.JsToJava(arg, argTypes[i]); if (x != arg) { if (args == origArgs) { args = origArgs.Clone(); } args[i] = x; } } } return ctor.NewInstance(args); }
internal static Scriptable ConstructSpecific(Context cx, Scriptable scope, object[] args, MemberBox ctor) { object instance = ConstructInternal(args, ctor); // we need to force this to be wrapped, because construct _has_ // to return a scriptable Scriptable topLevel = ScriptableObject.GetTopLevelScope(scope); return cx.GetWrapFactory().WrapNewObject(cx, topLevel, instance); }
private static MemberBox ExtractSetMethod(MemberBox[] methods, bool isStatic) { foreach (MemberBox method in methods) { if (!isStatic || method.IsStatic()) { if (method.Method().ReturnType == typeof(void)) { if (method.argTypes.Length == 1) { return method; } } } } return null; }
internal NativeJavaMethod(MemberBox method, string name) { this.functionName = name; this.methods = new MemberBox[] { method }; }
internal BeanProperty(MemberBox getter, MemberBox setter, NativeJavaMethod setters) { this.getter = getter; this.setter = setter; this.setters = setters; }
public NativeJavaConstructor(MemberBox ctor) { this.ctor = ctor; }