001 /*******************************************************************************
002 * Copyright (c) 2009 Progress Software, Inc.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *******************************************************************************/
009 package test;
010
011 import org.fusesource.hawtjni.runtime.JniArg;
012 import org.fusesource.hawtjni.runtime.JniClass;
013 import org.fusesource.hawtjni.runtime.JniMethod;
014 import org.fusesource.hawtjni.runtime.Library;
015
016 import static org.fusesource.hawtjni.runtime.ArgFlag.*;
017 import static org.fusesource.hawtjni.runtime.MethodFlag.*;
018
019 /**
020 *
021 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
022 */
023 @JniClass(conditional="defined(__APPLE__)")
024 public class ObjectiveCExample {
025
026 private static final Library LIBRARY = new Library("hawtjni-example", Example.class);
027 static {
028 LIBRARY.load();
029 }
030
031 public static final void main(String args[]) {
032
033 // Memory pool...
034 long NSAutoreleasePool = objc_getClass("NSAutoreleasePool");
035 long pool = $($(NSAutoreleasePool, alloc), init);
036
037 // Allocate and use a simple Objective C object
038 long NSString = objc_getClass("NSString");
039 long id = $(NSString, stringWithUTF8String, "Hello");
040 long value = $(id, length);
041 System.out.println("The length was: "+value);
042
043 // Release the pool to release the allocations..
044 $(pool, release);
045 }
046
047
048
049 public static final long stringWithUTF8String = sel_registerName("stringWithUTF8String:");
050 public static final long release = sel_registerName("release");
051 public static final long alloc = sel_registerName("alloc");
052 public static final long init = sel_registerName("init");
053 public static final long length = sel_registerName("length");
054
055 @JniMethod(cast="SEL", flags={POINTER_RETURN})
056 public static final native long sel_registerName(String selectorName);
057
058 @JniMethod(cast="id", flags={POINTER_RETURN})
059 public static final native long objc_getClass(String className);
060
061 @JniMethod(cast="id", flags={POINTER_RETURN}, accessor="objc_msgSend")
062 public static final native long $(
063 @JniArg(cast="id", flags={POINTER_ARG})long id,
064 @JniArg(cast="SEL", flags={POINTER_ARG})long sel
065 );
066
067 @JniMethod(cast="id", flags={POINTER_RETURN}, accessor="objc_msgSend")
068 public static final native long $(
069 @JniArg(cast="id", flags={POINTER_ARG})long id,
070 @JniArg(cast="SEL", flags={POINTER_ARG})long sel,
071 String arg0);
072
073 }