Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the fully qualified name of a .class file

Hey, I think the title sums it, but still.

I need to extract the fully qualified name of an object from its compiled .class file, could anyone point me in the right direction?

Thanks,
Adam.

like image 953
TacB0sS Avatar asked Jul 21 '10 12:07

TacB0sS


People also ask

How do I find my fully-qualified class name?

A fully-qualified class name in Java contains the package that the class originated from. Also, an inner class is a class that is another class member. So, the fully-qualified name of an inner class can be obtained using the getName() method.

What is a fully-qualified file name?

The term fully qualified file name (or FQFN) means a file on a computer whose exact name is completely specified such that it is unambiguous and cannot be mistaken for any other file on that computer system.

How do I get fully-qualified class name in IntelliJ?

Press Ctrl + Alt + Shift + C (Edit | Copy Reference) on "Editor" in the code and you'll have the fully-qualified name in your clipboard. Or alternatively just right-click on the code and select "Copy Reference".

What is the fully-qualified name of the String class in Java?

The fully qualified name of the type "array of array of array of array of String " is "java. lang. String[][][][] ".


3 Answers

public String getFullClassName(String classFileName) throws IOException {           
        File file = new File(classFileName);

        FileChannel roChannel = new RandomAccessFile(file, "r").getChannel(); 
        ByteBuffer bb = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int)roChannel.size());         

        Class<?> clazz = defineClass((String)null, bb, (ProtectionDomain)null);
        return clazz.getName();
    }
like image 67
Sergii Pozharov Avatar answered Sep 28 '22 06:09

Sergii Pozharov


getClass().getName()

Update: You can load the class-file into a byte[] (using standard i/o) and then use getClass().getClassLoader().defineClass(...)

like image 39
Bozho Avatar answered Sep 28 '22 04:09

Bozho


Use a library like BCEL to read the classfile into memory and query it for the class name.

like image 21
Tassos Bassoukos Avatar answered Sep 28 '22 06:09

Tassos Bassoukos