正在加载图片...
7:8 Yue Li,Tian Tan,and Jingling Xue class-retrieving methods,which create Class objects,e.g.,forName()in line 3 in Figure 3. member-retrieving methods,which introspect and retrieve member metaobjects,i.e.,Method (Constructor)and Field objects from a Class object,e.g.,getDeclaredMethod()in line 5 and getField()in line 7 in Figure 3. reflective-action methods,which affect the pointer information in the program reflectively, e.g.,newInstance(),invoke(),get()and set()in lines 4,6,8 and 9 in Figure 3 for creating an object,invoking a method,accessing and modifying a field,respectively. Class-Retrieving Methods.Everything in Java reflection begins with Class objects and they are returned by calling class-retrieving methods.There are many class-retrieving methods in the Java reflection API.In Figure 4,only the four most widely used ones are listed explicitly Note that forName()(loadclass())returns a Class object representing a class that is specified by the value of its string argument.The class object returned by o.getclass()and A.class represents the dynamic type(class)of o and A,respectively. Member-Retrieving Methods.Class provides a number of accessor methods for retrieving its member metaobjects,i.e.,the Method(Constructor)and Field objects.In addition,these member metaobjects can be used to introspect the methods,constructors and fields in their target class. Formally,these accessor methods are referred to here as the member-retrieving methods. As shown in Figure 4,for each kind of member metaobjects,there are four member-retrieving methods.We take a Method object as an example to illustrate these methods,whose receiver objects are the Class objects returned by the class-retrieving methods. getDeclaredMethod(String,Class[])returns a Method object that represents a declared method of the target Class object with the name(formal parameter types)specified by the first (second)parameter (line 5 in Figure 3). getMethod(String,Class[])is similar to getDeclaredMethod(String,Class[])ex- cept that the returned Method object is public(either declared or inherited).If the target Class does not have a matching method,then its superclasses are searched first recursively (bottom-up)before its interfaces(implemented). getDeclaredMethods()returns an array of Method objects representing all the methods declared in the target Class object. getMethods()is similar to getDeclaredMethods()except that all the public methods(either declared or inherited)in the target Class object are returned. Reflective-Action Methods.As shown in Figure 4,a total of nine reflective-action methods that can possibly modify or use(as their side effects)the pointer information in a program are listed. Accordingly,Table 1 explains how these methods affect the pointer information by giving their side effects on the pointer analysis. In Figure 4,the first five reflective-action methods use four kinds of metaobjects as their receiver objects while the last four methods use Class or Array objects as their arguments.Below we briefly examine them in the order given in Table 1. The side effect of newInstance()is allocating an object with the type specified by its metaobject clz or ctor(say A)and initializing it via a constructor of A,which is the default constructor in the case of Class:newInstance()and the constructor specified explicitly in the case of Constructor:newInstance(). The side effect of invoke()is a virtual call when the first argument of invoke(),say o,is not null.The receiver object is o as shown in the "Side Effect"column in Table 1.When o is null,invoke()should be a static call. ACM Trans.Softw.Eng.Methodol,Vol.28,No.2,Article 7.Publication date:February 2019.7:8 Yue Li, Tian Tan, and Jingling Xue • class-retrieving methods, which create Class objects, e.g., forName() in line 3 in Figure 3. • member-retrieving methods, which introspect and retrieve member metaobjects, i.e., Method (Constructor) and Field objects from a Class object, e.g., getDeclaredMethod() in line 5 and getField() in line 7 in Figure 3. • reflective-action methods, which affect the pointer information in the program reflectively, e.g., newInstance(), invoke(), get() and set() in lines 4, 6, 8 and 9 in Figure 3 for creating an object, invoking a method, accessing and modifying a field, respectively. Class-Retrieving Methods. Everything in Java reflection begins with Class objects and they are returned by calling class-retrieving methods. There are many class-retrieving methods in the Java reflection API. In Figure 4, only the four most widely used ones are listed explicitly. Note that forName() (loadClass()) returns a Class object representing a class that is specified by the value of its string argument. The Class object returned by o.getClass() and A.class represents the dynamic type (class) of o and A, respectively. Member-Retrieving Methods. Class provides a number of accessor methods for retrieving its member metaobjects, i.e., the Method (Constructor) and Field objects. In addition, these member metaobjects can be used to introspect the methods, constructors and fields in their target class. Formally, these accessor methods are referred to here as the member-retrieving methods. As shown in Figure 4, for each kind of member metaobjects, there are four member-retrieving methods. We take a Method object as an example to illustrate these methods, whose receiver objects are the Class objects returned by the class-retrieving methods. • getDeclaredMethod(String, Class[]) returns a Method object that represents a declared method of the target Class object with the name (formal parameter types) specified by the first (second) parameter (line 5 in Figure 3). • getMethod(String, Class[]) is similar to getDeclaredMethod(String, Class[]) ex￾cept that the returned Method object is public (either declared or inherited). If the target Class does not have a matching method, then its superclasses are searched first recursively (bottom-up) before its interfaces (implemented). • getDeclaredMethods() returns an array of Method objects representing all the methods declared in the target Class object. • getMethods() is similar to getDeclaredMethods() except that all the public methods (either declared or inherited) in the target Class object are returned. Reflective-Action Methods. As shown in Figure 4, a total of nine reflective-action methods that can possibly modify or use (as their side effects) the pointer information in a program are listed. Accordingly, Table 1 explains how these methods affect the pointer information by giving their side effects on the pointer analysis. In Figure 4, the first five reflective-action methods use four kinds of metaobjects as their receiver objects while the last four methods use Class or Array objects as their arguments. Below we briefly examine them in the order given in Table 1. • The side effect of newInstance() is allocating an object with the type specified by its metaobject clz or ctor (say A) and initializing it via a constructor of A, which is the default constructor in the case of Class::newInstance() and the constructor specified explicitly in the case of Constructor::newInstance(). • The side effect of invoke() is a virtual call when the first argument of invoke(), say o, is not null. The receiver object is o as shown in the “Side Effect” column in Table 1. When o is null, invoke() should be a static call. ACM Trans. Softw. Eng. Methodol., Vol. 28, No. 2, Article 7. Publication date: February 2019
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有