private boolean isValid(List<Name> variables) { int assignCounter = 0; for (Name variable : variables) { if (variable.parent instanceof Assign) { Assign assign = (Assign) variable.parent; /* The given name has to be one of the targets of this assignment */ for (exprType x : assign.targets) { if (x == variable) { assignCounter++; break; } } } if (variable.ctx == expr_contextType.Param || variable.ctx == expr_contextType.KwOnlyParam) { status.addFatalError(Messages.format(Messages.inlineLocalParameter, variable.getId())); return false; } } if (assignCounter > 1) { status.addFatalError( Messages.format(Messages.inlineLocalMultipleAssignments, variables.get(0).getId())); return false; } return true; }
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { AbstractScopeNode<?> scope = info.getScopeAdapter(); ITextSelection selection = info.getUserSelection(); SimpleNode node = scope.getASTNode(); LocalVariablesVisitor visitor = new LocalVariablesVisitor(); try { node.accept(visitor); } catch (Exception e) { throw new RuntimeException(e); } List<Name> variables = visitor.getVariables(); Name selectedVariable = findSelectedVariable(selection, variables); if (selectedVariable == null) { status.addFatalError(Messages.validationNoNameSelected); return status; } List<Name> relatedVariables = findAllRelatedVariables(variables, selectedVariable); Assign assignment = findAssignment(relatedVariables); if (assignment == null) { String id = selectedVariable.id; status.addFatalError(Messages.format(Messages.inlinelocalNoAssignment, id)); return status; } if (!isValid(relatedVariables)) { return status; } requestProcessor.setAssign(assignment); requestProcessor.setVariables(relatedVariables); return status; }