mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-149649 ("Private member access" inspection should not warn on inlined constants.)
This commit is contained in:
+11
-12
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,12 +20,12 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.FileTypeUtils;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.psiutils.ClassUtils;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -98,8 +98,7 @@ public class PrivateMemberAccessBetweenOuterAndInnerClassInspection
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFix(Project project, ProblemDescriptor descriptor)
|
||||
throws IncorrectOperationException {
|
||||
public void doFix(Project project, ProblemDescriptor descriptor) {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
if (constructor) {
|
||||
makeConstructorPackageLocal(project, element);
|
||||
@@ -127,8 +126,7 @@ public class PrivateMemberAccessBetweenOuterAndInnerClassInspection
|
||||
modifiers.setModifierProperty(PsiModifier.PRIVATE, false);
|
||||
}
|
||||
|
||||
private static void makeConstructorPackageLocal(Project project,
|
||||
PsiElement element) {
|
||||
private static void makeConstructorPackageLocal(Project project, PsiElement element) {
|
||||
final PsiNewExpression newExpression =
|
||||
PsiTreeUtil.getParentOfType(element,
|
||||
PsiNewExpression.class);
|
||||
@@ -175,8 +173,7 @@ public class PrivateMemberAccessBetweenOuterAndInnerClassInspection
|
||||
return new PrivateMemberAccessFromInnerClassVisitor();
|
||||
}
|
||||
|
||||
private static class PrivateMemberAccessFromInnerClassVisitor
|
||||
extends BaseInspectionVisitor {
|
||||
private static class PrivateMemberAccessFromInnerClassVisitor extends BaseInspectionVisitor {
|
||||
|
||||
@Override
|
||||
public void visitNewExpression(PsiNewExpression expression) {
|
||||
@@ -219,11 +216,9 @@ public class PrivateMemberAccessBetweenOuterAndInnerClassInspection
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitReferenceExpression(
|
||||
@NotNull PsiReferenceExpression expression) {
|
||||
public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
|
||||
super.visitReferenceExpression(expression);
|
||||
final PsiElement referenceNameElement =
|
||||
expression.getReferenceNameElement();
|
||||
final PsiElement referenceNameElement = expression.getReferenceNameElement();
|
||||
if (referenceNameElement == null) {
|
||||
return;
|
||||
}
|
||||
@@ -235,6 +230,10 @@ public class PrivateMemberAccessBetweenOuterAndInnerClassInspection
|
||||
if (!member.hasModifierProperty(PsiModifier.PRIVATE)) {
|
||||
return;
|
||||
}
|
||||
final Object value = ExpressionUtils.computeConstantExpression(expression);
|
||||
if (value != null) {
|
||||
return; // no synthetic accessor created, compile time constant will be inlined by javac
|
||||
}
|
||||
final PsiElement containingClass = getContainingContextClass(expression);
|
||||
if (containingClass == null) {
|
||||
return;
|
||||
|
||||
+3
@@ -3,6 +3,9 @@
|
||||
This inspection is intended for J2ME and other highly resource constrained environments.
|
||||
Applying the results of this inspection without consideration might have negative effects on code clarity and design.
|
||||
<p>
|
||||
Reports references to non-constant <b>private</b> members of an outer class from inside an inner class or
|
||||
to members of an inner class from a containing class.
|
||||
<p>
|
||||
An inner class and its containing class are compiled to separate
|
||||
class files. The virtual machine normally prohibits access from a class to private fields and methods of
|
||||
another class. To enable access from an inner class to private members of a
|
||||
|
||||
+2
@@ -2,12 +2,14 @@ package com.siyeh.igtest.j2me.private_member_access_between_outer_and_inner_clas
|
||||
|
||||
public class PrivateMemberAccessBetweenOuterAndInnerClass {
|
||||
private String caption = "Button";
|
||||
private final int N = 100;
|
||||
|
||||
private void initialize() {
|
||||
System.out.println(caption);
|
||||
Object btn = new Object() {
|
||||
public void foo() {
|
||||
System.out.println(<warning descr="Access to private member of class 'PrivateMemberAccessBetweenOuterAndInnerClass'">caption</warning>);
|
||||
System.out.println(N);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user