added equals to LocalsControlFlowPolicy, notnull

This commit is contained in:
Alexey Kudravtsev
2014-05-28 15:58:08 +04:00
parent 15ecfbd77f
commit 5d21da588a
7 changed files with 54 additions and 28 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -94,7 +94,7 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
try {
ControlFlowPolicy policy = new ControlFlowPolicy() {
@Override
public PsiVariable getUsedVariable(PsiReferenceExpression refExpr) {
public PsiVariable getUsedVariable(@NotNull PsiReferenceExpression refExpr) {
if (refExpr.isQualified()) return null;
PsiElement refElement = refExpr.resolve();
@@ -107,12 +107,12 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
}
@Override
public boolean isParameterAccepted(PsiParameter psiParameter) {
public boolean isParameterAccepted(@NotNull PsiParameter psiParameter) {
return isVariableDeclaredInMethod(psiParameter);
}
@Override
public boolean isLocalVariableAccepted(PsiLocalVariable psiVariable) {
public boolean isLocalVariableAccepted(@NotNull PsiLocalVariable psiVariable) {
return isVariableDeclaredInMethod(psiVariable);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -25,23 +25,24 @@
package com.intellij.psi.controlFlow;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
public class AllVariablesControlFlowPolicy implements ControlFlowPolicy {
private static final AllVariablesControlFlowPolicy INSTANCE = new AllVariablesControlFlowPolicy();
@Override
public PsiVariable getUsedVariable(PsiReferenceExpression refExpr) {
public PsiVariable getUsedVariable(@NotNull PsiReferenceExpression refExpr) {
PsiElement resolved = refExpr.resolve();
return resolved instanceof PsiVariable ? (PsiVariable)resolved : null;
}
@Override
public boolean isParameterAccepted(PsiParameter psiParameter) {
public boolean isParameterAccepted(@NotNull PsiParameter psiParameter) {
return true;
}
@Override
public boolean isLocalVariableAccepted(PsiLocalVariable psiVariable) {
public boolean isLocalVariableAccepted(@NotNull PsiLocalVariable psiVariable) {
return true;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -19,13 +19,14 @@ import com.intellij.psi.PsiLocalVariable;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiReferenceExpression;
import com.intellij.psi.PsiVariable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface ControlFlowPolicy {
@Nullable
PsiVariable getUsedVariable(PsiReferenceExpression refExpr);
PsiVariable getUsedVariable(@NotNull PsiReferenceExpression refExpr);
boolean isParameterAccepted(PsiParameter psiParameter);
boolean isParameterAccepted(@NotNull PsiParameter psiParameter);
boolean isLocalVariableAccepted(PsiLocalVariable psiVariable);
boolean isLocalVariableAccepted(@NotNull PsiLocalVariable psiVariable);
}
@@ -378,7 +378,8 @@ public class ControlFlowUtil {
return PsiTreeUtil.getParentOfType(element, PsiStatement.class, false);
}
public static PsiElement findCodeFragment(PsiElement element) {
@NotNull
public static PsiElement findCodeFragment(@NotNull PsiElement element) {
PsiElement codeFragment = element;
PsiElement parent = codeFragment.getParent();
while (parent != null) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -462,7 +462,7 @@ public class DefUseUtil {
private static final ControlFlowPolicy ourPolicy = new ControlFlowPolicy() {
@Override
public PsiVariable getUsedVariable(PsiReferenceExpression refExpr) {
public PsiVariable getUsedVariable(@NotNull PsiReferenceExpression refExpr) {
if (refExpr.isQualified()) return null;
PsiElement refElement = refExpr.resolve();
@@ -474,12 +474,12 @@ public class DefUseUtil {
}
@Override
public boolean isParameterAccepted(PsiParameter psiParameter) {
public boolean isParameterAccepted(@NotNull PsiParameter psiParameter) {
return true;
}
@Override
public boolean isLocalVariableAccepted(PsiLocalVariable psiVariable) {
public boolean isLocalVariableAccepted(@NotNull PsiLocalVariable psiVariable) {
return true;
}
};
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -16,17 +16,18 @@
package com.intellij.psi.controlFlow;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class LocalsControlFlowPolicy implements ControlFlowPolicy {
private final PsiElement myCodeFragment;
public LocalsControlFlowPolicy(PsiElement codeFragment) {
public LocalsControlFlowPolicy(@NotNull PsiElement codeFragment) {
myCodeFragment = codeFragment;
}
@Override
public PsiVariable getUsedVariable(PsiReferenceExpression refExpr) {
public PsiVariable getUsedVariable(@NotNull PsiReferenceExpression refExpr) {
if (refExpr.isQualified()) return null;
PsiElement refElement = refExpr.resolve();
@@ -34,8 +35,8 @@ public class LocalsControlFlowPolicy implements ControlFlowPolicy {
}
@Nullable
private PsiVariable checkCodeFragment(PsiElement refElement) {
PsiElement codeFragment = ControlFlowUtil.findCodeFragment(refElement);
private PsiVariable checkCodeFragment(@NotNull PsiElement refElement) {
PsiElement codeFragment;
if (refElement instanceof PsiParameter) {
final PsiElement declarationScope = ((PsiParameter)refElement).getDeclarationScope();
if (declarationScope instanceof PsiMethod) {
@@ -44,6 +45,12 @@ public class LocalsControlFlowPolicy implements ControlFlowPolicy {
else if (declarationScope instanceof PsiLambdaExpression) {
codeFragment = ((PsiLambdaExpression)declarationScope).getBody();
}
else {
codeFragment = ControlFlowUtil.findCodeFragment(refElement);
}
}
else {
codeFragment = ControlFlowUtil.findCodeFragment(refElement);
}
if (codeFragment == null) return null;
if (myCodeFragment.getContainingFile() == codeFragment.getContainingFile() && // in order for jsp includes to work
@@ -54,12 +61,27 @@ public class LocalsControlFlowPolicy implements ControlFlowPolicy {
}
@Override
public boolean isParameterAccepted(PsiParameter psiParameter) {
public boolean isParameterAccepted(@NotNull PsiParameter psiParameter) {
return checkCodeFragment(psiParameter) != null;
}
@Override
public boolean isLocalVariableAccepted(PsiLocalVariable psiVariable) {
public boolean isLocalVariableAccepted(@NotNull PsiLocalVariable psiVariable) {
return checkCodeFragment(psiVariable) != null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LocalsControlFlowPolicy policy = (LocalsControlFlowPolicy)o;
return myCodeFragment.equals(policy.myCodeFragment);
}
@Override
public int hashCode() {
return myCodeFragment.hashCode();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -25,6 +25,7 @@
package com.intellij.psi.controlFlow;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
public class LocalsOrMyInstanceFieldsControlFlowPolicy implements ControlFlowPolicy {
private static final LocalsOrMyInstanceFieldsControlFlowPolicy INSTANCE = new LocalsOrMyInstanceFieldsControlFlowPolicy();
@@ -33,7 +34,7 @@ public class LocalsOrMyInstanceFieldsControlFlowPolicy implements ControlFlowPol
}
@Override
public PsiVariable getUsedVariable(PsiReferenceExpression refExpr) {
public PsiVariable getUsedVariable(@NotNull PsiReferenceExpression refExpr) {
PsiExpression qualifier = refExpr.getQualifierExpression();
if (qualifier == null || qualifier instanceof PsiThisExpression) {
PsiElement resolved = refExpr.resolve();
@@ -45,12 +46,12 @@ public class LocalsOrMyInstanceFieldsControlFlowPolicy implements ControlFlowPol
}
@Override
public boolean isParameterAccepted(PsiParameter psiParameter) {
public boolean isParameterAccepted(@NotNull PsiParameter psiParameter) {
return true;
}
@Override
public boolean isLocalVariableAccepted(PsiLocalVariable psiVariable) {
public boolean isLocalVariableAccepted(@NotNull PsiLocalVariable psiVariable) {
return true;
}