Understanding and Analyzing Java Reflection YUE LI',UNSW Sydney,Australia TIAN TAN',UNSW Sydney,Australia JINGLING XUE,UNSW Sydney,Australia Java reflection has been widely used in a variety of applications and frameworks.It allows a software system to inspect and change the behaviour of its classes,interfaces,methods and fields at runtime,enabling the software to adapt to dynamically changing runtime environments.However,this dynamic language feature imposes significant challenges to static analysis,because the behaviour of reflection-rich software is logically complex and statically hard to predict.As a result,existing static analysis tools either ignore reflection or handle it partially,resulting in missed,important behaviours,i.e.,unsound results.Therefore,improving or even achieving soundness in(static)reflection analysis-an analysis that infers statically the behaviour of reflective code-will provide significant benefits to many analysis clients,such as bug detectors,security analyzers and program verifiers. In this paper,we provide a comprehensive understanding of Java reflection through examining its underlying concept,API and real-world usage,and,building on this,we introduce a new static approach to resolving Java reflection effectively in practice.We have implemented our reflection analysis in an open-source tool,called SoLAR,and evaluated its effectiveness extensively with large Java programs and libraries.Our experimental results demonstrate that SoLAR is able to(1)resolve reflection more soundly than the state-of-the-art reflection analyses:(2)automatically and accurately identify the parts of the program where reflection is resolved unsoundly or imprecisely:and(3)guide users to iteratively refine the analysis results by using lightweight annotations until their specific requirements are satisfied. CCS Concepts:·Theory of computation→Program analysis;:·Software and its engineering→ Object oriented languages. Additional Key Words and Phrases:Java reflection,static analysis,points-to analysis ACM Reference Format: Yue Li,Tian Tan,and Jingling Xue.2019.Understanding and Analyzing Java Reflection.ACM Trans.Softw. Eng.Methodol.28,2,Article 7(February 2019),51 pages.https://doi.org/10.1145/nnnnnnn.nnnnnnn 1 INTRODUCTION Java reflection allows a software system to inspect and change the behaviour of its classes,interfaces, methods and fields at runtime,enabling the software to adapt to dynamically changing runtime environments.This dynamic language feature eases the development and maintenance of Java programs in many programming tasks by,for example,facilitating their flexible integration with the third-party code and their main behaviours to be configured according to a deployed runtime "This work was mostly done while these authors were at UNSW Sydney.Both authors are now affiliated with Aarhus University,Denmark. Authors'addresses:Yue Li,Aarhus University,Denmark,yueli@cs.au.dk;Tian Tan,Aarhus University,Denmark, tiantan@cs.au.dk;Jingling Xue,UNSW Sydney,Australia,jingling@cs.au.dk. Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page.Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted.To copy otherwise,or republish,to post on servers or to redistribute to lists,requires prior specific permission and/or a fee.Request permissions from permissions@acm.org. 2019 Association for Computing Machinery. 1049-331X/2019/02-ART7$15.00 https://doi.org/10.1145/nnnnnnn.nnnnnnn ACM Trans.Softw.Eng.Methodol.,Vol.28,No.2,Article 7.Publication date:February 2019.7 Understanding and Analyzing Java Reflection YUE LI∗ , UNSW Sydney, Australia TIAN TAN∗ , UNSW Sydney, Australia JINGLING XUE, UNSW Sydney, Australia Java reflection has been widely used in a variety of applications and frameworks. It allows a software system to inspect and change the behaviour of its classes, interfaces, methods and fields at runtime, enabling the software to adapt to dynamically changing runtime environments. However, this dynamic language feature imposes significant challenges to static analysis, because the behaviour of reflection-rich software is logically complex and statically hard to predict. As a result, existing static analysis tools either ignore reflection or handle it partially, resulting in missed, important behaviours, i.e., unsound results. Therefore, improving or even achieving soundness in (static) reflection analysis—an analysis that infers statically the behaviour of reflective code—will provide significant benefits to many analysis clients, such as bug detectors, security analyzers and program verifiers. In this paper, we provide a comprehensive understanding of Java reflection through examining its underlying concept, API and real-world usage, and, building on this, we introduce a new static approach to resolving Java reflection effectively in practice. We have implemented our reflection analysis in an open-source tool, called Solar, and evaluated its effectiveness extensively with large Java programs and libraries. Our experimental results demonstrate that Solar is able to (1) resolve reflection more soundly than the state-of-the-art reflection analyses; (2) automatically and accurately identify the parts of the program where reflection is resolved unsoundly or imprecisely; and (3) guide users to iteratively refine the analysis results by using lightweight annotations until their specific requirements are satisfied. CCS Concepts: • Theory of computation → Program analysis; • Software and its engineering → Object oriented languages. Additional Key Words and Phrases: Java reflection, static analysis, points-to analysis ACM Reference Format: Yue Li, Tian Tan, and Jingling Xue. 2019. Understanding and Analyzing Java Reflection. ACM Trans. Softw. Eng. Methodol. 28, 2, Article 7 (February 2019), 51 pages. https://doi.org/10.1145/nnnnnnn.nnnnnnn 1 INTRODUCTION Java reflection allows a software system to inspect and change the behaviour of its classes, interfaces, methods and fields at runtime, enabling the software to adapt to dynamically changing runtime environments. This dynamic language feature eases the development and maintenance of Java programs in many programming tasks by, for example, facilitating their flexible integration with the third-party code and their main behaviours to be configured according to a deployed runtime ∗This work was mostly done while these authors were at UNSW Sydney. Both authors are now affiliated with Aarhus University, Denmark. Authors’ addresses: Yue Li, Aarhus University, Denmark, yueli@cs.au.dk; Tian Tan, Aarhus University, Denmark, tiantan@cs.au.dk; Jingling Xue, UNSW Sydney, Australia, jingling@cs.au.dk. Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from permissions@acm.org. © 2019 Association for Computing Machinery. 1049-331X/2019/02-ART7 $15.00 https://doi.org/10.1145/nnnnnnn.nnnnnnn ACM Trans. Softw. Eng. Methodol., Vol. 28, No. 2, Article 7. Publication date: February 2019