Java 如何为方法生成签名?

发布于 2024-09-25 16:53:18 字数 1607 浏览 3 评论 0原文

我有一个带有静态最终方法 getAll: 的 Java 类,

public static final Vector<Category> getAll(Context context, ContentValues where) {
    ArrayList<Integer> IDs = null;

    if(where != null && where.containsKey(DatabaseAdapter.KEY_PRODUCT)) {
        IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_PRODUCT_CATEGORY, new String[] { DatabaseAdapter.KEY_CATEGORY }, where);
    } else {
        IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_CATEGORIES, where);
    }

    Vector<Category> categories = new Vector<Category>();

    for(Integer id: IDs) {
        categories.add(Category.get(context, id));
    }

    return categories;        
}

现在我想将 null 作为 where 语句的值,以便稍后在代码中忽略它。无论如何,在这个方法的测试用例中,我有:

Vector<Category> categories = Category.getAll(context, null);

然后这又给了我一个NoSuchMethodError。我不知道它为什么这样做。我唯一能想象的是,我提交的 null 与上述方法的签名不匹配。但我怎样才能克服这个问题呢?我已经想到了超载。但这最终只会重写大部分代码。至少当我做的时候,我是怎么想的。

对此有何建议?

菲尔

P.S.这是我得到的堆栈跟踪:

java.lang.NoSuchMethodError: com.sap.catalogue.model.Category.getAll
at com.sap.overture.test.model.CategoryTest.testGetAll(CategoryTest.java:59)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

I have an Java class with a static final method getAll:

public static final Vector<Category> getAll(Context context, ContentValues where) {
    ArrayList<Integer> IDs = null;

    if(where != null && where.containsKey(DatabaseAdapter.KEY_PRODUCT)) {
        IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_PRODUCT_CATEGORY, new String[] { DatabaseAdapter.KEY_CATEGORY }, where);
    } else {
        IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_CATEGORIES, where);
    }

    Vector<Category> categories = new Vector<Category>();

    for(Integer id: IDs) {
        categories.add(Category.get(context, id));
    }

    return categories;        
}

Now I want to hand in null as a value for the where statemant so that it will just be ignored later on in the code. Anyway in the testcase for this method I have:

Vector<Category> categories = Category.getAll(context, null);

Which then in turn gives me a NoSuchMethodError. I don't know exactly why it does that. The only thing I could imagine is that the null I hand in would not match the signature of the above method. But how can I overcome this? I already thought of overloading. But this would just end in rewriting most of the code. At least when I do it, how I think.

Any suggestions on that?

Phil

P.S. This is the stack trace I get:

java.lang.NoSuchMethodError: com.sap.catalogue.model.Category.getAll
at com.sap.overture.test.model.CategoryTest.testGetAll(CategoryTest.java:59)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

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

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

发布评论

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

评论(2

巴黎盛开的樱花 2024-10-02 16:53:18

如果该方法在编译时不存在,则代码将无法编译。

如果您在运行时收到 NoSuchMethodError,则表明您正在运行的 Category 类的版本与 Category 的版本不同code> 您正在编译的类。

你的设置是什么样的——这个类在同一个项目中吗?您是否从另一个项目复制 JAR?

If the method did not exist at compile-time, then the code would not compile.

If you get NoSuchMethodError at run-time, then this suggests that the version of the Category class you are running against is different than the version of the Category class you are compiling against.

What is your setup like - is this class in the same project? Are you copying in JARs from another project?

不甘平庸 2024-10-02 16:53:18

真正的答案

所以我现在终于弄清楚了,但它并不像我想象的那么明显。我开始想知道,我编写的任何新方法的每个新测试用例何时都会给我 NoSuchMethodError 。所以我深入挖掘了一下,然后突然想到:“我更改了android应用程序的包名称”。我认为只要我在 AndroidManifest.xml 中保持正确的属性,这就不会对测试项目产生任何影响,但我错了!

事实上,当您的应用程序包名为 com.foo.bar.app 时,您的测试包必须命名为 com.foo.bar.app.test!发生的情况是,在我的旧配置中,不知何故使用了位于 bin/ 文件夹中的类。我想,当我清理项目时,它们应该被删除,但它们没有。这样,所有旧的测试用例仍然会通过,只有新的测试用例会给我NoSuchMethodError。在我手动删除 bin/ 文件夹后,出现了一大堆错误。然后,我重命名了包含测试用例的包,并对项目进行了全面的清理/重建,这样一切就又恢复正常了。

感谢您的所有提示!我真的很感谢你的帮助,让我不断挖掘问题的根源。希望这对将来遇到同样问题的人有所帮助。

菲尔

The real answer

So I now finally figured it out and it wasn't as obvious as I expected. I started wondering, when every new test case for any new method I wrote would give me the NoSuchMethodError. So I digged a little bit deeper and then, suddenly it came to my mind: "I changed the package name of the android application". I thought this would not make any difference to the test project as long as I kept the properties right in the AndroidManifest.xml but I was wrong!

In fact when your application package is named com.foo.bar.app, the package for your tests has to be named com.foo.bar.app.test! What happened was, that with my old configuration somehow the classes that sat in the bin/ folder were used. I thought, that they should have been deleted when I cleaned the project but they weren't. This way all of the older test cases would still pass and only the new ones would give me the NoSuchMethodError. After I deleted the bin/ folder manually I got a whole bunch of errors. I then renamed the package holding the test cases and did a full clean/ rebuild on the project et voilá everything is back to normal again.

Thanks for all the tips! I really appreciate your help that just kept me digging to the bottom of the problem. Hope this here will help anybody with the same problem in the future.

Phil

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