Find the class name from the. Class File

Source: Internet
Author: User


Find the class name from the. Class File
Rdfei translation (Participation score: 2879, expert score: 280) published: Version: 1.0 read:3826Times

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.