如何使用java反射获取一个方法,该反射只使用部分名称作为方法的字符串?
我正在尝试调用一个类的getter,我只有部分名称,如“name”、“age”等。我需要使用java反射api根据getter动态调用类中的方法,如getName/retriveName 例如:如何使用java反射获取一个方法,该反射只使用部分名称作为方法的字符串?,java,reflection,getter,java.lang,Java,Reflection,Getter,Java.lang,我正在尝试调用一个类的getter,我只有部分名称,如“name”、“age”等。我需要使用java反射api根据getter动态调用类中的方法,如getName/retriveName 例如: class PersonData{ private String personName; private int personId; private int personAge; public PersonData(){ } public int getPersonId(){ return
class PersonData{
private String personName;
private int personId;
private int personAge;
public PersonData(){
}
public int getPersonId(){
return this.personID;
}
public String getPersonName(){
return this.personName;
}
public int getPersonAge(){
return this.PersonAge;
}
}
我可能会根据用户输入得到“name”/“name”,我应该只调用getName()方法。请帮忙。嗯,这不是一个好主意,您必须获取所有方法并按名称搜索(例如regex),或者提供要调用的方法的全名
PersonData i = new PersonData();
String userInp = "name";
Class<PersonData> ri = (Class<PersonData>) Class.forName(PersonData.class.getName());
Method[] m = ri.getDeclaredMethods();
for (Method method : m) {
System.out.println(method);
if (method.getName().toLowerCase().indexOf(userInp.toLowerCase()) != -1) {
System.out.println("....method found! -> " + method);
break;
}
}
并且用户给出名称作为输入
然后,您可能会调用错误的方法….在名为
PersonData
的类中包含personName
、personAge
和personId
字段通常(但不总是)是不好的做法,这是高度冗余的,此反模式甚至有一个名称:
但幸运的是,这个问题是独立的。要基于部分匹配获取属性值,您必须列出所有属性获取程序,并选择名称包含所提供字符串的获取程序。在java 8中,您可以这样做:
public static void main(String[] args) throws Exception {
final PersonData person = new PersonData("Janek", 111, 59);
final Method method = getMethodLike("naMe");
final Object output = method.invoke(person);
System.out.println("Found method with name: " + method.getName() + " which returned: " + output);
}
private static Method getMethodLike(String partOfName) {
final Optional<Method> matchedMethod = asList(PersonData.class.getDeclaredMethods()).stream().filter(method ->
method.getName().toLowerCase().indexOf(partOfName.toLowerCase()) >= 0).findAny();
if (!matchedMethod.isPresent()) {
throw new RuntimeException("No method containing: " + partOfName);
}
return matchedMethod.get();
}
publicstaticvoidmain(字符串[]args)引发异常{
最终的个人资料=新的个人资料(“Janek”,111,59);
最终方法=getMethodLike(“名称”);
最终对象输出=method.invoke(person);
System.out.println(“找到名为:“+method.getName()+”的方法,返回:“+output”);
}
私有静态方法getMethodLike(名称的字符串部分){
最终可选matchedMethod=asList(PersonData.class.getDeclaredMethods()).stream().filter(方法->
方法.getName().toLowerCase().indexOf(partOfName.toLowerCase())>=0.findAny();
如果(!matchedMethod.isPresent()){
抛出新的RuntimeException(“没有包含:“+partOfName”的方法);
}
返回matchedMethod.get();
}
输出:“找到名为getPersonName的方法,返回:Janek”将输入转换为小写,然后大写第一个字母,然后将结果字符串附加到
“getPerson”
以获得完整的方法名?您可以使用数组.stream(PersonData.class.getDeclaredMethods())
而不是asList(…).stream()
绕道而行。
public static void main(String[] args) throws Exception {
final PersonData person = new PersonData("Janek", 111, 59);
final Method method = getMethodLike("naMe");
final Object output = method.invoke(person);
System.out.println("Found method with name: " + method.getName() + " which returned: " + output);
}
private static Method getMethodLike(String partOfName) {
final Optional<Method> matchedMethod = asList(PersonData.class.getDeclaredMethods()).stream().filter(method ->
method.getName().toLowerCase().indexOf(partOfName.toLowerCase()) >= 0).findAny();
if (!matchedMethod.isPresent()) {
throw new RuntimeException("No method containing: " + partOfName);
}
return matchedMethod.get();
}