java 反射 static_使用ABAP的RTTI和Java反射机制访问static private属性

In ABAP we can define a static attribute for a class via keyword CLASS-DATA, whose validity is not associated with instances of a class but with the class itself. In order to prove this fact I use the following simple Pointer class for demonstration:

class ZCL_POINT definition

public

final

create public .public section.

data X type I .

methods CONSTRUCTOR

importing

!IV_X type I

!IV_Y type I .private section.

data Y type I .

class-data COUNT type I .ENDCLASS.CLASS ZCL_POINT IMPLEMENTATION.

method CONSTRUCTOR.

me->x = iv_x.

me->y = iv_y.

count = count + 1.

endmethod.ENDCLASS.

In this class, static attribute count is responsible to maintain the number of created Point instances. Then create four point instances:

data(a) = new zcl_point( iv_x = 1 iv_y = 1 ).data(b) = new zcl_point( iv_x = 1 iv_y = 2 ).data(c) = new zcl_point( iv_x = 1 iv_y = 3 ).data(d) = new zcl_point( iv_x = 1 iv_y = 4 ).

Via any variable of a, b, c or d, we can monitor the value of count in debugger.

606913681d3ff325e4018018b7e7a126.png

Can we access the static attribute of a class without object instance in debugger?

Since in theory the static attribute belongs to class instead of any dedicated object instance, so question comes: is there approach to monitor the static attribute value in ABAP debugger directly from class instead? Yes it is possible.

(1) type text “{C:ZCL_POINT} in debugger and press enter key

adcfe00b7c144901e2552be61115bab1.png

(2) double click, and you can see the attribute value is directly maintained in class ZCL_POINT, without any object instance created on top of it.

d7f3c5374ba7c745c898185c799ba446.png

And I try to change its visibility dynamically via class descriptor via the following code and actually it is not possible:

data(lo) = CAST cl_abap_objectdescr( cl_abap_classdescr=>describe_by_name( 'ZCL_POINT' ) ).read TABLE lo->attributes ASSIGNING FIELD-SYMBOL() WITH KEY name = 'COUNT'.CHECK SY-SUBRC = 0.-visibility = 'U'.

Since the structure is read-only and not editable outside cl_abap_objectdescr.

6e42f3ca97650b04940a39f2d49f0ebc.png

eb25c8b78d2d06c9554b2acc928af218.png

This makes sense otherwise the encapsulation will be violated. Just check many other attribute marked as read-only in Class/Object descriptor class.

e30754eff340958401c13ceb030e97da.png

Reflection in Java

Check the following code which demonstrates how to access private static attribute value in code via Reflection.

import java.lang.reflect.Field;public class Point {

private int x;

private int y;

static private int count = 0;

public Point(int x, int y){

this.x = x;

this.y = y;

count++;

}

private static void accessStaticPrivate(Point point){

Class classObject = point.getClass();

try {

Field countField = classObject.getDeclaredField("count");

System.out.println("count: " + countField.get(point));

} catch (NoSuchFieldException | SecurityException | IllegalArgumentException

| IllegalAccessException e1 ) {

e1.printStackTrace();

}

}

public static void main(String[] arg){

Point a = new Point(1,2);

accessStaticPrivate(a);

Point b = new Point(1,3);

accessStaticPrivate(b);

Point c = new Point(1,4);

accessStaticPrivate(c);

Point d = new Point(1,5);

accessStaticPrivate(d);

}}

For ABAPer it is easy to understand the usage of Class object in Java by just comparing it with CL_ABAP_CLASSDESCR in ABAP. When running this small program locally, you will get output in console:

count: 1count: 2count: 3count: 4

Unlike RTTI in ABAP, Java reflection can sometimes lead to

security issues, see one example how Java Singleton would be bypassed in blog

Singleton bypass – ABAP and Java.

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

90adae986e1d8e7f9d5a5d6a431d178e.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值