OnJava8-Examples/onjava/atunit/ClassNameFinder.java

181 lines
4.8 KiB
Java
Raw Normal View History

2015-12-06 11:45:16 -08:00
// onjava/atunit/ClassNameFinder.java
2020-10-07 13:35:40 -06:00
// (c)2020 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.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-07-28 12:48:23 -06:00
// {java onjava.atunit.ClassNameFinder}
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<>();
2015-06-15 17:47:35 -07:00
try {
DataInputStream data = new DataInputStream(
new ByteArrayInputStream(classBytes));
int magic = data.readInt(); // 0xcafebabe
int minorVersion = data.readShort();
int majorVersion = data.readShort();
int constantPoolCount = data.readShort();
int[] constantPool = new int[constantPoolCount];
for(int i = 1; i < constantPoolCount; i++) {
2015-06-15 17:47:35 -07:00
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
2017-05-15 16:15:22 -06:00
data.readInt(); // discard 4 bytes
2015-06-15 17:47:35 -07:00
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);
2015-06-15 17:47:35 -07:00
}
}
short accessFlags = data.readShort();
String access = (accessFlags & 0x0001) == 0 ?
2015-12-06 11:45:16 -08:00
"nonpublic:" : "public:";
int thisClass = data.readShort();
int superClass = data.readShort();
2015-12-06 11:45:16 -08:00
return access + classNameTable.get(
offsetTable.get(thisClass)).replace('/', '.');
2015-06-15 17:47:35 -07:00
} catch(IOException | RuntimeException e) {
throw new RuntimeException(e);
}
}
// Demonstration:
2016-01-25 18:05:55 -08:00
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)
.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:
2016-07-22 14:45:35 -06:00
onjava.ArrayShow
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
2016-07-22 14:45:35 -06:00
onjava.BasicSupplier
onjava.CollectionMethodDifferences
onjava.ConvertTo
onjava.Count$Boolean
onjava.Count$Byte
onjava.Count$Character
onjava.Count$Double
onjava.Count$Float
onjava.Count$Integer
onjava.Count$Long
onjava.Count$Pboolean
onjava.Count$Pbyte
onjava.Count$Pchar
onjava.Count$Pdouble
onjava.Count$Pfloat
onjava.Count$Pint
onjava.Count$Plong
onjava.Count$Pshort
onjava.Count$Short
2016-07-22 14:45:35 -06:00
onjava.Count
onjava.CountingIntegerList
onjava.CountMap
onjava.Countries
onjava.Enums
onjava.FillMap
onjava.HTMLColors
2016-07-22 14:45:35 -06:00
onjava.MouseClick
onjava.Nap
2016-07-22 14:45:35 -06:00
onjava.Null
onjava.Operations
2016-07-22 14:45:35 -06:00
onjava.OSExecute
onjava.OSExecuteException
onjava.Pair
onjava.ProcessFiles$Strategy
onjava.ProcessFiles
onjava.Rand$Boolean
onjava.Rand$Byte
onjava.Rand$Character
onjava.Rand$Double
onjava.Rand$Float
onjava.Rand$Integer
onjava.Rand$Long
onjava.Rand$Pboolean
onjava.Rand$Pbyte
onjava.Rand$Pchar
onjava.Rand$Pdouble
onjava.Rand$Pfloat
onjava.Rand$Pint
onjava.Rand$Plong
onjava.Rand$Pshort
onjava.Rand$Short
2016-07-22 14:45:35 -06:00
onjava.Rand$String
onjava.Rand
onjava.Range
onjava.Repeat
onjava.RmDir
onjava.Sets
onjava.Stack
onjava.Suppliers
onjava.TimedAbort
onjava.Timer
2016-07-22 14:45:35 -06:00
onjava.Tuple
onjava.Tuple2
onjava.Tuple3
onjava.Tuple4
onjava.Tuple5
onjava.TypeCounter
2015-09-07 11:44:36 -06:00
*/