Agent-based application development must face the issues related to the interactions among agents. In fact, their sociality allows decomposing large applications into collaborating agents, while open environments, such as the Internet, require agents belonging to different applications to compete to gain resources. In the BRAIN framework, interactions among agents are fruitfully modeled and implemented on the basis of roles. This approach achieves several advantages, from separation of concerns between the algorithmic issues and the interaction issues, to the reuse of solutions and experiences in different applications. In this paper we propose a mechanism to enable Java agents to dynamically assume roles at runtime. Our approach is based on the modification of the bytecode of Java agents, in order to implement an appropriate interface and to add the related methods. An application example and the comparison with other approaches show the effectiveness of our approach.