/**
  * @return if given unqualified {@link SearchMatch} intersects with visibility range of {@link
  *     LocalElement}.
  */
 protected static boolean isReferenceInLocalRange(
     LocalElement localElement, SearchMatch reference) {
   if (reference.isQualified()) {
     return false;
   }
   Source localSource = localElement.getSource();
   Source referenceSource = reference.getElement().getSource();
   SourceRange localRange = localElement.getVisibleRange();
   SourceRange referenceRange = reference.getSourceRange();
   return Objects.equal(referenceSource, localSource) && referenceRange.intersects(localRange);
 }
 /**
  * @return {@code true} if two given {@link Element}s are {@link LocalElement}s and have
  *     intersecting with visibility ranges.
  */
 protected static boolean haveIntersectingRanges(LocalElement localElement, Element element) {
   if (!(element instanceof LocalElement)) {
     return false;
   }
   LocalElement localElement2 = (LocalElement) element;
   Source localSource = localElement.getSource();
   Source localSource2 = localElement2.getSource();
   SourceRange localRange = localElement.getVisibleRange();
   SourceRange localRange2 = localElement2.getVisibleRange();
   return Objects.equal(localSource2, localSource)
       && localRange != null
       && localRange2 != null
       && localRange2.intersects(localRange);
 }
 private RefactoringStatus analyzePossibleConflicts(ProgressMonitor pm) {
   pm.beginTask("Analyze possible conflicts", 1);
   try {
     final RefactoringStatus result = new RefactoringStatus();
     // checks the resolved CompilationUnit(s)
     Source unitSource = element.getSource();
     Source[] librarySources = context.getLibrariesContaining(unitSource);
     for (Source librarySource : librarySources) {
       analyzePossibleConflicts_inLibrary(result, unitSource, librarySource);
     }
     pm.worked(1);
     // done
     return result;
   } finally {
     pm.done();
   }
 }
 private void analyzePossibleConflicts_inLibrary(
     final RefactoringStatus result, Source unitSource, Source librarySource) {
   // prepare resolved unit
   CompilationUnit unit = null;
   try {
     unit = context.resolveCompilationUnit(unitSource, librarySource);
   } catch (AnalysisException e) {
   }
   if (unit == null) {
     return;
   }
   // check for conflicts in the unit
   final SourceRange elementRange = element.getVisibleRange();
   unit.accept(
       new RecursiveAstVisitor<Void>() {
         @Override
         public Void visitSimpleIdentifier(SimpleIdentifier node) {
           Element nameElement = node.getBestElement();
           if (nameElement != null && nameElement.getName().equals(newName)) {
             // duplicate declaration
             if (haveIntersectingRanges(element, nameElement)) {
               String message =
                   MessageFormat.format(
                       "Duplicate local {0} ''{1}''.", getElementKindName(nameElement), newName);
               result.addError(message, new RefactoringStatusContext(nameElement));
               return null;
             }
             // shadowing referenced element
             if (elementRange.contains(node.getOffset()) && !node.isQualified()) {
               nameElement = HierarchyUtils.getSyntheticAccessorVariable(nameElement);
               String nameElementSourceName = nameElement.getSource().getShortName();
               String message =
                   MessageFormat.format(
                       "Usage of {0} ''{1}'' declared in ''{2}'' will be shadowed by renamed {3}.",
                       getElementKindName(nameElement),
                       getElementQualifiedName(nameElement),
                       nameElementSourceName,
                       getElementKindName(element));
               result.addError(message, new RefactoringStatusContext(node));
             }
           }
           return null;
         }
       });
 }
 @Override
 public Change createChange(ProgressMonitor pm) throws Exception {
   pm = checkProgressMonitor(pm);
   SourceChangeManager changeManager = new SourceChangeManager();
   // update declaration
   {
     Source source = element.getSource();
     SourceChange change = changeManager.get(source);
     addDeclarationEdit(change, element);
   }
   // update references
   List<SearchMatch> refMatches = searchEngine.searchReferences(element, null, null);
   List<SourceReference> references = getSourceReferences(refMatches);
   for (SourceReference reference : references) {
     SourceChange refChange = changeManager.get(reference.source);
     addReferenceEdit(refChange, reference);
   }
   // prepare change
   return new CompositeChange(getRefactoringName(), changeManager.getChanges());
 }
 @Override
 public boolean shouldReportUnsafeRefactoringSource(AnalysisContext context, Source source) {
   return element.getSource().equals(source);
 }