Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions src/main/java/run/slicer/vf/impl/ClassSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,16 @@ private static Map<String, ResourceData> resources(String[] names, String[] reso
return res;
}

private record DependencyAnalyzer(Map<String, ResourceData> data, Function<String, byte[]> source) {
private static final class DependencyAnalyzer {
private final Map<String, ResourceData> data;
private final Function<String, byte[]> source;
private final Set<String> checkedSuper = new HashSet<>();

DependencyAnalyzer(Map<String, ResourceData> data, Function<String, byte[]> source) {
this.data = data;
this.source = source;
}

void analyze() {
for (final ResourceData resource : new ArrayList<>(data.values())) {
if (resource == null) {
Expand All @@ -117,7 +126,7 @@ void analyze() {
if (name.charAt(0) == '[') {
addType(name);
} else {
addName(name);
addSuperTypes(name);
}
}
case PooledConstant.CONSTANT_NameAndType -> {
Expand All @@ -138,6 +147,40 @@ void analyze() {
}
}

private void addSuperTypes(String className) {
if (!checkedSuper.add(className)) {
return; // already visited
}

addName(className);
ResourceData resource = data.get(className);
if (resource == null) {
return;
}
final var is = new DataInputFullStream(resource.data());
try {
is.discard(8); // skip magic and version
final var cp = new ConstantPool(is);
is.discard(4); //skip access flags and this class
int superclassIndex = is.readUnsignedShort();
if (superclassIndex == 0) {
return;
}
PooledConstant superclass = cp.getConstant(superclassIndex);
String superclassName = ((PrimitiveConstant) superclass).getString();
addSuperTypes(superclassName);
int interfacesCount = is.readUnsignedShort();
for (int i = 0; i < interfacesCount; i++) {
int interfaceIndex = is.readUnsignedShort();
PooledConstant interfaceConstant = cp.getConstant(interfaceIndex);
String str = ((PrimitiveConstant) interfaceConstant).getString();
addSuperTypes(str);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private void addMethodType(String descriptor) {
if (descriptor == null || descriptor.isEmpty()) {
return;
Expand Down