reflection

How to access a static method via a class reference

只愿长相守 提交于 2020-04-08 08:57:08
问题 class A { public static void foo() {} } class B { public static void foo() {} } I have Class clazz = A.class; or B.class ; How do I access this via "clazz" assuming it might be assigned either 'A' or 'B' 回答1: It is only possible to access those methods using reflection. You cannot reference a class directly, only an instance of type Class. To use reflection to invoke methodname(int a, String b): Method m = clazz.getMethod("methodname", Integer.class, String.class); m.invoke(null, 1, "Hello

How to access a static method via a class reference

会有一股神秘感。 提交于 2020-04-08 08:55:31
问题 class A { public static void foo() {} } class B { public static void foo() {} } I have Class clazz = A.class; or B.class ; How do I access this via "clazz" assuming it might be assigned either 'A' or 'B' 回答1: It is only possible to access those methods using reflection. You cannot reference a class directly, only an instance of type Class. To use reflection to invoke methodname(int a, String b): Method m = clazz.getMethod("methodname", Integer.class, String.class); m.invoke(null, 1, "Hello

How to cast instance by classname in Spring's validator interface realization [closed]

心不动则不痛 提交于 2020-04-07 10:39:26
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 9 days ago . I have class - AbstractOrder - it have basic realization and fields. And also i have three another which extends AbstractOrder - EmployeeOrder, StudentOrder, PostgraduateOrder, but have additional field - id. I would like to create a single validation class for all 3 entities. Here is my code. So,

GetEntryAssembly for web applications

若如初见. 提交于 2020-03-30 04:37:08
问题 Assembly.GetEntryAssembly() does not work for web applications. But... I really need something like that. I work with some deeply-nested code that is used in both web and non-web applications. My current solution is to browse the StackTrace to find the first called assembly. /// <summary> /// Version of 'GetEntryAssembly' that works with web applications /// </summary> /// <returns>The entry assembly, or the first called assembly in a web application</returns> public static Assembly

How to get annotations in nested parameter parameter?

浪子不回头ぞ 提交于 2020-03-26 04:02:13
问题 If I have a function like this, public void park( List< @myNote("list") List< @myNote("integer") Integer>> l){ System.out.println("park"); } If I use Parameter.getAnnotations() and Paramter.getDeclaredAnnotation, I can only get @myNote("list"). Are any ways to get the annotation @myNote("integer")? This may be related to How to get all types of the inner structures of a nested type? 回答1: Here is code to print the annotations, so you can see where in the reflection API they can be found.

How would I convert a int to enum value at runtime when I have a string and an integer? [duplicate]

天涯浪子 提交于 2020-03-18 08:52:03
问题 This question already has answers here : Create instance of unknown Enum with string value using reflection in C# (2 answers) Closed 24 days ago . What I have is a string variable that is the name of a enum. I have the integer of the enum. How would I convert that into instance of the enum itself? Enum TestEnum { One = 1, Two = 2, Three = 3 } string str = "TestEnum"; int intValue = 2; I see many posts that require you to have an instance of the enum to get it's like this highly upvoted answer

Why does Assembly.GetTypes() require references?

别说谁变了你拦得住时间么 提交于 2020-03-14 11:08:30
问题 I want to get all of the types from my assembly, but I don't have the references, nor do I care about them. What does finding the interface types have to do with the references? and is there a way for me to get around this? Assembly assembly = Assembly.LoadFrom(myAssemblyPath); Type[] typeArray = assembly.GetTypes(); Throws: FileNotFoundException Could not load file or assembly 'Some referenced assembly' or one of its dependencies. The system cannot find the file specified. 回答1: Loading an

Why does Assembly.GetTypes() require references?

醉酒当歌 提交于 2020-03-14 11:07:02
问题 I want to get all of the types from my assembly, but I don't have the references, nor do I care about them. What does finding the interface types have to do with the references? and is there a way for me to get around this? Assembly assembly = Assembly.LoadFrom(myAssemblyPath); Type[] typeArray = assembly.GetTypes(); Throws: FileNotFoundException Could not load file or assembly 'Some referenced assembly' or one of its dependencies. The system cannot find the file specified. 回答1: Loading an

Why does Assembly.GetTypes() require references?

随声附和 提交于 2020-03-14 11:06:05
问题 I want to get all of the types from my assembly, but I don't have the references, nor do I care about them. What does finding the interface types have to do with the references? and is there a way for me to get around this? Assembly assembly = Assembly.LoadFrom(myAssemblyPath); Type[] typeArray = assembly.GetTypes(); Throws: FileNotFoundException Could not load file or assembly 'Some referenced assembly' or one of its dependencies. The system cannot find the file specified. 回答1: Loading an

How can I obtain the type parameter of a generic interface from an implementing class?

谁说胖子不能爱 提交于 2020-03-14 10:52:31
问题 I have this interface: public interface EventHandler<T extends Event> { void handle(T event); } And this class implementing it: public class MyEventHandler implements EventHandler<MyEvent> { @Override public void handle(MyEvent event) { //do something } } In this clase, T parameter is MyEvent , that is a concrete implementation of Event . How can obtain this using reflection? 回答1: Resolve the type of T by the generic interface. E.g. public interface SomeInterface<T> { } public class