Assume that the user selects a class file through the GUI or through the command line, but he or she does not provide a fully valid class name. For example, for c: \ code \ hacks \ research \ com \ generationjava \ hack. Class, the correct class name is com. generationjava. Hack. You can determine the name of a class in multiple ways: Assume that all classes start with COM, org, or net. Search for a class named hack by classpath. Search for classpath to find the Directory Entry mapped to this file Directly view the class itself. In all cases, this is the most direct The Java specification specifies how to view a class file. The first value stored in the class file is the complete class description in the format of the Java internal delimiter. The followingCodeThe file c: \ code \ hacks \ research \ com \ generationjava \ hack is returned. class internal class name COM/generationjava/hack, readers can further write code to convert "/" to ". ": Static Final Public IntMagic = 0 xcafebabe; // Pass c: \ code \ hacks \ research \ com \ generationjava \ hack. Class // Assume that such a class exists. PublicString getfullyqualifiedname (string filename)ThrowsIoexception { String name = NULL; Try{ Datainputstream in =New Datainputstream (NewFileinputstream (filename )); If(In. readint ()! = Magic ){ // Not a. Class File Thrownew ioexception ("not a class file "); } In. readunsignedshort ();// Times version number In. readunsignedshort ();// Main version number In. readunsignedshort ();// Length In. readbyte ();// Class = 7 In. readunsignedshort ();// Ignore this location In. readbyte ();// Utf8 = 1 Name = in. readutf ();// Class name !!! In. Close (); }Catch(Ioexception IOE ){ IOE. printstacktrace (); } ReturnName; } When you directly obtain class information from the. Class file, the development is simplified. Finding the complete class name of an anonymous. Class class file is an important part of any. Class file-based application. |