reflection

java equivalent of c# typeof()

人走茶凉 提交于 2020-04-13 17:07:07
问题 I'm very new to java. Say I've a xml parser and I'd create object from it. In c# i'd do like: parser p = new parser(typeof(myTargetObjectType)); In my parser class I need to know which object I'm making, so that i can throw exception if parsing is not possible. How to Do the same in jave? I mean how I can take arguments like typeof(myObject) I understand every language has it's own way of doing something. I'm asking what's way in java 回答1: Java has the Class class as the entry point for any

java equivalent of c# typeof()

无人久伴 提交于 2020-04-13 17:06:25
问题 I'm very new to java. Say I've a xml parser and I'd create object from it. In c# i'd do like: parser p = new parser(typeof(myTargetObjectType)); In my parser class I need to know which object I'm making, so that i can throw exception if parsing is not possible. How to Do the same in jave? I mean how I can take arguments like typeof(myObject) I understand every language has it's own way of doing something. I'm asking what's way in java 回答1: Java has the Class class as the entry point for any

Java Reflections: Get Classes but not SubTypes

六月ゝ 毕业季﹏ 提交于 2020-04-13 08:05:08
问题 I'm upgrading from org.reflections:reflections:0.9.5 to version 0.9.9. I am using: Reflections reflectionPaths = new Reflections("resources", new TypeAnnotationsScanner()); Set<Class<?>> rootResourceClasses = reflectionPaths.getTypesAnnotatedWith(Path.class); Which gets me all classes in the resources package with an @Path Annotation. Since the library has been updated the top line requires an extra new SubTypesScanner() for the code to run. I however do not want sub types to be returned. How

How to Inject Dependencies to Dynamically Loaded Assemblies

ⅰ亾dé卋堺 提交于 2020-04-10 18:08:55
问题 I have a manager class that loads various plug-in modules contained in separate assemblies through reflection. This modules are communication to the outside world (WebAPI, various other web protocols). public class Manager { public ILogger Logger; // Modules need to access this. private void LoadAssemblies() { // Load assemblies through reflection. } } These plug-in modules must communicate with an object contained within the manager class. How can I implement this? I thought about using

How to Inject Dependencies to Dynamically Loaded Assemblies

 ̄綄美尐妖づ 提交于 2020-04-10 18:07:21
问题 I have a manager class that loads various plug-in modules contained in separate assemblies through reflection. This modules are communication to the outside world (WebAPI, various other web protocols). public class Manager { public ILogger Logger; // Modules need to access this. private void LoadAssemblies() { // Load assemblies through reflection. } } These plug-in modules must communicate with an object contained within the manager class. How can I implement this? I thought about using

sun.reflect.Reflection(转)

佐手、 提交于 2020-04-10 16:55:00
几个礼拜前,我和我一位叫Niko Brummer的朋友聊天。他是一位音响检验方面的专家,热爱水上运动,并且喜欢把他的理论知识应用到运动中。Niko十分愿意随时与你谈论他在Java(和海洋)方面的最新发现。 我们聊天的时候,Niko提到了一个我未曾听闻的、叫作“Reflection”的类。这个类是Sun JVM一起附带的,可以在sun.reflect.*包里找到它。其中最有用的方法是: Class getCallerClass(int i) . 这个类能告诉你在我们当前的调用堆栈中有哪些类。 让我们来看一个例子,是做汤时需要用到的一些类。(假设你想为几百人做一道美味的蔬菜汤,请看一下 我们网站上的这个菜谱 ;-) 我们希望土豆(Potato)有一个指向它所在汤(Soup)的一个引用,并且我们希望汤(Soup)明白它自己包含哪些土豆(Potato)。请记住,这个解决方法只是众多可能的一种,并且只是作为一个演示如何使用这个Reflection类的例子。 import java.util.*; public class Soup { private final List potatos = new ArrayList(); public void add(Potato p) { potatos.add(p); p.setSoup( this ); } public String

How to list out the method name in an interface type?

我怕爱的太早我们不能终老 提交于 2020-04-08 18:06:46
问题 For example, type FooService interface { Foo1(x int) int Foo2(x string) string } What I am attempting to do is getting list ["Foo1", "Foo2"] using runtime reflection. 回答1: Try this: t := reflect.TypeOf((*FooService)(nil)).Elem() var s []string for i := 0; i < t.NumMethod(); i++ { s = append(s, t.Method(i).Name) } playground example Getting the reflect.Type for the interface type is the tricky part. See How to get the reflect.Type of an interface? for an explanation. 来源: https://stackoverflow

How to list out the method name in an interface type?

依然范特西╮ 提交于 2020-04-08 18:04:53
问题 For example, type FooService interface { Foo1(x int) int Foo2(x string) string } What I am attempting to do is getting list ["Foo1", "Foo2"] using runtime reflection. 回答1: Try this: t := reflect.TypeOf((*FooService)(nil)).Elem() var s []string for i := 0; i < t.NumMethod(); i++ { s = append(s, t.Method(i).Name) } playground example Getting the reflect.Type for the interface type is the tricky part. See How to get the reflect.Type of an interface? for an explanation. 来源: https://stackoverflow

How to list out the method name in an interface type?

眉间皱痕 提交于 2020-04-08 18:04:03
问题 For example, type FooService interface { Foo1(x int) int Foo2(x string) string } What I am attempting to do is getting list ["Foo1", "Foo2"] using runtime reflection. 回答1: Try this: t := reflect.TypeOf((*FooService)(nil)).Elem() var s []string for i := 0; i < t.NumMethod(); i++ { s = append(s, t.Method(i).Name) } playground example Getting the reflect.Type for the interface type is the tricky part. See How to get the reflect.Type of an interface? for an explanation. 来源: https://stackoverflow

How to access a static method via a class reference

余生长醉 提交于 2020-04-08 08:57:57
问题 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