反射静态方法参数字符串

发布于 2024-11-07 18:40:07 字数 898 浏览 0 评论 0原文

public class Star{  
    public static ArrayList initdata(String pattern) {
        ArrayList data = new ArrayList();
            if (pattern != "") {
                ModelCollection mc = Star.find(pattern, 0);
                Iterator dataIterator = mc.iterator();
                    while (dataIterator.hasNext()) {
                        Star star = (Star) dataIterator.next();
                        data.add(star.getName());
                        Debug.trace("StarName" + star.getName());
                    }
            }
        Collections.sort(data);
        return data;
    }
}

我想使用反射调用 initdata 方法,我尝试编写类似这样的内容,但它不起作用:

Class c = Class.forName("com.cubiware.fyretv.application.model.Star");
par[0] = String.class;
Method mthd = c.getMethod("initdata", par);        
ArrayList output = (ArrayList) mthd.invoke(null, null);
public class Star{  
    public static ArrayList initdata(String pattern) {
        ArrayList data = new ArrayList();
            if (pattern != "") {
                ModelCollection mc = Star.find(pattern, 0);
                Iterator dataIterator = mc.iterator();
                    while (dataIterator.hasNext()) {
                        Star star = (Star) dataIterator.next();
                        data.add(star.getName());
                        Debug.trace("StarName" + star.getName());
                    }
            }
        Collections.sort(data);
        return data;
    }
}

I want to invoke method initdata using reflection, I tried to write something like this , but it does not work:

Class c = Class.forName("com.cubiware.fyretv.application.model.Star");
par[0] = String.class;
Method mthd = c.getMethod("initdata", par);        
ArrayList output = (ArrayList) mthd.invoke(null, null);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺渁∝ 2024-11-14 18:40:07

尝试

ArrayList output = (ArrayList) mthd.invoke(null, (String)null);

当方法需要 Object 时,传递 null 不是一个好主意...

可能这会有所帮助

使用单个 null 参数调用 Java varargs 方法?

try

ArrayList output = (ArrayList) mthd.invoke(null, (String)null);

It's not good idea to pass null, when method expects Object...

May be this will help

Calling Java varargs method with single null argument?

金兰素衣 2024-11-14 18:40:07

首先,你的检查对我来说似乎很奇怪:尝试 if (pattern != null) 而不是 if (pattern != "")

你为什么不传递 par 数组,我认为你有非法参数异常。尝试传递参数数组。

Object[] args = {"someString / maybe null"};
ArrayList output = (ArrayList) mthd.invoke(null, args);

First, Your check seems weird to me: try if (pattern != null) instead of if (pattern != "").

Why don't you pass the par array, you have illegal argument exception I think. try passing arguments array.

Object[] args = {"someString / maybe null"};
ArrayList output = (ArrayList) mthd.invoke(null, args);
池木 2024-11-14 18:40:07

显然,您的调用类似于

initdata(null);

现在,在 initdata 内,您不会过滤 pattern == null 的情况,这会导致我们调用

Star.find(null, 0);

我们不知道的实现这种方法 - 如果幸运的话,我们会得到一个空集合。否则,我预计在 Star.find 中或稍后在 mc.iterator() 中出现 NullPointerException

Obviously, your invoke call is similiar to

initdata(null);

Now, inside initdata you do not filter the case where pattern == null which leads us to a call

Star.find(null, 0);

We do not know the implementation of this method - if we're lucky, we get an empty collection. Otherwise, I expect a NullPointerException either in Star.find or later at mc.iterator()

箹锭⒈辈孓 2024-11-14 18:40:07
$ javac -cp dp4j-1.2-SNAPSHOT-jar-with-dependencies.jar -Averbose -All Star.java
Star.java:12:

import com.dp4j.*;

public class Star {

    public Star() {
        super();
    }

    public static ArrayList initdata(String pattern) {
        return null;
    }

    @Reflect()
    public static void main(String[] args) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.IllegalArgumentException {
        final java.lang.reflect.Method initdataWithStringMethod = Class.forName("Star").getDeclaredMethod("initdata", .java.lang.String.class);
        initdataWithStringMethod.setAccessible(true);
        initdataWithStringMethod.invoke("", new .java.lang.Object[1][]{null});
        final java.lang.reflect.Method printlnWithStringMethod = Class.forName("java.io.PrintStream").getDeclaredMethod("println", .java.lang.String.class);
        printlnWithStringMethod.setAccessible(true);
        printlnWithStringMethod.invoke(System.out, new .java.lang.Object[1][]{"Varargs + reflection? No problem"});
    }
}
    public static void main(String args[]) {
                       ^

$ java Star
Varargs + reflection? No problem
$ javac -cp dp4j-1.2-SNAPSHOT-jar-with-dependencies.jar -Averbose -All Star.java
Star.java:12:

import com.dp4j.*;

public class Star {

    public Star() {
        super();
    }

    public static ArrayList initdata(String pattern) {
        return null;
    }

    @Reflect()
    public static void main(String[] args) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.IllegalArgumentException {
        final java.lang.reflect.Method initdataWithStringMethod = Class.forName("Star").getDeclaredMethod("initdata", .java.lang.String.class);
        initdataWithStringMethod.setAccessible(true);
        initdataWithStringMethod.invoke("", new .java.lang.Object[1][]{null});
        final java.lang.reflect.Method printlnWithStringMethod = Class.forName("java.io.PrintStream").getDeclaredMethod("println", .java.lang.String.class);
        printlnWithStringMethod.setAccessible(true);
        printlnWithStringMethod.invoke(System.out, new .java.lang.Object[1][]{"Varargs + reflection? No problem"});
    }
}
    public static void main(String args[]) {
                       ^

$ java Star
Varargs + reflection? No problem
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文