示例#1
0
 @Override
 public LightRef asLightUsage(@NotNull PsiElement element, @NotNull ByteArrayEnumerator names) {
   if (mayBeVisibleOutsideOwnerFile(element)) {
     if (element instanceof PsiField) {
       final PsiField field = (PsiField) element;
       final PsiClass aClass = field.getContainingClass();
       if (aClass == null || aClass instanceof PsiAnonymousClass) return null;
       final String jvmOwnerName = ClassUtil.getJVMClassName(aClass);
       final String name = field.getName();
       if (name == null || jvmOwnerName == null) return null;
       return new LightRef.JavaLightFieldRef(id(jvmOwnerName, names), id(name, names));
     } else if (element instanceof PsiMethod) {
       final PsiClass aClass = ((PsiMethod) element).getContainingClass();
       if (aClass == null || aClass instanceof PsiAnonymousClass) return null;
       final String jvmOwnerName = ClassUtil.getJVMClassName(aClass);
       if (jvmOwnerName == null) return null;
       final PsiMethod method = (PsiMethod) element;
       final String name = method.isConstructor() ? "<init>" : method.getName();
       final int parametersCount = method.getParameterList().getParametersCount();
       return new LightRef.JavaLightMethodRef(
           id(jvmOwnerName, names), id(name, names), parametersCount);
     } else if (element instanceof PsiClass) {
       final String jvmClassName = ClassUtil.getJVMClassName((PsiClass) element);
       if (jvmClassName != null) {
         return new LightRef.JavaLightClassRef(id(jvmClassName, names));
       }
     }
   }
   return null;
 }
  public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    if (isReferenceTo(element)) return getElement();

    final String newName;
    if (element instanceof PsiClass) {
      PsiClass psiClass = (PsiClass) element;
      final boolean jvmFormat =
          Boolean.TRUE.equals(JavaClassReferenceProvider.JVM_FORMAT.getValue(getOptions()));
      newName = jvmFormat ? ClassUtil.getJVMClassName(psiClass) : psiClass.getQualifiedName();
    } else if (element instanceof PsiPackage) {
      PsiPackage psiPackage = (PsiPackage) element;
      newName = psiPackage.getQualifiedName();
    } else {
      throw new IncorrectOperationException("Cannot bind to " + element);
    }
    assert newName != null;

    TextRange range =
        new TextRange(
            myJavaClassReferenceSet.getReference(0).getRangeInElement().getStartOffset(),
            getRangeInElement().getEndOffset());
    final ElementManipulator<PsiElement> manipulator = getManipulator(getElement());
    if (manipulator != null) {
      final PsiElement finalElement = manipulator.handleContentChange(getElement(), range, newName);
      range = new TextRange(range.getStartOffset(), range.getStartOffset() + newName.length());
      myJavaClassReferenceSet.reparse(finalElement, range);
      return finalElement;
    }
    return element;
  }