mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
[java] static modifier of created method should depend on containing class (IDEA-203737)
This commit is contained in:
@@ -16,6 +16,20 @@ internal fun PsiExpression.isInStaticContext(): Boolean {
|
||||
return isWithinStaticMember() || isWithinConstructorCall()
|
||||
}
|
||||
|
||||
internal fun PsiExpression.isWithinStaticMemberOf(clazz: PsiClass): Boolean {
|
||||
var currentPlace: PsiElement = this
|
||||
while (true) {
|
||||
val enclosingMember = currentPlace.parentOfType<PsiMember>() ?: return false
|
||||
val enclosingClass = enclosingMember.containingClass ?: return false
|
||||
if (enclosingClass == clazz) {
|
||||
return enclosingMember.hasModifierProperty(PsiModifier.STATIC)
|
||||
}
|
||||
else {
|
||||
currentPlace = enclosingClass.parent ?: return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun PsiExpression.isWithinStaticMember(): Boolean {
|
||||
return parentOfType<PsiMember>()?.hasModifierProperty(PsiModifier.STATIC) ?: false
|
||||
}
|
||||
|
||||
@@ -61,17 +61,17 @@ private class CreateMethodRequests(val myCall: PsiMethodCallExpression) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
val inStaticContext = myCall.isInStaticContext()
|
||||
for (outerClass in collectOuterClasses(myCall)) {
|
||||
processClass(outerClass, inStaticContext)
|
||||
processClass(outerClass, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun processClass(clazz: PsiClass, staticContext: Boolean) {
|
||||
private fun processClass(clazz: PsiClass, inStaticContext: Boolean?) {
|
||||
if (isMethodSignatureExists(myCall, clazz)) return // TODO generic check
|
||||
val visibility = computeVisibility(myCall.project, myCall.parentOfType(), clazz)
|
||||
val modifiers = mutableSetOf<JvmModifier>()
|
||||
val staticContext = inStaticContext ?: (myCall.isWithinConstructorCall() || myCall.isWithinStaticMemberOf(clazz))
|
||||
if (staticContext) modifiers += JvmModifier.STATIC
|
||||
if (visibility != null) modifiers += visibility
|
||||
myRequests[clazz] = CreateMethodFromJavaUsageRequest(myCall, modifiers)
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Create method 'doSomething' in 'Test'" "true"
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
blaat(new Runnable() {
|
||||
public void run() {
|
||||
doSomething();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void doSomething() {
|
||||
<caret>
|
||||
}
|
||||
|
||||
public static void blaat(Runnable o) {}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Create method 'doSomething' in 'Test'" "true"
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
blaat(new Runnable() {
|
||||
public void run() {
|
||||
<caret>doSomething();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void blaat(Runnable o) {}
|
||||
}
|
||||
Reference in New Issue
Block a user