Java Runtime subclass identification

  Java Image API (Reflection API) and Java interface for the preparation of reusable code provides excellent tools.    To a generic order starter example: Suppose you have a group of the implementation of the various tasks, such as closed or open Electric Company, opened, closed or Suoshangmen, and so on.    These categories are the names of LightOn, LightOff, DoorOpen, DoorClose and DoorLock, all of these categories to realize Command Interface. 

  Command Interface is defined as follows: 

  (Public interface Command 
  Public void process (); 
  ) 

  You can prepare a simple universal starter, as follows: 

  (Public class Launcher 
  Public static void main (String [] args) ( 
  If (args.length> 0) ( 
  Try ( 
  Command command = 
  (Command) Class.forName (args [0]). NewInstance (); 
  Command.process (); 
  ) Catch (Exception ex) ( 
  System.out.println ( "Invalid command"); 
  ) 
  Else () 
  System.out.println ( "Usage: Launcher    "); 
  ) 
  ) 
  / / Launcher 

  This procedure was used method Class.forName parameters specified in the category of Class object, and then use newInstance () method to create such an example.    Upon request, such realization of the Command Interface, the program targets stereotypes (cast) become Command, and then calling process () method, process approaches to the implementation of practical tasks.    If the anomaly, for example, because of the name spelling errors or security issues, will display an "Invalid command" information. 

  This command can be activated for use in the following way: 

  % Java Launcher LightOn 

  If, after a number of new tasks, orders starter not to be revised.    From the programmer's point of view, this is actually very good.    However, it is also for the user?    Assumes that a user enter the following commands: 

  % Java Launcher OpenDoor 
  Invalid command 

  "Invalid command" means that users can not open the door?    No, it only said that naming the wrong category (DoorOpen into the OpenDoor).    Therefore, the procedure should be available to allow users to view the list of orders.    Starter order to ensure that the general, users should be able to find them in the running order. 

  Java image in the run-time API to provide a large number of the designated categories of information: We can be easily informed of all the ultra-designated category, it achieved interface, method, constructor function, Victoria, and so on.    But here, we are interested in all types of achieving specific interface, such information can not be obtained directly Java Image API.    On the remaining part of this paper for you on how to get the interface to achieve a specific category of information. 

  In Java, a package corresponding directory, through the File object list () method in the package include access to all categories is very easy.    Our approach is to use instanceof statement inspection: For every package inside a class paper, the corresponding realization of the Command of the interface.    This means that only one type of document for each of the common type, and interface and it must be achieved in a bag inside.    Below is the code: 

  Public static void find (String pckgname) ( 
  / / The package into absolute path name 
  String name = new String (pckgname); 
  If (! Name.startsWith ("/")) ( 
  Name = "/" + name; 
  ) 
  Name = name.replace ('.','/'); 

  / / Get a File object 
  URL url = Launcher.class.getResource (name); 
  File directory = new File (url.getFile ()); 

  If (directory.exists ()) ( 
  / / Get list of documents inside packets 
  String [] = directory.list files (); 
  For (int i = 0; I <files.length; i + +) ( 

  / / We only right. Class interested in the paper 
  If (files [i]. EndsWith (. "Class")) ( 
  / / Delete. Class file extension 
  String classname [i] = files. Substring (0, files [i]. Length () -6); 
  Try ( 
  / / Attempt to create an instance of the object 
  Object o = Class.forName (pckgname +"."+ classname). NewInstance (); 
  If (o instanceof Command) ( 
  System.out.println (classname); 
  ) 
  ) Catch (ClassNotFoundException cnfex) ( 
  System.err.println (cnfex); 
  ) Catch (InstantiationException iex) ( 
  / / We are trying to examples of an interface or 
  / / Default constructor is not a target 
  ) Catch (IllegalAccessException iaex) ( 
  / / Instead of such common type 
  ) 
  ) 
  ) 
  ) 
  ) 

  To perform the tasks at hand, we need only slightly amend the original starter.    Now, we can envisage the realization of its interface and commands in a package inside: 

  Public static void main (String [] args) ( 
  If (args.length> 0) ( 
  Try ( 
  Command command = (Command) Class.forName ( "commands." + 
  Args [0]). NewInstance (); 
  Command.process (); 
  ) Catch (Exception ex) ( 
  System.out.println ( "Invalid command"); 
  System.out.println ( "Available commands:"); 
  Find (the "commands"); 
  ) 
  Else () 
  System.out.println ( "Usage: Launcher <command>"); 
  ) 
  ) 

  Below is the implementation of the wrong order, improved after the start of the results show that: 

  % Java Launcher OpenDoor 
  Invalid command 
  Available commands: 
  LightOn 
  LightOff 
  DoorOpen 
  DoorClose 
  DoorLock 

  We can amend find () method, it will be able to find any of the categories specified category.    To this end, we use the dynamic version of the instanceof that isInstance ().    By (tosubclass.isInstance (o)) Replacement (o instanceof Command), which is tosubclass find () method specified in the parameters of the class.    We now have a method, which can identify specific package of any type of designated categories.    We can refine the methodology, it has been loaded in the current package to find subclass.    To this end, we use to Package.getPackages () method, this method accurate to return to the current category loaded with various loading packages.    Then, we need only call for each packet find () method: 

  Public static void find (String tosubclassname) ( 
  Try ( 
  Class tosubclass = Class.forName (tosubclassname); 
  Package = [] pcks Package.getPackages (); 
  For (int i = 0; I <pcks.length; i + +) ( 
  Find (pcks [i]. GetName (), tosubclass); 
  ) 
  ) Catch (ClassNotFoundException ex) ( 
  System.err.println ( "Class" tosubclassname + + "not found!"); 
  ) 
  ) 

  This method relies mainly on the return of results in the time it has been called.    For this paper, the generic launch order, call find () method fashion into memory only a small number of packages.    For instance, here is my call on NT machines find () before the loaded package: 

  Package java.util.zip, Java Platform API Specification, version 1.3 
  Package java.security, Java Platform API Specification, version 1.3 
  Package java.io, Java Platform API Specification, version 1.3 
  Package sun.net.www.protocol.file, Java Platform API Specification, version 1.3 
  Package sun.net.www.protocol.jar, Java Platform API Specification, version 1.3 
  Package sun.net.www, Java Platform API Specification, version 1.3 
  Package java.util.jar, Java Platform API Specification, version 1.3 
  Package sun.security.action, Java Platform API Specification, version 1.3 
  Package java.lang, Java Platform API Specification, version 1.3 
  Package sun.io, Java Platform API Specification, version 1.3 
  Package java.util, Java Platform API Specification, version 1.3 
  Package sun.misc, Java Platform API Specification, version 1.3 
  Package java.security.cert, Java Platform API Specification, version 1.3 
  Package java.lang.reflect, Java Platform API Specification, version 1.3 
  Package java.net, Java Platform API Specification, version 1.3 
  Package sun.security.util, Java Platform API Specification, version 1.3 
  Package java.lang.ref, Java Platform API Specification, version 1.3 
  Package sun.security.provider, Java Platform API Specification, version 1.3 
  Package com.sun.rsajca 

  In order starter, the interface and all of its realization in the same package inside, into categories can be after the package has been loaded.    Therefore, we will be able to search inside the packet subclass.    This is related to find the only way to pack.    RTSI integrity of the source code may be the last in this paper to find the reference resources.    Untie packages downloaded ZIP compression, you can use the following command test code: 

  % Java-cp classes RTSI commands.Command 

  When the packet to the operating system in the form of files and directories exist, the code can be discussed in front of a smooth operation, but if the class files in a jar or more inside the paper, the code is no longer valid.    In this paper, download the code, you will find the problem a solution.    You can use the following command procedures jar test the ability of the paper: 

  % Java-jar RTSI.jar commands.Command 

  â–  concluding remarks in the article, we discussed how, in a designated packet inside (or already loaded packages) dynamically extract all designated categories of categories.    This feature not only for the design of common procedures useful, but, as the orders start of this example shows, its users the same benefits. 

  The author stated that: some readers pointed out, this procedure can only be detected with the default constructor function subclass.    They suggested the use Class isAssignableFrom () method alternative isInstance (). 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply