简体   繁体   English

使用PrivilegedAccessor带有原始参数的方法

[英]Method with primitive argument using PrivilegedAccessor

Within my Junit, I am trying to execute a Method using PrivilegedAccessor as following but getting NoSuchMethodException: Invalid Method getInsurance - 在我的Junit中,我尝试使用PrivilegedAccessor执行以下方法,但得到NoSuchMethodException: Invalid Method getInsurance

PrivilegedAccessor.invokeMethod(InsuranceRetriever,
                    "getInsurance", new Object[] { code}, new Class[] {
                            Long.class});

Following is the tested method declaration. 以下是经过测试的方法声明。

private InsObj getInsurance(long code)

*Please note here that i am passing Long.class whereas the real argument is primitive long. *请注意,我正在传递Long.class,而实际参数是原始long。 How to pass primitive argument. 如何传递原始参数。

Try this: 尝试这个:

PrivilegedAccessor.invokeMethod(InsuranceRetriever,
                "getInsurance", new Object[] { code }, 
                new Class[] { Long.TYPE });

Explanation: 说明:

The class objects for the primitive types are found under the TYPE static final constant in each of the java.lang objects for each of the primitive types. 基本类型的类对象可在每种基本类型的每个java.lang对象的TYPE静态最终常量下找到。 Boolean.TYPE , Integer.TYPE , Byte.TYPE , Short.TYPE , Long.TYPE etc. Boolean.TYPEInteger.TYPEByte.TYPEShort.TYPELong.TYPE等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当使用原始参数模拟方法时,Mockito中的NullPointerException - NullPointerException in Mockito when mocking method with primitive argument 方法参数中的原始类型和包装器类 - Primitive type and wrapper class within method argument 如果方法参数是原始int,那么方法内循环中的myArrayList.contains(primitiveArg)效率极低还是可以? - If method argument is a primitive int, then myArrayList.contains(primitiveArg) within a loop within a method is enormously inefficient or OK? 在方法参数中使用通配符 - Using wildcard in method argument 在方法的参数中使用OR - using OR in an argument to a method 为什么我们将String数组作为参数传递给main()方法,为什么不是任何集合类型或包装类型或基本类型? - Why we Pass String array as argument to main() method, why not any collection type or wrapper type or primitive type? 使用反射获取具有原始类型参数的类的方法 - Get a method of class with parameter of primitive type using Reflection 如何使用 Java 反射将原始类型值分配给方法 args? - How to assign primitive type value to a method args using Java reflection? Java在原始类型上使用instanceof方法产生编译器错误 - Java using instanceof method on primitive type gives compiler error 框原始类型的方法 - Method to box primitive type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM