public static void fullyQualifyReference(@NotNull CompositeElement reference, @NotNull PsiClass targetClass) {
    if (((SourceJavaCodeReference)reference).isQualified()) { // qualified 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);
        final PsiManager manager = reference.getManager();
        final PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(manager.getProject()).getParserFacade();

        final TreeElement qualifier;
        if (reference instanceof PsiReferenceExpression) {
          qualifier = (TreeElement)parserFacade.createExpressionFromText(prefix, null).getNode();
        }
        else {
          qualifier = (TreeElement)parserFacade.createReferenceFromText(prefix, null).getNode();
        }

        if (qualifier != null) {
          final CharTable systemCharTab = SharedImplUtil.findCharTableByTree(qualifier);
          final LeafElement dot = Factory.createSingleLeafElement(JavaTokenType.DOT, ".", 0, 1, systemCharTab, manager);
          qualifier.rawInsertAfterMe(dot);
          reference.addInternal(qualifier, dot, null, Boolean.FALSE);
        }
      }
    }
  }
public static void fullyQualifyReference(@NotNull CompositeElement reference, @NotNull PsiClass targetClass)