Initial work on Bug 395013.
diff --git a/org.eclipse.ajdt.ui.tests/src/org/eclipse/ajdt/ui/tests/FindFilesForLocationURITests.java b/org.eclipse.ajdt.ui.tests/src/org/eclipse/ajdt/ui/tests/FindFilesForLocationURITests.java
new file mode 100644
index 0000000..fdae502
--- /dev/null
+++ b/org.eclipse.ajdt.ui.tests/src/org/eclipse/ajdt/ui/tests/FindFilesForLocationURITests.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2011 SpringSource, a division of VMware, Inc
+ * 
+ * andrew - Initial API and implementation
+ *
+ * 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.eclipse.ajdt.ui.tests;
+
+import java.io.StringReader;
+import java.net.URI;
+
+import junit.framework.TestCase;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+
+/**
+ * 
+ * @author Andrew Eisenberg
+ * @created Nov 19, 2012
+ */
+public class FindFilesForLocationURITests extends UITestCase {
+    class StringInputStream extends ReaderInputStream {
+
+        public StringInputStream(String s) {
+            super(new StringReader(s));
+        }
+
+    }
+    
+    private IFile file;
+    private URI uri;
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        final IProject project = getRoot().getProject("Proj");
+        if (! project.exists()) {
+            project.create(null);
+            project.open(null);
+        }       
+        file = project.getFile("foo.txt");
+        if (!file.exists()) {
+            file.create(new StringInputStream("foo"), true, null);
+        }
+        uri = file.getLocation().toFile().toURI();
+    }
+    private IWorkspaceRoot getRoot() {
+        return ResourcesPlugin.getWorkspace().getRoot();
+    }
+    public void testFindFilesForLocationURI() throws Exception {
+        long start = System.currentTimeMillis();
+        for (int i = 0; i < 1000; i++) {
+            getRoot().findFilesForLocationURI(uri);
+        }
+        long end = System.currentTimeMillis();
+        System.out.println("testFindFilesForLocationURI took " + (end - start) );
+    }
+    public void testGetFileForLocation() throws Exception {
+        long start = System.currentTimeMillis();
+        for (int i = 0; i < 1000; i++) {
+            getRoot().getFileForLocation(file.getLocation());
+        }
+        long end = System.currentTimeMillis();
+        System.out.println("testGetFileForLocation took " + (end - start) );
+    }
+}
diff --git a/org.eclipse.ajdt.ui/src/org/eclipse/ajdt/internal/ui/refactoring/ITDRenameRefactoringProvider.java b/org.eclipse.ajdt.ui/src/org/eclipse/ajdt/internal/ui/refactoring/ITDRenameRefactoringProvider.java
index c67f607..4e1953b 100644
--- a/org.eclipse.ajdt.ui/src/org/eclipse/ajdt/internal/ui/refactoring/ITDRenameRefactoringProvider.java
+++ b/org.eclipse.ajdt.ui/src/org/eclipse/ajdt/internal/ui/refactoring/ITDRenameRefactoringProvider.java
@@ -18,6 +18,7 @@
 import org.eclipse.ajdt.core.codeconversion.AspectsConvertingParser;
 import org.eclipse.ajdt.core.codeconversion.AspectsConvertingParser.Replacement;
 import org.eclipse.ajdt.core.codeconversion.ConversionOptions;
+import org.eclipse.ajdt.core.codeconversion.ITDAwareNameEnvironment;
 import org.eclipse.ajdt.core.javaelements.AJCompilationUnit;
 import org.eclipse.ajdt.core.javaelements.IntertypeElement;
 import org.eclipse.contribution.jdt.refactoring.IRefactoringProvider;
@@ -28,11 +29,14 @@
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.ITypeRoot;
 import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.WorkingCopyOwner;
 import org.eclipse.jdt.core.dom.AST;
 import org.eclipse.jdt.core.dom.ASTNode;
 import org.eclipse.jdt.core.dom.ASTParser;
 import org.eclipse.jdt.core.dom.ASTVisitor;
 import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.internal.core.CancelableNameEnvironment;
+import org.eclipse.jdt.internal.core.JavaProject;
 import org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor;
 import org.eclipse.jdt.internal.ui.refactoring.UserInterfaceManager;
 import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameUserInterfaceManager;
@@ -145,8 +149,8 @@
         return null;
     }
 
-    public boolean inInterestingProject(ICompilationUnit unit) {
-        IProject project = unit.getJavaProject().getProject();
+    public boolean inInterestingProject(IJavaElement elt) {
+        IProject project = elt.getJavaProject().getProject();
         return AspectJPlugin.isAJProject(project);
     }
     
@@ -181,4 +185,10 @@
         });
         return result;
     }
