⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hacking.texinfo

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 TEXINFO
📖 第 1 页 / 共 5 页
字号:
feel like you have to do it perfect right away or that contributionsaren't welcome if they aren't ``perfect''.  We all learn by doing andinteracting with each other.@node Programming Goals, API Compatibility, Hacking Code, Top@comment node-name, next, previous, up@chapter Programming GoalsWhen you write code for Classpath, write with three things in mind, andin the following order: portability, robustness, and efficiency.If efficiency breaks portability or robustness, then don't do it theefficient way.  If robustness breaks portability, then bye-bye robustcode.  Of course, as a programmer you would probably like to find sneakyways to get around the issue so that your code can be all three ... thefollowing chapters will give some hints on how to do this.@menu* Portability::                 Writing Portable Software                * Utility Classes::             Reusing Software* Robustness::                  Writing Robust Software               * Java Efficiency::             Writing Efficient Java            * Native Efficiency::           Writing Efficient JNI          * Security::                    Writing Secure Software@end menu@node Portability, Utility Classes, Programming Goals, Programming Goals@comment node-name, next, previous, up@section PortabilityThe portability goal for Classpath is the following:@enumerate@itemnative functions for each platform that work across all VMs on thatplatform@itema single classfile set that work across all VMs on all platforms thatsupport the native functions.@end enumerateFor almost all of Classpath, this is a very feasible goal, using acombination of JNI and native interfaces.  This is what you should shootfor.  For those few places that require knowledge of the Virtual Machinebeyond that provided by the Java standards, the VM Interface was designed.Read the Virtual Machine Integration Guide for more information.Right now the only supported platform is Linux.  This will change as thatversion stabilizes and we begin the effort to port to many otherplatforms.  Jikes RVM runs Classpath on AIX, and generally the JikesRVM team fixes Classpath to work on that platform. @node Utility Classes, Robustness, Portability, Programming Goals@comment  node-name,  next,  previous,  up@section Utility ClassesAt the moment, we are not very good at reuse of the JNI code.  Therehave been some attempts, called @dfn{libclasspath}, tocreate generally useful utility classes.  The utility classes are inthe directory @file{native/jni/classpath} and they are mostly declaredin @file{native/jni/classpath/jcl.h}.  These utility classes arecurrently only discussed in @ref{Robustness} and in @ref{NativeEfficiency}.There are more utility classes available that could be factored out ifa volunteer wants something nice to hack on.  The error reporting andexception throwing functions and macros in@file{native/jni/gtk-peer/gthread-jni.c} might be goodcandidates for reuse.  There are also some generally useful utilityfunctions in @file{gnu_java_awt_peer_gtk_GtkMainThread.c} that couldbe split out and put into libclasspath.@node Robustness, Java Efficiency, Utility Classes, Programming Goals@comment node-name, next, previous, up@section RobustnessNative code is very easy to make non-robust.  (That's one reason Java isso much better!)  Here are a few hints to make your native code morerobust.Always check return values for standard functions.  It's sometimes easyto forget to check that malloc() return for an error.  Don't make thatmistake.  (In fact, use JCL_malloc() in the jcl library instead--it willcheck the return value and throw an exception if necessary.)Always check the return values of JNI functions, or call@code{ExceptionOccurred} to check whether an error occurred.  You mustdo this after @emph{every} JNI call.  JNI does not work well when anexception has been raised, and can have unpredictable behavior.Throw exceptions using @code{JCL_ThrowException}.  This guarantees that ifsomething is seriously wrong, the exception text will at least get outsomewhere (even if it is stderr).Check for null values of @code{jclass}es before you send them to JNI functions.JNI does not behave nicely when you pass a null class to it: itterminates Java with a "JNI Panic."In general, try to use functions in @file{native/jni/classpath/jcl.h}.  Theycheck exceptions and return values and throw appropriate exceptions.@node Java Efficiency, Native Efficiency, Robustness, Programming Goals@comment node-name, next, previous, up@section Java EfficiencyFor methods which explicitly throw a @code{NullPointerException} when anargument is passed which is null, per a Sun specification, do not writecode like:@exampleint strlen (String foo) throws NullPointerException@{  if (foo == null)    throw new NullPointerException ("foo is null");  return foo.length ();@}@end exampleInstead, the code should be written as:@exampleintstrlen (String foo) throws NullPointerException@{  return foo.length ();@}@end exampleExplicitly comparing foo to null is unnecessary, as the virtual machinewill throw a NullPointerException when length() is invoked.  Classpathis designed to be as fast as possible -- every optimization, no matterhow small, is important.@node Native Efficiency, Security, Java Efficiency, Programming Goals@comment node-name, next, previous, up@section Native EfficiencyYou might think that using native methods all over the place would giveour implementation of Java speed, speed, blinding speed.  You'd bethinking wrong.  Would you believe me if I told you that an empty@emph{interpreted} Java method is typically about three and a half times@emph{faster} than the equivalent native method?Bottom line: JNI is overhead incarnate.  In Sun's implementation, eventhe JNI functions you use once you get into Java are slow.A final problem is efficiency of native code when it comes to thingslike method calls, fields, finding classes, etc.  Generally you shouldcache things like that in static C variables if you're going to use themover and over again.  GetMethodID(), GetFieldID(), and FindClass() are@emph{slow}.  Classpath provides utility libraries for caching methodIDsand fieldIDs in @file{native/jni/classpath/jnilink.h}.  Other native data canbe cached between method calls using functions found in@file{native/jni/classpath/native_state.h}.Here are a few tips on writing native code efficiently:Make as few native method calls as possible.  Note that this is not thesame thing as doing less in native method calls; it just means that, ifgiven the choice between calling two native methods and writing a singlenative method that does the job of both, it will usually be better towrite the single native method.  You can even call the other two nativemethods directly from your native code and not incur the overhead of amethod call from Java to C.Cache @code{jmethodID}s and @code{jfieldID}s wherever you can.  Stringlookups are expensive.  The best way to do this is to use the @file{native/jni/classpath/jnilink.h}library.  It will ensure that @code{jmethodID}s are always valid, even if theclass is unloaded at some point.  In 1.1, jnilink simply caches a@code{NewGlobalRef()} to the method's underlying class; however, when 1.2 comesalong, it will use a weak reference to allow the class to be unloadedand then re-resolve the @code{jmethodID} the next time it is used.Cache classes that you need to access often.  jnilink will help withthis as well.  The issue here is the same as the methodID and fieldIDissue--how to make certain the class reference remains valid.If you need to associate native C data with your class, use PaulFisher's native_state library (NSA).  It will allow you to get and setstate fairly efficiently.  Japhar now supports this library, makingnative state get and set calls as fast as accessing a C variabledirectly.If you are using native libraries defined outside of Classpath, thenthese should be wrapped by a Classpath function instead and definedwithin a library of their own.  This makes porting Classpath's nativelibraries to new platforms easier in the long run.  It would be niceto be able to use Mozilla's NSPR or Apache's APR, as these librariesare already ported to numerous systems and provide all the necessarysystem functions as well.@node Security,  , Native Efficiency, Programming Goals@comment  node-name,  next,  previous,  up@section SecuritySecurity is such a huge topic it probably deserves its own chapter.Most of the current code needs to be audited for security to ensureall of the proper security checks are in place within the Javaplatform, but also to verify that native code is reasonably secure andavoids common pitfalls, buffer overflows, etc.  A good source forinformation on secure programming is the excellent HOWTO by DavidWheeler,@uref{http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/index.html,SecureProgramming for Linux and Unix HOWTO}.@node API Compatibility, Specification Sources, Programming Goals, Top@comment  node-name,  next,  previous,  up@chapter API Compatibility@menu* Serialization::               Serialization* Deprecated Methods::          Deprecated methods@end menu@node Serialization, Deprecated Methods, API Compatibility, API Compatibility@comment  node-name,  next,  previous,  up@section SerializationSun has produced documentation concerning much of the informationneeded to make Classpath serializable compatible with Sunimplementations.  Part of doing this is to make sure that every classthat is Serializable actually defines a field named serialVersionUIDwith a value that matches the output of serialver on Sun'simplementation.  The reason for doing this is below.If a class has a field (of any accessibility) named serialVersionUIDof type long, that is what serialver uses. Otherwise it computes avalue using some sort of hash function on the names of all methodsignatures in the .class file.  The fact that different compilerscreate different synthetic method signatures, such as access$0() if aninner class needs access to a private member of an enclosing class,make it impossible for two distinct compilers to reliably generate thesame serial #, because their .class files differ. However, once youhave a .class file, its serial # is unique, and the computation willgive the same result no matter what platform you execute on.Serialization compatibility can be tested using tools provided with@uref{http://www.kaffe.org/~stuart/japi/,Japitools}.  Thesetools can test binary serialization compatibility and also provideinformation about unknown serialized formats by writing these in XMLinstead.  Japitools is also the primary means of checking APIcompatibility for GNU Classpath with Sun's Java Platform.@node Deprecated Methods,  , Serialization, API Compatibility@comment  node-name,  next,  previous,  up@section Deprecated MethodsSun has a practice of creating ``alias'' methods, where a public orprotected method is deprecated in favor of a new one that has the samefunction but a different name.  Sun's reasons for doing this vary; asan example, the original name may contain a spelling error or it maynot follow Java naming conventions.Unfortunately, this practice complicates class library code that callsthese aliased methods.  Library code must still call the deprecatedmethod so that old client code that overrides it continues to work.But library code must also call the new version, because new code isexpected to override the new method.The correct way to handle this (and the way Sun does it) may seemcounterintuitive because it means that new code is less efficient thanold code: the new method must call the deprecated method, and throughoutthe library code calls to the old method must be replaced with calls tothe new one.Take the example of a newly-written container laying out a component andwanting to know its preferred size.  The Component class has adeprecated preferredSize method and a new method, getPreferredSize. Assume that the container is laying out an old component that overridespreferredSize and a new component that overrides getPreferredSize.  Ifthe container calls getPreferredSize and the default implementation ofgetPreferredSize calls preferredSize, then the old component will haveits preferredSize method called and new code will have itsgetPreferredSize method called.Even using this calling scheme, an old component may still be laid outimproperly if it implements a method, getPreferredSize, that has thesame signature as the new Component.getPreferredSize.  But that is ageneral problem -- adding new public or protected methods to awidely-used class that calls those methods internally is risky, becauseexisting client code may have already declared methods with the samesignature.The solution may still seem counterintuitive -- why not have thedeprecated method call the new method, then have the library always callthe old method?  One problem with that, using the preferred size exampleagain, is that new containers, which will use the non-deprecatedgetPreferredSize, will not get the preferred size of old components.@node Specification Sources, Naming Conventions, API Compatibility, Top@comment node-name, next, previous, up@chapter Specification SourcesThere are a number of specification sources to use when working onClasspath.  In general, the only place you'll find your classesspecified is in the JavaDoc documentation or possibly in thecorresponding white paper.  In the case of java.lang, java.io andjava.util, you should look at the Java Language Specification.Here, however, is a list of specs, in order of canonicality:@enumerate@item@uref{http://java.sun.com/docs/books/jls/clarify.html,Clarifications and Amendments to the JLS - 1.1}@item@uref{http://java.sun.com/docs/books/jls/html/1.1Update.html,JLS Updates- 1.1}@item@uref{http://java.sun.com/docs/books/jls/html/index.html,The 1.0 JLS}@item@uref{http://java.sun.com/docs/books/vmspec/index.html,JVM spec - 1.1}@item@uref{http://java.sun.com/products/jdk/1.1/docs/guide/jni/spec/jniTOC.doc.html,JNI spec - 1.1}@item@uref{http://java.sun.com/products/jdk/1.1/docs/api/packages.html,Sun's javadoc - 1.1}(since Sun's is the reference implementation, the javadoc is

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -