/** * This method performs checks on the constant name which are quick enough to be performed every * time the ui input component contents are changed. * * @return return the resulting status * @throws JavaModelException thrown when the operation could not be executed */ public RefactoringStatus checkConstantNameOnChange() throws JavaModelException { if (Arrays.asList(getExcludedVariableNames()).contains(fConstantName)) return RefactoringStatus.createErrorStatus( Messages.format( RefactoringCoreMessages.ExtractConstantRefactoring_another_variable, BasicElementLabels.getJavaElementName(getConstantName()))); return Checks.checkConstantName(fConstantName, fCu); }
@Override /** * Checks the final conditions in order to perform the refactoring. Specifically, we check if the * variable is used in only a single lower scope than it is declared in, and if this check fails * for any reason, the appropriate error status is set. */ public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { RefactoringStatus status = new RefactoringStatus(); ICompilationUnit Unit = method.getCompilationUnit(); compilationUnit = parse(Unit, pm); try { pm.beginTask("Checking validity...", 1); // Do the work to check the final conditions variableVisitor = new LocalVarVisitor(this); variableVisitor.visit(compilationUnit); compilationUnit.accept(variableVisitor); // Now check for any errors that may be present ASTNode sp = variableVisitor.getScopeParent(); ASTNode lsp = variableVisitor.getLowerScopeParent(); if (sp == null) { // the variable was never found setRefactoringStatus(VARIABLE_NOT_FOUND); } else if (lsp == null) { setRefactoringStatus(NO_SCOPE_TO_REDUCE_TO); } if (refactoringStatus == VARIABLE_USED_IN_DECLARING_SCOPE) { status.merge( RefactoringStatus.createErrorStatus( varName + " in method " + method.getElementName() + " is used in the same scope it is declared in.")); } else if (refactoringStatus == VARIABLE_IS_FIELD) { status.merge( RefactoringStatus.createErrorStatus( varName + " is a field variable and" + " its scope cannot be reduced.")); } else if (refactoringStatus == NO_SCOPE_TO_REDUCE_TO) { status.merge( RefactoringStatus.createErrorStatus( "No scope to possibly reduce " + varName + " in method " + method.getElementName() + " to.")); } else if (refactoringStatus == VARIABLE_USED_IN_MULTIPLE_LOWER_SCOPES) { status.merge( RefactoringStatus.createErrorStatus( varName + " is used in multiple lower scopes" + " and cannot be reduced in method " + method.getElementName())); } else if (refactoringStatus == VARIABLE_FIRST_INITIALIZED_IN_FOR_DECLARATION) { status.merge( RefactoringStatus.createErrorStatus( varName + " is first initialized in a for loop" + " and cannot be reduced in method " + method.getElementName())); } else if (refactoringStatus == VARIABLE_NOT_FOUND) { status.merge( RefactoringStatus.createErrorStatus(varName + " not found in method " + method)); } } finally { pm.done(); } return status; }