java4iphone-jug-cologne-v0
TRANSCRIPT
-
8/7/2019 java4iphone-jug-cologne-v0
1/33
H ow t o w r i t e iPh on e a pp l i c a t i on sus ing J ava
Arno Puder
San Francisco State University
1. Introduction to Objective-C2. Cross-Compiling Java applications3. Running the toolchain
Outline:
-
8/7/2019 java4iphone-jug-cologne-v0
2/33
Java4iPhone
Slide 2 A. Puder
J ava fo r t he iPhone
iPhone applications are based on Objective-C andCocao.
Sun Microsystems would like to ports its JVM to theiPhone.Apples iPhone SDK prohibits the development of certainapplications (e.g., VoIP).
There have been questions about whether Apple'siPhone SDK agreement would allow a JVM on theiPhone.
-
8/7/2019 java4iphone-jug-cologne-v0
3/33
Java4iPhone
Slide 3 A. Puder
His t o ry o f Objec t ive -C
Early 1980s: Objective-C language was designed by Brad J. Cox: Object-oriented extension of C. Inspired by Smalltalk. Strict superset of C.
1988: NeXT Software licensed the Objective-C language anddeveloped its libraries and a development environment calledNEXTSTEP.1992: FSF adds Objective-C support to GNU compiler suite.
1994: NeXT Computer and Sun Microsystems released astandardized specification of the NEXTSTEP system calledOPENSTEP.The Free Software Foundation's implementation of OPENSTEP iscalled GNUStep.1996: Apple acquires NeXT Software and theNEXTSTEP/OPENSTEP environment became the basis for thenext major release of Apple's operating system, OS X. Apple'sversion of this development environment is called Cocoa.
-
8/7/2019 java4iphone-jug-cologne-v0
4/33
Java4iPhone
Slide 4 A. Puder
@int e r f ac e
#import
@interface Fraction: NSObject {int numerator;int denominator;
}-(void) print;-(void) setNumerator: (int) d;-(void) setDenominator: (int) d;-(int) numerator;-(int) denominator;@end
-
8/7/2019 java4iphone-jug-cologne-v0
5/33
Java4iPhone
Slide 5 A. Puder
@im ple m ent a t ion
#import "Fraction.h#import
@implementation Fraction-(void) print
{ printf("%i/%i", numerator,denominator); }
-(void) setNumerator: (int) n{ numerator = n; }
-(void) setDenominator: (int) d{ denominator = d; }
-(int) denominator{ return denominator; }
-(int) numerator{ return numerator; }
@end
-
8/7/2019 java4iphone-jug-cologne-v0
6/33
Java4iPhone
Slide 6 A. Puder
Crea t ing and Us ing Ob jec t s
int main(int argc, const char *argv[]){
// create a new instance Fraction
Fraction *frac = [[Fraction alloc] init];// set the values[frac setNumerator: 1];
[frac setDenominator: 3];// print itprintf("The fraction is: ");[frac print];printf("\n");// free memory [frac release];return 0;
}
-
8/7/2019 java4iphone-jug-cologne-v0
7/33
Java4iPhone
Slide 7 A. Puder
Mul t ip l e Pa ram e t e r s
...-(void) setNumerator: (int) n andDenominator: (int) d;...
...-(void) setNumerator: (int) n andDenominator: (int) d
{numerator = n; denominator = d;
}...
Fraction *frac = [[Fraction alloc] init];[frac setNumerator: 1 andDenominator: 5];
Invocation
setNumber:andDenominator: is called a selector.
@interface
@implementation
-
8/7/2019 java4iphone-jug-cologne-v0
8/33
Java4iPhone
Slide 8 A. Puder
Referenc e Count ing
Class inheriting from NSObject have reference counting.
Fraction *frac = [[Fraction alloc] init];printf(Retain count: %i\n", [frac retainCount]);[frac retain]; // 2[frac retain]; // 3
printf(Retain count: %i\n", [frac retainCount]);[frac release]; // 2printf(Retain count: %i\n", [frac retainCount]);[frac release]; // 1
[frac release]; // 0. Object will be deleted
-
8/7/2019 java4iphone-jug-cologne-v0
9/33
Java4iPhone
Slide 9 A. Puder
Dy n am i c I n vo c a t i o ns
Method invocations are bound at runtime:id obj = ;[obj someMethod];
id is a generic object type (similar to java.lang.Object )obj may or may not implement someMethod . If not, aruntime error occurs.
Dynamic invocations are heavily used in the delegate pattern(e.g., UITable )
-
8/7/2019 java4iphone-jug-cologne-v0
10/33
Java4iPhone
Slide 10 A. Puder
Objec t ive-C Hel lo Wor ld
@interface helloWorld : UIApplication-(void) applicationDidFinishLaunching: (NSNotification) n;@end
@implementation helloWorld
-(void) applicationDidFinishLaunching: (NSNotification) n{
CGRect rect = [UIHardware fullScreenApplicationContentRect];UIWindow* window = [[UIWindow alloc] initWithContentRect: rect];[window orderFront: self];[window makeKey: self];[window _setHidden: false];rect.origin.x = rect.origin.y = 0;UIView* mainView = [[UIView alloc] initWithFrame: rect];[window setContentView: mainView];UITextLabel *_title = [[UITextLabel alloc] initWithFrame: rect];[_title setText: @"Hello World!"];[_title setCentersHorizontally: true];[mainView addSubview: _title];
}@end
-
8/7/2019 java4iphone-jug-cologne-v0
11/33
Java4iPhone
Slide 11 A. Puder
J ava He l lo Wor ld
import org.xmlvm.iphone.*;
public class HelloWorld extends UIApplication{
public void applicationDidFinishLaunching(NSNotification n){
CGRect rect = UIHardware.fullScreenApplicationContentRect();UIWindow window = new UIWindow(rect);window.orderFront( this );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);
}
}
-
8/7/2019 java4iphone-jug-cologne-v0
12/33
Java4iPhone
Slide 12 A. Puder
Chal lenges
Design goal: if possible, Java API mimics 1:1 theObjective-C API.Objective-C challenges: Namespaces, method overloading: use name-mangling. Garbage collection: use Objective-C reference counting. No multiple inheritance: use Objective-C dynamic
invocations. No static member variables: use global variables.
Cocoa challenges: Makes use of C-functions (e.g., CGColorCreate )
Uses value types (e.g., CGRect ) Uses pointers for output parameters (e.g.,
NSURLConnection ) Makes heavy use of delegation (e.g., data source for
UITable )
-
8/7/2019 java4iphone-jug-cologne-v0
13/33
Java4iPhone
Slide 13 A. Puder
Ex am ple : Crea t ing XMLVM
// Javaclass Calc {
int x;
void add(int y){x += y;
}}
Calc.java Calc.class
Calc.xml
javacBCEL/JDOM
-
8/7/2019 java4iphone-jug-cologne-v0
14/33
Java4iPhone
Slide 14 A. Puder
Ex am ple : XMLVM o f Class Calc
-
8/7/2019 java4iphone-jug-cologne-v0
15/33
Java4iPhone
Slide 15 A. Puder
Ex am ple : Ex ec u t ing XMLVM
locals[0]: this
locals[1]: 31
Stack:
x: 11
(y)
-
8/7/2019 java4iphone-jug-cologne-v0
16/33
Java4iPhone
Slide 16 A. Puder
Ex am ple : Ex ec u t ing XMLVM
thisStack:
locals[0]: this
locals[1]: 31
x: 11
(y)
-
8/7/2019 java4iphone-jug-cologne-v0
17/33
Java4iPhone
Slide 17 A. Puder
Ex am ple : Ex ec u t ing XMLVM
thisthis
Stack:
locals[0]: this
locals[1]: 31
x: 11
(y)
-
8/7/2019 java4iphone-jug-cologne-v0
18/33
Java4iPhone
Slide 18 A. Puder
Ex am ple : Ex ec u t ing XMLVM
this11
Stack:
locals[0]: this
locals[1]: 31
x: 11
(y)
-
8/7/2019 java4iphone-jug-cologne-v0
19/33
Java4iPhone
Slide 19 A. Puder
Ex am ple : Ex ec u t ing XMLVM
this1131
Stack:
locals[0]: this
locals[1]: 31
x: 11
(y)
-
8/7/2019 java4iphone-jug-cologne-v0
20/33
Java4iPhone
Slide 20 A. Puder
Ex am ple : Ex ec u t ing XMLVM
this42
Stack:
locals[0]: this
locals[1]: 31
x: 11
(y)
-
8/7/2019 java4iphone-jug-cologne-v0
21/33
Java
4iPhone
Slide 21 A. Puder
Ex am ple : Ex ec u t ing XMLVM
Stack:
locals[0]: this
locals[1]: 31
x: 42
(y)
-
8/7/2019 java4iphone-jug-cologne-v0
22/33
Java
4iPhone
Slide 22 A. Puder
XMLVM t o Ot he r Languages
Since the Java VM is a simple, stack-based machine,XMLVM can easily be mapped to other languages.These translations are done using XSLT.Mappings exist for JavaScript and Objective-C.The XSLT excerpt below demonstrates the translation of (Integer add) to JavaScript.
-
8/7/2019 java4iphone-jug-cologne-v0
23/33
Java
4iPhone
Slide 23 A. Puder
He l lo Wor ld in XMLVM
-
8/7/2019 java4iphone-jug-cologne-v0
24/33
Java
4iPhone
Slide 24 A. Puder
Hel lo Wor ld in Objec t ive -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
{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;...
-
8/7/2019 java4iphone-jug-cologne-v0
25/33
Java
4iPhon
e
Slide 25 A. Puder
Out look
Still lots to do: Complete Java-to-Objective-C mapping. Support for all iPhone UI elements.
iPhone UI for other phones: iPhone simulator mimics the iPhone UI. The simulator is completely written in Java.
Idea: why not port these Java classes to other cell phonesthat support Java? Benefit: iPhone-like UI on different cell phones!
-
8/7/2019 java4iphone-jug-cologne-v0
26/33
Java
4iPhon
e
Slide 26 A. Puder
I n s t a l l i ng t h e To ol c h a in
Prerequisites for running the iPhone simulator: Java 5 or 6 Eclipse
Prerequisites for cross-compiling to the iPhone: Jail-broken iPhone/iPod Touch:
http://www.ziphone.org/
iPhone development toolchain:http://code.google.com/p/iphone-dev/wiki/BuildingXMLVM Cross-Compiler and iPhone simulator availablefrom SourceForge:
http://sourceforge.net/cvs/?group_id=152977Use anonymous CVS to checkout module xmlvm .
http://www.ziphone.org/http://code.google.com/p/iphone-dev/wiki/Buildinghttp://sourceforge.net/cvs/?group_id=152977http://sourceforge.net/cvs/?group_id=152977http://code.google.com/p/iphone-dev/wiki/Buildinghttp://www.ziphone.org/ -
8/7/2019 java4iphone-jug-cologne-v0
27/33
Java
4iPhon
e
Slide 27 A. Puder
Runn ing t he Sim ula t o r
Simulator automatically launched when a Java-basediPhone application is executed.Currently there are four demos:
org.xmlvm.test.iphone.HelloWorld
org.xmlvm.test.iphone.Android
org.xmlvm.test.iphone.ifireworks.Main
org.xmlvm.test.iphone.todo.MainAll these applications are in source foldersrc/test/iphone
The implementation of the iPhone simulator is in sourcefolder src/xmlvm2objc/compat-lib/java
-
8/7/2019 java4iphone-jug-cologne-v0
28/33
Java
4iPhon
e
Slide 28 A. Puder
Cross-Com pi l ing Hel lo Wor ld t o Objec t ive-C
The cross-compiler is in source folder src/xmlvm classorg.xmlvm.Main
To generate the XMLVM for Hello World use the followingcommand line options (under Eclipse):--console${workspace_loc:xmlvm}/bin/org/xmlvm/test/iphone/HelloWorld.class
To cross-compile the XMLVM to Objective-C use:--console --objc${workspace_loc:xmlvm}/bin/org/xmlvm/test/iphone/HelloWorld.class
To write the Objective-C source code to a file, use:--out=tmp --objc
${workspace_loc:xmlvm}/bin/org/xmlvm/test/iphone/HelloWorld.class
The previous command will generate two files: ${workspace_loc:xmlvm}/tmp/HelloWorld.h ${workspace_loc:xmlvm}/tmp/HelloWorld.m
-
8/7/2019 java4iphone-jug-cologne-v0
29/33
Java
4iPhon
e
Slide 29 A. Puder
Info .p l i s t
-
8/7/2019 java4iphone-jug-cologne-v0
30/33
Java
4iPhon
e
Slide 30 A. Puder
Mak ef i le 1 /3
PRODUCT_NAME=HelloWorldSOURCES=\
HelloWorld.m \../bin/XMLVMCompatLib.m
SRCROOT=.BUILT_PRODUCTS_DIR=buildCONFIGURATION_TEMP_DIR=objINFOPLIST_FILE=Info.plist
CC=arm-apple-darwin-gccCFLAGS=-g -O2 -Wall -I. -I../binLD=$(CC)LDFLAGS=-lobjc -ObjC -framework CoreFoundation \
-framework Foundation -framework CoreGraphics \-framework GraphicsServices \-framework UIKit -framework LayerKit
-
8/7/2019 java4iphone-jug-cologne-v0
31/33
Java
4iPhon
e
Slide 31 A. Puder
Mak ef i le 2 /3
WRAPPER_NAME=$(PRODUCT_NAME).appEXECUTABLE_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))) \$(patsubst %.m,%.o,$(filter %.m,$(SOURCES))) \
$(patsubst %.mm,%.o,$(filter %.mm,$(SOURCES)))OBJECTS_ABS=$(addprefix $(CONFIGURATION_TEMP_DIR)/,$(OBJECTS))APP_ABS=$(BUILT_PRODUCTS_DIR)/$(WRAPPER_NAME)PRODUCT_ABS=$(APP_ABS)/$(EXECUTABLE_NAME)
all: $(PRODUCT_ABS)
f
-
8/7/2019 java4iphone-jug-cologne-v0
32/33
Java
4iPhon
e
Slide 32 A. Puder
Mak ef i le 3 /3
$(PRODUCT_ABS): $(APP_ABS) $(OBJECTS_ABS)$(LD) $(LDFLAGS) -o $(PRODUCT_ABS) $(OBJECTS_ABS)
$(APP_ABS): $(INFOPLIST_ABS)
mkdir -p $(APP_ABS)cp $(INFOPLIST_ABS) $(APP_ABS)/cp $(SRCROOT)/$(RESOURCES)/*.png $(APP_ABS)/
$(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 *~
C i l d H l l W ld
-
8/7/2019 java4iphone-jug-cologne-v0
33/33
Java
4iPhon
e
Slide 33 A. Puder
Com pi le and run Hel lo Wor ld
Type make in ${workspace_loc:xmlvm}/tmpType make deployOn the iPhone, type killall SpringBoardThe previous command can be accomplished by eitherssh-ing into the iPhone, or by using a terminal applicationon the iPhone.
Restarting the SpringBoard is necessary only once sothat it picks up the new Hello World application.