notnull to prevent moronic EA-115317

This commit is contained in:
Alexey Kudravtsev
2018-03-19 13:03:11 +03:00
parent d175238d4a
commit c4b6ca5bcb
21 changed files with 147 additions and 107 deletions
@@ -64,28 +64,30 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
private static final String REFACTORING_NAME = RefactoringBundle.message("inline.variable.title");
@Override
public boolean canInlineElement(PsiElement element) {
return element instanceof PsiLocalVariable;
}
@Override
public void inlineElement(Project project, Editor editor, PsiElement element) {
final PsiReference psiReference = TargetElementUtil.findReference(editor);
final PsiReferenceExpression refExpr = psiReference instanceof PsiReferenceExpression ? ((PsiReferenceExpression)psiReference) : null;
final PsiReferenceExpression refExpr = psiReference instanceof PsiReferenceExpression ? (PsiReferenceExpression)psiReference : null;
invoke(project, editor, (PsiLocalVariable) element, refExpr);
}
/**
* should be called in AtomicAction
*/
public static void invoke(@NotNull final Project project, final Editor editor, final PsiLocalVariable local, PsiReferenceExpression refExpr) {
public static void invoke(@NotNull final Project project, final Editor editor, @NotNull PsiLocalVariable local, PsiReferenceExpression refExpr) {
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, local)) return;
final HighlightManager highlightManager = HighlightManager.getInstance(project);
final String localName = local.getName();
final List<PsiElement> innerClassesWithUsages = Collections.synchronizedList(new ArrayList<PsiElement>());
final List<PsiElement> innerClassUsages = Collections.synchronizedList(new ArrayList<PsiElement>());
final List<PsiElement> innerClassesWithUsages = Collections.synchronizedList(new ArrayList<>());
final List<PsiElement> innerClassUsages = Collections.synchronizedList(new ArrayList<>());
final PsiElement containingClass = PsiTreeUtil.getParentOfType(local, PsiClass.class, PsiLambdaExpression.class);
final Query<PsiReference> query = ReferencesSearch.search(local);
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
@@ -155,7 +157,7 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
refsToInlineList.add(innerClassUsage);
}
}
if (refsToInlineList.size() == 0) {
if (refsToInlineList.isEmpty()) {
String message = RefactoringBundle.message("variable.is.never.used.before.modification", localName);
CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.INLINE_VARIABLE);
return;
@@ -320,7 +322,7 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
}
@Nullable
public static PsiElement checkRefsInAugmentedAssignmentOrUnaryModified(final PsiElement[] refsToInline, PsiElement defToInline) {
static PsiElement checkRefsInAugmentedAssignmentOrUnaryModified(final PsiElement[] refsToInline, PsiElement defToInline) {
for (PsiElement element : refsToInline) {
PsiElement parent = element.getParent();
@@ -350,7 +352,7 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
@Nullable
static PsiExpression getDefToInline(final PsiVariable local,
final PsiElement refExpr,
final PsiCodeBlock block,
@NotNull PsiCodeBlock block,
final boolean rethrow) {
if (refExpr != null) {
PsiElement def;
@@ -20,10 +20,8 @@ import com.intellij.codeInsight.TargetElementUtil;
import com.intellij.codeInspection.sameParameterValue.SameParameterValueInspection;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
@@ -50,10 +48,10 @@ import java.util.*;
* @author yole
*/
public class InlineParameterHandler extends JavaInlineActionHandler {
private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.inline.InlineParameterHandler");
public static final String REFACTORING_NAME = RefactoringBundle.message("inline.parameter.refactoring");
public static final String REFACTORING_ID = "refactoring.inline.parameter";
private static final String REFACTORING_ID = "refactoring.inline.parameter";
@Override
public boolean canInlineElement(PsiElement element) {
if (element instanceof PsiParameter) {
final PsiElement parent = element.getParent();
@@ -66,7 +64,8 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
return false;
}
public void inlineElement(final Project project, final Editor editor, final PsiElement psiElement) {
@Override
public void inlineElement(final Project project, final Editor editor, @NotNull PsiElement psiElement) {
final PsiParameter psiParameter = (PsiParameter) psiElement;
final PsiParameterList parameterList = (PsiParameterList) psiParameter.getParent();
if (!(parameterList.getParent() instanceof PsiMethod)) {
@@ -84,8 +83,8 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
final Ref<PsiExpression> refInitializer = new Ref<>();
final Ref<PsiExpression> refConstantInitializer = new Ref<>();
final Ref<PsiCallExpression> refMethodCall = new Ref<>();
final List<PsiReference> occurrences = Collections.synchronizedList(new ArrayList<PsiReference>());
final Collection<PsiFile> containingFiles = Collections.synchronizedSet(new HashSet<PsiFile>());
final List<PsiReference> occurrences = Collections.synchronizedList(new ArrayList<>());
final Collection<PsiFile> containingFiles = Collections.synchronizedSet(new HashSet<>());
containingFiles.add(psiParameter.getContainingFile());
boolean[] result = new boolean[1];
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
@@ -111,7 +110,8 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
else if (!isSameConstant(argument, refConstantInitializer.get())) {
return false;
}
} else if (!isRecursiveReferencedParameter(argument, psiParameter)) {
}
else if (!isRecursiveReferencedParameter(argument, psiParameter)) {
if (!refConstantInitializer.isNull()) return false;
refInitializer.set(argument);
refMethodCall.set(methodCall);
@@ -123,7 +123,7 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
return;
}
final PsiReference reference = TargetElementUtil.findReference(editor);
final PsiReferenceExpression refExpr = reference instanceof PsiReferenceExpression ? ((PsiReferenceExpression)reference) : null;
final PsiReferenceExpression refExpr = reference instanceof PsiReferenceExpression ? (PsiReferenceExpression)reference : null;
final PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(refExpr, PsiCodeBlock.class);
if (codeBlock != null) {
final PsiElement[] defs = DefUseUtil.getDefs(codeBlock, psiParameter, refExpr);
@@ -268,7 +268,7 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
}
Object value1 = JavaPsiFacade.getInstance(expr1.getProject()).getConstantEvaluationHelper().computeConstantExpression(expr1);
Object value2 = JavaPsiFacade.getInstance(expr2.getProject()).getConstantEvaluationHelper().computeConstantExpression(expr2);
return value1 != null && value2 != null && value1.equals(value2);
return value1 != null && value1.equals(value2);
}
@Nullable
@@ -16,21 +16,24 @@
package com.intellij.psi.controlFlow;
import org.jetbrains.annotations.NotNull;
public abstract class BranchingInstruction extends InstructionBase {
public int offset;
@NotNull
public final Role role;
public enum Role {
THEN, ELSE, END
}
public BranchingInstruction(int offset, Role role) {
public BranchingInstruction(int offset, @NotNull Role role) {
this.offset = offset;
this.role = role;
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitBranchingInstruction(this, offset, nextOffset);
}
}
@@ -20,8 +20,8 @@ import org.jetbrains.annotations.NotNull;
public class CallInstruction extends GoToInstruction {
public final ControlFlowStack stack;
public int procBegin;
public int procEnd;
int procBegin;
int procEnd;
public CallInstruction(int procBegin, int procEnd, @NotNull ControlFlowStack stack) {
super(procBegin);
@@ -41,7 +41,7 @@ public class CallInstruction extends GoToInstruction {
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitCallInstruction(this, offset, nextOffset);
}
}
@@ -15,10 +15,12 @@
*/
package com.intellij.psi.controlFlow;
import org.jetbrains.annotations.NotNull;
public class CommentInstruction extends SimpleInstruction {
private final String myText;
public CommentInstruction(String text) {
public CommentInstruction(@NotNull String text) {
myText = text;
}
@@ -27,7 +29,7 @@ public class CommentInstruction extends SimpleInstruction {
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitCommentInstruction(this, offset, nextOffset);
}
}
@@ -17,6 +17,8 @@ package com.intellij.psi.controlFlow;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Author: msk
@@ -25,7 +27,7 @@ public abstract class ConditionalBranchingInstruction extends BranchingInstructi
protected static final Logger LOG = Logger.getInstance("#com.intellij.psi.controlFlow.ConditionalGoToInstruction");
public final PsiExpression expression;
public ConditionalBranchingInstruction(int offset, final PsiExpression expression, Role role) {
ConditionalBranchingInstruction(int offset, @Nullable PsiExpression expression, @NotNull Role role) {
super(offset, role);
this.expression = expression;
}
@@ -39,13 +41,13 @@ public abstract class ConditionalBranchingInstruction extends BranchingInstructi
case 0: return offset;
case 1: return index + 1;
default:
LOG.assertTrue (false);
LOG.assertTrue(false);
return -1;
}
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitConditionalBranchingInstruction(this, offset, nextOffset);
}
}
@@ -3,13 +3,14 @@ package com.intellij.psi.controlFlow;
import com.intellij.psi.PsiExpression;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class ConditionalGoToInstruction extends ConditionalBranchingInstruction {
public ConditionalGoToInstruction(int offset, final PsiExpression expression) {
ConditionalGoToInstruction(int offset, final PsiExpression expression) {
this(offset, Role.END, expression);
}
public ConditionalGoToInstruction(int offset, Role role, final PsiExpression expression) {
ConditionalGoToInstruction(int offset, @NotNull Role role, final PsiExpression expression) {
super(offset, expression, role);
}
@@ -19,7 +20,7 @@ public class ConditionalGoToInstruction extends ConditionalBranchingInstruction
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitConditionalGoToInstruction(this, offset, nextOffset);
}
}
@@ -1,9 +1,10 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi.controlFlow;
public class ConditionalThrowToInstruction extends ConditionalBranchingInstruction {
import org.jetbrains.annotations.NotNull;
public ConditionalThrowToInstruction(final int offset) {
public class ConditionalThrowToInstruction extends ConditionalBranchingInstruction {
ConditionalThrowToInstruction(final int offset) {
super(offset, null, Role.END);
}
@@ -12,7 +13,7 @@ public class ConditionalThrowToInstruction extends ConditionalBranchingInstructi
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitConditionalThrowToInstruction(this, offset, nextOffset);
}
}
@@ -41,20 +41,24 @@ public class DefUseUtil {
private DefUseUtil() { }
public static class Info {
@NotNull
private final PsiVariable myVariable;
@NotNull
private final PsiElement myContext;
private final boolean myIsRead;
public Info(PsiVariable variable, PsiElement context, boolean read) {
public Info(@NotNull PsiVariable variable, @NotNull PsiElement context, boolean read) {
myVariable = variable;
myContext = context;
myIsRead = read;
}
@NotNull
public PsiVariable getVariable() {
return myVariable;
}
@NotNull
public PsiElement getContext() {
return myContext;
}
@@ -66,29 +70,32 @@ public class DefUseUtil {
private static class InstructionState implements Comparable<InstructionState> {
private Set<PsiVariable> myUsed;
@NotNull
private final InstructionKey myInstructionKey;
private final List<InstructionKey> myBackwardTraces;
private boolean myIsVisited;
public InstructionState(@NotNull InstructionKey instructionKey) {
InstructionState(@NotNull InstructionKey instructionKey) {
myInstructionKey = instructionKey;
myBackwardTraces = new ArrayList<>(2);
myUsed = null;
}
public void addBackwardTrace(InstructionKey key) {
void addBackwardTrace(@NotNull InstructionKey key) {
myBackwardTraces.add(key);
}
public List<InstructionKey> getBackwardTraces() {
@NotNull
List<InstructionKey> getBackwardTraces() {
return myBackwardTraces;
}
public InstructionKey getInstructionKey() {
@NotNull
InstructionKey getInstructionKey() {
return myInstructionKey;
}
void addUsed(PsiVariable psiVariable) {
void addUsed(@NotNull PsiVariable psiVariable) {
touch();
myUsed.add(psiVariable);
}
@@ -102,7 +109,7 @@ public class DefUseUtil {
if (myUsed == null) myUsed = new THashSet<>();
}
public void addUsedFrom(InstructionState state) {
void addUsedFrom(InstructionState state) {
touch();
myUsed.addAll(state.myUsed);
}
@@ -112,7 +119,7 @@ public class DefUseUtil {
myUsed.containsAll(state.myUsed);
}
public void markVisited() {
void markVisited() {
myIsVisited = true;
}
@@ -263,12 +270,12 @@ public class DefUseUtil {
}
@NotNull
public static PsiElement[] getDefs(PsiCodeBlock body, final PsiVariable def, PsiElement ref) {
public static PsiElement[] getDefs(@NotNull PsiCodeBlock body, @NotNull PsiVariable def, @NotNull PsiElement ref) {
return getDefs(body, def, ref, false);
}
@NotNull
public static PsiElement[] getDefs(PsiCodeBlock body, final PsiVariable def, PsiElement ref, boolean rethrow) {
public static PsiElement[] getDefs(@NotNull PsiCodeBlock body, @NotNull PsiVariable def, @NotNull PsiElement ref, boolean rethrow) {
try {
RefsDefs refsDefs = new RefsDefs(body) {
private final IntArrayList[] myBackwardTraces = getBackwardTraces(instructions);
@@ -289,7 +296,7 @@ public class DefUseUtil {
}
@Override
protected void processInstruction(final Set<PsiElement> res, final Instruction instruction, int index) {
protected void processInstruction(@NotNull final Set<PsiElement> res, @NotNull final Instruction instruction, int index) {
if (instruction instanceof WriteVariableInstruction) {
WriteVariableInstruction instructionW = (WriteVariableInstruction)instruction;
if (instructionW.variable == def) {
@@ -327,7 +334,7 @@ public class DefUseUtil {
}
@NotNull
public static PsiElement[] getRefs(PsiCodeBlock body, final PsiVariable def, PsiElement ref) {
public static PsiElement[] getRefs(@NotNull PsiCodeBlock body, @NotNull PsiVariable def, @NotNull PsiElement ref) {
try {
RefsDefs refsDefs = new RefsDefs(body) {
@Override
@@ -346,7 +353,7 @@ public class DefUseUtil {
}
@Override
protected void processInstruction(final Set<PsiElement> res, final Instruction instruction, int index) {
protected void processInstruction(@NotNull final Set<PsiElement> res, @NotNull final Instruction instruction, int index) {
if (instruction instanceof ReadVariableInstruction) {
ReadVariableInstruction instructionR = (ReadVariableInstruction)instruction;
if (instructionR.variable == def) {
@@ -375,22 +382,23 @@ public class DefUseUtil {
protected abstract int nNext(int index);
protected abstract int getNext(int index, int no);
@NotNull
final List<Instruction> instructions;
final ControlFlow flow;
final PsiCodeBlock body;
protected RefsDefs(PsiCodeBlock body) throws AnalysisCanceledException {
RefsDefs(@NotNull PsiCodeBlock body) throws AnalysisCanceledException {
this.body = body;
flow = ControlFlowFactory.getInstance(body.getProject()).getControlFlow(body, ourPolicy);
instructions = flow.getInstructions();
}
protected abstract void processInstruction(Set<PsiElement> res, final Instruction instruction, int index);
protected abstract void processInstruction(@NotNull Set<PsiElement> res, @NotNull Instruction instruction, int index);
protected abstract boolean defs ();
@NotNull
private PsiElement[] get (final PsiVariable def, PsiElement refOrDef) {
private PsiElement[] get(@NotNull PsiVariable def, @NotNull PsiElement refOrDef) {
if (body == null) {
return PsiElement.EMPTY_ARRAY;
}
@@ -413,8 +421,7 @@ public class DefUseUtil {
final Set<PsiElement> res = new THashSet<>();
class Inner {
void traverse (int index) {
private void traverse(int index) {
visited [index] = true;
if (defs ()) {
@@ -444,7 +451,8 @@ public class DefUseUtil {
if (instructionW.variable == def) {
continue;
}
} else {
}
else {
processInstruction(res, instruction, prev);
}
}
@@ -454,7 +462,7 @@ public class DefUseUtil {
}
}
}
new Inner ().traverse (elem);
new Inner ().traverse(elem);
return PsiUtilCore.toPsiElementArray(res);
}
return PsiElement.EMPTY_ARRAY;
@@ -463,7 +471,7 @@ public class DefUseUtil {
@NotNull
private static IntArrayList[] getBackwardTraces(final List<Instruction> instructions) {
private static IntArrayList[] getBackwardTraces(@NotNull List<Instruction> instructions) {
final IntArrayList[] states = new IntArrayList[instructions.size()];
for (int i = 0; i < states.length; i++) {
states[i] = new IntArrayList();
@@ -491,15 +499,17 @@ public class DefUseUtil {
myNext = new Stack<>(size);
}
void push(InstructionKey fromKey, InstructionKey nextKey) {
void push(@NotNull InstructionKey fromKey, @NotNull InstructionKey nextKey) {
myFrom.push(fromKey);
myNext.push(nextKey);
}
@NotNull
InstructionKey peekFrom() {
return myFrom.peek();
}
@NotNull
InstructionKey popNext() {
myFrom.pop();
return myNext.pop();
@@ -525,12 +535,13 @@ public class DefUseUtil {
private final WalkThroughStack myWalkThroughStack;
private final List<Instruction> myInstructions;
private InstructionStateWalker(List<Instruction> instructions) {
private InstructionStateWalker(@NotNull List<Instruction> instructions) {
myStates = new THashMap<>(instructions.size());
myWalkThroughStack = new WalkThroughStack(instructions.size() / 2);
myInstructions = instructions;
}
@NotNull
private Map<InstructionKey, InstructionState> walk() {
InstructionKey startKey = InstructionKey.create(0);
myStates.put(startKey, new InstructionState(startKey));
@@ -550,7 +561,7 @@ public class DefUseUtil {
return myStates;
}
private void visit(InstructionKey fromKey) {
private void visit(@NotNull InstructionKey fromKey) {
if (fromKey.getOffset() >= myInstructions.size()) return;
final Instruction instruction = myInstructions.get(fromKey.getOffset());
if (instruction instanceof CallInstruction) {
@@ -574,7 +585,7 @@ public class DefUseUtil {
}
}
private void addBackwardTrace(InstructionKey fromKey, InstructionKey nextKey) {
private void addBackwardTrace(@NotNull InstructionKey fromKey, @NotNull InstructionKey nextKey) {
if (fromKey.getOffset() >= 0 && nextKey.getOffset() < myInstructions.size()) {
InstructionState state = myStates.get(nextKey);
if (state == null) myStates.put(nextKey, state = new InstructionState(nextKey));
@@ -582,7 +593,8 @@ public class DefUseUtil {
}
}
static Map<InstructionKey, InstructionState> getStates(final List<Instruction> instructions) {
@NotNull
static Map<InstructionKey, InstructionState> getStates(@NotNull List<Instruction> instructions) {
return new InstructionStateWalker(instructions).walk();
}
}
@@ -15,6 +15,8 @@
*/
package com.intellij.psi.controlFlow;
import org.jetbrains.annotations.NotNull;
public class EmptyInstruction extends SimpleInstruction {
public static final EmptyInstruction INSTANCE = new EmptyInstruction();
@@ -26,7 +28,7 @@ public class EmptyInstruction extends SimpleInstruction {
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitEmptyInstruction(this, offset, nextOffset);
}
}
@@ -16,19 +16,20 @@
package com.intellij.psi.controlFlow;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull;
public class GoToInstruction extends BranchingInstruction {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.controlFlow.GoToInstruction");
public final boolean isReturn; //true if goto has been generated as a result of return statement
public GoToInstruction(int offset) {
GoToInstruction(int offset) {
this(offset, BranchingInstruction.Role.END);
}
public GoToInstruction(int offset, Role role) {
GoToInstruction(int offset, @NotNull Role role) {
this (offset,role,false);
}
public GoToInstruction(int offset, Role role, boolean isReturn) {
GoToInstruction(int offset, @NotNull Role role, boolean isReturn) {
super(offset, role);
this.isReturn = isReturn;
}
@@ -49,7 +50,7 @@ public class GoToInstruction extends BranchingInstruction {
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitGoToInstruction(this, offset, nextOffset);
}
}
@@ -15,10 +15,15 @@
*/
package com.intellij.psi.controlFlow;
public interface Instruction extends Cloneable {
Instruction clone();
int nNext ();
int getNext (int index, int no);
import org.jetbrains.annotations.NotNull;
void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset);
public interface Instruction extends Cloneable {
@NotNull
Instruction clone();
int nNext();
int getNext(int index, int no);
void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset);
}
@@ -17,15 +17,17 @@
package com.intellij.psi.controlFlow;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public abstract class InstructionBase implements Instruction, Cloneable{
@NotNull
@Override
public Instruction clone() {
try {
return (Instruction)super.clone();
}
catch (CloneNotSupportedException e) {
return null;
throw new RuntimeException(e);
}
}
@@ -37,10 +37,12 @@ class InstructionKey implements Comparable<InstructionKey> {
return new InstructionKey(offset, ArrayUtil.EMPTY_INT_ARRAY);
}
@NotNull
InstructionKey next(int nextOffset) {
return new InstructionKey(nextOffset, myCallStack);
}
@NotNull
InstructionKey push(int nextOffset, int returnOffset) {
if(myCallStack.length > 100) { // normally it's way below 100, as it's the number of levels of nested 'finally' blocks
throw new OverflowException(myOffset); // most likely the graph traversal is in an endless loop
@@ -49,6 +51,7 @@ class InstructionKey implements Comparable<InstructionKey> {
return new InstructionKey(nextOffset, nextStack);
}
@NotNull
InstructionKey pop(int overriddenOffset) {
int returnOffset = myCallStack[myCallStack.length - 1];
int[] nextStack = ArrayUtil.realloc(myCallStack, myCallStack.length - 1);
@@ -105,7 +108,7 @@ class InstructionKey implements Comparable<InstructionKey> {
}
static class OverflowException extends RuntimeException {
public OverflowException(int offset) {
OverflowException(int offset) {
super("Instruction key overflow at offset " + offset);
}
}
@@ -19,6 +19,7 @@ import com.intellij.psi.PsiVariable;
import org.jetbrains.annotations.NotNull;
class ReadVariableInstruction extends SimpleInstruction {
@NotNull
public final PsiVariable variable;
ReadVariableInstruction(@NotNull PsiVariable variable) {
@@ -30,7 +31,7 @@ class ReadVariableInstruction extends SimpleInstruction {
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitReadVariableInstruction(this, offset, nextOffset);
}
}
@@ -22,11 +22,11 @@ import org.jetbrains.annotations.NotNull;
public class ReturnInstruction extends GoToInstruction {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.controlFlow.ReturnInstruction");
private final ControlFlowStack myStack;
private CallInstruction myCallInstruction;
@NotNull private final ControlFlowStack myStack;
@NotNull private CallInstruction myCallInstruction;
private boolean myRethrowFromFinally;
public ReturnInstruction(int offset, @NotNull ControlFlowStack stack, CallInstruction callInstruction) {
public ReturnInstruction(int offset, @NotNull ControlFlowStack stack, @NotNull CallInstruction callInstruction) {
super(offset, Role.END, false);
myStack = stack;
myCallInstruction = callInstruction;
@@ -50,7 +50,7 @@ public class ReturnInstruction extends GoToInstruction {
}
@NotNull
public int[] getPossibleReturnOffsets() {
int[] getPossibleReturnOffsets() {
return offset == 0 ?
new int[]{
getProcBegin() - 5, // call normal
@@ -64,15 +64,15 @@ public class ReturnInstruction extends GoToInstruction {
}
public int getProcBegin() {
int getProcBegin() {
return myCallInstruction.procBegin;
}
public int getProcEnd() {
int getProcEnd() {
return myCallInstruction.procEnd;
}
public void setCallInstruction(CallInstruction callInstruction) {
void setCallInstruction(@NotNull CallInstruction callInstruction) {
myCallInstruction = callInstruction;
}
@@ -101,19 +101,20 @@ public class ReturnInstruction extends GoToInstruction {
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitReturnInstruction(this, offset, nextOffset);
}
@NotNull
public ControlFlowStack getStack() {
return myStack;
}
public void setRethrowFromFinally() {
void setRethrowFromFinally() {
myRethrowFromFinally = true;
}
public boolean isRethrowFromFinally() {
boolean isRethrowFromFinally() {
return myRethrowFromFinally;
}
}
@@ -17,6 +17,7 @@
package com.intellij.psi.controlFlow;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull;
public abstract class SimpleInstruction extends InstructionBase {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.controlFlow.SimpleInstruction");
@@ -31,7 +32,7 @@ public abstract class SimpleInstruction extends InstructionBase {
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitSimpleInstruction(this, offset, nextOffset);
}
}
@@ -16,13 +16,14 @@
package com.intellij.psi.controlFlow;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull;
public class ThrowToInstruction extends BranchingInstruction {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.controlFlow.ThrowToInstruction");
public ThrowToInstruction(int offset) {
ThrowToInstruction(int offset) {
super(offset, Role.END);
}
@@ -40,7 +41,7 @@ public class ThrowToInstruction extends BranchingInstruction {
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitThrowToInstruction(this, offset, nextOffset);
}
}
@@ -19,6 +19,7 @@ import com.intellij.psi.PsiVariable;
import org.jetbrains.annotations.NotNull;
public class WriteVariableInstruction extends SimpleInstruction {
@NotNull
public final PsiVariable variable;
WriteVariableInstruction(@NotNull PsiVariable variable) {
@@ -30,7 +31,7 @@ public class WriteVariableInstruction extends SimpleInstruction {
}
@Override
public void accept(ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
public void accept(@NotNull ControlFlowInstructionVisitor visitor, int offset, int nextOffset) {
visitor.visitWriteVariableInstruction(this, offset, nextOffset);
}
}
@@ -78,7 +78,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
return new StringConcatenationInLoopsVisitor();
}
static PsiLoopStatement getOutermostCommonLoop(PsiExpression expression, PsiVariable variable) {
private static PsiLoopStatement getOutermostCommonLoop(@NotNull PsiExpression expression, @NotNull PsiVariable variable) {
PsiElement stopAt = null;
PsiCodeBlock block = StringConcatenationInLoopsVisitor.getSurroundingBlock(expression);
if (block != null) {
@@ -176,7 +176,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
}
private static boolean isUsedCompletely(PsiVariable variable, PsiLoopStatement loop) {
boolean notUsedCompletely = ReferencesSearch.search(variable, new LocalSearchScope(loop)).forEach(ref -> {
return !ReferencesSearch.search(variable, new LocalSearchScope(loop)).forEach(ref -> {
PsiExpression expression = ObjectUtils.tryCast(ref.getElement(), PsiExpression.class);
if (expression == null) return true;
PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
@@ -184,14 +184,13 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
parent = PsiUtil.skipParenthesizedExprUp(parent.getParent());
}
if (parent instanceof PsiExpressionList ||
(parent instanceof PsiAssignmentExpression &&
PsiTreeUtil.isAncestor(((PsiAssignmentExpression)parent).getRExpression(), expression, false))) {
parent instanceof PsiAssignmentExpression &&
PsiTreeUtil.isAncestor(((PsiAssignmentExpression)parent).getRExpression(), expression, false)) {
PsiStatement statement = PsiTreeUtil.getParentOfType(parent, PsiStatement.class);
return ControlFlowUtils.isExecutedOnceInLoop(statement, loop) || ControlFlowUtils.isVariableReassigned(statement, variable);
}
return true;
});
return !notUsedCompletely;
}
@Nullable
@@ -199,9 +198,11 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
PsiElement parent = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, PsiClassInitializer.class, PsiLambdaExpression.class);
if(parent instanceof PsiMethod) {
return ((PsiMethod)parent).getBody();
} else if(parent instanceof PsiClassInitializer) {
}
if(parent instanceof PsiClassInitializer) {
return ((PsiClassInitializer)parent).getBody();
} else if(parent instanceof PsiLambdaExpression) {
}
if(parent instanceof PsiLambdaExpression) {
PsiElement body = ((PsiLambdaExpression)parent).getBody();
if(body instanceof PsiCodeBlock) {
return (PsiCodeBlock)body;
@@ -338,11 +339,10 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
PsiExpression rExpression = assignment.getRExpression();
return rExpression == null || NullnessUtil.getExpressionNullness(rExpression, true) == Nullness.NOT_NULL;
};
boolean notNull = ReferencesSearch.search(var).forEach(isNotNullableWrite);
return !notNull;
return !ReferencesSearch.search(var).forEach(isNotNullableWrite);
}
static abstract class AbstractStringBuilderFix extends InspectionGadgetsFix {
abstract static class AbstractStringBuilderFix extends InspectionGadgetsFix {
static final Pattern PRINT_OR_PRINTLN = Pattern.compile("print|println");
final String myName;
@@ -350,7 +350,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
final boolean myNullSafe;
Set<PsiExpression> myNullables = Collections.emptySet();
AbstractStringBuilderFix(PsiVariable variable, boolean nullSafe) {
AbstractStringBuilderFix(@NotNull PsiVariable variable, boolean nullSafe) {
myName = variable.getName();
myTargetType = PsiUtil.isLanguageLevel5OrHigher(variable) ?
CommonClassNames.JAVA_LANG_STRING_BUILDER : CommonClassNames.JAVA_LANG_STRING_BUFFER;
@@ -534,7 +534,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
case "lastIndexOf":
if(args.length >= 1 && args.length <= 2 && TypeUtils.isJavaLangString(args[0].getType())) return;
break;
case "isEmpty": {
case "isEmpty":
String sign = "==";
PsiExpression negation = BoolUtils.findNegation(call);
PsiElement toReplace = call;
@@ -551,7 +551,6 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
}
ct.replace(toReplace, emptyCheck);
return;
}
default:
}
}
@@ -644,7 +643,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
}
static class IntroduceStringBuilderFix extends AbstractStringBuilderFix {
public IntroduceStringBuilderFix(PsiVariable variable, boolean nullSafe) {
IntroduceStringBuilderFix(@NotNull PsiVariable variable, boolean nullSafe) {
super(variable, nullSafe);
}
@@ -731,7 +730,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
}
static class ReplaceWithStringBuilderFix extends AbstractStringBuilderFix {
public ReplaceWithStringBuilderFix(PsiVariable variable, boolean nullSafe) {
ReplaceWithStringBuilderFix(@NotNull PsiVariable variable, boolean nullSafe) {
super(variable, nullSafe);
}
@@ -120,7 +120,7 @@ public class InvalidPropertyKeyInspection extends AbstractBaseJavaLocalInspectio
private final boolean onTheFly;
public UnresolvedPropertyVisitor(final InspectionManager manager, boolean onTheFly) {
UnresolvedPropertyVisitor(final InspectionManager manager, boolean onTheFly) {
myManager = manager;
this.onTheFly = onTheFly;
}
@@ -160,14 +160,14 @@ public class InvalidPropertyKeyInspection extends AbstractBaseJavaLocalInspectio
final PsiExpression initializer = field.getInitializer();
String key = computeStringValue(initializer);
visitPropertyKeyAnnotationParameter(expression, key,
(field.getContainingFile() == expression.getContainingFile()) ? initializer : expression);
field.getContainingFile() == expression.getContainingFile() ? initializer : expression);
}
else if (resolvedExpression instanceof PsiLocalVariable) {
checkLocalVariable((PsiLocalVariable)resolvedExpression, expression);
}
}
private void checkLocalVariable(PsiLocalVariable variable, PsiReferenceExpression expression) {
private void checkLocalVariable(@NotNull PsiLocalVariable variable, PsiReferenceExpression expression) {
PsiCodeBlock block = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class);
final PsiElement[] defs = DefUseUtil.getDefs(block, variable, expression);
for (PsiElement def : defs) {
@@ -237,7 +237,7 @@ public class InvalidPropertyKeyInspection extends AbstractBaseJavaLocalInspectio
if (!JavaI18nUtil.mustBePropertyKey(expression, annotationParams)) return;
final SortedSet<Integer> paramsCount = JavaI18nUtil.getPropertyValueParamsCount(highlightedExpression, resourceBundleName.get());
if (paramsCount.isEmpty() || (paramsCount.size() != 1 && resourceBundleName.get() == null)) {
if (paramsCount.isEmpty() || paramsCount.size() != 1 && resourceBundleName.get() == null) {
return;
}
@@ -315,9 +315,9 @@ public class InvalidPropertyKeyInspection extends AbstractBaseJavaLocalInspectio
PsiElement parent = expression.getParent();
while (true) {
if (parent instanceof PsiParenthesizedExpression ||
(parent instanceof PsiConditionalExpression &&
parent instanceof PsiConditionalExpression &&
(expression == ((PsiConditionalExpression)parent).getThenExpression() ||
expression == ((PsiConditionalExpression)parent).getElseExpression()))) {
expression == ((PsiConditionalExpression)parent).getElseExpression())) {
expression = (PsiExpression)parent;
parent = expression.getParent();
}