blob: 115b80a33b294aeda17458afd36820845f3cff0f [file] [log] [blame]
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.highlighting;
import com.intellij.psi.*;
import com.intellij.psi.util.PropertyUtil;
import com.intellij.psi.util.PsiUtil;
/**
* @author yole
*/
public class JavaReadWriteAccessDetector extends ReadWriteAccessDetector {
@Override
public boolean isReadWriteAccessible(final PsiElement element) {
return element instanceof PsiVariable && !(element instanceof ImplicitVariable) || element instanceof PsiClass;
}
@Override
public boolean isDeclarationWriteAccess(final PsiElement element) {
if (element instanceof PsiVariable && ((PsiVariable)element).getInitializer() != null) {
return true;
}
if (element instanceof PsiParameter && ((PsiParameter)element).getDeclarationScope() instanceof PsiForeachStatement) {
return true;
}
return false;
}
@Override
public Access getReferenceAccess(final PsiElement referencedElement, final PsiReference reference) {
return getExpressionAccess(reference.getElement());
}
@Override
public Access getExpressionAccess(final PsiElement expression) {
if (!(expression instanceof PsiExpression)) return Access.Read;
PsiExpression expr = (PsiExpression) expression;
boolean readAccess = PsiUtil.isAccessedForReading(expr);
boolean writeAccess = PsiUtil.isAccessedForWriting(expr);
if (!writeAccess && expr instanceof PsiReferenceExpression) {
//when searching usages of fields, should show all found setters as a "only write usage"
PsiElement actualReferee = ((PsiReferenceExpression) expr).resolve();
if (actualReferee instanceof PsiMethod && PropertyUtil.isSimplePropertySetter((PsiMethod)actualReferee)) {
writeAccess = true;
readAccess = false;
}
}
if (writeAccess && readAccess) return Access.ReadWrite;
return writeAccess ? Access.Write : Access.Read;
}
}