Skip to content

Commit

Permalink
Fix indices of method parameters when using tsrg files (#32)
Browse files Browse the repository at this point in the history
* Fix indices of method parameters when using tsrg files

* Fix enum parameter offsets

* Fix tests

* Fix inner ctors
  • Loading branch information
Matyrobbrt authored Jul 4, 2024
1 parent 26d5837 commit eb609a9
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/net/neoforged/jst/api/PsiHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private static boolean isNonStaticInnerClassConstructor(PsiMethod method) {
return false;
}

private static boolean isNonStaticInnerClass(PsiClass psiClass) {
public static boolean isNonStaticInnerClass(PsiClass psiClass) {
return psiClass.getContainingClass() != null && !psiClass.hasModifierProperty(PsiModifier.STATIC);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ public void visitElement(@NotNull PsiElement element) {
var parameters = psiMethod.getParameterList().getParameters();
var parametersLvtIndices = PsiHelper.getParameterLvtIndices(psiMethod);
boolean hadReplacements = false;

int parameterOffset = 0;
if (psiMethod.isConstructor()) {
// Enums offset the parameter index by 2 since they add an int and a string param which are not visible in the source
if (psiMethod.getContainingClass().isEnum()) {
parameterOffset = 2;
}
// Non-static inner classes capture their owner as the first parameter
else if (PsiHelper.isNonStaticInnerClass(psiMethod.getContainingClass())) {
parameterOffset = 1;
}
}

for (int i = 0; i < parameters.length; i++) {
var psiParameter = parameters[i];
// We cannot replace parameters with no name, sadly
Expand All @@ -115,7 +128,7 @@ public void visitElement(@NotNull PsiElement element) {
// to account for synthetic parameter not found in the source-code, we must adjust the index accordingly.
var jvmIndex = parametersLvtIndices[i];

var paramData = methodData.getParameter(jvmIndex);
var paramData = methodData.getParameter(parameterOffset + i, jvmIndex);
// Optionally replace the parameter name, but skip record constructors, since those could have
// implications for the field names.
if (paramData != null && paramData.getName() != null && !PsiHelper.isRecordConstructor(psiMethod)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
public interface NamesAndDocsForMethod {
List<String> getJavadoc();

NamesAndDocsForParameter getParameter(int index);
NamesAndDocsForParameter getParameter(int index, int jvmIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public List<String> getJavadoc() {
}

@Override
public NamesAndDocsForParameter getParameter(int index) {
var paramData = methodData.getParameter((byte) index);
public NamesAndDocsForParameter getParameter(int index, int jvmIndex) {
var paramData = methodData.getParameter((byte) jvmIndex);
return paramData != null ? new ParchmentNamesAndDocsForParameter(paramData) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public List<String> getJavadoc() {
}

@Override
public NamesAndDocsForParameter getParameter(int index) {
public NamesAndDocsForParameter getParameter(int index, int jvmIndex) {
var paramData = methodData.getParameter(index);
if (paramData == null || paramData.getMapped() == null) {
return null;
Expand Down
12 changes: 12 additions & 0 deletions tests/data/parchment/tsrg_file/expected/pkg/TestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@
public class TestClass {
public void m(String p_254545_, List<String> p_254546_) {
}

public class Inner {
public Inner(int p_298711_) {

}
}

public enum Enm {
;

Enm(boolean p_123452_) {}
}
}
13 changes: 11 additions & 2 deletions tests/data/parchment/tsrg_file/merged.tsrg
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
tsrg2 left right
obfuscated_c pkg/TestClass
obfuscated_m (Ljava/lang/String;Ljava/util/List;)V m
1 o p_254545_
2 o p_254546_
0 o p_254545_
1 o p_254546_
obfuscated_c$i pkg/TestClass$Inner
<init> (Lpkg/TestClass;I)V <init>
0 o p_298710_
1 o p_298711_
obfuscated_c$e pkg/TestClass$Enm
<init> (Ljava/lang/String;IZ)V <init>
0 o p_123450_
1 o p_123451_
2 o p_123452_
12 changes: 12 additions & 0 deletions tests/data/parchment/tsrg_file/source/pkg/TestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@
public class TestClass {
public void m(String a1, List<String> a2) {
}

public class Inner {
public Inner(int a1) {

}
}

public enum Enm {
;

Enm(boolean a3) {}
}
}

0 comments on commit eb609a9

Please sign in to comment.