Example #1
0
 public static ObjCClass toObjCClass(final long handle) {
   long classPtr = handle;
   ObjCClass c = ObjCObject.getPeerObject(classPtr);
   if (c == null) {
     c = getByNameNotLoaded(VM.newStringUTF(ObjCRuntime.class_getName(classPtr)));
   }
   while (c == null && classPtr != 0L) {
     classPtr = ObjCRuntime.class_getSuperclass(classPtr);
     c = ObjCObject.getPeerObject(classPtr);
     if (c == null) {
       c = getByNameNotLoaded(VM.newStringUTF(ObjCRuntime.class_getName(classPtr)));
     }
   }
   if (c == null) {
     String name = VM.newStringUTF(ObjCRuntime.class_getName(handle));
     throw new ObjCClassNotFoundException(
         "Could not find Java class corresponding to Objective-C class: " + name);
   }
   return c;
 }
Example #2
0
 @SuppressWarnings("unchecked")
 private static ObjCClass register(Class<? extends ObjCObject> type, String name) {
   ObjCClass superclass = getByType((Class<? extends ObjCObject>) type.getSuperclass());
   long handle =
       ObjCRuntime.objc_allocateClassPair(superclass.getHandle(), VM.getStringUTFChars(name), 0);
   if (handle == 0L) {
     throw new ObjCClassNotFoundException(
         "Failed to create custom Objective-C class for Java class: " + type);
   }
   for (Entry<String, Method> entry : getCallbacks(type).entrySet()) {
     String selName = entry.getKey();
     Method method = entry.getValue();
     Selector selector = Selector.register(selName);
     String encoding = null;
     TypeEncoding typeEncoding = method.getAnnotation(TypeEncoding.class);
     if (typeEncoding != null) {
       encoding = typeEncoding.value();
     } else {
       long methodPtr =
           ObjCRuntime.class_getInstanceMethod(superclass.getHandle(), selector.getHandle());
       if (methodPtr != 0L) {
         long encodingPtr = ObjCRuntime.method_getTypeEncoding(methodPtr);
         if (encodingPtr != 0L) {
           encoding = VM.newStringUTF(encodingPtr);
         }
       }
     }
     long impl = VM.getCallbackMethodImpl(method);
     if (!ObjCRuntime.class_addMethod(
         handle,
         selector.getHandle(),
         impl,
         encoding != null ? VM.getStringUTFChars(encoding) : 0L)) {
       throw new ObjCClassNotFoundException(
           "Failed to add method "
               + selName
               + " to custom Objective-C class for Java class: "
               + type);
     }
   }
   ObjCRuntime.objc_registerClassPair(handle);
   return new ObjCClass(handle, type, name, true);
 }