Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use reflection to get an interface in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Java how to use reflection to obtain an interface, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

/**

* * * @param target * @return */public static ArrayList> subclasses = Lists.newArrayList(); try { //determine if the class object is an interface if (target.isInterface()) { @NotNull String basePackage = target.getClassLoader().getResource("").getPath(); File[] files = new File(basePackage).listFiles(); //list of class paths ArrayList classpaths = Lists.newArrayList(); for (File file : files) { //scan all compiled classes of the project if (file.isDirectory()) { listPackages(file.getName(), classpaths); } } //Get all classes, and then determine if they are implementation classes of target interface for (String classpath : classpaths) { Class classObject = Class.forName(classpath); if (classObject.getSuperclass() == null) continue; //Determine whether the parent class of the object is null Set object array//The data contained in this array is a subclass of the SophonInit interface//ArrayList

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report