OnJava8-Examples/onjava/atunit/ClassNameFinder.java

110 lines
3.6 KiB
Java
Raw Normal View History

2015-12-06 11:45:16 -08:00
// onjava/atunit/ClassNameFinder.java
2015-12-15 11:47:04 -08:00
// (c)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-12-06 11:45:16 -08:00
package onjava.atunit;
2015-06-15 17:47:35 -07:00
import java.io.*;
2015-12-06 11:45:16 -08:00
import java.nio.file.*;
2015-06-15 17:47:35 -07:00
import java.util.*;
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();
2015-12-06 11:45:16 -08:00
// int tableSize;
2015-06-15 17:47:35 -07:00
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
2015-12-06 11:45:16 -08:00
case 18: // Invoke Dynamic
2015-06-15 17:47:35 -07:00
data.readInt(); // discard 4 bytes;
break;
2015-12-06 11:45:16 -08:00
case 15: // Method Handle
data.readByte();
data.readShort();
break;
case 16: // Method Type
data.readShort();
break;
2015-06-15 17:47:35 -07:00
default:
throw new RuntimeException("Bad tag " + tag);
}
}
short access_flags = data.readShort();
2015-12-06 11:45:16 -08:00
String access = (access_flags & 0x0001) == 0 ?
"nonpublic:" : "public:";
2015-06-15 17:47:35 -07:00
int this_class = data.readShort();
int super_class = data.readShort();
2015-12-06 11:45:16 -08:00
return access + classNameTable.get(
2015-06-15 17:47:35 -07:00
offsetTable.get(this_class)).replace('/', '.');
} catch(IOException | RuntimeException e) {
throw new RuntimeException(e);
}
}
// Demonstration:
public static void main(String[] args) throws Exception {
2015-12-06 11:45:16 -08:00
PathMatcher matcher = FileSystems.getDefault()
.getPathMatcher("glob:**/*.class");
// Walk the entire tree:
Files.walk(Paths.get("."))
.filter(matcher::matches)
//.peek(System.out::println)
.map(p -> {
try {
return thisClass(Files.readAllBytes(p));
} catch(Exception e) {
throw new RuntimeException(e);
}
})
.filter(s -> s.startsWith("public:"))
// .filter(s -> s.indexOf('$') >= 0)
.map(s -> s.split(":")[1])
.filter(s -> !s.startsWith("enums."))
.filter(s -> s.contains("."))
.forEach(System.out::println);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-12-06 11:45:16 -08:00
onjava.atunit.AtUnit$TestMethods
onjava.atunit.AtUnit
onjava.atunit.ClassNameFinder
onjava.atunit.Test
onjava.atunit.TestObjectCleanup
onjava.atunit.TestObjectCreate
onjava.atunit.TestProperty
2015-09-07 11:44:36 -06:00
*/