Java注释仅获取声明的方法
我使用以下代码获取类中声明的方法。但是代码也会返回不必要的值以及声明的方法 以下是我的代码: 编辑:Java注释仅获取声明的方法,java,annotations,Java,Annotations,我使用以下代码获取类中声明的方法。但是代码也会返回不必要的值以及声明的方法 以下是我的代码: 编辑: Class cls; List classNames = hexgenClassUtils.findMyTypes("com.hexgen.*"); Iterator<Class> it = classNames.iterator(); while(it.hasNext()) {
Class cls;
List classNames = hexgenClassUtils.findMyTypes("com.hexgen.*");
Iterator<Class> it = classNames.iterator();
while(it.hasNext())
{
Class obj = it.next();
System.out.println("Methods available in : "+obj.getName());
System.out.println("===================================");
cls = Class.forName(obj.getName());
Method[] method = cls.getDeclaredMethods();
int i=1;
Method[] method = cls.getDeclaredMethods();
int i=1;
for (Method method2 : method) {
System.out.println(+i+":"+method2.getName());
}
}
在此之后,我只得到在我给出的类中声明的方法。上述价值观是什么?如何避免
致以最诚挚的问候试试这个:
Method[] method = cls.getClass().getDeclaredMethods();
而不是
Method[] method = cls.getDeclaredMethods();
请参见此示例:
import java.lang.reflect.Method;
public class Example {
private void one() {
}
private void two() {
}
private void three() {
}
public static void main(String[] args) {
Example program = new Example();
Class progClass = program.getClass();
// Get all the methods associated with this class.
Method[] methods = progClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println("Method " + (i + 1) + " :"
+ methods[i].toString());
}
}
}
这似乎是一个代理对象。如果不想包含这些方法,可能需要检查这些方法的其他属性。还是使用反射检查实现的接口?你是如何获得这门课的?请包括cls课程。请看一下我编辑的问题,我认为这已经足够了,只需要几点,很抱歉误导。没有否决投票的意思,如果你认为你能提供帮助,请向该人寻求更多的澄清。阿普鲁夫发表了评论,离开了这个地方,甚至删除了他的帖子。可笑。谢谢你的回答A,但现在它给出了很多甚至不是方法的东西,比如
1:newInstance0 2:forName 3:forName 4:forName0
我的实际方法缺失了:(@Anto看到我上面的例子了。可能会有帮助。如果我单独使用它,我会得到相同的答案,但在程序中我得到的答案不同,我调查的类是spring类
import java.lang.reflect.Method;
public class Example {
private void one() {
}
private void two() {
}
private void three() {
}
public static void main(String[] args) {
Example program = new Example();
Class progClass = program.getClass();
// Get all the methods associated with this class.
Method[] methods = progClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println("Method " + (i + 1) + " :"
+ methods[i].toString());
}
}
}
Method 1 :public static void Example.main(java.lang.String[])
Method 2 :private void Example.one()
Method 3 :private void Example.two()
Method 4 :private void Example.three()