Java4iphone Jug Cologne v0
Java4iphone Jug Cologne v0
using Java
Arno Puder
San Francisco State University
Outline:
1. Introduction to Objective-C
2. Cross-Compiling Java applications
3. Running the toolchain
Java for the iPhone
Slide 2
© A. Puder
History of Objective-C
Slide 3
© A. Puder
@interface
#import <Foundation/NSObject.h>
}
-(void) print;
-(void) setNumerator: (int) d;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
Slide 4
© A. Puder
@implementation
#import "Fraction.h“
#import <stdio.h>
@implementation Fraction
-(void) print
{ printf("%i/%i", numerator,
denominator); }
Java4iPhone
@implementation
...
-(void) setNumerator: (int) n andDenominator: (int) d
Java4iPhone
{
numerator = n; denominator = d;
}
...
Invocation
Fraction *frac = [[Fraction alloc] init];
[frac setNumerator: 1 andDenominator: 5];
Slide 8
© A. Puder
Dynamic Invocations
Slide 9
© A. Puder
Objective-C “Hello World”
@interface helloWorld : UIApplication
-(void) applicationDidFinishLaunching: (NSNotification) n;
@end
@implementation helloWorld
-(void) applicationDidFinishLaunching: (NSNotification) n
{
CGRect rect = [UIHardware fullScreenApplicationContentRect];
UIWindow* window = [[UIWindow alloc] initWithContentRect: rect];
Java4iPhone
import org.xmlvm.iphone.*;
window.makeKey(this);
window._setHidden(false);
rect.origin.x = rect.origin.y = 0;
UIView mainView = new UIView(rect);
window.setContentView(mainView);
UITextLabel _title = new UITextLabel(rect);
_title.setText("Hello World!");
_title.setCentersHorizontally(true);
mainView.addSubview(_title);
}
}
Slide 11
© A. Puder
Challenges
// Java
class Calc {
int x;
void add(int y)
{
x += y;
}
}
Java4iPhone
Calc.java Calc.class
javac
BCEL/JDOM
Calc.xml
Slide 13
© A. Puder
Example: XMLVM of Class Calc
<?xml version="1.0" encoding="UTF-8"?>
<xmlvm xmlns:jvm="http://xmlvm.org/jvm">
<class name="Calc">
<field name="x" type="int"/>
<method name="add" stack="3" locals="2">
<signature>
<return type="void"/>
<parameter type="int"/>
</signature>
Java4iPhone
<code>
<jvm:load type="Calc" index="0"/>
<jvm:dup/>
<jvm:getfield class-type="Calc" type="int" field="x"/>
<jvm:load type="int" index="1"/>
<jvm:iadd/>
<jvm:putfield class-type="Calc" type="int" field="x"/>
<jvm:return/>
</code>
</method>
</class>
</xmlvm>
Slide 14
© A. Puder
Example: Executing XMLVM
<code>
<jvm:load type="Calc" index="0"/>
<jvm:dup/>
<jvm:getfield class-type="Calc" type="int" field="x"/>
<jvm:load type="int" index="1"/>
<jvm:iadd/>
<jvm:putfield class-type="Calc" type="int" field="x"/>
<jvm:return/>
Java4iPhone
</code>
Stack:
x: 11
locals[0]: this
locals[1]: 31 (y)
Slide 15
© A. Puder
Example: Executing XMLVM
<code>
<jvm:load type="Calc" index="0"/>
<jvm:dup/>
<jvm:getfield class-type="Calc" type="int" field="x"/>
<jvm:load type="int" index="1"/>
<jvm:iadd/>
<jvm:putfield class-type="Calc" type="int" field="x"/>
<jvm:return/>
Java4iPhone
</code>
Stack:
x: 11
this
locals[0]: this
locals[1]: 31 (y)
Slide 16
© A. Puder
Example: Executing XMLVM
<code>
<jvm:load type="Calc" index="0"/>
<jvm:dup/>
<jvm:getfield class-type="Calc" type="int" field="x"/>
<jvm:load type="int" index="1"/>
<jvm:iadd/>
<jvm:putfield class-type="Calc" type="int" field="x"/>
<jvm:return/>
Java4iPhone
</code>
Stack:
x: 11
this
locals[0]: this this
locals[1]: 31 (y)
Slide 17
© A. Puder
Example: Executing XMLVM
<code>
<jvm:load type="Calc" index="0"/>
<jvm:dup/>
<jvm:getfield class-type="Calc"
<jvm:getfield class-type="Calc" type="int"
type="int" field="x"/>
field="x"/>
<jvm:load type="int" index="1"/>
<jvm:iadd/>
<jvm:putfield class-type="Calc" type="int" field="x"/>
<jvm:return/>
Java4iPhone
</code>
Stack:
x: 11
this
locals[0]: this 11
locals[1]: 31 (y)
Slide 18
© A. Puder
Example: Executing XMLVM
<code>
<jvm:load type="Calc" index="0"/>
<jvm:dup/>
<jvm:getfield class-type="Calc" type="int" field="x"/>
<jvm:load type="int" index="1"/>
<jvm:iadd/>
<jvm:putfield class-type="Calc" type="int" field="x"/>
<jvm:return/>
Java4iPhone
</code>
Stack:
x: 11
this
locals[0]: this 11
31
locals[1]: 31 (y)
Slide 19
© A. Puder
Example: Executing XMLVM
<code>
<jvm:load type="Calc" index="0"/>
<jvm:dup/>
<jvm:getfield class-type="Calc" type="int" field="x"/>
<jvm:load type="int" index="1"/>
<jvm:iadd/>
<jvm:putfield class-type="Calc" type="int" field="x"/>
<jvm:return/>
Java4iPhone
</code>
Stack:
x: 11
this
locals[0]: this 42
locals[1]: 31 (y)
Slide 20
© A. Puder
Example: Executing XMLVM
<code>
<jvm:load type="Calc" index="0"/>
<jvm:dup/>
<jvm:getfield class-type="Calc" type="int" field="x"/>
<jvm:load type="int" index="1"/>
<jvm:iadd/>
<jvm:putfield class-type="Calc" type="int" field="x"/>
<jvm:return/>
Java4iPhone
</code>
Stack:
x: 42
locals[0]: this
locals[1]: 31 (y)
Slide 21
© A. Puder
XMLVM to Other Languages
Slide 22
© A. Puder
“Hello World” in XMLVM
<vm:xmlvm ...>
<vm:class name="HelloWorld" package="org.xmlvm.test.iphone“
extends="org.xmlvm.iphone.UIApplication">
<vm:method name="applicationDidFinishLaunching“
stack="4" locals="6">
<vm:signature>
<vm:return type="void" />
<vm:parameter type="org.xmlvm.iphone.NSNotification" />
Java4iPhone
</vm:signature>
<vm:code language="ByteCode">
<!-- ... -->
<jvm:var name="rect" id="2" type="org.xmlvm.iphone.CGRect" />
<jvm:invokestatic class-type="org.xmlvm.iphone.UIHardware“
method="fullScreenApplicationContentRect">
<vm:signature>
<vm:return type="org.xmlvm.iphone.CGRect" />
</vm:signature>
</jvm:invokestatic>
<jvm:astore type="java.lang.Object" index="2" />
Slide 23
© A. Puder
“Hello World” in Objective-C
@interface org_xmlvm_test_iphone_HelloWorld :
org_xmlvm_iphone_UIApplication
-(void) applicationDidFinishLaunching___org_xmlvm_iphone_NSNotification
:(org_xmlvm_iphone_NSNotification*)n1;
@end
-(void) applicationDidFinishLaunching___org_xmlvm_iphone_NSNotification
:(org_xmlvm_iphone_NSNotification*)n1
{
Java4iPhone
XMLVMElem _stack[4];
XMLVMElem _locals[6];
int _sp = 0;
XMLVMElem _op1, _op2, _op3;
_op1.o = [org_xmlvm_iphone_UIHardware
fullScreenApplicationContentRect];
_stack[_sp++].o = _op1.o;
_op1.o = _stack[--_sp].o;
[_op1.o retain];
[_locals[2].o release];
_locals[2].o = _op1.o;
...
Slide 24
© A. Puder
Outlook
– Idea: why not port these Java classes to other cell phones
that support Java?
– Benefit: iPhone-like UI on different cell phones!
Slide 25
© A. Puder
Installing the Toolchain
Slide 26
© A. Puder
Running the Simulator
– org.xmlvm.test.iphone.todo.Main
y All these applications are in source folder
src/test/iphone
y The implementation of the iPhone simulator is in source
folder src/xmlvm2objc/compat-lib/java
Slide 27
© A. Puder
Cross-Compiling “Hello World” to Objective-C
--console --objc
${workspace_loc:xmlvm}/bin/org/xmlvm/test/iphone/
HelloWorld.class
y To write the Objective-C source code to a file, use:
--out=tmp --objc
${workspace_loc:xmlvm}/bin/org/xmlvm/test/iphone/
HelloWorld.class
y The previous command will generate two files:
– ${workspace_loc:xmlvm}/tmp/HelloWorld.h
– ${workspace_loc:xmlvm}/tmp/HelloWorld.m
Slide 28
© A. Puder
Info.plist
<string>org.puder.HelloWorld</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
</dict>
</plist>
Slide 29
© A. Puder
Makefile 1/3
PRODUCT_NAME=HelloWorld
SOURCES=\
HelloWorld.m \
../bin/XMLVMCompatLib.m
SRCROOT=.
BUILT_PRODUCTS_DIR=build
CONFIGURATION_TEMP_DIR=obj
Java4iPhone
INFOPLIST_FILE=Info.plist
CC=arm-apple-darwin-gcc
CFLAGS=-g -O2 -Wall -I. -I../bin
LD=$(CC)
LDFLAGS=-lobjc -ObjC -framework CoreFoundation \
-framework Foundation -framework CoreGraphics \
-framework GraphicsServices \
-framework UIKit -framework LayerKit
Slide 30
© A. Puder
Makefile 2/3
WRAPPER_NAME=$(PRODUCT_NAME).app
EXECUTABLE_NAME=$(PRODUCT_NAME)
SOURCES_ABS=$(addprefix $(SRCROOT)/,$(SOURCES))
INFOPLIST_ABS=$(addprefix $(SRCROOT)/,$(INFOPLIST_FILE))
OBJECTS=\
$(patsubst %.c,%.o,$(filter %.c,$(SOURCES))) \
$(patsubst %.cc,%.o,$(filter %.cc,$(SOURCES))) \
$(patsubst %.cpp,%.o,$(filter %.cpp,$(SOURCES))) \
Java4iPhone
all: $(PRODUCT_ABS)
Slide 31
© A. Puder
Makefile 3/3
$(APP_ABS): $(INFOPLIST_ABS)
mkdir -p $(APP_ABS)
cp $(INFOPLIST_ABS) $(APP_ABS)/
cp $(SRCROOT)/$(RESOURCES)/*.png $(APP_ABS)/
Java4iPhone
$(CONFIGURATION_TEMP_DIR)/%.o: $(SRCROOT)/%.m
mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
deploy:
cd build; scp -r $(WRAPPER_NAME) \
[email protected]:/Applications
clean:
rm -rf $(CONFIGURATION_TEMP_DIR)
rm -rf $(BUILT_PRODUCTS_DIR)
rm -f *~
Slide 32
© A. Puder
Compile and run “Hello World”
Slide 33
© A. Puder