.class 파일의 정규화 된 이름을 읽는 방법
-
18-09-2020 - |
문제
헤이, 제목이 합계되었지만 여전히.
컴파일 된 파일에서 누구든지 올바른 방향으로 나를 가리킬 수 있습니까??
감사합니다,
아담.
해결책
getClass().getName()
.
업데이트 : class 파일을 byte[]
(표준 I / O 사용)로로드 한 다음 getClass().getClassLoader().defineClass(...)
다른 팁
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();
}
. BCEL과 같은 라이브러리를 사용하여 클래스 파일을 메모리로 읽고 클래스 이름에 대해 쿼리하십시오.
이진을 구문 분석하여 읽을 수 있습니다. 클래스 파일 형식 가 정의됩니다 VM spec/a>.
사용중인 IDE에 따라이 작업을 수행하는 메커니즘이있을 수 있습니다.예를 들어 Eclipse에서는 .class 파일로 드릴 다운하고이를 마우스 오른쪽 버튼으로 클릭하고 "완전한 이름 복사"를 선택할 수 있습니다.
이전 버전의 Eclipse에는이 기능이 없지만 이전 에이 플러그인을 사용했습니다 :
http://www.jave.de/eclipse/copyfully/index.html.
그것은 거의 같은 방식으로 작동합니다.이것이 도움이되기를 바랍니다.
JSF 구현에서 Annotationscanner의 Annotationscanner에서 살펴볼 수 있으므로 JSF 주석을 찾기 위해 클래스를 수동으로로드합니다.특히 이것을보십시오 :
/**
* This class is encapsulating binary .class file information as defined at
* http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html
* <p/>
* This is used by the annotation frameworks to quickly scan .class files
* for the presence of annotations. This avoid the annotation framework
* having to load each .class file in the class loader.
* <p/>
* Taken from the GlassFish V2 source base.
*/
@SuppressWarnings({"UnusedDeclaration"})
private static final class ClassFile {
private static final int magic = 0xCAFEBABE;
public static final int ACC_PUBLIC = 0x1;
public static final int ACC_PRIVATE = 0x2;
public static final int ACC_PROTECTED = 0x4;
public static final int ACC_STATIC = 0x8;
public static final int ACC_FINAL = 0x10;
public static final int ACC_SYNCHRONIZED = 0x20;
public static final int ACC_THREADSAFE = 0x40;
public static final int ACC_TRANSIENT = 0x80;
public static final int ACC_NATIVE = 0x100;
public static final int ACC_INTERFACE = 0x200;
public static final int ACC_ABSTRACT = 0x400;
public short majorVersion;
public short minorVersion;
public ConstantPoolInfo constantPool[];
public short accessFlags;
public ConstantPoolInfo thisClass;
public ConstantPoolInfo superClass;
public ConstantPoolInfo interfaces[];
/**
* bunch of stuff I really don't care too much for now.
* <p/>
* FieldInfo fields[]; MethodInfo methods[];
* AttributeInfo attributes[];
*/
ByteBuffer header;
ConstantPoolInfo constantPoolInfo = new ConstantPoolInfo();
// ------------------------------------------------------------ Constructors
/**
* Creates a new instance of ClassFile
*/
public ClassFile() {
header = ByteBuffer.allocate(12000);
}
// ---------------------------------------------------------- Public Methods
public void setConstantPoolInfo(ConstantPoolInfo poolInfo) {
constantPoolInfo = poolInfo;
}
/**
* Read the input channel and initialize instance data structure.
*
* @param in a <code>ReadableByteChannel</code> that provides the bytes
* of the classfile
*
* @return <code>true</code> if the bytes representing this classfile include
* one of the annotations we're looking for.
*
* @throws IOException if an I/O error occurs while reading the class
*/
public boolean containsAnnotation(ReadableByteChannel in)
throws IOException {
/**
* this is the .class file layout
*
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
**/
header.clear();
long read = (long) in.read(header);
if (read == -1) {
return false;
}
header.rewind();
if (header.getInt() != magic) {
return false;
}
minorVersion = header.getShort();
majorVersion = header.getShort();
int constantPoolSize = header.getShort();
return constantPoolInfo
.containsAnnotation(constantPoolSize, header, in);
}
} // END ClassFile
. 제휴하지 않습니다 StackOverflow