+
+    public CancelableNameEnvironment createNameEnvironment(
+            JavaProject project, WorkingCopyOwner owner,
+            IProgressMonitor monitor) throws JavaModelException {
+        return new ITDAwareNameEnvironment(project, owner, monitor);
+    }
 }
diff --git a/org.eclipse.ajdt.ui/src/org/eclipse/ajdt/internal/ui/visualiser/AJDTMarkupProvider.java b/org.eclipse.ajdt.ui/src/org/eclipse/ajdt/internal/ui/visualiser/AJDTMarkupProvider.java
index a4d89e9..8c6661c 100644
--- a/org.eclipse.ajdt.ui/src/org/eclipse/ajdt/internal/ui/visualiser/AJDTMarkupProvider.java
+++ b/org.eclipse.ajdt.ui/src/org/eclipse/ajdt/internal/ui/visualiser/AJDTMarkupProvider.java
@@ -115,9 +115,21 @@
                                     if (enclosingType == null) {
                                         // Bug 324706  I don't know why the sloc is null.  Log the bug and
                                         // continue on.
+                                        String handleIdentifier;
+                                        try {
+                                            if (target.getElementName().equals(AJProjectModelFacade.ERROR_JAVA_ELEMENT.getElementName())) {
+                                                handleIdentifier = AJProjectModelFacade.ERROR_JAVA_ELEMENT.getElementName();
+                                            } else {
+                                                handleIdentifier = target.getHandleIdentifier();
+                                            }
+                                        } catch (NullPointerException e) {
+                                            VisualiserPlugin.log(IStatus.WARNING, 
+                                                    "Error computeing handle identifier");
+                                            handleIdentifier = "<CAN'T COMPUTE>";
+                                        }
                                         VisualiserPlugin.log(IStatus.WARNING, 
                                                 "Bug 324706: null containing type found for " + target.getElementName() + 
-                                                "\nHandle identifier is: " + target.getHandleIdentifier());
+                                                "\nHandle identifier is: " + handleIdentifier);
                                         // avoid an npe
                                         continue;
                                     }
diff --git a/org.eclipse.contribution.weaving.jdt.tests/src/org/eclipse/contribution/weaving/jdt/tests/refactoring/MockRefactoringProvider.java b/org.eclipse.contribution.weaving.jdt.tests/src/org/eclipse/contribution/weaving/jdt/tests/refactoring/MockRefactoringProvider.java
index 8d51e72..991e9b7 100644
--- a/org.eclipse.contribution.weaving.jdt.tests/src/org/eclipse/contribution/weaving/jdt/tests/refactoring/MockRefactoringProvider.java
+++ b/org.eclipse.contribution.weaving.jdt.tests/src/org/eclipse/contribution/weaving/jdt/tests/refactoring/MockRefactoringProvider.java
@@ -14,14 +14,19 @@
 import org.eclipse.contribution.jdt.refactoring.IRefactoringProvider;
 import org.eclipse.contribution.weaving.jdt.tests.MockCompilationUnit;
 import org.eclipse.contribution.weaving.jdt.tests.MockNature;
+import org.eclipse.contribution.weaving.jdt.tests.itdawareness.MockNameEnvironmentProvider;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.ITypeRoot;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.WorkingCopyOwner;
 import org.eclipse.jdt.core.dom.AST;
 import org.eclipse.jdt.core.dom.ASTParser;
 import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.internal.core.CancelableNameEnvironment;
+import org.eclipse.jdt.internal.core.JavaProject;
 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
 
@@ -71,8 +76,8 @@
         return null;
     }
 
-    public boolean inInterestingProject(ICompilationUnit unit) {
-        return createSourceConvertedAST = unit.getElementName().endsWith("mock");
+    public boolean inInterestingProject(IJavaElement elt) {
+        return createSourceConvertedAST = elt.getElementName().endsWith("mock");
     }
 
     public CompilationUnit createSourceConvertedAST(String contents, ICompilationUnit unit, boolean resolveBindings, boolean statementsRecovery, boolean bindingsRecovery, IProgressMonitor monitor) {
@@ -88,4 +93,10 @@
         CompilationUnit newCUNode= (CompilationUnit) fParser.createAST(monitor);
         return newCUNode;
     }
+
+    public CancelableNameEnvironment createNameEnvironment(
+            JavaProject project, WorkingCopyOwner owner,
+            IProgressMonitor monitor2) throws JavaModelException {
+        return null;
+    }
 }
