Alternate method of discover class name - TechRepublic
General discussion
April 17, 2002 at 07:25 PM
per lundholm

Alternate method of discover class name

by per lundholm . Updated 24 years, 2 months ago

Today I read in our newsletter a way of discovering the classname of a class in a .class file. The concept relied on the knowledge of the .class file format.

Since I’ve runned into the problem before, here is how I did it: subclass ClassLoader.
lStorage is a byte array loaded with the file.

MyLoader lLoader = new MyLoader(lStorage);
iClass = lLoader.load();
return iClass.getName();

static class MyLoader extends ClassLoader {
byte[] iStorage;

public MyLoader(byte b[]) { iStorage = b;
}

Class load() {
return defineClass(null, iStorage, 0, iStorage.length);
}}

The main difference is that the class is loaded at the same time but you may be short on time and memory. In that case, the newsletter suggestion might be better.

This discussion is locked

All Comments