正在加载图片...
7:6 Yue Li,Tian Tan,and Jingling Xue Intercession:the ability of a program to modify its own execution state or alter its own interpretation or meaning. Providing full reflective abilities as shown above is challenging in practice,as this will introduce both implementation complexities and performance problems [10].Thus,in modern programming languages like Java,reflective abilities are only partially supported [6,19]. 2.1.3 Java Reflection.Java reflection supports introspection and very limited intercession;in particular,an introspection step is usually followed by behaviour changes such as object creation, method invocation and attribute manipulation [9,19].Note that some other researchers hold a different view that Java reflection does not support intercession [6,16],as they adopt a more strict definition of intercession,which implies the ability to modify the self-representation of a program. Despite its limited reflective abilities,Java reflection is able to allow programmers to break the constraints of staticity and encapsulation,enabling the program to adapt to dynamically changing runtime environments.As a result,Java reflection has been widely used in real-world Java applications to facilitate flexibly different programming tasks,such as reasoning about control (i.e.,about which computations to pursue next)[19],interfacing (e.g.,interaction with GUIs or database systems)[21,46],and self-activation (e.g.,through monitors)[13]. Java reflection does not have a reify operation as described in Section 2.1.1(Figure 2(b))to turn the basic(running)system(including stack frames)into a representation(data structure)that is passed to a metasystem.Instead,a kind of metarepresentation,based on metaobjects,exists when the system starts running and persists throughout the execution of the system[19]. A metaobject is like the reflection in a mirror:one can adjust one's smile(behaviour changes)by looking at oneself in a mirror(introspection).In Section 2.2,we will look at how Java reflection uses metaobjects and its API to facilitate reflective programming. 1 Aa new A(); 2 String cName,mName,fName =... 3 class clz Class.forName(cName); 4 Object obj clz.newInstance(); 5 Method mtd clz.getDeclaredMethod(mName,A.class); 6 Object 1 mtd.invoke(obj,a); 7 Field fld clz.getField(fName); 8 x r (X)fld.get(a); 9 fld.set(null,a); Fig.3.An example of reflection usage in Java. 2.2 Interface We first use a toy example to illustrate some common uses of the Java reflection API(Section 2.2.1). We then delve into the details of its core methods,which are relevant to (and thus should be handled by)any reflection analysis (Section 2.2.2). 2.2.1 An Example.There are two kinds of metaobjects:Class objects and member objects.In Java reflection,one always starts with a Class object and then obtain its member objects(e.g., Method and Field objects)from the Class object by calling its corresponding accessor methods (e.g.,getMethod()and getField()). ACM Trans.Softw.Eng.Methodol.,Vol.28,No.2,Article 7.Publication date:February 2019.7:6 Yue Li, Tian Tan, and Jingling Xue • Intercession: the ability of a program to modify its own execution state or alter its own interpretation or meaning. Providing full reflective abilities as shown above is challenging in practice, as this will introduce both implementation complexities and performance problems [10]. Thus, in modern programming languages like Java, reflective abilities are only partially supported [6, 19]. 2.1.3 Java Reflection. Java reflection supports introspection and very limited intercession; in particular, an introspection step is usually followed by behaviour changes such as object creation, method invocation and attribute manipulation [9, 19]. Note that some other researchers hold a different view that Java reflection does not support intercession [6, 16], as they adopt a more strict definition of intercession, which implies the ability to modify the self-representation of a program. Despite its limited reflective abilities, Java reflection is able to allow programmers to break the constraints of staticity and encapsulation, enabling the program to adapt to dynamically changing runtime environments. As a result, Java reflection has been widely used in real-world Java applications to facilitate flexibly different programming tasks, such as reasoning about control (i.e., about which computations to pursue next) [19], interfacing (e.g., interaction with GUIs or database systems) [21, 46], and self-activation (e.g., through monitors) [13]. Java reflection does not have a reify operation as described in Section 2.1.1 (Figure 2(b)) to turn the basic (running) system (including stack frames) into a representation (data structure) that is passed to a metasystem. Instead, a kind of metarepresentation, based on metaobjects, exists when the system starts running and persists throughout the execution of the system [19]. A metaobject is like the reflection in a mirror: one can adjust one’s smile (behaviour changes) by looking at oneself in a mirror (introspection). In Section 2.2, we will look at how Java reflection uses metaobjects and its API to facilitate reflective programming. 1 A a = new A(); 2 String cName, mName, fName = ...; 3 Class clz = Class.forName(cName); 4 Object obj = clz.newInstance(); 5 Method mtd = clz.getDeclaredMethod(mName, A.class); 6 Object l = mtd.invoke(obj, a); 7 Field fld = clz.getField(fName); 8 X r = (X)fld.get(a); 9 fld.set(null, a); Fig. 3. An example of reflection usage in Java. 2.2 Interface We first use a toy example to illustrate some common uses of the Java reflection API (Section 2.2.1). We then delve into the details of its core methods, which are relevant to (and thus should be handled by) any reflection analysis (Section 2.2.2). 2.2.1 An Example. There are two kinds of metaobjects: Class objects and member objects. In Java reflection, one always starts with a Class object and then obtain its member objects (e.g., Method and Field objects) from the Class object by calling its corresponding accessor methods (e.g., getMethod() and getField()). ACM Trans. Softw. Eng. Methodol., Vol. 28, No. 2, Article 7. Publication date: February 2019
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有