简体   繁体   English

Java反射调用方法“ getDeclaredConstructors”

[英]Java reflection calling method “getDeclaredConstructors”

I didn't really know how to put the title of the question. 我真的不知道该怎么写问题的标题。 Basically i'm trying to do some tricky stuff with the java reflect api. 基本上我想用Java反射API做一些棘手的事情。 Here's what im trying to do: 我正在尝试做的是:

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class Test {

public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, InstantiationException {
    Scanner in = new Scanner(System.in);
    String className = in.nextLine();
    Class c = Class.forName(className);
    Object myObj = c.newInstance();
    Method m = c.getMethod("getDeclaredConstructors");
    Object retn = m.invoke(myObj);
    System.out.println(retn);
    in.close();
}
}

I'm testing this with the input: java.lang.String I always get as output: 我正在使用输入进行测试:java.lang.String我总是得到以下输出:

Exception in thread "main" java.lang.NoSuchMethodException: java.lang.String.getDeclaredConstructors()
    at java.lang.Class.getMethod(Class.java:1778)
    at Test.main(Test.java:15)

I understand that the method "getDeclaredConstructors()" is from the class Class. 我知道方法“ getDeclaredConstructors()”来自类Class.

Can someone please tell me what am i doing wrong here? 有人可以告诉我我在做什么错吗? What i was expecting was a list of the declared constructors for the class name i gave as input, in this case: java.lang.String 我期望的是我作为输入提供的类名的声明构造函数的列表,在这种情况下: java.lang.String

EDIT: I should have detailed more my problem. 编辑:我应该详细说明我的问题。 I know that i could execute c.getDeclaredConstructors(), but thats not the case. 我知道我可以执行c.getDeclaredConstructors(),但事实并非如此。

This is only a small part of the program i want to build. 这只是我要构建的程序的一小部分。 In the final version, the user will provide as input a name of a class, say "java.lang.String" and then it will provide generic methods that he wishes to apply to that class. 在最终版本中,用户将提供一个类名(例如“ java.lang.String”)作为输入,然后它将提供他希望应用于该类的通用方法。 So the user would enter "getDeclaredConstructors" and i would have to apply the method with that name to the class "java.lang.String". 因此,用户将输入“ getDeclaredConstructors”,并且我必须将具有该名称的方法应用于类“ java.lang.String”。

Im only doing this to understand how exactly i can achive that. 我只是这样做是为了了解我到底能达到什么程度。

getMethod() returns the methods of String (in this case), not of Class . getMethod()返回String的方法(在这种情况下),而不是Class String doesn't have such a method. String没有这种方法。

And why exactly would you want to call this method reflectively in the first place? 以及为什么您首先要确切地反射地调用此方法? It's a public API. 这是一个公共API。

I would say, you had a right problem but tested with a wrong method. 我会说,您有一个正确的问题,但是用错误的方法进行了测试。 Methods of java.lang.Class are accessed through the object of other classes. 可通过其他类的对象访问java.lang.Class的方法。 For this one, you can try access a method from java.lang.Object, like below, 为此,您可以尝试从java.lang.Object访问一种方法,如下所示,

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class Test {

public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, InstantiationException {
Scanner in = new Scanner(System.in);
String className = in.nextLine();
Class c = Class.forName(className);
Object myObj = c.newInstance();
Method m = c.getMethod("hashCode");
Object retn = m.invoke(myObj);
System.out.println(retn);
in.close();
}
}

Hope this helps you. 希望这对您有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM