Java: handle classes without a qualified name better (IJ-CR-144804)

GitOrigin-RevId: 88ee8c15122aaca6028b591d8538e18de0882643
This commit is contained in:
Bas Leijdekkers
2024-09-16 18:13:55 +00:00
committed by intellij-monorepo-bot
parent c82f14e928
commit cb7ac04d17
7 changed files with 102 additions and 3 deletions
@@ -336,8 +336,11 @@ public class PsiDocMethodOrFieldRef extends CompositePsiElement implements PsiDo
}
}
else if (containingClass != null && PsiTreeUtil.getParentOfType(PsiDocMethodOrFieldRef.this, PsiClass.class) != containingClass) {
String qName = containingClass.getQualifiedName();
if (qName == null) qName = containingClass.getName(); // local class has no qualified name, but has a short name
if (qName == null) return PsiDocMethodOrFieldRef.this; // ref can't be fixed
PsiDocComment fromText = JavaPsiFacade.getElementFactory(containingClass.getProject())
.createDocCommentFromText("/**{@link " + containingClass.getQualifiedName() + "#" + newName + "}*/");
.createDocCommentFromText("/**{@link " + qName + "#" + newName + "}*/");
PsiDocMethodOrFieldRef methodOrFieldRefFromText = PsiTreeUtil.findChildOfType(fromText, PsiDocMethodOrFieldRef.class);
addAfter(Objects.requireNonNull(methodOrFieldRefFromText).getFirstChild(), null);
}
@@ -676,8 +676,10 @@ public class PsiReferenceExpressionImpl extends ExpressionPsiElement implements
PsiClass psiClass = member.getContainingClass();
if (psiClass == null) throw new IncorrectOperationException();
boolean isStatic = ((PsiMember)element).hasModifierProperty(PsiModifier.STATIC);
String qName = psiClass.getQualifiedName() + (isStatic ? "." : ".this.") + member.getName();
PsiExpression ref = parserFacade.createExpressionFromText(qName, this);
String qName = psiClass.getQualifiedName();
if (qName == null) qName = psiClass.getName(); // local class has no qualified name, but has a short name
if (qName == null) return this; // ref can't be fixed
PsiExpression ref = parserFacade.createExpressionFromText(qName + (isStatic ? "." : ".this.") + member.getName(), this);
getTreeParent().replaceChildInternal(this, (TreeElement)ref.getNode());
return ref;
}
@@ -0,0 +1,21 @@
class Main {
public static void main(String[] args) {
interface Add {
static OptionalLong set(long a) {
return null;
}
interface Async {
/**
* @see #set(long)
*/
CompletableFuture<OptionalLong> add<caret>(long a);
static void x() {
set(1);
}
}
}
}
}
@@ -0,0 +1,21 @@
class Main {
public static void main(String[] args) {
interface Add {
static OptionalLong set(long a) {
return null;
}
interface Async {
/**
* @see Add#set(long)
*/
CompletableFuture<OptionalLong> set(long a);
static void x() {
Add.set(1);
}
}
}
}
}
@@ -0,0 +1,22 @@
class Main {
public static void main(String[] args) {
new Object() {
static OptionalLong set(long a) {
return null;
}
interface Async {
/**
* @see #set(long)
* incorrect but can't be fixed
*/
CompletableFuture<OptionalLong> add<caret>(long a);
static void x() {
set(1); // incorrect but can't be fixed
}
}
};
}
}
@@ -0,0 +1,22 @@
class Main {
public static void main(String[] args) {
new Object() {
static OptionalLong set(long a) {
return null;
}
interface Async {
/**
* @see #set(long)
* incorrect but can't be fixed
*/
CompletableFuture<OptionalLong> set(long a);
static void x() {
set(1); // incorrect but can't be fixed
}
}
};
}
}
@@ -77,6 +77,14 @@ public class RenameMembersInplaceTest extends LightJavaCodeInsightTestCase {
public void testMethodWithJavadocRef2() {
doTestInplaceRename("set");
}
public void testMethodWithJavadocRef3() {
doTestInplaceRename("set");
}
public void testMethodWithJavadocRef4() {
doTestInplaceRename("set");
}
public void testEnumConstructor() {
doTestInplaceRename("Bar");