added missing parameter list in ClassObject/Method
This commit is contained in:
parent
e22d507c30
commit
75e40ae502
3 changed files with 76 additions and 11 deletions
|
|
@ -2,8 +2,11 @@ package nl.sander.jsontoy2.java;
|
|||
|
||||
import nl.sander.jsontoy2.java.constantpool.ConstantPoolEntry;
|
||||
import nl.sander.jsontoy2.java.constantpool.Utf8Entry;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
|
@ -34,10 +37,50 @@ public class ClassObject {
|
|||
|
||||
public Set<Method> getMethods() {
|
||||
return Arrays.stream(methodInfos)
|
||||
.map(mi -> new Method(getUtf8(mi.getNameIndex()), getUtf8(mi.getDescriptorIndex())))
|
||||
.map(this::createMethod)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Method createMethod(Info mi) {
|
||||
final String descriptor = getUtf8(mi.getDescriptorIndex());
|
||||
final int split = descriptor.indexOf(')');
|
||||
final List<String> parameters = getParameterTypes(descriptor.substring(0, split));
|
||||
|
||||
String returnType = descriptor.substring(split + 1);
|
||||
returnType = returnType.substring(0, returnType.length() - 1);
|
||||
return new Method(getUtf8(mi.getNameIndex()), parameters, returnType);
|
||||
}
|
||||
|
||||
private List<String> getParameterTypes(String parameterDescriptor) {
|
||||
List<String> result = new ArrayList<>();
|
||||
boolean array = false;
|
||||
for (int i = 1; i < parameterDescriptor.length(); i++) {
|
||||
if (parameterDescriptor.charAt(i) == '[') {
|
||||
array = true;
|
||||
} else {
|
||||
if (parameterDescriptor.charAt(i) == 'L') {
|
||||
int i2 = i;
|
||||
while (i2 < parameterDescriptor.length() && parameterDescriptor.charAt(i2) != ';') {
|
||||
i2++;
|
||||
}
|
||||
result.add(getArrayIndicator(array) + parameterDescriptor.substring(i, i2));
|
||||
array = false;
|
||||
i = i2;
|
||||
} else {
|
||||
result.add(getArrayIndicator(array) + parameterDescriptor.charAt(i));
|
||||
array = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getArrayIndicator(boolean array) {
|
||||
return array ? "[" : "";
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (ConstantPoolEntry entry : constantPool) {
|
||||
|
|
|
|||
|
|
@ -1,23 +1,28 @@
|
|||
package nl.sander.jsontoy2.java;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Method {
|
||||
public class Method implements Comparable<Method> {
|
||||
|
||||
private final String name;
|
||||
private final String type;
|
||||
private final List<String> parameterTypes;
|
||||
private final String returnType;
|
||||
|
||||
public Method(String name, String type) {
|
||||
public Method(String name, List<String> parameterTypes, String returnType) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.parameterTypes = parameterTypes;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
public String getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -26,19 +31,25 @@ public class Method {
|
|||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Method field = (Method) o;
|
||||
return name.equals(field.name) &&
|
||||
type.equals(field.type);
|
||||
returnType.equals(field.returnType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, type);
|
||||
return Objects.hash(name, returnType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Method{" +
|
||||
"name='" + name + '\'' +
|
||||
", type='" + type + '\'' +
|
||||
", parameterTypes=" + parameterTypes +
|
||||
", returnType='" + returnType + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull Method o) {
|
||||
return name.compareTo(o.name);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ package nl.sander.jsontoy2.java;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
|
|
@ -11,10 +13,19 @@ public class ClassReaderTest {
|
|||
//part of the test
|
||||
public int field;
|
||||
|
||||
public String method(String value, int[] value2, int value3) {
|
||||
return value;
|
||||
}
|
||||
//
|
||||
|
||||
@Test
|
||||
public void testReadClass() {
|
||||
ClassObject object = new ClassReader().parse(ClassReaderTest.class);
|
||||
assertEquals(Set.of(new Field("field", "I")), object.getFields());
|
||||
assertEquals(Set.of(new Method("<init>", "()V"), new Method("testReadClass", "()V")), object.getMethods());
|
||||
assertEquals(new TreeSet<>(Set.of(
|
||||
new Method("method", List.of("Ljava/lang/String", "[I", "I"), "Ljava/lang/String"),
|
||||
new Method("testReadClass", null, "V"),
|
||||
new Method("<init>", null, "V"))
|
||||
), new TreeSet<>(object.getMethods()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue