Skip to content

Commit

Permalink
Fix referenced class names sometimes including descriptors instead of…
Browse files Browse the repository at this point in the history
… names
  • Loading branch information
Col-E committed Jun 3, 2024
1 parent cc30959 commit 79ad5cb
Showing 1 changed file with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ default NavigableSet<String> getReferencedClasses() {
return classes;

Set<String> classNames = new HashSet<>();
Consumer<String> nameHandler = className -> {
if (className.indexOf(0) == '[')
className = className.substring(className.lastIndexOf('[') + 1, className.indexOf(';'));
classNames.add(className);
};
Consumer<Type> typeConsumer = t -> {
if (t.getSort() == Type.ARRAY)
t = t.getElementType();
if (!Types.isPrimitive(t))
nameHandler.accept(t.getInternalName());
};

ClassReader reader = getClassReader();
int itemCount = reader.getItemCount();
char[] buffer = new char[reader.getMaxStringLength()];
Expand All @@ -88,22 +76,20 @@ default NavigableSet<String> getReferencedClasses() {
String className = reader.readUTF8(offset, buffer);
if (className.isEmpty())
continue;
if (className.indexOf(0) == '[')
className = className.substring(className.lastIndexOf('[') + 1, className.indexOf(';'));
classNames.add(className);
addName(className, classNames);
} else if (itemTag == ConstantPoolConstants.NAME_TYPE) {
String desc = reader.readUTF8(offset + 2, buffer);
if (desc.isEmpty())
continue;
if (desc.charAt(0) == '(') {
Type methodType = Type.getMethodType(desc);
for (Type argumentType : methodType.getArgumentTypes())
typeConsumer.accept(argumentType);
addType(argumentType, classNames);
Type returnType = methodType.getReturnType();
typeConsumer.accept(returnType);
addType(returnType, classNames);
} else {
Type type = Type.getType(desc);
typeConsumer.accept(type);
addType(type, classNames);
}
}
}
Expand All @@ -112,6 +98,22 @@ default NavigableSet<String> getReferencedClasses() {
return Objects.requireNonNull(ReferencedClassesProperty.get(this));
}

private static void addType(@Nonnull Type type, @Nonnull Set<String> classNames) {
if (type.getSort() == Type.ARRAY)
type = type.getElementType();
if (!Types.isPrimitive(type))
addName(type.getInternalName(), classNames);
}

private static void addName(@Nonnull String className, @Nonnull Set<String> classNames) {
if (className.isEmpty())
return;
if (className.indexOf(0) == '[' || className.charAt(className.length() - 1) == ';')
addType(Type.getType(className), classNames);
else
classNames.add(className);
}

/**
* @return Set of all string constants listed in the constant pool.
*/
Expand Down

0 comments on commit 79ad5cb

Please sign in to comment.