2015-09-07 11:44:36 -06:00
|
|
|
|
// com/mindviewinc/atunit/ClassNameFinder.java
|
2015-11-14 16:18:05 -08:00
|
|
|
|
// <20>2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
|
package com.mindviewinc.atunit;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.*;
|
2015-11-11 20:20:04 -08:00
|
|
|
|
import onjava.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
|
|
public class ClassNameFinder {
|
|
|
|
|
public static String thisClass(byte[] classBytes) {
|
|
|
|
|
Map<Integer,Integer> offsetTable = new HashMap<>();
|
|
|
|
|
Map<Integer,String> classNameTable = new HashMap<>();
|
|
|
|
|
try {
|
|
|
|
|
DataInputStream data = new DataInputStream(
|
|
|
|
|
new ByteArrayInputStream(classBytes));
|
|
|
|
|
int magic = data.readInt(); // 0xcafebabe
|
|
|
|
|
int minorVersion = data.readShort();
|
|
|
|
|
int majorVersion = data.readShort();
|
|
|
|
|
int constant_pool_count = data.readShort();
|
|
|
|
|
int[] constant_pool = new int[constant_pool_count];
|
|
|
|
|
for(int i = 1; i < constant_pool_count; i++) {
|
|
|
|
|
int tag = data.read();
|
|
|
|
|
int tableSize;
|
|
|
|
|
switch(tag) {
|
|
|
|
|
case 1: // UTF
|
|
|
|
|
int length = data.readShort();
|
|
|
|
|
char[] bytes = new char[length];
|
|
|
|
|
for(int k = 0; k < bytes.length; k++)
|
|
|
|
|
bytes[k] = (char)data.read();
|
|
|
|
|
String className = new String(bytes);
|
|
|
|
|
classNameTable.put(i, className);
|
|
|
|
|
break;
|
|
|
|
|
case 5: // LONG
|
|
|
|
|
case 6: // DOUBLE
|
|
|
|
|
data.readLong(); // discard 8 bytes
|
|
|
|
|
i++; // Special skip necessary
|
|
|
|
|
break;
|
|
|
|
|
case 7: // CLASS
|
|
|
|
|
int offset = data.readShort();
|
|
|
|
|
offsetTable.put(i, offset);
|
|
|
|
|
break;
|
|
|
|
|
case 8: // STRING
|
|
|
|
|
data.readShort(); // discard 2 bytes
|
|
|
|
|
break;
|
|
|
|
|
case 3: // INTEGER
|
|
|
|
|
case 4: // FLOAT
|
|
|
|
|
case 9: // FIELD_REF
|
|
|
|
|
case 10: // METHOD_REF
|
|
|
|
|
case 11: // INTERFACE_METHOD_REF
|
|
|
|
|
case 12: // NAME_AND_TYPE
|
|
|
|
|
data.readInt(); // discard 4 bytes;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new RuntimeException("Bad tag " + tag);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
short access_flags = data.readShort();
|
|
|
|
|
int this_class = data.readShort();
|
|
|
|
|
int super_class = data.readShort();
|
|
|
|
|
return classNameTable.get(
|
|
|
|
|
offsetTable.get(this_class)).replace('/', '.');
|
|
|
|
|
} catch(IOException | RuntimeException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Demonstration:
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
if(args.length > 0) {
|
|
|
|
|
for(String arg : args)
|
2015-12-02 09:20:27 -08:00
|
|
|
|
System.out.println(
|
|
|
|
|
thisClass(BinaryFile.read(new File(arg))));
|
2015-06-15 17:47:35 -07:00
|
|
|
|
} else
|
2015-11-03 12:00:44 -08:00
|
|
|
|
// Walk the entire tree: <* Use NIO2 here *>
|
2015-06-15 17:47:35 -07:00
|
|
|
|
for(File klass : Directory.walk(".", ".*\\.class"))
|
2015-12-02 09:20:27 -08:00
|
|
|
|
System.out.println(
|
|
|
|
|
thisClass(BinaryFile.read(klass)));
|
2015-06-15 17:47:35 -07:00
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
}
|
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
|
com.mindviewinc.atunit.AtUnit$TestMethods
|
|
|
|
|
com.mindviewinc.atunit.AtUnit
|
|
|
|
|
com.mindviewinc.atunit.ClassNameFinder
|
|
|
|
|
com.mindviewinc.atunit.Test
|
|
|
|
|
com.mindviewinc.atunit.TestObjectCleanup
|
|
|
|
|
com.mindviewinc.atunit.TestObjectCreate
|
|
|
|
|
com.mindviewinc.atunit.TestProperty
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|