// using reflect to get private variable public Integer getReflectObjectColor() { try { Field mField = mObject.getClass().getDeclaredField(reflectObjectVariableName); // Inorder to access private variable, you need to set accessible to true mField.setAccessible(true); // setter usage // mField.set(mObject, 222); return mField.getInt(mObject); } catch (Exception e) { } return null; }
public String getReflectObjectMethod() { try { Method method = mObject .getClass() .getDeclaredMethod(reflectObjectMethodName, Integer.class, Double.class); // set true to have the right to access private method method.setAccessible(true); // it's important to konw the return object's type String invorkReult = (String) method.invoke(mObject, 123, 456.0); return invorkReult; } catch (Exception e) { } return null; }
static TclObject getArrayElt( Interp interp, // Current interpreter. Object arrayObj, // Array to dereference. Must be an array. Class arrayClass, // Class object of array to deref int index, // First elt to dereference. boolean convert) throws TclException // May encounter bad index. { // Set "obj" to the index'th element of the arrayObj. If Array.get() // fails, reset the interp result to cover error message, and set // "obj" to null. Wrap "obj" in a TclObject and append it to the // result list. Object obj; try { obj = Array.get(arrayObj, index); } catch (ArrayIndexOutOfBoundsException e) { int max = Array.getLength(arrayObj) - 1; throw new TclException( interp, "array index \"" + index + "\" is out of bounds: must be between 0 and " + max); } /* * * System.out.println("object class is " + arrayObj.getClass()); * System.out.println("object component type is " + * arrayObj.getClass().getComponentType()); * * System.out.println("array class is " + arrayClass); * System.out.println("array component type is " + * arrayClass.getComponentType()); * System.out.println("obj derived type is " + obj.getClass()); */ if (convert) { return JavaInvoke.convertJavaObject(interp, arrayClass, obj); } else { return ReflectObject.newInstance(interp, arrayClass, obj); } }
public void cmdProc( Interp interp, // Current interpreter. TclObject argv[]) // Argument list. throws TclException // If wrong number of args, a // standard Tcl exception; // If an exception is encountered // during the invokation of the method, // the Exception object will be stored // in the errorCode of the interp. { boolean convert; int optionIdx, numArgs; Object subArrayObj; Class subArrayClass; int option, index, numDims, count; TclObject indexListObj; if (argv.length < 2) { throw new TclNumArgsException(interp, 1, argv, "?-noconvert? option ?arg arg ...?"); } String arg1 = argv[1].toString(); if ((arg1.length() >= 2) && (NOCONVERT.startsWith(arg1))) { // numArgs is the number of optional arguments after the // sub-command. convert = false; optionIdx = 2; numArgs = argv.length - 3; } else { convert = true; optionIdx = 1; numArgs = argv.length - 2; } if (numArgs < 0) { throw new TclNumArgsException(interp, 1, argv, "?-noconvert? option ?arg arg ...?"); } // If the <option> argument to the array object command is one of the // array sub-commands, then proceed to the switch statement below. // Otherwise, the array object behaves as a reflect object by calling // ReflectObject.cmdProc, and <option> will be treated as a method // of the array object. // // We can be sure that there is no conflect between the array // sub-commands and the method of the array object. This is // because the array object is an instance of java.lang.Object, // which has only the following methods (as of JDK 1.1): getClass, // hashCode, equals, wait, toString, notify, notifyAll try { option = TclIndex.get(interp, argv[optionIdx], validCmds, "option", TCL.EXACT); } catch (TclException e) { try { int startIdx = optionIdx + 1; FuncSig.get( interp, javaClass, argv[optionIdx], argv, startIdx, argv.length - startIdx, false); } catch (TclException e1) { throw new TclException( interp, "bad option \"" + argv[optionIdx] + "\": must be length, get, getrange, " + "set, setrange, or a valid method signature"); } super.cmdProc(interp, argv); return; } switch (option) { case OPT_LENGTH: if (numArgs != 0) { throw new TclNumArgsException(interp, optionIdx + 1, argv, ""); } if (convert == false) { throw new TclException( interp, "-noconvert flag not allowed for the \"length\" sub-command"); } interp.setResult(Array.getLength(javaObj)); return; case OPT_GET: if (numArgs != 1) { throw new TclNumArgsException(interp, optionIdx + 1, argv, "indexList"); } indexListObj = argv[optionIdx + 1]; numDims = TclList.getLength(interp, indexListObj); if (numDims == 0) { subArrayObj = javaObj; subArrayClass = dereferenceClassDims(interp, javaClass, 1); index = 0; } else { // Dereference all but the last dimension specified. Set // the interpreter result to the index'th element of the // "subArrayObj". subArrayObj = dereferenceArrayDims(interp, javaObj, numDims, indexListObj); subArrayClass = dereferenceClassDims(interp, javaClass, numDims); index = TclInteger.getInt(interp, TclList.index(interp, indexListObj, numDims - 1)); } // Set the interpreter result to a TclObject containing the index'th // element of "subArrayObj". interp.setResult(getArrayElt(interp, subArrayObj, subArrayClass, index, convert)); return; case OPT_SET: if (numArgs != 2) { throw new TclNumArgsException(interp, optionIdx + 1, argv, "indexList value"); } if (convert == false) { throw new TclException(interp, "-noconvert flag not allowed for the \"set\" sub-command"); } indexListObj = argv[optionIdx + 1]; numDims = TclList.getLength(interp, indexListObj); if (numDims == 0) { subArrayObj = javaObj; subArrayClass = dereferenceClassDims(interp, javaClass, 1); index = 0; } else { // Dereference all but the last dimension specified. Set // the value of index'th element of the "subArrayObj" to // the value in argv[optionIdx + 2]. subArrayObj = dereferenceArrayDims(interp, javaObj, numDims, indexListObj); subArrayClass = dereferenceClassDims(interp, javaClass, numDims); index = TclInteger.getInt(interp, TclList.index(interp, indexListObj, numDims - 1)); } // Set the value of the index'th element of "subArrayObj" to the // value // in the TclObject argv[optionIdx + 2]. setArrayElt(interp, subArrayObj, subArrayClass, index, argv[optionIdx + 2]); interp.resetResult(); return; case OPT_GETRANGE: if (numArgs > 2) { throw new TclNumArgsException(interp, optionIdx + 1, argv, "?indexList ?count??"); } // If an index list is specified, dereference all but the last // dimension specified. If the index list is empty, getrange // behaves as an identity function and returns argv[0]. subArrayObj = javaObj; subArrayClass = dereferenceClassDims(interp, javaClass, 1); index = 0; if (numArgs > 0) { indexListObj = argv[optionIdx + 1]; numDims = TclList.getLength(interp, indexListObj); if (numDims > 0) { subArrayObj = dereferenceArrayDims(interp, javaObj, numDims, indexListObj); subArrayClass = dereferenceClassDims(interp, javaClass, numDims); index = TclInteger.getInt(interp, TclList.index(interp, indexListObj, numDims - 1)); } } // The variable "count" represents the number of elements to // return. The default is the array size less the first index // (the remaining elements of the array). If a count is // specified and is smaller than the default, the default is // overridden. count = Array.getLength(subArrayObj) - index; if (numArgs > 1) { count = Math.min(count, TclInteger.getInt(interp, argv[optionIdx + 2])); } // Set the interpreter result to a TclList containing "count" // elements // of the "subArrayObj", starting with the index'th element. interp.setResult(getArrayElts(interp, subArrayObj, subArrayClass, index, count, convert)); return; case OPT_SETRANGE: if ((numArgs < 1) || (numArgs > 3)) { throw new TclNumArgsException( interp, optionIdx + 1, argv, "?indexList ?count?? valueList"); } if (convert == false) { throw new TclException( interp, "-noconvert flag not allowed for the \"setrange\" sub-command"); } TclObject tclValueListObj = argv[argv.length - 1]; // If an index list is specified, dereference all but the last // dimension specified. If the index list is empty, setrange // initialized the array object as it would in the java::new // command. subArrayObj = javaObj; subArrayClass = dereferenceClassDims(interp, javaClass, 1); index = 0; if (numArgs > 1) { indexListObj = argv[optionIdx + 1]; numDims = TclList.getLength(interp, indexListObj); if (numDims > 0) { subArrayObj = dereferenceArrayDims(interp, javaObj, numDims, indexListObj); subArrayClass = dereferenceClassDims(interp, javaClass, numDims); index = TclInteger.getInt(interp, TclList.index(interp, indexListObj, numDims - 1)); } } // "count" represents the number of elements to set. The // default is the minimum of the valueList size and array size // less the first index (the remaining elements of the array). // If a count is specified and is smaller than the default, // the default is overridden. count = Math.min( TclList.getLength(interp, tclValueListObj), Array.getLength(subArrayObj) - index); if (numArgs > 2) { count = Math.min(count, TclInteger.getInt(interp, argv[optionIdx + 2])); } // Set the value of "count" elements of the "subArrayObj", starting // with the index'th element, to the values in tclValueListObj. setArrayElts(interp, subArrayObj, subArrayClass, index, count, tclValueListObj); interp.resetResult(); return; } }