Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / doc / vmintegration.texinfo
blobe9f10460125ac848bc4d63689dbbe05e8b2d9d7e
1 \input texinfo @c -*-texinfo-*-
3 @c %**start of header
4 @setfilename vmintegration.info
5 @settitle GNU Classpath VM Integration Guide
6 @c %**end of header
8 @setchapternewpage off
10 @ifinfo
11 This file contains important information you will need to know if you
12 are going to write an interface between GNU Classpath and a Virtual
13 Machine.
15 Copyright (C) 1998-2002, 2004, 2005 Free Software Foundation, Inc.
17 @ifnotplaintext
18 @dircategory GNU Libraries
19 @direntry
20 * VM Integration: (vmintegration).   GNU Classpath VM Integration Guide
21 @end direntry
22 @end ifnotplaintext
23 @end ifinfo
25 @titlepage
26 @title GNU Classpath VM Integration Guide
27 @author John Keiser
28 @author C. Brian Jones
29 @author Mark Wielaard
31 @page
32 @vskip 0pt plus 1filll
33 Copyright @copyright{} 1998-2002 Free Software Foundation, Inc.
34 @sp 2
35 Permission is granted to make and distribute verbatim copies of
36 this document provided the copyright notice and this permission notice
37 are preserved on all copies.
39 Permission is granted to copy and distribute modified versions of this
40 document under the conditions for verbatim copying, provided that the
41 entire resulting derived work is distributed under the terms of a
42 permission notice identical to this one.
44 Permission is granted to copy and distribute translations of this manual
45 into another language, under the above conditions for modified versions,
46 except that this permission notice may be stated in a translation
47 approved by the Free Software Foundation.
49 @end titlepage
51 @ifinfo
52 @node Top, Introduction, (dir), (dir)
53 @top GNU Classpath Hacker's Guide
55 This file contains important information you will need to know if you
56 are going to write an interface between GNU Classpath and a Virtual
57 Machine.
59 This document is incomplete, as we are still in alpha with the interface.
61 @end ifinfo
63 @menu
64 * Introduction::                An introduction to the Classpath project
65 * Initialization::              Initializing the classes
66 * Classpath Hooks::             Hooks from Classpath to the VM
67 * VM Hooks::                    Hooks from the underlying VM to Classpath
68 * JNI Implementation::          Hooking the VM to jni.h
69 * Miscellaneous VM Requirements::  
70 @end menu
72 @node Introduction, Initialization, Top, Top
73 @comment node-name, next, previous, up
74 @chapter Introduction
76 The Classpath Project's ambition to be a 100% clean room implementation
77 of the standard Java class libraries cannot be fulfilled without some
78 level of integration with the Virtual Machine, the underlying machinery
79 that actually runs Java.
81 There are several VMs out there, here is a small list.
83 @itemize @bullet
84 @item @uref{http://www.hungry.com/old-hungry/products/japhar/,Japhar}
85 Japhar was the first VM to use GNU Classpath.  Today you can see that
86 sort of relationship in the source tree which denotes several Japhar
87 specific files as a reference implementation of those pieces.  This VM
88 has been primarily tested against Linux and lacks garbage collections, a
89 JIT, and suffers recently from slow development.
91 @item @uref{http://www.intel.com/research/mrl/orp/,Intel's Open Runtime Platform}
92 Intel surprised us not long ago with the release of this rather advanced
93 VM that uses GNU Classpath for a set of class libraries and works on
94 Linux and Windows 2000.  As of June, 2004, it does not appear that ORP
95 is under active development.
97 @item @uref{http://www.sablevm.org/,SableVM}
98 SableVM is a robust, extremely portable, efficient, and
99 specifications-compliant Java Virtual Machine that aims to be easy to
100 maintain and to extend. It features a state-of-the-art, efficient
101 interpreter engine. Its source code is very accessible and easy to
102 understand, and has many robustness features that have been the object
103 of careful design.
105 @item @uref{http://www.kaffe.org,Kaffe}
106 Kaffe is an advanced VM and together with its own class libraries
107 provides a Java 1.1 compatible environment.
109 @item @uref{http://www.mozilla.org/projects/ef,Electrical Fire}
110 The Electrical File VM continues to be listed as a Mozilla project
111 though development has been somewhat quiet.  A number of concepts from
112 EF were expected at one point to be rolled into Japhar, but that
113 development has not occurred as of yet.
115 @item @uref{http://latte.snu.ac.kr/,LaTTe}
116 This VM project so far supports only Sun UltraSparc processors using the
117 proprietary Solaris 2.5.1 or higher operating system.  LaTTe was derived
118 from Kaffe but claims a number of improvements.
120 @item @uref{http://gcc.gnu.org/java/,GNU Compiler for Java (GCJ)}
121 This is a portable, optimizing, ahead-of-time compiler for the Java
122 Programming Language. It can compile Java source code directly to native
123 machine code, Java source code to Java bytecode (class files), and Java
124 bytecode to native machine code. Compiled applications are linked with the
125 GCJ runtime, libgcj which is based on the GNU Classpath code, which provides
126 the core class libraries, a garbage collector, and a bytecode interpreter.
127 libgcj can dynamically load and interpret class files, resulting in mixed
128 compiled/interpreted applications.
129 GCJ is part of the GNU Compiler Collection (@uref{http://gcc.gnu.org/,GCC}).
130 On March 6 2000 the libgcj and GNU Classpath projects were officially merged
131 and there is active work on merging all the classes between the projects.
132 Licensed under GPL+exception, just as GNU Classpath is.
134 @item @uref{http://kissme.sourceforge.net/,Kissme}
135 This is a free Java Virtual Machine that is being developed on GNU/Linux
136 and can run console Java applications.  Kissme also provides support for
137 orthogonally persistent Java.
138 @c I don't know what ``orthogonally persistent Java'' is, and I bet
139 @c there are other people don't know either. -- Steve Augart, 4 June 2004
141 @item @uref{http://jamvm.sourceforge.net/,JamVM}
142 A simple, small bytecode interpreter that works out-of-the-box with
143 pure GNU Classpath; it is emerging as the preferred platform for
144 quickly testing a new build of GNU Classpath.  Licensed under the GPL.
146 @item @uref{http://oss.software.ibm.com/jikesrvm,Jikes RVM}
147 A free runtime environment for Java, written in Java.  Works
148 out-of-the-box with pure GNU Classpath.  Features an optimizing JIT.
149 Runs on the x86 and PowerPC architectures, on the AIX, Linux, and Mac
150 OS/X operating systems.  Licensed under the CPL (Common Public
151 License).  Extensively documented.  Actively developed as of June,
152 2004.
154 @end itemize
156 In the past integration efforts were focused mainly on Japhar with an eye
157 towards getting Electrical Fire to work.  Most information contained in
158 this document is gleaned from these efforts. Recently more work has been
159 done on getting gcj, orp and kissme to work out of the box with GNU Classpath
160 but there is much to do before that becomes a reality.
163 @node Initialization, Classpath Hooks, Introduction, Top
164 @comment node-name, next, previous, up
165 @chapter Initialization
167 The order of initialization, as far as I can tell, doesn't matter just
168 yet.  However, when we move to 1.2 support, it probably will matter, so
169 we'll have a note in here at that time.
171 The initialization order is currently documented in the
172 @file{Runtime.java} source file.
174 @node Classpath Hooks, VM Hooks, Initialization, Top
175 @comment node-name, next, previous, up
176 @chapter Classpath Hooks
178 The primary method of interaction between Classpath and the VM is via
179 the helper classes, which are named after the relevant core library
180 class, but include an additional `VM' prefix.  The library classes from
181 Classpath call out to these to get certain VM-specific dirty work done.
182 A reference copy of each VM class exists.  The majority consist of a
183 series of static methods, some of which are simply declared
184 @code{native}, and some which provide a default implementation.  VMs may
185 either use these as is, or create their own local variations.  When
186 using the default implementations, the VM is responsible for
187 implementing any of the code marked as @code{native} which corresponds
188 to functionality they wish their VM to provide.  When using their own
189 versions of the classes, VM implementors may choose to change the mix of
190 native and non-native methods from that below, so as to best suit their
191 implementation.
193 @menu
194 * java.lang::
195 * gnu.classpath::
196 * java.util::
197 * java.io::
198 * java.security::
199 * java.net::
200 * java.nio::
201 * java.nio.channels::
202 * gnu.java.nio::
203 * Classpath Callbacks::
204 @end menu
206 @node java.lang, gnu.classpath, Classpath Hooks, Classpath Hooks
207 @comment  node-name,  next,  previous,  up
209 @section @code{java.lang}
211 @code{java.lang} is the core Java package, being imported automatically by all
212 classes.  It includes basic classes as @code{Object} and @code{String}.
213 A VM must implement at least some parts of this package in order to
214 become operable.
216 @menu
217 * java.lang.VMClass::
218 * java.lang.VMObject::
219 * java.lang.VMClassLoader::
220 * java.lang.VMSystem::
221 * java.lang.VMThrowable::
222 * java.lang.VMCompiler::
223 * java.lang.VMDouble::
224 * java.lang.VMFloat::
225 * java.lang.VMProcess::
226 * java.lang.VMRuntime::
227 * java.lang.VMString::
228 * java.lang.VMThread::
229 * java.lang.VMInstrumentationImpl::
230 * java.lang.VMMath::
231 @end menu
233 @node java.lang.VMClass, java.lang.VMObject ,java.lang,java.lang
234 @subsection @code{java.lang.VMClass}
236 The core class, @code{java.lang.Class}, and the corresponding VM class,
237 @code{java.lang.VMClass}, provide two main functions within GNU Classpath.
239 @enumerate
240 @item For basic VM operation, @code{java.lang.Class} provides the link between
241 the Java-based representation of a class it embodies and the VM's own
242 internal structure for a class.  @xref{VM Hooks}.
244 @item As far as the user is concerned, the main function of
245 @code{java.lang.Class} is as an entry point to the reflection
246 facilities, and so it also provides this functionality, backed by the
247 VM class.
248 @end enumerate
250 This VM class lists the following methods, organized by the version of the
251 Java specification in which they occur.  All are @code{native}, unless
252 otherwise specified, and pertain to reflection.  As a result, the VM only
253 needs to implement these methods in order to provide reflection support,
254 and then only to the degree required.
256 @itemize @bullet
257 @item 1.0
258 @itemize @bullet
259 @item @code{isInterface(Class)} -- This is simply a property test, and matches
260 the presence of an appropriate flag within the class file.
261 @item @code{getName(Class)} -- Returns the fully-qualified name of the class.
262 @item @code{getSuperclass(Class)} -- Returns a @code{Class} instance which
263 represents the superclass.  Again, the class file contains an element directly
264 relating to this.  @code{null} is returned for primitives, interfaces and
265 @code{Object}.
266 @item @code{getInterfaces(Class)} -- Same as the above, but the implemented
267 or extended interfaces rather than the superclass.  An empty array should
268 be returned, rather than @code{null}.
269 @item @code{getDeclaredClasses(Class,boolean)} -- Returns the internal classes
270 this instance declares directly.  The flag determines whether or not the
271 VM should filter out non-public classes.
272 @item @code{getDeclaredFields(Class,boolean)} -- The same for fields.
273 @item @code{getDeclaredMethods(Class,boolean)} -- And for methods.
274 @item @code{getDeclaredConstructors(Class,boolean)} -- And constructors.
275 @item @code{getClassLoader(Class)} -- Returns the @code{ClassLoader} instance
276 which is responsible for the specified class.
277 @item @code{forName(String)} -- The VM should create a @code{Class} instance
278 corresponding to the named class.  As noted in @ref{VM Hooks}, the internal
279 content of the instance is the responsibility of the VM.
280 @item @code{isArray(Class)} -- Another property test, corresponding to a
281 class file flag.
282 @item @code{initialize(Class)} -- The VM should initialize the class fully,
283 if it has not already done so.
284 @item @code{loadArrayClass(String,ClassLoader)} -- This is called if
285 @code{forName} returns @code{null} and the string specifies an array class.
286 The specified array class should be loaded with the supplied class loader.
287 @item @code{throwException(Throwable)} -- The VM should throw the supplied
288 checked exception, without declaring it.
289 @end itemize
290 @item 1.1
291 @itemize @bullet
292 @item @code{isInstance(Class,Object)} -- This is a reflection-based equivalent
293 of the @code{instanceof} operator.
294 @item @code{isAssignableFrom(Class,Class)} -- Mainly a shorthand for the above,
295 removing the need to create an instance to test assignability.  
296 @item @code{isPrimitive(Class)} -- Returns true if this class is simply
297 a representation of one of the primitive types: @code{boolean}, @code{byte},
298 @code{char}, @code{short}, @code{int}, @code{long}, @code{float},
299 @code{double} and @code{void}.
300 @item @code{getComponentType(Class)} -- Produces a @code{Class} instance which
301 represents the type of the members of the array the class instance represents.
302 Classes which don't represent an array type return @code{null}.
303 @item @code{getModifiers(Class,boolean)} -- Returns an integer which encodes
304 the class' modifiers, such as @code{public}.  Again, this relates to
305 information stored in the class file.
306 @item @code{getDeclaringClass(Class)} -- Returns the class that declared
307 an inner or member class, or @code{null} if the instance refers to a top-level
308 class.
309 @end itemize
310 @end itemize
312 @node java.lang.VMObject, java.lang.VMClassLoader, java.lang.VMClass, java.lang
313 @subsection @code{java.lang.VMObject}
315 @code{VMObject} is the bridge between the low level @code{Object} facilities
316 such as making a clone, getting the class of the object and the wait/notify
317 semantics.  This is accomplished using the following @code{native}
318 methods.
320 @itemize @bullet
321 @item @code{getClass(Object)} -- Returns the @code{Class} instance for the
322 object.  @code{Class} objects are produced by the VM, as described in
323 @ref{VM Hooks}.
324 @item @code{clone(Cloneable)} -- The VM should produce a low-level clone of the
325 specified object, creating a field-by-field shallow copy of the original.
326 The only difference between the two is that the new object should still be
327 @code{finalizable}, even if the original is not.
328 @item @code{notify(Object)} -- The VM should choose one of the threads waiting
329 for a lock on the specified object arbitrarily, and wake it.  If the current
330 thread does not currently hold the lock on the object, then an
331 @code{IllegalMonitorStateException} should be thrown.
332 @item @code{notifyAll(Object)} -- Same as the above, but all threads are
333 awakened.
334 @item @code{wait(Object,long,int)} -- The VM should set the current thread
335 into a waiting state, which persists until it receives a notify signal or the
336 specified time (in milliseconds and nanoseconds) is exceeded.  The nanoseconds
337 restriction may be ignored if such granularity is not available, and a
338 @code{IllegalMonitorStateException} should be thrown if the current thread
339 doesn't own the object.
340 @end itemize
342 @node java.lang.VMClassLoader, java.lang.VMSystem, java.lang.VMObject, java.lang
343 @subsection @code{java.lang.VMClassLoader}
344 @code{VMClassLoader} provides methods for defining and resolving core and
345 primitive classes, as well as handling resources, packages and assertions.
346 The class is a mixture of @code{native} methods and Java-based
347 implementations, with some of the latter being @emph{stubs}.
349 @itemize @bullet
350 @item Native Methods
351 @itemize @bullet
352 @item @code{defineClass(ClassLoader,String,byte[],int,int,ProtectionDomain)}
353 -- The VM should create a @code{Class} instance from the supplied byte array.
354 @item @code{resolveClass(Class)} -- Resolve references to other classes in the
355 supplied class.
356 @item @code{loadClass(name,boolean)} -- Load a class using the bootstrap
357 loader.
358 @item @code{getPrimitiveClass(char)} -- The VM should provide a @code{Class}
359 implementation for one of the primitive classes.  The supplied character
360 matches the JNI code for the primitive class e.g. `B' for byte and
361 `Z' for boolean.
362 @end itemize
363 @item Java Methods
364 @itemize @bullet
365 @item @code{getResource(String)} -- The default implementation calls
366 @code{getResources} and returns the first element in the returned enumeration,
367 or @code{null} if there are no elements.
368 @item @code{getResources(String)} -- By default, this compiles a list of
369 URLs via the boot class path.  Any matching files within a zip file are added,
370 and directories on the boot class path are automatically converted to file
371 URLs that refer to join the directory with the resource name (whether or not
372 it actually exists).
373 @item @code{getPackage(String)} -- Always returns null, which may be suitable
374 if the VM does not wish to return a @code{Package} implementation. Otherwise,
375 it may be necessary to make this a @code{native} method.
376 @item @code{getPackages()} -- As with the last, a default stub implementation
377 exists (returning an empty array) which may be replaced if support is
378 required. 
379 @item @code{defaultAssertionStatus()} -- A stub which can be implemented
380 by VMs providing assertion support.  At present, it always returns @code{true}.
381 @item @code{packageAssertionStatus()} -- Much the same status as the above.
382 The method should return a map converting package names to boolean status
383 values.  The stub implementation provides an empty map.
384 @item @code{classAssertionStatus()} -- Same as the last, but for classes.
385 @item @code{getSystemClassLoader()} -- The default calls @code{ClassLoader}
386 to create a new auxillary class loader with a system and extension class
387 loader.  The VM may wish to replace it if it wishes to supply its own custom
388 system class loader.
389 @end itemize
390 @end itemize
391 @node java.lang.VMSystem, java.lang.VMThrowable, java.lang.VMClassLoader, java.lang
392 @subsection @code{java.lang.VMSystem}
393 @code{VMSystem} handles the default I/O streams, provides access to the
394 system clock and environment variables and provides methods for
395 @code{System.arraycopy} and the @code{identityHashCode} of an
396 @code{Object}.  It consists of @code{native} methods, but the default
397 implementation also provides some helper methods to simplify stream
398 creation.  
400 @itemize @bullet
401 @item Native Methods
402 @itemize @bullet
403 @item @code{arraycopy(Object,int,Object,int,int)} -- The VM should copy
404 a specified number of array objects from one array to another, with 
405 appropriate checks for compatible typing, available elements and space.
406 The VM should be able to perform this more efficiently using native code
407 and direct memory manipulation than would have been achieved by using Java.
408 @item @code{identityHashCode(Object)} -- This is the hashcode for
409 @code{Object}, which relates to the actual location of the object in memory.
410 @item @code{setIn(InputStream)} -- Set the system input stream.
411 @item @code{setOut(PrintStream)} -- Set the system output stream.
412 @item @code{setErr(PrintStream)} -- Set the system error stream.
413 @item @code{currentTimeMillis()} -- Gets the system time in milliseconds.
414 @item @code{getenv(String)} -- Returns the value of the specified environment
415 variable.
416 @end itemize
417 @item Java Methods
418 @itemize @bullet
419 @item @code{makeStandardInputStream()} -- Helps provide the functionality of
420 @code{System.in} by wrapping the appropriate file descriptor in a buffered
421 file input stream.  VMs may choose to create the stream from the descriptor
422 differently rather than using this method.
423 @item @code{makeStandardOutputStream()} -- Helps provide the functionality of
424 @code{System.out} by wrapping the appropriate file descriptor in a buffered
425 file output stream.  VMs may choose to create the stream from the descriptor
426 differently rather than using this method.
427 @item @code{makeStandardErrorStream()} -- Helps provide the functionality of
428 @code{System.err} by wrapping the appropriate file descriptor in a buffered
429 file output stream.  VMs may choose to create the stream from the descriptor
430 differently rather than using this method.
431 @end itemize
432 @end itemize
434 Classpath also provides native implementations of
436 @itemize @bullet
437 @item @code{setIn(InputStream)} 
438 @item @code{setOut(PrintStream)} 
439 @item @code{setErr(PrintStream)} 
440 @item @code{currentTimeMillis()} 
441 @item @code{getenv(String)}
442 @end itemize
444 making a VM implementation optional.
446 @node java.lang.VMThrowable, java.lang.VMCompiler, java.lang.VMSystem, java.lang
447 @subsection @code{java.lang.VMThrowable}
448 @code{VMThrowable} is used to hold the VM state of a throwable, created either
449 when a @code{Throwable} is created or the @code{fillInStackTrace()} method is
450 called (i.e. when the actual stack trace is needed, as a lot of exceptions are
451 never actually used).  The actual class has two @code{native} methods,
452 one (@code{fillInStackTrace()}) being a method of the class used to obtain
453 instances, and the other an instance method, @code{getStackTrace()}.
454 @itemize @bullet
455 @item @code{fillInStackTrace(Throwable)} -- The VM should return the current
456 execution state of the @code{Throwable} in the form of a @code{VMThrowable}
457 instance.  The VM may also return @code{null} if it does not support this
458 functionality.
459 @item @code{getStackTrace()} -- This is used to create a real
460 @code{StackTraceElement} array for the exception, using the state data
461 stored during creation of the instance.
462 @end itemize
464 @node java.lang.VMCompiler, java.lang.VMDouble, java.lang.VMThrowable, java.lang
465 @subsection @code{java.lang.VMCompiler}
467 @code{VMCompiler} provides an interface for VMs which wish to provide
468 JIT compilation support.  The default implementation is simply a series
469 of stubs. The property, @code{java.compiler}, should point to a library
470 containing the function @code{java_lang_Compiler_start()} if such support
471 is to be provided.
473 @itemize @bullet
474 @item @code{compileClass(Class)} -- Invoke the compiler to compile the specific
475 class, returning @code{true} if successful.
476 @item @code{compileClasses(String)} -- The compiler should compile the classes
477 matching the specified string, again returning @code{true} on success.
478 @item @code{command(Object)} -- The object represents a command given to the
479 compiler, and is specific to the compiler implementation.
480 @item @code{enable} -- Enable the operation of the compiler.
481 @item @code{disable} -- Disable compiler operation.
482 @end itemize
484 @node java.lang.VMDouble, java.lang.VMFloat, java.lang.VMCompiler, java.lang
485 @subsection @code{java.lang.VMDouble}
487 @code{VMDouble} provides native support for the conversion and parsing
488 of doubles.
490 @itemize @bullet
491 @item @code{doubleToLongBits(double)} -- Converts the double to the IEEE 754
492 bit layout, collapsing NaNs to @code{0x7ff8000000000000L}.
493 @item @code{doubleToRawLongBits(double)} -- Same as the above, but preserves
494 NaNs.
495 @item @code{longBitsToDouble(long)} -- This is the inverse of the last method,
496 preserving NaNs so that the output of one can be fed into the other without
497 data loss.
498 @item @code{toString(double,boolean)} -- Converts the double to a string,
499 giving a shorter value if the flag @code{isFloat} is @code{true}, indicating
500 that the conversion was requested by @code{java.lang.Float} rather than
501 @code{java.lang.Double}.
502 @item @code{initIDs} -- Used by JNI-based solutions to initialize the cache
503 of the static field IDs.  The default @code{VMDouble} implementation has a
504 static initializer which loads the JNI library and calls this method.
505 @item @code{parseDouble} -- Turn the string into a usable double value.
506 @end itemize
508 Classpath provides native implementations of all these, making VM
509 implementation optional.
511 @node java.lang.VMFloat, java.lang.VMProcess, java.lang.VMDouble, java.lang
512 @subsection @code{java.lang.VMFloat}
514 @code{VMFloat} provides native support for the conversion of floats.
516 @itemize @bullet
517 @item @code{floatToIntBits(float)} -- Converts the float to the IEEE 754
518 bit layout, collapsing NaNs to @code{0x7fc00000}.
519 @item @code{floatToRawIntBits(float)} -- Same as the above, but preserves
520 NaNs.
521 @item @code{intBitsToFloat(int)} -- This is the inverse of the last method,
522 preserving NaNs so that the output of one can be fed into the other without
523 data loss.
524 @end itemize
526 Classpath provides native implementations of all these, making VM
527 implementation optional.
529 @node java.lang.VMProcess, java.lang.VMRuntime, java.lang.VMFloat, java.lang
530 @subsection @code{java.lang.VMProcess}
532 @code{VMProcess} handles the execution of external processes.  In the
533 default implementation, threads are spawned and reaped by @code{ProcessThread}.
534 A constructor creates a new @code{VMProcess}, which extends rather than
535 complements @code{Process}, using an array of arguments, an array of
536 environment variables and a working directory.  The instance maintains
537 system input, output and error streams linked to the external process.
538 Three @code{native} methods are used, and implementations are provided
539 for all three by Classpath, making VM implementation optional.  These use
540 the POSIX functions, @code{fork()}, @code{waitpid()} and @code{kill()}.
542 @itemize @bullet
543 @item @code{nativeSpawn(String[],String[],File)} -- The VM should create a
544 new process which uses the specified command-line arguments, environment
545 variables and working directory.  Unlike the other two methods, this
546 method is linked to an instance, and must call @code{setProcessInfo()} with
547 the results before returning.
548 @item @code{nativeReap()} -- This is called to perform a reap of any
549 zombie processes, and should not block, instead returning a boolean as to
550 whether reaping actually took place.
551 @item @code{nativeKill(long)} -- The VM should terminate the specified PID.
552 @end itemize
554 @node java.lang.VMRuntime, java.lang.VMString, java.lang.VMProcess, java.lang
555 @subsection @code{java.lang.VMRuntime}
557 The @code{VMRuntime} class provides a series of native methods
558 which divulge information about the runtime or invoke certain
559 operations.  This includes retrieving the amount of available memory,
560 and scheduling the garbage collector.  There are two exceptions: the
561 @code{enableShutdownHooks} method, which allows the VM to put in its own
562 shutdown hooks when @code{Runtime.addShutdownHook()} is first invoked,
563 and @code{exec(String[],String[],File)} which spawns an external process.
564 These are Java-based static methods instead.  The first is simply a stub by
565 default, while the second simply links to the functionality of
566 @code{VMProcess} (and should be changed if a different @code{Process}
567 implementation is used).
569 @itemize @bullet
570 @item @code{availableProcessors()} -- Returns the number of processors 
571 available to the VM.
572 @item @code{freeMemory()} -- Returns the amount of memory the VM has available
573 on the heap for allocating.
574 @item @code{totalMemory()} -- Returns the size of the heap.
575 @item @code{maxMemory()} -- Returns the maximum memory block the VM will
576 attempt to allocate.  May be simply @code{Long.MAX_VALUE} (8 exabytes!)
577 @item @code{gc()} -- Allows users to explicitly invoke the garbage collector.
578 This is a suggestion to the VM, rather than a command, and the garbage
579 collector should run anyway @emph{without} it being invoked.
580 @item @code{runFinalization()} -- Like the above, but related to the
581 finalilzation of objects rather than the garbage collector.
582 @item @code{runFinalizationForExit()} -- Called immediately prior to VM
583 shutdown in order to finalize all objects (including `live' ones)
584 @item @code{traceInstructions(boolean)} -- This turns on and off the optional
585 VM functionality of printing a trace of executed bytecode instructions.
586 @item @code{traceMethodCalls(boolean)} -- This turns on and off the optional
587 VM functionality of printing a trace of methods called.
588 @item @code{runFinalizersOnExit(boolean)} -- A toggleable setting for
589 running the finalization process at exit.
590 @item @code{exit(int)} -- The VM should shutdown with the specified exit code.
591 @item @code{nativeLoad(String,ClassLoader)} -- Attempts to load a file,
592 returning an integer which is non-zero for success.  Nothing happens if the
593 file has already been loaded.
594 @item @code{mapLibraryName(String)} -- The VM should map the system-independent
595 library name supplied to the platform-dependent equivalent (e.g. a @code{.so}
596 or @code{.dll} file)
597 @end itemize
599 @node java.lang.VMString, java.lang.VMThread, java.lang.VMRuntime, java.lang
600 @subsection @code{java.lang.VMString}
601 @code{VMString} is responsible for handling interned strings.  If two strings
602 are equal (using the @code{equals()} method), then the results of calling
603 the @code{intern()} method on each of them makes them equal
604 (using @code{==}).  Thus, the same string object is always returned by
605 @code{intern} if the two strings are equal.  The default implementation
606 is Java-based and implements @code{intern(String)} by maintaining a
607 @code{WeakHashMap} which links the strings to their @code{WeakReference}.
608 A new mapping is created for each new string being @code{intern}ed.  
609 A VM may implement this differently by implementing this method,
610 which is @code{static} and the only one in @code{VMString}.
612 @node java.lang.VMThread,java.lang.VMInstrumentationImpl, java.lang.VMString, java.lang
613 @subsection @code{java.lang.VMThread}
615 @code{VMThread} provides the link between Java's threads and the platform
616 threading support.  A @code{VMThread} is created via a private constructor
617 and linked to a @code{Thread} instance.  This occurs when the @code{Thread}
618 instance is started by the static @code{create(Thread,long)} method (the second
619 argument requests a certain stack size, usually zero).  The thread itself is
620 executed via the @code{run()} method, which handles any problems with the
621 running of the thread and its eventual death.
623 @code{VMThread} provides the following accessors and mutators for accessing
624 the thread state via @code{VMThread},
626 @itemize @bullet
627 @item @code{getName()}
628 @item @code{setName(String)}
629 @item @code{getPriority()}
630 @item @code{setPriotity(int)}
631 @item @code{isDaemon()}
632 @end itemize
634 all of which refer to the @code{Thread} instance. @code{setPriority(int)} also
635 calls the appropriate native method.  @code{stop(Throwable)} similarly wraps
636 a native method, merely adding in a check for the state of the thread.
638 The default implementation also provides Java-based implementations of
639 @code{join(long,int)}, @code{sleep(long,int)} and
640 @code{holdsLock(Object)}.  @code{join} and @code{sleep} simply wait for
641 the appropriate amount of time, with @code{join} additionally waiting
642 for the thread instance to become @code{null}.  @code{holdsLock} simply
643 checks if an object is locked by the current thread by trying to invoke
644 the @code{notify} method, and catching the failing exception if this is
645 not the case.
647 The remainder of the class is a series of @code{native} methods, some of
648 which are mandatory for VM implementation and others which provide optional
649 or deprecated functionality.
651 @itemize @bullet
652 @item Mandatory Instance Methods
653 @itemize @bullet
654 @item @code{start(long)} -- The VM should create the native thread and start
655 it running using the @code{run} method of the @code{VMThread} instance on
656 which this method is called.
657 @item @code{interrupt()} -- The VM should interrupt the running thread and
658 throw an appropriate exception.
659 @item @code{isInterrupted()} -- Checks the interrupted state of the thread.
660 @item @code{suspend()} -- The thread should be suspended until resumed.
661 @item @code{resume()} -- The thread should be resumed from its suspended state.
662 This pair of methods are deprecated, due to the possibility of a deadlock
663 occuring when a thread with locks is suspended.
664 @item @code{nativeSetPriority(int)} -- Called by @code{setPriority}
665 to allow the setting to flow down to the native thread.
666 @item @code{nativeStop(Throwable)} -- The VM should stop the thread abnormally
667 and throw the specified exception.  This is clearly deprecated, due to the
668 ambiguous state an abruptly-stopped thread may leave.
669 @end itemize
670 @item Mandatory Class Methods
671 @itemize @bullet
672 @item @code{currentThread()} -- Return a reference to the thread currently
673 being executed.
674 @item @code{yield()} -- The VM should allow some other thread to run.
675 The current thread maintains its locks even though it stops executing for
676 the time being.
677 @item @code{interrupted()} -- A shortcut to obtaining the interrupted state
678 of the current thread.
679 @end itemize
680 @item Other Methods
681 @itemize @bullet
682 @item @code{countStackFrames()} -- Returns a count of the number of stack
683 frames in the thread.  This depends on the deprecated method @code{suspend()}
684 having returned true, and is thus deprecated as a result.
685 @end itemize
686 @end itemize
688 @node java.lang.VMInstrumentationImpl, java.lang.VMMath, java.lang.VMThread, java.lang
689 @subsection @code{java.lang.VMInstrumentationImpl}
691 The @code{java.lang.VMInstrumentationImpl} and
692 @code{java.lang.InstrumentationImpl} classes provide an implementation of the
693 @code{java.lang.instrument.Instrument} interface. This interface is for java
694 1.5 and is only in the generics branch.
695 A @code{InstrumentationImpl} object should be created by the VM when agents
696 are given in the command line (see the @code{java.lang.instrument} package
697 documentation). The VM has to set the static field
698 @code{VMClassLoader.instrumenter} to this object. The VM should implement the
699 static native methods of the @code{VMInstrumentationImpl} class.
701 @itemize @bullet
702 @item @code{isRedefineClassesSupported()} -- Returns true if the JVM supports
703 class redefinition.
704 @item @code{redefineClasses()} -- Gives a set of classes with new bytecodes.
705 The VM must redefine the classes by reading the new bytecodes.
706 @item @code{getAllLoadedClass()} -- Returns an array of all loaded classes.
707 @item @code{getInitiatedClass()} -- Returns an array of all classes loaded
708 by a specific class loader.
709 @item @code{getObjectSize()} -- Gives the size of an object.
710 @end itemize
712 Instrumentation allows to modify the bytecode of a class before it gets read
713 by the VM. In GNU Classpath, the @code{ClassLoader.defineClass} method calls
714 the @code{VMClassLoader.defineClassWithTransformers} method which first checks
715 if @code{VMClassLoader.instrumenter} is @code{null}. If it's the case, it
716 directly calls @code{VMClassLoader.defineClass}. If it's not the case, the
717 method calls at first the @code{InstrumentationImpl.callTransformers} method,
718 which calls each transformer registered to the @code{InstrumentationImpl}
719 object and returns a new bytecode array. Then, it calls the
720 @code{VMClassLoader.defineClass} method with this new bytecode array.
722 The second use of instrumentation is to redefine a class after it has been
723 loaded by the VM. This is done in the Java application by calling the
724 @code{Instrumentation.redefineClasses} method of the standard interface on
725 a @code{Instrumentation} object. The @code{InstrumentationImpl.redefineClasses}
726 method calls the @code{VMInstrumentationImpl.redefineClasses} native method
727 which must be implemented by the VM. The implementation should call the
728 @code{InstrumentationImpl.callTransformers} method.
730 @node java.lang.VMMath, , java.lang.VMInstrumentationImpl, java.lang
731 @subsection @code{java.lang.VMMath}
733 The @code{VMMath} class provides a series of native methods
734 for some of the mathematical functions present in @code{java.lang.Math}.
735 Classpath provides a default implementation of these which maps the
736 functions to those provided by @code{fdlibm}.  VM implementors are welcome
737 to replace this with more efficent implementations, as long as the accuracy
738 contract of these methods, specified in @code{java.lang.Math}, is maintained.
740 @itemize @bullet
741 @item 1.0
742 @itemize @bullet
743 @item @code{sin(double)} -- Returns the sine value for the given angle.
744 @item @code{cos(double)} -- Returns the cosine value for the given angle.
745 @item @code{tan(double)} -- Returns the tangent value for the given angle.
746 @item @code{asin(double)} -- Returns the arc sine value for the given angle.
747 @item @code{acos(double)} -- Returns the arc cosine value for the given angle.
748 @item @code{atan(double)} -- Returns the arc tangent value for the given angle.
749 @item @code{atan2(double,double)} -- Returns the arc tangent of the ratio of
750 the two arguments.
751 @item @code{exp(double)} -- Returns the exponent raised to the given power.
752 @item @code{log(double)} -- Returns the natural logarithm for the given value.
753 @item @code{sqrt(double)} -- Returns the square root of the value.
754 @item @code{pow(double,double)} -- Returns x to the power of y.
755 @item @code{IEEEremainder(double,double)} -- Returns the IEEE 754 remainder
756 for the two values.
757 @item @code{ceil(double)} -- Returns the nearest integer >= the value.
758 @item @code{floor(double)} -- Returns the nearest integer <= the value.
759 @item @code{rint(double)} -- Returns the nearest integer or the even one
760 if the distance between the two is equal.
761 @end itemize
762 @item 1.5
763 @itemize @bullet
764 @item @code{cbrt(double)} -- Returns the cube root of the value.
765 @item @code{cosh(double)} -- Returns the hyperbolic cosine value for the given
766 angle.
767 @item @code{expm1(double)} -- Returns the exponent of the value minus one.
768 @item @code{hypot(double,double)} -- Returns the hypotenuse corresponding to
769 x and y.
770 @item @code{log10(double)} -- Returns the base 10 logarithm of the given value.
771 @item @code{log1p(double)} -- Returns the natural logarithm of the value plus
772 one.
773 @item @code{sinh(double)} -- Returns the hyperbolic sine value for the given
774 angle.
775 @item @code{tanh(double)} -- Returns the hyperbolic tangent value for the given angle.
776 @end itemize
777 @end itemize
779 @node gnu.classpath, java.util, java.lang, Classpath Hooks
780 @section @code{gnu.classpath}
782 The @code{gnu.classpath} package provides Classpath-specific functionality,
783 primarily relating to the features in @code{java.lang}.  At present, this
784 includes the context of a class (the stack) and the system properties.
786 @menu
787 * gnu.classpath.VMStackWalker::
788 * gnu.classpath.VMSystemProperties::
789 @end menu
791 @node gnu.classpath.VMStackWalker,gnu.classpath.VMSystemProperties,gnu.classpath,gnu.classpath
792 @subsection @code{gnu.classpath.VMStackWalker}
794 @code{VMStackWalker} provides access to the class context or stack.  The
795 default implementation consists of a @code{native} @code{static} method,
796 @code{getClassContext()}, which obtains the class context, and two helper
797 methods which obtain the calling class (the 3rd element in the context array)
798 and its class loader, respectively.
800 @itemize @bullet
801 @item @code{getClassContext()} -- The VM should return an array of
802 @code{Class} objects, each of which relates to the method currently being
803 executed at that point on the stack.  Thus, the first item (index 0) is the
804 class that contains this method.
805 @item @code{getCallingClass()} -- A Java-based helper method which returns
806 the @code{Class} object which contains the method that called the method
807 accessing @code{getCallingClass()}. 
808 @item @code{getCallingClassLoader()} -- Like the last, but returning the class
809 loader of the class.
810 @end itemize
812 @node gnu.classpath.VMSystemProperties,,gnu.classpath.VMStackWalker,gnu.classpath
813 @subsection @code{gnu.classpath.VMSystemProperties}
815 @code{VMSystemProperties} allows the VM to hook into the property creation
816 process, both before and after the system properties are added by GNU
817 Classpath.  The default implementation assumes that the VM will add its
818 properties first, by making the pre-initialisation method @code{native},
819 and that the Classpath properties may then be altered by a Java-based
820 post-initialisation method.
822 As these methods are called as part of the bootstrap process, caution should
823 be used as to what classes are used, and properties should only be set
824 using @code{Properties.setProperty()}.  Specifically, I/O classes should be
825 avoided at this early stage.
827 @itemize @bullet
828 @item @code{preInit(Properties)} -- Allows the VM to add properties
829 @emph{before} the Classpath properties are added. The default implementation
830 includes a full list of properties that @emph{must} be added by the VM, but
831 additional VM-specific ones may also be added.  
832 @item @code{postInit(Properties)} -- Same as the last, but called after the
833 Classpath properties have been added.  The main purpose of this is to allow
834 the VM to alter the properties added by GNU Classpath to suit it.
835 @end itemize
837 @node java.util, java.io, gnu.classpath, Classpath Hooks
838 @section java.util
840 The @code{java.util} VM hooks provide links between the mix of functionality
841 present in that package, which includes collections, date and time handling
842 and parsing.  At present, there is only one hook, which connects GNU Classpath
843 to the timezone information provided by the underlying platform.
845 @menu
846 * java.util.VMTimeZone::
847 @end menu
849 @node java.util.VMTimeZone,,java.util,java.util
850 @subsection @code{java.util.VMTimeZone}
852 @code{VMTimeZone} joins @code{TimeZone} to the platform timezone information
853 via the static method, @code{getDefaultTimeZoneId()}.  The VM hook is
854 expected to return a @code{TimeZone} instance that represents the current
855 timezone in use by the platform.  The default implementation provides
856 this functionality for POSIX or GNU-like systems, and VMs that want this
857 functionality can keep this implementation and implement the native
858 method, @code{getSystemTimeZoneId()}.  This method is only called when
859 obtaining the timezone name from the @code{TZ} environment variable,
860 @code{/etc/timezone} and @code{/etc/localtime} all fail.  This fallback
861 mechanism also means that a system which doesn't provide the above three
862 methods, but does provide a timezone in string form, can still use this
863 implementation.
865 @node java.io, java.security, java.util, Classpath Hooks
866 @section java.io
868 The @code{java.io} package is heavily reliant on access to the I/O facilities
869 of the underlying platform.  As far as its VM hooks go, they provide two
870 areas of functionality to GNU Classpath, these being
872 @itemize @bullet
873 @item File and directory queries and manipulation
874 @item Serialization of objects
875 @end itemize
877 The first corresponds directly to most of the @code{File} class, while
878 the latter underlies the functionality provided by the
879 @code{ObjectInputStream} and @code{ObjectOutputStream}.  More low-level I/O
880 is provided by @ref{java.nio}.
882 @menu
883 * java.io.VMFile::
884 * java.io.VMObjectInputStream::
885 * java.io.VMObjectStreamClass::
886 @end menu
888 @node java.io.VMFile,java.io.VMObjectInputStream,java.io,java.io
889 @subsection @code{java.io.VMFile}
891 @code{VMFile} allows GNU Classpath's @code{File} representations to
892 probe and modify the file system using the native functions of the
893 platform.  The default implementation (which consists of both a
894 @code{VMFile} class and the native methods) is primarily UNIX-centric,
895 working with POSIX functions and assuming case-sensitive filenames,
896 without the restriction of the 8.3 format.  It consists mainly of
897 @code{static} @code{native} methods, with a few Java helper methods.
898 The native methods represent the file as a string containing its path,
899 rather than using the object itself.
901 @itemize @bullet
902 @item Native Methods
903 @itemize @bullet
904 @item @code{lastModified(String)} -- The native method should return a
905 @code{long} value that represents the last modified date of the file.
906 @item @code{setReadOnly(String)} -- Sets the file's permissions to read only,
907 in whichever way this is realised by the platform.
908 @item @code{create(String)} -- Create the named file.
909 @item @code{list(String)} -- The native method opens the named directory,
910 reads the contents and returns them as a Java @code{String} array.
911 @item @code{renameTo(String,String)} -- Renames the first file to the second.
912 @item @code{length(String)} -- Returns a @code{long} value representing
913 the file size.
914 @item @code{exists(String)} -- Tests for the existence of the named file
915 or directory.
916 @item @code{delete(String)} -- Deletes the file or directory.
917 @item @code{setLastModified(String,long)} -- Change the last modified time.
918 @item @code{mkdir(String)} -- Creates the named directory.
919 @item @code{isFile(String)} -- Tests that the named path references a file.
920 @item @code{canWrite(String)} -- Tests that the file can be written to.
921 This method is @code{synchronized}, so the object is locked during the check.
922 @item @code{canRead(String)} -- Complement of the last method.
923 @item @code{isDirectory(String)} -- Tests that the named path references
924 a directory.
925 @end itemize
926 @item Java Helper Methods
927 @itemize @bullet
928 @item @code{canWriteDirectory(File)} -- Checks that the directory can be
929 written to, by trying to create a temporary file in it.
930 @item @code{listRoots()} -- Returns the root of a GNU filesystem i.e. `/'
931 in an array.
932 @item @code{isHidden(String)} -- Checks whether the file starts with `.',
933 which is how files are hidden on UNIX-style systems.
934 @item @code{getName(String)} -- Pulls the actual filename from the end of
935 the path, by breaking off the characters after the last occurrence of the
936 platform's file separator.
937 @item @code{getCanonicalForm(String)} -- This converts a UNIX path to
938 its canonical form by removing the `.' and `..' sections that occur within.
939 @end itemize
940 @end itemize
942 @node java.io.VMObjectInputStream,java.io.VMObjectStreamClass,java.io.VMFile,java.io
943 @subsection @code{java.io.VMObjectInputStream}
945 This class consists of two methods which provide functionality used in
946 deserializing an object.  @code{currentClassLoader()} provides the first
947 user-defined class loader from the class context
948 (@xref{gnu.classpath.VMStackWalker},) via a @code{PrivilegedAction}.
949 @code{allocateObject(Class,Class,Constructor)} is a @code{native} method
950 (a reference implementation is provided) which creates an object but
951 calls the constructor of another class, which is a superclass of the
952 object's class.
954 @node java.io.VMObjectStreamClass,,java.io.VMObjectInputStream,java.io
955 @subsection @code{java.io.VMObjectStreamClass}
957 @code{VMObjectStreamClass} is a series of @code{static} @code{native}
958 methods that provide some of the groundwork for @code{ObjectStreamClass}
959 and @code{ObjectStreamField}.  @code{hasClassInitializer(Class)} works
960 with the former, and checks for the presence of a static initializer.
961 The remaining methods are of the form @code{setXXXNative(Field,Object,XXX)}
962 and support @code{ObjectStreamField}.  One exists for each of the main types
963 (boolean, float, double, long, int, short, char, byte and object) and is used
964 to set the specified field in the supplied instance to the given value.
966 A default implementation is provided for all of them, so a VM implementation
967 is optional.
969 @node java.security, java.net, java.io, Classpath Hooks
970 @section java.security
972 The @code{java.security} package provides support for Java's security
973 architecture.  At present, @code{VMAccessController} represents the sole
974 VM hook for this.
976 @menu
977 * java.security.VMAccessController::
978 @end menu
980 @node java.security.VMAccessController,,java.security,java.security
981 @subsection @code{java.security.VMAccessController}
983 The @code{AccessController} is used to perform privileged actions.  Its
984 hook class, @code{VMAccessController}, maintains the
985 @code{AccessControlContext} and the default implementation is purely
986 Java-based.  The VM may choose to replace this with their own.
987 The methods in the reference version are as follows:
989 @itemize @bullet
990 @item @code{pushContext(AccessControlContext)} -- Adds a new context to the
991 stack for the current thread.  This is called before a privileged action
992 takes place.
993 @item @code{popContext()} -- Removes the top context from the stack.  This
994 is performed after the privileged action takes place.
995 @item @code{getContext()} -- Either derives a context based on the 
996 @code{ProtectionDomain}s of the call stack (see the next method) or returns
997 the top of the context stack.
998 @item @code{getStack()} -- Provides access to the call stack as a pair of
999 arrays of classes and method names.  The actual implementation returns
1000 an empty array, indicating that there are no permissions.
1001 @end itemize
1003 @node java.net, java.nio, java.security, Classpath Hooks
1004 @section java.net
1006 The @code{java.net} package is heavily reliant on access to the networking
1007 facilities of the underlying platform.  The VM hooks provide information
1008 about the available network interfaces, and access to lookup facilities
1009 for network addresses.
1011 @menu
1012 * java.net.VMInetAddress::
1013 * java.net.VMNetworkInterface::
1014 @end menu
1016 @node java.net.VMInetAddress,java.net.VMNetworkInterface,java.net,java.net
1017 @subsection @code{java.net.VMInetAddress}
1019 @code{VMInetAddress} is a series of @code{static} @code{native} methods
1020 which provide access to the platform's lookup facilities.  All the methods
1021 are implemented by GNU Classpath, making VM implementation optional, and
1022 are as follows:
1024 @itemize @bullet
1025 @item @code{getLocalHostname()} -- Wraps the @code{gethostname} function, and
1026 falls back on `localhost'.
1027 @item @code{lookupInaddrAny()} -- Returns the value of @code{INADDR_ANY}.
1028 @item @code{getHostByAddr(byte[])} -- Looks up the hostname based on an IP
1029 address.
1030 @item @code{getHostByName(String)} -- The reverse of the last method, it
1031 returns the IP addresses which the given host name resolves to.
1032 @end itemize
1034 @node java.net.VMNetworkInterface,,java.net.VMInetAddress,java.net
1035 @subsection @code{java.net.VMNetworkInterface}
1037 @code{VMNetworkInterface} currently consists of a single @code{static}
1038 @code{native} method, @code{getInterfaces()}, which retrieves the
1039 network interfaces available on the underlying platform as a @code{Vector}.
1040 The current GNU Classpath implementation is a native stub.
1042 @node java.nio, java.nio.channels, java.net, Classpath Hooks
1043 @section java.nio
1045 The @code{java.nio} package is part of the New I/O framework added in
1046 Java 1.4.  This splits I/O into the concepts of @emph{buffers},
1047 @emph{charsets}, @emph{channels} and @emph{selectors}, and
1048 @code{java.nio} defines the buffer classes.  As far as native and VM
1049 code is concerned, the new package needs support for low-level efficient
1050 buffer operations.
1052 @menu
1053 * java.nio.VMDirectByteBuffer::
1054 @end menu
1056 @node java.nio.VMDirectByteBuffer,,java.nio,java.nio
1057 @subsection @code{java.nio.VMDirectByteBuffer}
1059 A @code{ByteBuffer} maintains a buffer of bytes, and allows it to be
1060 manipulated using primitive operations such as @code{get}, @code{put},
1061 @code{allocate} and @code{free}.  A direct buffer avoids intermediate
1062 copying, and uses native data which shouldn't be manipulated by a
1063 garbage collector.  The VM class consists of @code{static} @code{native}
1064 methods, all of which are given default implementations by GNU
1065 Classpath.
1067 @itemize @bullet
1068 @item @code{init()} -- Creates an instance of an appropriate
1069 @code{gnu.classpath.RawData} class.  This class is not garbage
1070 collected, is created natively and is used in the other methods to reference
1071 the buffered data.
1072 @item @code{allocate(int)} -- Allocates the memory for the buffer using
1073 @code{malloc} and returns a reference to the @code{RawData} class.
1074 @item @code{free(RawData)} -- Frees the memory used by the buffer.
1075 @item @code{get(RawData,int)}  -- Returns the data at the specified index.
1076 @item @code{get(RawData,int,byte[],int,int)} -- Copies a section of the
1077 data into a byte array using @code{memcpy}.
1078 @item @code{put(RawData,int,byte)} -- Puts the given data in the buffer
1079 at the specified index.
1080 @item @code{adjustAddress(RawData,int)} -- Adjusts the pointer into the buffer.
1081 @item @code{shiftDown(RawData,int,int,int)} -- Moves the content of the buffer
1082 at an offset down to a new offset using @code{memmove}.
1083 @end itemize
1085 @node java.nio.channels, gnu.java.nio, java.nio, Classpath Hooks
1086 @section java.nio.channels
1088 Channels provide the data for the buffers with the New I/O packages.
1089 For example, a channel may wrap a file or a socket.  The VM hooks,
1090 at the moment, simply allow the channels to be accessed by @code{java.io}
1091 streams.
1093 @menu
1094 * java.nio.channels.VMChannels::
1095 @end menu
1097 @node java.nio.channels.VMChannels,,java.nio.channels,java.nio.channels
1098 @subsection @code{java.nio.channels.VMChannels}
1100 @code{VMChannels} provides the methods that create the channels or
1101 streams.  The default implementation is in pure Java and simply wraps
1102 the channels in standard I/O classes from @code{java.io}.
1104 @itemize @bullet
1105 @item @code{createStream(Class,Channel)} -- Creates a @code{FileChannel}
1106 which wraps an instance of the specified stream class, created by reflection.
1107 This method is private, and is used by the other two.
1108 @item @code{newInputStream(ReadableByteChannel)} -- Wraps the channel
1109 in a @code{FileInputStream}.
1110 @item @code{newOutputStream(WritableByteChannel)} -- Wraps the channel
1111 in a @code{FileOutputStream}.
1112 @end itemize
1114 @node gnu.java.nio, Classpath Callbacks, java.nio.channels, Classpath Hooks
1115 @section gnu.java.nio
1117 The @code{gnu.java.nio} class provides Classpath implementations of the
1118 interfaces provided by @code{java.nio}.  The VM classes provide the native
1119 support necessary to implement @emph{pipes} and @emph{selectors}.
1121 @menu
1122 * gnu.java.nio.VMPipe::
1123 * gnu.java.nio.VMSelector::
1124 @end menu
1126 @node gnu.java.nio.VMPipe,gnu.java.nio.VMSelector,gnu.java.nio,gnu.java.nio
1127 @subsection @code{gnu.java.nio.VMPipe}
1129 @code{VMPipe} provides the native functionality for a uni-directional pipe
1130 between a source and a destination (sink) channel.  It consists of one 
1131 @code{static} @code{native} method, @code{init(PipeImpl,SelectorProvider)},
1132 the reference implementation of which is currently a native stub.  Ideally,
1133 this should initialise the pipe at the native level.
1135 @node gnu.java.nio.VMSelector,,gnu.java.nio.VMPipe,gnu.java.nio
1136 @subsection @code{gnu.java.nio.VMSelector}
1138 A @code{Selector} selects between multiple @code{SelectableChannel}s based
1139 on their readiness and a key set.  The VM hook for the Classpath implementation
1140 of this is @code{VMSelector}, and this allows the actual @code{select()}
1141 operation to be performed.  This is represented by the @code{static}
1142 @code{native} method, @code{select(int[],int[],int[],long)}, and a default
1143 implementation of this is provided.
1145 @node Classpath Callbacks, , gnu.java.nio, Classpath Hooks
1146 Some of the classes you implement for the VM will need to call back to
1147 package-private methods in Classpath:
1149 @itemize @bullet
1150 @item @code{java.lang.ThreadGroup.addThread(Thread)}
1151 Call this method from @code{Thread} when a new @code{Thread} is created, to add it to
1152 the group.
1154 @item @code{java.lang.ThreadGroup.removeThread(Thread)}
1155 Call this method from @code{Thread} when a @code{Thread} is stopped or destroyed.
1156 @end itemize
1158 @node VM Hooks, JNI Implementation, Classpath Hooks, Top
1159 @comment node-name, next, previous, up
1160 @chapter VM Hooks
1162 VMs need to do some dirty work; there are some things in the VM that
1163 unfortunately are dependent on the internal structure of various
1164 classes.  This is a guide to all of the things the VM itself needs to
1165 know about classes.
1167 Some of the core classes, while being implemented by GNU Classpath,
1168 provide space for state (in the form of a @code{vmdata} object) to be
1169 stored by the VM, and can not be constructed normally.
1171 @itemize @bullet
1172 @item java.lang.Class
1173 @item java.lang.ClassLoader
1174 @end itemize
1176 The default implementations of some VM classes also follow this methodology,
1177 when it is intended that most VMs will keep the default.
1179 @itemize @bullet
1180 @item java.lang.VMThread
1181 @item java.lang.VMThrowable
1182 @end itemize
1184 Several core classes must be completely implemented by the VM for Classpath to
1185 work, although reference implementations are provided.  These classes are:
1187 @itemize @bullet
1188 @item java.lang.reflect.Constructor
1189 @item java.lang.reflect.Method
1190 @item java.lang.reflect.Field
1191 @end itemize
1193 The following issues are of note;
1195 @itemize @bullet
1196 @item @code{java.lang.Class} @*
1197 The GNU Classpath implementation of @code{java.lang.Class} provides an
1198 object for storing the internal state of the class maintained by the VM.
1199 This is the only known place where this matters.  The class is
1200 constructed with this data by the VM.  Some VMs do not create the
1201 @code{Class} object at the point where the class is defined; instead,
1202 they wait until a @code{Class} object is actually used.
1204 @item Array Classes @*
1205 When you are creating an array class, you should set the
1206 @code{ClassLoader} of the array class to the @code{ClassLoader} of its
1207 component type.  Whenever you add a class to a @code{ClassLoader}, you
1208 need to notify the @code{ClassLoader} and add the new @code{Class} to
1209 its internal cache of classes.  To do this, call
1210 @code{ClassLoader.addVMCreatedClass(Class)}.  @emph{Note: this is
1211 written in anticipation of 1.2 support and does not apply just yet.}
1213 @item Primordial Class Loader @*
1214 When the primordial class loader loads a class, it needs to tell
1215 Classpath what it has done in order for security stuff to work right.
1216 To do this, call the static method
1217 @code{ClassLoader.newPrimordialClass(Class)}.
1219 Even the first few core classes need to do this; in order to do it,
1220 simply call this method @emph{after} the initial class loading has been
1221 done.  No harm will come, as long as you follow the guidelines in the
1222 @pxref{Initialization} section.
1224 @emph{Note: this is written in anticipation of 1.2 support and does not
1225 apply just yet.}
1227 @item Top-level Exception Handler @*
1228 Exceptions take care of themselves in Classpath; all you need to do in
1229 the top-level exception handler is call @code{Throwable.printStackTrace()}.
1231 @item Security and Traces @*
1232 There will eventually be a feature in the 1.2 security that keeps the
1233 @code{AccessController} from having to evaluate @emph{all} of the
1234 @code{ProtectionDomain}s every time a security check is made.  I think a common
1235 case is a single method doing a lot of things that require security
1236 checks.  However, I don't want to bog down the method stack too much, so
1237 this feature of the VM will have the @code{AccessController} for a thread
1238 calling out to the VM to tell it how high it was on the stack when it
1239 made the last security request.  Every time the stack goes lower than
1240 that number, the VM will decrement the number.  The @code{AccessController}
1241 will remember what the accumulated protection status was at every stack
1242 level (an @code{AccessControlContext}) and use that aggregated information to
1243 do the check.  I am not sure, however, whether the savings are
1244 substantial enough to outweigh the integer check and set after every
1245 method call.  I will investigate.
1247 @item Threading @*
1248 I figured I'd put this here because a VM guy might be wondering about it.
1249 We implement @code{ThreadGroup}, but that class is almost entirely
1250 VM-independent.  The root @code{ThreadGroup}, a static field called
1251 @code{ThreadGroup.root}, should be initialized by Classpath, but if you wish to
1252 reinitialize it yourself, there should be no harm.
1254 @end itemize
1256 @node JNI Implementation, Miscellaneous VM Requirements, VM Hooks, Top
1257 @comment  node-name,  next,  previous,  up
1258 @chapter JNI Implementation
1260 Classpath comes with its own implementation of @file{jni.h}.  This
1261 file can be customized by the VM in a few ways, by defining macros
1262 that affect the interpretation of the file.  These macros are all
1263 intended for use by a VM which uses GNU Classpath and which wants to
1264 use a single copy of @file{jni.h} for both internal and external use.
1266 @itemize @bullet
1267 @item _CLASSPATH_VM_JNI_TYPES_DEFINED
1268 Some VMs like to define JNI ``object'' types in a special way.  If
1269 this macro is defined, the Classpath @file{jni.h} will avoid defining
1270 these types.  By default, these types are defined in @file{jni.h}.
1271 The full list of types and macros treated this way is: @samp{jobject},
1272 @samp{jclass}, @samp{jstring}, @samp{jthrowable}, @samp{jweak},
1273 @samp{jarray}, @samp{jobjectArray}, @samp{jbyteArray},
1274 @samp{jshortArray}, @samp{jintArray}, @samp{jlongArray},
1275 @samp{jbooleanArray}, @samp{jcharArray}, @samp{jfloatArray},
1276 @samp{jdoubleArray}, @samp{JNIEnv}, @samp{JavaVM}, @samp{JNI_TRUE}
1277 (macro), @samp{JNI_FALSE} (macro).
1279 @item _CLASSPATH_VM_INTERNAL_TYPES_DEFINED
1280 If the VM has its own definitions for @samp{jfieldID} and
1281 @samp{jmethodID}, then it should define this macro.  Otherwise,
1282 @file{jni.h} will provide definitions for these types.
1284 @item _CLASSPATH_JNIIMPEXP
1285 Three functions -- @samp{JNI_GetDefaultJavaVMInitArgs},
1286 @samp{JNI_CreateJavaVM}, and @samp{JNI_GetCreatedJavaVMs} -- must be
1287 marked as @samp{JNIIMPORT} when seen by user code, but most likely
1288 should be marked as @samp{JNIEXPORT} when defined in the VM
1289 implementation.  This macro can be defined to one or the other by the
1290 VM as appropriate.  If this macro is not defined, it defaults to
1291 @samp{JNIIMPORT}.
1293 @item _CLASSPATH_JNIENV_CONTENTS
1294 A VM can add fields to the @samp{JNIEnv} structure by defining this to
1295 be a sequence of field declarations.
1297 @end itemize
1300 @node Miscellaneous VM Requirements,  , JNI Implementation, Top
1301 @comment  node-name,  next,  previous,  up
1302 @chapter Miscellaneous VM Requirements
1304 Classpath places a few requirements on the VM that uses it.
1306 @menu
1307 * JNI Version::                 
1308 * VM Threading Model::          
1309 * Boot Library Path Property::
1310 @end menu
1312 @node JNI Version, VM Threading Model, Miscellaneous VM Requirements, Miscellaneous VM Requirements
1313 @comment  node-name,  next,  previous,  up
1314 @section JNI Version
1316 Classpath currently uses only JNI 1.1, except for one JNI 1.2 function
1317 in the JNI Invocation API: GetEnv().  And GetEnv() is only used in the
1318 ``portable native sync'' code, so it's only actually used by Jikes RVM
1319 and Kaffe.  
1321 A future direction will probably be to require that all VMs provide
1322 JNI 1.2.  If this poses problems, please raise them on the classpath
1323 mailing list. 
1325 @node VM Threading Model, Boot Library Path Property, JNI Version, Miscellaneous VM Requirements
1326 @comment  node-name,  next,  previous,  up
1327 @section VM Threading Model
1329 Classpath's AWT peers use GTK+.  GTK+ uses GLIB.  Normally, Classpath
1330 will initialize GLIB's @dfn{gthreads} to use
1331 the platform's native threading model@footnote{The native threading
1332 model is pthreads on Linux and AIX, the two platforms Classpath
1333 currently runs on.}
1335 If the Java runtime doesn't use the native threading model, then you
1336 will want Classpath to tell GLIB to use the Java threading primitives
1337 instead.  Otherwise, GLIB would use the native threading model to
1338 perform operations such as creating thread-local data, and that just
1339 doesn't work on systems (such as Kaffe in some configurations, and
1340 such as Jikes RVM) that use @i{m}:@i{n} threading.
1342 Historically, enabling the Java threading primitives had been done at
1343 build time, by configuring classpath with the
1344 @option{--portable-native-sync} option.  This had bad consequences,
1345 though -- it meant that the prebuild GNU Classpath package distributed
1346 with Debian GNU/Linux would not be usable with VMs that could
1347 otherwise have used it.  Instead, we encourage
1348 the use of the Java system property
1349 @code{gnu.classpath.awt.gtk.portable.native.sync}.  A VM that wants
1350 GLIB to use the Java threading primitives should modify
1351 @code{VMRuntime.insertSystemProperties()} to include code like the
1352 following:
1354 @example
1355 static void insertSystemProperties(Properties @var{p}) 
1356 @end example
1358 @example
1359 @var{p}.put("gnu.classpath.awt.gtk.portable.native.sync", "true");
1360 @end example
1362 So, the configure option
1363 @option{--portable-native-sync} is deprecated, and should go away in a
1364 subsequent release of GNU Classpath.
1366 @node Boot Library Path Property,  , VM Threading Model, Miscellaneous VM Requirements
1367 @comment  node-name,  next,  previous,  up
1368 @section Boot Library Path Property
1370 As of GNU Classpath 0.15 a system property named @code{gnu.classpath.boot.library.path}
1371 can be set by the VM to specify the directories which contain GNU Classpath's native
1372 libraries. Usually this value is given at configuration time and is then hardcoded
1373 in the VM. However for development purposes it is handy to switch to another installation
1374 by overriding the properties' value on the command line.
1376 A VM that does not support this feature can simply ignore the property.
1378 For compatibility reasons we suggest to set the default value of @code{java.library.path}
1379 to the value of the @code{LD_LIBRARY_PATH} environment if it exists on your platform.
1381 @bye