Refactoring: base class extracted from PreliminaryLoopVisitor

This commit is contained in:
Mikhail Glukhikh
2015-09-11 12:52:47 +03:00
parent aa3a7c2838
commit 8348f204cd
2 changed files with 43 additions and 27 deletions
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2015 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 org.jetbrains.kotlin.types.expressions
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.JetBinaryExpression
import org.jetbrains.kotlin.psi.JetNameReferenceExpression
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.psi.JetTreeVisitorVoid
import java.util.*
abstract class AssignedVariablesSearcher: JetTreeVisitorVoid() {
protected val assignedNames: MutableSet<Name> = LinkedHashSet()
override fun visitBinaryExpression(binaryExpression: JetBinaryExpression) {
if (binaryExpression.operationToken === JetTokens.EQ) {
val left = JetPsiUtil.deparenthesize(binaryExpression.left)
if (left is JetNameReferenceExpression) {
assignedNames += left.getReferencedNameAsName()
}
}
}
}
@@ -18,8 +18,6 @@ package org.jetbrains.kotlin.types.expressions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue;
@@ -31,20 +29,11 @@ import java.util.*;
* The purpose of this class is to find all variable assignments
* <b>before</b> loop analysis
*/
class PreliminaryLoopVisitor extends JetTreeVisitor<Void> {
// loop under analysis
private final JetLoopExpression loopExpression;
private final Set<Name> assignedNames = new LinkedHashSet<Name>();
private PreliminaryLoopVisitor(@NotNull JetLoopExpression loopExpression) {
this.loopExpression = loopExpression;
}
class PreliminaryLoopVisitor extends AssignedVariablesSearcher {
@NotNull
static public PreliminaryLoopVisitor visitLoop(@NotNull JetLoopExpression loopExpression) {
PreliminaryLoopVisitor visitor = new PreliminaryLoopVisitor(loopExpression);
PreliminaryLoopVisitor visitor = new PreliminaryLoopVisitor();
loopExpression.accept(visitor, null);
return visitor;
}
@@ -57,7 +46,7 @@ class PreliminaryLoopVisitor extends JetTreeVisitor<Void> {
// Only uncaptured local variables are under interest here
if (value.isUncapturedLocalVariable() && value.getId() instanceof LocalVariableDescriptor) {
LocalVariableDescriptor descriptor = (LocalVariableDescriptor)value.getId();
if (assignedNames.contains(descriptor.getName())) {
if (getAssignedNames().contains(descriptor.getName())) {
valueSetToClear.add(value);
}
}
@@ -67,17 +56,4 @@ class PreliminaryLoopVisitor extends JetTreeVisitor<Void> {
}
return dataFlowInfo;
}
@Override
public Void visitBinaryExpression(@NotNull JetBinaryExpression binaryExpression, Void arg) {
if (binaryExpression.getOperationToken() == JetTokens.EQ) {
JetExpression left = JetPsiUtil.deparenthesize(binaryExpression.getLeft());
if (left instanceof JetNameReferenceExpression) {
// deparenthesize
assignedNames.add(((JetNameReferenceExpression) left).getReferencedNameAsName());
}
}
return null;
}
}