سؤال

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?

هل كانت مفيدة؟

المحلول

In my experience, no it does not. if you look on the conversions page, it says it supports a subset of reflection capabilities to support test frameworks.

I'm using j2objc 0.56. If I try to invoke a method:

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);
        }

I get a JavaNullPointer exception, but of course, it works fine in my Java JUnit. I've been trying to work around this by creating a selector and invoking the method from there, but curiously, thats not working either. (Not crashing, but not returning the right result either.)

نصائح أخرى

java.lang.reflect.Field has recently been fixed (or at least all reported bugs are). Source is up-to-date, and the distribution bundles are on the Downloads page.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top