3

I'm trying to use j2objc to translate some Java code. Unfortunately, my code relies on reflection. The j2objc documentation claims that reflection is supported, but when I try to import e.g java.lang.reflect.Field, I get an error message saying it cannot be resolved.

That's the same error I get if I try to import classes I know aren't supported, e.g. Swing.

Other classes that the documentation says is supported, e.g. java.util, do translate cleanly.

Is there something special which needs to be done to enable reflection support in j2objc?

4

2 に答える 2

3

java.lang.reflect.Field最近修正されました (または、少なくとも報告されたすべてのバグは修正されました)。ソースは最新で、配布バンドルはダウンロード ページにあります。

于 2012-11-30T18:36:29.600 に答える
0

私の経験では、そうではありません。変換ページを見ると、テストフレームワークをサポートするためのリフレクション機能のサブセットをサポートしていることがわかります。

j2objc0.56を使用しています。メソッドを呼び出そうとすると:

java.lang.reflect.Method method;
        try {

          method = biometry.getClass().getMethod(methodName, int.class, double.class);
            CalcResult r = (CalcResult) method.invoke(biometry,days, measurement);
            return r;
        } catch (SecurityException e) {
            throw new BiometryException("Security Problem executing " + methodName,e);
        } catch (NoSuchMethodException e) {
            throw new BiometryException("No such method " + methodName,e);
        } catch (IllegalArgumentException e) {
            throw new BiometryException("bad argument for " + methodName,e);
        } catch (IllegalAccessException e) {
            throw new BiometryException("bad access for " + methodName,e);
        } catch (InvocationTargetException e) {
            throw new BiometryException("bad target for " + methodName,e);
        }

JavaNullPointer例外が発生しますが、もちろん、JavaJUnitでは正常に機能します。私はセレクターを作成し、そこからメソッドを呼び出すことでこれを回避しようとしていますが、不思議なことに、それも機能していません。(クラッシュしませんが、正しい結果を返すこともありません。)

于 2012-12-20T15:23:59.297 に答える