reflection

Java - Create new Instance using Reflection without knowing constructor params

◇◆丶佛笑我妖孽 提交于 2021-02-18 11:56:07
问题 I need to create new instances of many classes. I'm using reflection but when I make c.newInstance(), the classes without 'empty parameter constructor' throws an java.lang.InstantiationException. Now, how can i do to create instances of every classes ? I know that i can use c.getConstructor(Class).newinstance(params) to create instances of classes that doesn't have 'empty parameter constructor', but i do not know the params of each classes. One more thing, all those classes extend from

Mockito: is it possible to combine mock with a method name to create a methodCall inside a when() call?

落爺英雄遲暮 提交于 2021-02-18 11:15:09
问题 my first question on StackOverflow. I'd like to be able to do something like: SomeClass mock = mock(SomeClass.class); String methodName = "someMethod"; OR Method method = ...someMethod... Both of these things (the mock and the method) would combine to do the following: when(mock.someMethod()).thenReturn(null); Of course, the 'null' value will be changed accordingly for my needs, but I am trying to determine two things: 1) Is it even possible to do something like this in Java? This = combining

Types in a LambdaMetaFactory

女生的网名这么多〃 提交于 2021-02-18 10:28:11
问题 I get an exception when I call metafactory . It says: java.lang.invoke.LambdaConversionException: Incorrect number of parameters for instance method invokeVirtual my.ExecuteTest$AProcess.step_1:()Boolean; 0 captured parameters, 0 functional interface method parameters, 0 implementation parameters I do not understand all from the documentation of LambdaMetafactory.metafactory . I have problems figuring out the correct parameters: MethodHandles.Lookup caller -- thats easy String invokedName --

kotlin reflection get list of fields

久未见 提交于 2021-02-18 09:00:21
问题 is there an equivalent for the java reflection foo.getClass().getFields() in Kotlin? I could only find that I can access a field when I know it's name, but I would like to handle fields in a generic way. 回答1: Did you want fields as-in "backing field" or fields as in "properties" ... Kotlin really only has properties. You can get these for some class using: MyTest::class.memberProperties // or MyTest::class.declaredMemberProperties And from a Java Class<T> , use the kotlin extension property

How do Java Module directives impact reflection access into a module?

橙三吉。 提交于 2021-02-18 07:37:05
问题 According to https://www.oracle.com/corporate/features/understanding-java-9-modules.html, the Java Module system introduces the following directives: exports, exports ... to uses provides ... with open, opens, opens ... to What (if any) impact does each directive have on an external module accessing internal members using reflection? For example, does exports <package> allow external modules to access all public , protected , private members of the exported package using reflection? What

Attributes on Field in Class

夙愿已清 提交于 2021-02-17 06:15:29
问题 I want to get a list of fields with an Attribute Sync.Field on each of the field in the class. The field can / cannot have the attribute of Sync.Field I have been trying the following, but having trouble getting the custom attribute for each field. FieldInfo[] fiClass = typClass.GetFields(); FieldInfo[] lst = fiClass .Where(c => c.CustomAttribute().GetType() == typeOf(Sync.Field)) .ToList(); 回答1: I have a generic collection class, which uses a data class to match an SNMP table with data class

Getting the best fit Instance Method in Java

蹲街弑〆低调 提交于 2021-02-16 20:22:17
问题 If I run the following program: class Runit{ public static void main(String[] argsWut) throws Exception { String arg = "what?"; Class[] parameters = { new Object().getClass() }; Object[] args = { arg }; System.out.println("".getClass().getMethod("equals",parameters).invoke("what?",args)); } }; I get the following on the command line: true On the other hand, if I modify the parameters line a little: class Runit{ public static void main(String[] argsWut) throws Exception { String arg = "what?";

Strategy for detecting (via Reflection) if Enum is of type “Flags” in C#

痴心易碎 提交于 2021-02-16 18:26:09
问题 I'm using Reflection to read types inside an assembly (to generate code). I can see that some enumerations should have been marked with the [Flags] attribute but whoever wrote those enums forgot to add this attribute. Is there any reliable way of detecting when an enum can be considered a "Flags" enum? My strategy at the moment is to read the enum in descending order, and checking if the value of element(last -1) * 2 == element(last). That works great in most cases, except when I have enums

Getting an Object by reference in Java

旧巷老猫 提交于 2021-02-16 14:30:08
问题 New to this website, and excited to share my first question :) Ok so I'm going to be explaining what I have set-up currently so that my question can be understood better. I have 2 java applications: Logic Application (Where all the heavy load occurs) Instrumented Application (Application instrumented into a running game) What I do with these 2 applications is extract information from a game using the Instrumented Application then send it to the Logic Application. The extraction of information

How do you pass parameters by ref when calling a static method using reflection?

允我心安 提交于 2021-02-16 06:06:26
问题 I'm calling a static method on an object using reflection: MyType.GetMethod("MyMethod", BindingFlags.Static).Invoke(null, new object[] { Parameter1, Parameter2 }); How do you pass parameters by ref, rather that by value? I assume they would be by value by default. The first parameter ("Parameter1" in the array) should be by ref, but I can't figure out how to pass it that way. 回答1: For a reference parameter (or out in C#), reflection will copy the new value into the object array at the same