Skip to content

Commit

Permalink
Fix inner classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Jun 27, 2024
1 parent 1d58da9 commit 6d17f63
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private void checkImplicitConstructor(PsiClass psiClass, String className, @Null
} else if (psiClass.getClassKind() == JvmClassKind.CLASS) {
// When widening the access of a class, we must take into consideration the fact that implicit constructors follow the access level of their owner
if (psiClass.getConstructors().length == 0) {
var constructorTarget = new Target.MethodTarget(className, "<init>", "()V");
var constructorTarget = new Target.MethodTarget(className, "<init>", PsiHelper.getImplicitConstructorSignature(psiClass));
var constructorAt = pendingATs.remove(constructorTarget);

if (classAt != null && detectModifier(psiClass.getModifierList(), null).ordinal() > classAt.modifier().ordinal()) {
Expand Down
22 changes: 19 additions & 3 deletions api/src/main/java/net/neoforged/jst/api/PsiHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ public String next() {
};
}

public static String getImplicitConstructorSignature(PsiClass psiClass) {
StringBuilder signature = new StringBuilder();
signature.append("(");
// Non-Static inner class constructors have the enclosing class as their first argument
if (isNonStaticInnerClass(psiClass)) {
var parent = Objects.requireNonNull(Objects.requireNonNull(psiClass.getContainingClass()));
signature.append("L");
getBinaryClassName(parent, signature);
signature.append(";");
}
signature.append(")V");
return signature.toString();
}

public static String getBinaryMethodSignature(PsiMethod method) {
StringBuilder signature = new StringBuilder();
signature.append("(");
Expand Down Expand Up @@ -165,13 +179,15 @@ private static boolean isEnumConstructor(PsiMethod method) {
private static boolean isNonStaticInnerClassConstructor(PsiMethod method) {
if (method.isConstructor()) {
var containingClass = method.getContainingClass();
return containingClass != null
&& containingClass.getContainingClass() != null
&& !containingClass.hasModifierProperty(PsiModifier.STATIC);
return containingClass != null && isNonStaticInnerClass(containingClass);
}
return false;
}

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

/**
* Gets the local variable table indices of the parameters for the given method
* or lambda expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ public PrivateRecord # Error because the ctor needs to be AT'd too

public PrivateRecord$Nested
public PrivateRecord$Nested <init>(I)V

protected PrivateClass$Inner # Make the class itself protected
public PrivateClass$Inner <init>(LPrivateClass;)V # and the constructor public
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ protected static class NestedProtected {
public static class Both {

}

protected class Inner {
public Inner() {}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ protected static class NestedProtected {
protected static class Both {

}

private class Inner {

}
}

0 comments on commit 6d17f63

Please sign in to comment.