diff --git a/org.eclipse.contribution.weaving.jdt/src/org/eclipse/contribution/jdt/refactoring/IRefactoringProvider.java b/org.eclipse.contribution.weaving.jdt/src/org/eclipse/contribution/jdt/refactoring/IRefactoringProvider.java
index 67c84f1..9adaa91 100644
--- a/org.eclipse.contribution.weaving.jdt/src/org/eclipse/contribution/jdt/refactoring/IRefactoringProvider.java
+++ b/org.eclipse.contribution.weaving.jdt/src/org/eclipse/contribution/jdt/refactoring/IRefactoringProvider.java
@@ -16,7 +16,11 @@
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.ITypeRoot;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.WorkingCopyOwner;
 import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.internal.core.CancelableNameEnvironment;
+import org.eclipse.jdt.internal.core.JavaProject;
 
 /**
  * @author Andrew Eisenberg
@@ -42,10 +46,10 @@
     CompilationUnit createASTForRefactoring(ITypeRoot root);
 
     /**
-     * @param unit
+     * @param elt
      * @return true iff this CU is in a project type that we care about.
      */
-    boolean inInterestingProject(ICompilationUnit unit);
+    boolean inInterestingProject(IJavaElement elt);
     
     /**
      * Creates an AST for the given compilation unit with source code translated.
@@ -61,4 +65,15 @@
      * @return
      */
     CompilationUnit createSourceConvertedAST(String contents, ICompilationUnit unit, boolean resolveBindings, boolean statementsRecovery, boolean bindingsRecovery, IProgressMonitor monitor);
+
+    /**
+     * Creates a name environment suitable for refactoring
+     * @param project
+     * @param owner
+     * @param monitor2
+     * @return
+     * @throws JavaModelException 
+     */
+    CancelableNameEnvironment createNameEnvironment(JavaProject project,
+            WorkingCopyOwner owner, IProgressMonitor monitor2) throws JavaModelException;
 }
diff --git a/org.eclipse.contribution.weaving.jdt/src/org/eclipse/contribution/jdt/refactoring/RenameJavaElementActionAspect.aj b/org.eclipse.contribution.weaving.jdt/src/org/eclipse/contribution/jdt/refactoring/RenameJavaElementActionAspect.aj
index 8b740ab..625c98d 100644
--- a/org.eclipse.contribution.weaving.jdt/src/org/eclipse/contribution/jdt/refactoring/RenameJavaElementActionAspect.aj
+++ b/org.eclipse.contribution.weaving.jdt/src/org/eclipse/contribution/jdt/refactoring/RenameJavaElementActionAspect.aj
@@ -14,10 +14,17 @@
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.WorkingCopyOwner;
 import org.eclipse.jdt.core.dom.CompilationUnit;
 import org.eclipse.jdt.internal.ui.refactoring.actions.RenameJavaElementAction;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.internal.core.JavaProject;
 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
+import org.eclipse.jdt.internal.core.CancelableNameEnvironment;
+import org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor;
+import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
 
 /**
  * This aspect ensures that ITDs are renamed with the proper rename refactoring
@@ -76,7 +83,25 @@
         } else {
             return proceed(contents, unit, resolveBindings, statementsRecovery, monitor);
         }
-        
     }
-
+    
+    /**
+     * Captures calls to creating a name environment while doing a refactoring parse
+     */
+//    pointcut creatingRefactoringNameEnvironment(String contents, ICompilationUnit unit, boolean resolveBindings, boolean statementsRecovery, IProgressMonitor monitor1,
+//            JavaProject project, WorkingCopyOwner owner, IProgressMonitor monitor2) : cflowbelow(refactoringParse(contents, unit, resolveBindings, statementsRecovery, monitor1)) &&
+//        call(CancelableNameEnvironment.new(JavaProject, WorkingCopyOwner, IProgressMonitor)) && 
+//                args(project, owner, monitor2);
+    pointcut creatingRefactoringNameEnvironment(JavaProject project, WorkingCopyOwner owner, IProgressMonitor monitor2) : 
+        call(CancelableNameEnvironment.new(JavaProject, WorkingCopyOwner, IProgressMonitor)) && 
+                args(project, owner, monitor2) && cflow(execution(public RefactoringStatus JavaRenameProcessor+.checkFinalConditions(IProgressMonitor, CheckConditionsContext)));
+    
+    CancelableNameEnvironment around(JavaProject project, WorkingCopyOwner owner, IProgressMonitor monitor2) throws JavaModelException : creatingRefactoringNameEnvironment(project, owner, monitor2) {
+        IRefactoringProvider provider = adapter.getProvider();
+        if (provider != null && provider.inInterestingProject(project)) {
+            return provider.createNameEnvironment(project, owner, monitor2);
+        } else {
+            return proceed(project, owner, monitor2);
+        }
+    }
 }