asm 最新版手册

所需积分/C币:28 2018-10-22 10:52:05 1.28MB PDF

ASM 是一个 Java 字节码操控框架。它能被用来动态生成类或者增强既有类的功能。ASM 可以直接产生二进制 class 文件,也可以在类被加载入 Java 虚拟机之前动态改变类行为。Java class 被存储在严格格式定义的 .class 文件里,这些类文件拥有足够的元数据来解析类中的所有元素:类名称、方法、属性以及 Java 字节码(指令)。ASM 从类文件中读入信息后,能够改变类行为,分析类信息,甚至能够根据用户要求生成新类
Contents ntroductio 1.1. Motivations 1. 2. Overview 1.2.1 copel 2.2. Model 1. 2.3. Architecturel 3. Organization 1223446 1. 4. Acknowledgments I. Core APl 2. Classes 2.1. Structurel 799 Oⅴ erview 2.1.2. Internal names 2. 1.3. Type descriptors 2. 1.4. Method descriptors 2.2. Interfaces and component 2.2. 1. Presentation 12 2.2.2. Parsing classes 14 2.2.3. Generating classes 2. 2.4. Transforming classes 2.2.5. Removing class members 2.2.6. Adding class members 2.2.7. Transformation chains 25 2.3. Tools 2. pe 26 2.3.2. TraceClassvisitorl 2.3.3. CheckClassAdapter 2. 3.4. ASMifier 28 Contents 3. Methods 31 1. Structure 31 3.1 Execution model 31 3.1.2. Bytecode instructions 3.1.3. Examples 35 3. 1.4. Exception handlers 3.1.5. Frames 3. 2. Interfaces and components 41 3. 2. 1. Presentation 3.2.2. Generating methods 3.2.3. Transforming method 46 3.2.4. Stateless transformations 3.2.5. Statefull transformations 52 3.3. Tools 3.3.1. Basic tools 58 3.3.2. AnalyzerAdapter 61 3.3.3. Localvariablessorter 63 B.3.4. AdviceAdapter 65 4. Metadata 67 4.1. Generics 67 4.1.1. Structure 67 4. 1.2. Interfaces and components 69 4.1.3. TOols 4.2. Annotations 4.2.1. Structure 4. 2.2. Interfaces and components 73 4.2.3. Tools 76 4. 3. Debug 4.3. 1. Structure 4.3. 2. Interfaces and components 4.3,10o 5. Backward compatibility 81 5.1. Introductionl 5.1. 1. Backward compatibility contract 82 5. 1.2. An example ⑤.2. Guidelines 5.2. 1. Basic rule Contents 5.2.2. Inheritance rule 山, Tree AP 89 6. Classes 91 6.1. Interfaces and components 6.1.1. Presentation 91 6. 2. Generating classes 6.1.3. Adding and removing class members 93 6.2. Components composition 6.2. 1. Presentation ⑥.2.2. Patterns 7. Methods 101 7. 1. Interfaces and components 101 7. 1.1. Presentation 101 1.2.Generating methods .103 7.1.3. Transforming methods 105 7. 1.4. Stateless and statefull transformations 7.1.5. Global transformations .109 7.2. Components composition 112 7. 2.1. Presentation 112 7.2.2. Patterns 112 B. Method Analysis引 115 8.1. Presentation 8.1.1. Data How analyses 115 8.1.2. Control How analyses 117 8.2. Interfaces and components 117 8.2.1. Basic data flow analysis 8.2.2. Basic data Hlow verifier 120 8.2.3. Simple data flow verifier .120 8. 2.4. User defined data flow analysis 12 8. 2.5. Control How analysis ..125 9. Metadata 127 9.1. Generic 127 19.2. Annotations .127 Contents .3. Debug 128 10. Backward compatibility 129 10.1. Introduction 129 10.2. Guidelines 129 10.2. 1. Basic rules 129 10.2.2. Inheritance rules 132 10.2.3. Other packages 133 风A. Appendix× 135 A 1. Bytecode instructions 135 A. 2. Subroutines 139 A. 3. Attributes 141 A. 4. Guidelines 143 LA.5. Performances 145 ndex 147 1. Introduction 11. Motivations Program analysis, generation and transformation are useful techniques that can be used in many situations Program analysis, which can range from a simple syntaxic parsing to a full semantic analysis, can be used to find potential bugs in applications to detect unused code. to reverse engineer code, etc Program generation is used in compilers. This include traditional com pilers, but also stub or skeleton compilers used for distributed program- ming, Just in Time compilers, etc Program transformation can be used to optimize or obfuscate programs to insert debugging or performance monitoring code into applications for aspect oriented programming, etc All these techniques can be used for any programming language but this is more or less easy to do, depending on the language. In the case of Java they can be used on Java source code or on compiled Java classes. One of the advantages of working on compiled classes is that, obviously, the source code is not needed. Program transformations can therefore be used on any applications, including closed source and commercial ones. Another advantage of working on compiled code is that it becomes possible to analyze, generate or transform classes at runtime, just before they are loaded into the Java virtual Machine(generating and compiling source code at runtime is possible, but this is really slow and requires a full Java compiler). The advantage is that tools such as stub compilers or aspect weavers become transparent to users Due to the many possible usages of program analysis, generation and transfor- mation techniques, many tools to analyze, generate and transform programs have been implemented, for many languages, Java included. ASM is one of these tools for the java language designed for runtime- but also offine - class 1. Introduction generation and transformation. The ASM library was therefore designed to work on compiled Java classes. It was also designed to be as fast and as small as possible. Being as fast as possible is important in order not to slow down too much the applications that use Asm at runtime, for dynamic class gener- ation or transformation. And being as small as possible is important in order to be used in memory constrained environments, and to avoid bloating the size of small applications or libraries using ASM ASM is not the only tool for generating and transforming compiled Java classes but it is one of the most recent and efficient It can be downloaded fromhttp://asm.objectweb.orgitSmainadvantagesarethefollowing It has a simple, well designed and modular api that is easy to use It is well documented and has an associated eclipse plugin provides support for the latest Java version, Java 7 It is small, fast, and very robust Its large user community can provide support for new users Its open source license allows you to use it in almost any way you want 1.2. Overview 1.2. 1. Scope The goal of the ASM library is to generate transform and analyze compiled ava classes, represented as byte arrays(as they are stored on disk and loaded in the Java Virtual Machine). For this purpose ASM provides tools to read write and transform such byte arrays by using higher level concepts than bytes such as numeric constants, strings, Java identifiers, Java types, Java class structure elements, etc. Note that the scope of the AsM library is strictly limited to reading, writing transforming and analyzing classes. In particular the class loading process is out of scope the asm name does not mean anything: it is just a reference to the__asm_ keyword in C, which allows some functions to be implemented in assembly language 1.2. Overvie 1.2.2.Mod The Asm library provides two aPis for generating and transforming compiled classes: the core aPi provides an event based representation of classes, while the tree aPi provides an object based representation With the event based model a class is represented with a sequence of events each event representing an element of the class, such as its header, a field, a method declaration. an instruction, etc. The event based api defines the set of possible events and the order in which they must occur, and provides a class parser that generates one event per element that is parsed, as well as a class writer that generates compiled classes from sequences of such events With the object based model a class is represented with a tree of objects, each object representing a part of the class, such as the class itself, a field, a method an instruction, etc. and each object having references to the objects that represent its constituents. The object based Api provides a way to convert a sequence of events representing a class to the object tree representing the same class and, vice versa, to convert an object tree to the equivalent event sequence. In other words the object based API is built on top of the event based api These two APIs can be compared to the Simple API for XML(SAX) and Document Object Model (DoM) APIs for XML documents: the event based API is similar to SAX, while the object based API is similar to DOM. The object based APi is built on top of the event based one, like DOm can be provided on top of saX asm provides both aPis because there is no best API. Indeed each APi has s own advantages and drawbacks The event based API is faster and requires less memory than the object based aPi, since there is no need to create and store in memory a tree of objects representing the class(the same difference also exists between SAX and DOm) However implementing class transformations can be more difficult with the event based API, since only one element of the class is available at any given time(the element that corresponds to the current event) while the whole class is available in memory with the object based APi ote that the two APIs manage only one class at a time, and independently of the others: no information about the class hierarchy is maintained, and if a 1. Introduction class transformation affects other classes, it is up to the user to modify these other classes 1.2.3. Architecture ASM applications have a strong architectural aspect. Indeed the event based API is organized around event producers(the class parser, event consumers (the class writer) and various predefined event filters, to which user defined producers, consumers and filters can be added. Using this APi is therefore a wo step process assembling event producer, filter and consumer components into possibly complex architectures and then starting the event producers to run the generation or transfor- mation process The object based APi also has an architectural aspect: indeed class generator or transformer components that operate on object trees can be composed, the links between them representing the order of transformations Although most component architectures in typical Asm applications are quite simple, it is possible to imagine complex architectures like the following, where arrows represent event based or object based communications between class parsers, writers or transformers, with possible conversions between the event based and object based representations anywhere in the chain CR CR 1.3. Organization The AsM library is organized in several packages that are distributed in several jar files


评论 下载该资源后可以进行评论 1

cf2005fly 清晰很有帮助谢谢
  • 分享王者


关注 私信 TA的资源