Refactoring: base class extracted from PreliminaryLoopVisitor
This commit is contained in:
+40
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+3
-27
@@ -18,8 +18,6 @@ package org.jetbrains.kotlin.types.expressions;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor;
|
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.psi.*;
|
||||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
|
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
|
||||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue;
|
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
|
* The purpose of this class is to find all variable assignments
|
||||||
* <b>before</b> loop analysis
|
* <b>before</b> loop analysis
|
||||||
*/
|
*/
|
||||||
class PreliminaryLoopVisitor extends JetTreeVisitor<Void> {
|
class PreliminaryLoopVisitor extends AssignedVariablesSearcher {
|
||||||
|
|
||||||
// loop under analysis
|
|
||||||
private final JetLoopExpression loopExpression;
|
|
||||||
|
|
||||||
private final Set<Name> assignedNames = new LinkedHashSet<Name>();
|
|
||||||
|
|
||||||
private PreliminaryLoopVisitor(@NotNull JetLoopExpression loopExpression) {
|
|
||||||
this.loopExpression = loopExpression;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
static public PreliminaryLoopVisitor visitLoop(@NotNull JetLoopExpression loopExpression) {
|
static public PreliminaryLoopVisitor visitLoop(@NotNull JetLoopExpression loopExpression) {
|
||||||
PreliminaryLoopVisitor visitor = new PreliminaryLoopVisitor(loopExpression);
|
PreliminaryLoopVisitor visitor = new PreliminaryLoopVisitor();
|
||||||
loopExpression.accept(visitor, null);
|
loopExpression.accept(visitor, null);
|
||||||
return visitor;
|
return visitor;
|
||||||
}
|
}
|
||||||
@@ -57,7 +46,7 @@ class PreliminaryLoopVisitor extends JetTreeVisitor<Void> {
|
|||||||
// Only uncaptured local variables are under interest here
|
// Only uncaptured local variables are under interest here
|
||||||
if (value.isUncapturedLocalVariable() && value.getId() instanceof LocalVariableDescriptor) {
|
if (value.isUncapturedLocalVariable() && value.getId() instanceof LocalVariableDescriptor) {
|
||||||
LocalVariableDescriptor descriptor = (LocalVariableDescriptor)value.getId();
|
LocalVariableDescriptor descriptor = (LocalVariableDescriptor)value.getId();
|
||||||
if (assignedNames.contains(descriptor.getName())) {
|
if (getAssignedNames().contains(descriptor.getName())) {
|
||||||
valueSetToClear.add(value);
|
valueSetToClear.add(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,17 +56,4 @@ class PreliminaryLoopVisitor extends JetTreeVisitor<Void> {
|
|||||||
}
|
}
|
||||||
return dataFlowInfo;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user