/*
 * Copyright 2000-2009 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 com.intellij.psi.impl.source.tree;

import com.intellij.lang.ASTNode;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiReferenceExpression;
import com.intellij.psi.impl.source.SourceJavaCodeReference;
import com.intellij.psi.impl.source.parsing.ExpressionParsing;
import com.intellij.psi.impl.source.parsing.Parsing;
import com.intellij.util.CharTable;

public class JavaSourceUtil {
  public static void fullyQualifyReference(CompositeElement reference, PsiClass targetClass) {
    if (((SourceJavaCodeReference)reference).isQualified()) { // qualifed reference
      final PsiClass parentClass = targetClass.getContainingClass();
      if (parentClass == null) return;
      final ASTNode qualifier = reference.findChildByRole(ChildRole.QUALIFIER);
      if (qualifier instanceof SourceJavaCodeReference) {
        ((SourceJavaCodeReference)qualifier).fullyQualify(parentClass);
      }
    }
    else { // unqualified reference, need to qualify with package name
      final String qName = targetClass.getQualifiedName();
      if (qName == null) {
        return; // todo: local classes?
      }
      final int i = qName.lastIndexOf('.');
      if (i > 0) {
        final String prefix = qName.substring(0, i);
        PsiManager manager = reference.getManager();

        final CharTable table = SharedImplUtil.findCharTableByTree(reference);
        final CompositeElement qualifier;
        if (reference instanceof PsiReferenceExpression) {
          qualifier = ExpressionParsing.parseExpressionText(manager, prefix, 0, prefix.length(), table);
        }
        else {
          qualifier = Parsing.parseJavaCodeReferenceText(manager, prefix, table);
        }
        if (qualifier != null) {
          final CharTable systemCharTab = SharedImplUtil.findCharTableByTree(qualifier);
          final LeafElement dot = Factory.createSingleLeafElement(JavaTokenType.DOT, ".", 0, 1, systemCharTab, SharedImplUtil.getManagerByTree(qualifier));
          qualifier.rawInsertAfterMe(dot);
          reference.addInternal(qualifier, dot, null, Boolean.FALSE);
        }
      }
    }
  }
}

@@ -700,316 +700,70 @@
 psi.
-JavaTokenType;%0Aimport com.intellij.psi.PsiClass;%0Aimport com.intellij.psi.PsiManager;%0Aimport com.intellij.psi.PsiReferenceExpression;%0Aimport com.intellij.psi.impl.source.SourceJavaCodeReference;%0Aimport com.intellij.psi.impl.source.parsing.ExpressionParsing;%0Aimport com.intellij.psi.impl.source.parsing.Parsing
+*;%0Aimport com.intellij.psi.impl.source.SourceJavaCodeReference
 ;%0Aim
@@ -827,16 +827,48 @@
 eUtil %7B%0A
+  private JavaSourceUtil() %7B %7D%0A%0A
   public
@@ -902,16 +902,22 @@
 ference(
+final 
 Composit
@@ -935,16 +935,22 @@
 ference,
+ final
  PsiClas
@@ -1037,16 +1037,17 @@
 / qualif
+i
 ed refer
@@ -1712,16 +1712,22 @@
         
+final 
 PsiManag
@@ -1763,17 +1763,16 @@
 ager();%0A
-%0A
         
@@ -1781,73 +1781,111 @@
 nal 
-CharTable table = SharedImplUtil.findCharTableByTree(reference
+PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(manager.getProject()).getParserFacade(
 );%0A
+%0A
     
@@ -1890,32 +1890,27 @@
       final 
-Composit
+Tre
 eElement qua
@@ -2002,30 +2002,39 @@
 r = 
-ExpressionParsing.pars
+(TreeElement)parserFacade.creat
 eExp
@@ -2044,55 +2044,39 @@
 sion
+From
 Text(
-manager, prefix, 0, prefix.length(), table
+prefix, null).getNode(
 );%0A 
@@ -2125,28 +2125,39 @@
 r = 
-Parsing.parseJavaCod
+(TreeElement)parserFacade.creat
 eRef
@@ -2166,35 +2166,39 @@
 ence
+From
 Text(
-manager, prefix, table
+prefix, null).getNode(
 );%0A 
@@ -2198,32 +2198,33 @@
 de();%0A        %7D%0A
+%0A
         if (qual
@@ -2444,50 +2444,15 @@
 ab, 
-SharedImplUtil.getManagerByTree(qualifier)
+manager
 );%0A 

Use new Java parser