使用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.
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
(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.
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(<count>) WITH KEY name = 'COUNT'.
CHECK SY-SUBRC = 0.
<count>-visibility = 'U'.
Since the structure is read-only and not editable outside cl_abap_objectdescr.
This makes sense otherwise the encapsulation will be violated. Just check many other attribute marked as read-only in Class/Object descriptor class.
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: 1
count: 2
count: 3
count: 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.
- 网络域名与注册商标冲突的解决途径
- 网站代码优化我们必须要做的那些事
- 真是热闹! Slade.com等多个域名被曝交易
- python编码问题之"encode"&"decode"
- python3编码问题终结者--还搞不懂你来找我
- Pycharm集成PyQt4并使用
- python遍历一个目录,输出所有文件名
- pyqt4实现tab界面切换
- 腾讯云Fintech云端系列论坛首站北京,揭秘如何全链路赋能互联网金融
- Flask入门笔记(一)
- 刷脸还是指纹识别,that's a question
- c#:使用using关键字自动释放资源未必一定就会有明显好处
- MongoDB 学习笔记(原创)
- Silverlight:ScorllViewer随Tab键自动跟随子控件的Focus滚动
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- 设计模式-七大原则(图解一目了然)
- 公平组合游戏-巴什游戏、尼姆游戏和SG函数
- 数据库应用技术系列第一讲 创建数据库和表
- 怎样让数据库再快一点?
- 大数据场景下,如何快速将Linux 大文件处理小
- Mysql系列第十六讲 变量详解
- Mysql系列第十七讲 流程控制语句(高手进阶)
- 私藏的5个好用的Pandas函数!
- 漫画:如何在数组中找到和为 “特定值” 的两个数?
- 当你在百度搜索关键字的时候,哪个网站会排在最前面?今天给大家科普一下“网站SEO”
- Mysql系列第十九讲 异常捕获及处理详解
- 【原创】经验分享:一个小小emoji尽然牵扯出来这么多东西?
- jQuery 事件对象,拷贝对象,多库共存
- jQuery 事件注册与事件处理
- v-charts 修改文本颜色