README.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # Copyright (C) 2008 The Android Open Source Project
  2. - Description -
  3. ---------------
  4. Layoutlib_create generates a JAR library used by the Eclipse graphical layout editor to perform
  5. layout.
  6. - Usage -
  7. ---------
  8. ./layoutlib_create destination.jar path/to/android1.jar path/to/android2.jar
  9. - Design Overview -
  10. -------------------
  11. Layoutlib_create uses a few jars from the framework containing the Java code used by Android as
  12. generated by the Android build, right before the classes are converted to a DEX format.
  13. These jars can't be used directly in Eclipse as:
  14. - they contains references to native code (which we want to avoid in Eclipse),
  15. - some classes need to be overridden, for example all the drawing code that is replaced by Java 2D
  16. calls in Eclipse.
  17. - some of the classes that need to be changed are final and/or we need access to their private
  18. internal state.
  19. Consequently this tool:
  20. - parses the input JAR,
  21. - modifies some of the classes directly using some bytecode manipulation,
  22. - filters some packages and removes those we don't want in the output JAR,
  23. - injects some new classes,
  24. - generates a modified JAR file that is suitable for the Android plugin for Eclipse to perform
  25. rendering.
  26. The ASM library is used to do the bytecode modification using its visitor pattern API.
  27. The layoutlib_create is *NOT* generic. There is no configuration file. Instead all the configuration
  28. is done in the main() method and the CreateInfo structure is expected to change with the Android
  29. platform as new classes are added, changed or removed. Some configuration that may be platform
  30. dependent is also present elsewhere in code.
  31. The resulting JAR is used by layoutlib_bridge (a.k.a. "the bridge"), also part of the platform, that
  32. provides all the necessary missing implementation for rendering graphics in Eclipse.
  33. - Implementation Notes -
  34. ------------------------
  35. The tool works in two phases:
  36. - first analyze the input jar (AsmAnalyzer class)
  37. - then generate the output jar (AsmGenerator class),
  38. - Analyzer
  39. ----------
  40. The goal of the analyzer is to create a graph of all the classes from the input JAR with their
  41. dependencies and then only keep the ones we want.
  42. To do that, the analyzer is created with a list of base classes to keep -- everything that derives
  43. from these is kept. Currently the one such class is android.view.View: since we want to render
  44. layouts, anything that is sort of a view needs to be kept.
  45. The analyzer is also given a list of class names to keep in the output. This is done using
  46. shell-like glob patterns that filter on the fully-qualified class names, for example "android.*.R**"
  47. ("*" does not matches dots whilst "**" does, and "." and "$" are interpreted as-is). In practice we
  48. almost but not quite request the inclusion of full packages.
  49. The analyzer is also given a list of classes to exclude. A fake implementation of these classes is
  50. injected by the Generator.
  51. With this information, the analyzer parses the input zip to find all the classes. All classes
  52. deriving from the requested bases classes are kept. All classes whose name match the glob pattern
  53. are kept. The analysis then finds all the dependencies of the classes that are to be kept using an
  54. ASM visitor on the class, the field types, the method types and annotations types. Classes that
  55. belong to the current JRE are excluded.
  56. The output of the analyzer is a set of ASM ClassReader instances which are then fed to the
  57. generator.
  58. - Generator
  59. -----------
  60. The generator is constructed from a CreateInfo struct that acts as a config file and lists:
  61. - the classes to inject in the output JAR -- these classes are directly implemented in
  62. layoutlib_create and will be used to interface with the renderer in Eclipse.
  63. - specific methods to override (see method stubs details below).
  64. - specific methods for which to delegate calls.
  65. - specific methods to remove based on their return type.
  66. - specific classes to rename.
  67. - specific classes to refactor.
  68. Each of these are specific strategies we use to be able to modify the Android code to fit within the
  69. Eclipse renderer. These strategies are explained below.
  70. The core method of the generator is transform(): it takes an input ASM ClassReader and modifies it
  71. to produce a byte array suitable for the final JAR file.
  72. The first step of the transformation is to implement the method delegates.
  73. The TransformClassAdapter is then used to process the potentially renamed class. All protected or
  74. private classes are market as public. All classes are made non-final. Interfaces are left as-is.
  75. If a method has a return type that must be erased, the whole method is skipped. Methods are also
  76. changed from protected/private to public. The code of the methods is then kept as-is, except for
  77. native methods which are replaced by a stub. Methods that are to be overridden are also replaced by
  78. a stub.
  79. Finally fields are also visited and changed from protected/private to public.
  80. The next step of the transformation is changing the name of the class in case we requested the class
  81. to be renamed. This uses the RenameClassAdapter to also rename all inner classes and references in
  82. methods and types. Note that other classes are not transformed and keep referencing the original
  83. name.
  84. The class is then fed to RefactorClassAdapter which is like RenameClassAdapter but updates the
  85. references in all classes. This is used to update the references of classes in the java package that
  86. were added in the Dalvik VM but are not a part of the Desktop VM. The existing classes are
  87. modified to update all references to these non-desktop classes. An alternate implementation of
  88. these (com.android.tools.layoutlib.java.*) is injected.
  89. RenameClassAdapter and RefactorClassAdapter both inherit from AbstractClassAdapter which changes the
  90. class version (version of the JDK used to compile the class) to 50 (corresponding to Java 6), if the
  91. class was originally compiled with Java 7 (version 51). This is because we don't currently generate
  92. the StackMapTable correctly and Java 7 VM enforces that classes with version greater than 51 have
  93. valid StackMapTable. As a side benefit of this, we can continue to support Java 6 because Java 7 on
  94. Mac has horrible font rendering support.
  95. ReplaceMethodCallsAdapter replaces calls to certain methods. This is different from the
  96. DelegateMethodAdapter since it doesn't preserve the original copy of the method and more importantly
  97. changes the calls to a method in each class instead of changing the implementation of the method.
  98. This is useful for methods in the Java namespace where we cannot add delegates. The configuration
  99. for this is not done through the CreateInfo class, but done in the ReplaceMethodAdapter.
  100. The ClassAdapters are chained together to achieve the desired output. (Look at section 2.2.7
  101. Transformation chains in the asm user guide, link in the References.) The order of execution of
  102. these is:
  103. ClassReader -> [DelegateClassAdapter] -> TransformClassAdapter -> [RenameClassAdapter] ->
  104. RefactorClassAdapter -> [ReplaceMethodCallsAdapter] -> ClassWriter
  105. - Method stubs
  106. --------------
  107. As indicated above, all native and overridden methods are replaced by a stub. We don't have the
  108. code to replace with in layoutlib_create. Instead the StubMethodAdapter replaces the code of the
  109. method by a call to OverrideMethod.invokeX(). When using the final JAR, the bridge can register
  110. listeners from these overridden method calls based on the method signatures.
  111. The listeners are currently pretty basic: we only pass the signature of the method being called, its
  112. caller object and a flag indicating whether the method was native. We do not currently provide the
  113. parameters. The listener can however specify the return value of the overridden method.
  114. This strategy is now obsolete and replaced by the method delegates.
  115. - Strategies
  116. ------------
  117. We currently have 6 strategies to deal with overriding the rendering code and make it run in
  118. Eclipse. Most of these strategies are implemented hand-in-hand by the bridge (which runs in Eclipse)
  119. and the generator.
  120. 1- Class Injection
  121. This is the easiest: we currently inject the following classes:
  122. - OverrideMethod and its associated MethodListener and MethodAdapter are used to intercept calls to
  123. some specific methods that are stubbed out and change their return value.
  124. - CreateInfo class, which configured the generator. Not used yet, but could in theory help us track
  125. what the generator changed.
  126. - AutoCloseable and Objects are part of Java 7. To enable us to still run on Java 6, new classes are
  127. injected. The implementation for these classes has been taken from Android's libcore
  128. (platform/libcore/luni/src/main/java/java/...).
  129. - Charsets, IntegralToString and UnsafeByteSequence are not part of the Desktop VM. They are
  130. added to the Dalvik VM for performance reasons. An implementation that is very close to the
  131. original (which is at platform/libcore/luni/src/main/java/...) is injected. Since these classees
  132. were in part of the java package, where we can't inject classes, all references to these have been
  133. updated (See strategy 4- Refactoring Classes).
  134. 2- Overriding methods
  135. As explained earlier, the creator doesn't have any replacement code for methods to override. Instead
  136. it removes the original code and replaces it by a call to a specific OveriddeMethod.invokeX(). The
  137. bridge then registers a listener on the method signature and can provide an implementation.
  138. This strategy is now obsolete and replaced by the method delegates (See strategy 6- Method
  139. Delegates).
  140. 3- Renaming classes
  141. This simply changes the name of a class in its definition, as well as all its references in internal
  142. inner classes and methods. Calls from other classes are not modified -- they keep referencing the
  143. original class name. This allows the bridge to literally replace an implementation.
  144. An example will make this easier: android.graphics.Paint is the main drawing class that we need to
  145. replace. To do so, the generator renames Paint to _original_Paint. Later the bridge provides its own
  146. replacement version of Paint which will be used by the rest of the Android stack. The replacement
  147. version of Paint can still use (either by inheritance or delegation) all the original non-native
  148. code of _original_Paint if it so desires.
  149. Some of the Android classes are basically wrappers over native objects and since we don't have the
  150. native code in Eclipse, we need to provide a full alternate implementation. Sub-classing doesn't
  151. work as some native methods are static and we don't control object creation.
  152. This won't rename/replace the inner static methods of a given class.
  153. 4- Refactoring classes
  154. This is very similar to the Renaming classes except that it also updates the reference in all
  155. classes. This is done for classes which are added to the Dalvik VM for performance reasons but are
  156. not present in the Desktop VM. An implementation for these classes is also injected.
  157. 5- Method erasure based on return type
  158. This is mostly an implementation detail of the bridge: in the Paint class mentioned above, some
  159. inner static classes are used to pass around attributes (e.g. FontMetrics, or the Style enum) and
  160. all the original implementation is native.
  161. In this case we have a strategy that tells the generator that anything returning, for example, the
  162. inner class Paint$Style in the Paint class should be discarded and the bridge will provide its own
  163. implementation.
  164. 6- Method Delegates
  165. This strategy is used to override method implementations. Given a method SomeClass.MethodName(), 1
  166. or 2 methods are generated:
  167. a- A copy of the original method named SomeClass.MethodName_Original(). The content is the original
  168. method as-is from the reader. This step is omitted if the method is native, since it has no Java
  169. implementation.
  170. b- A brand new implementation of SomeClass.MethodName() which calls to a non-existing static method
  171. named SomeClass_Delegate.MethodName(). The implementation of this 'delegate' method is done in
  172. layoutlib_bridge.
  173. The delegate method is a static method. If the original method is non-static, the delegate method
  174. receives the original 'this' as its first argument. If the original method is an inner non-static
  175. method, it also receives the inner 'this' as the second argument.
  176. - References -
  177. --------------
  178. The JVM Specification 2nd edition:
  179. http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html
  180. Understanding bytecode:
  181. http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/
  182. Bytecode opcode list:
  183. http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings
  184. ASM user guide:
  185. http://download.forge.objectweb.org/asm/asm4-guide.pdf
  186. --
  187. end