mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
convert to atomic: add rule for array.length
This commit is contained in:
+14
@@ -12,6 +12,8 @@ import com.intellij.refactoring.typeMigration.TypeConversionDescriptor;
|
||||
import com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase;
|
||||
import com.intellij.refactoring.typeMigration.TypeEvaluator;
|
||||
import com.intellij.refactoring.typeMigration.TypeMigrationLabeler;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.siyeh.HardcodedMethodConstants;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -342,6 +344,9 @@ public class AtomicConversionRule extends TypeConversionRule {
|
||||
if (context instanceof PsiArrayAccessExpression) {
|
||||
return new TypeConversionDescriptor("$qualifier$[$idx$]", "$qualifier$.get($idx$)", (PsiExpression)context);
|
||||
}
|
||||
if (parent instanceof PsiReferenceExpression && isReferenceToLengthField((PsiReferenceExpression)parent)) {
|
||||
return new TypeConversionDescriptor("$qualifier$.length", "$qualifier$.length()", (PsiExpression)parent);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -438,4 +443,13 @@ public class AtomicConversionRule extends TypeConversionRule {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isReferenceToLengthField(@NotNull PsiReferenceExpression refExpr) {
|
||||
if (!"length".equals(refExpr.getReferenceName())) {
|
||||
return false;
|
||||
}
|
||||
PsiClass aClass = JavaPsiFacade.getElementFactory(refExpr.getProject()).getArrayClass(PsiUtil.getLanguageLevel(refExpr));
|
||||
PsiField lengthField = ObjectUtils.notNull(aClass.findFieldByName(HardcodedMethodConstants.LENGTH, false));
|
||||
return refExpr.isReferenceTo(lengthField);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
|
||||
// "Convert to atomic" "true"
|
||||
class Test {
|
||||
final AtomicIntegerArray ii = new AtomicIntegerArray(new int[12]);
|
||||
|
||||
void m() {
|
||||
int k = ii.length();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Convert to atomic" "true"
|
||||
class Test {
|
||||
int[] i<caret>i = new int[12];
|
||||
|
||||
void m() {
|
||||
int k = ii.length;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user