@Nullable
  public static PsiQualifiedReference getClassReferenceToShorten(
      @NotNull final PsiClass refClass,
      final boolean addImports,
      @NotNull final PsiQualifiedReference reference) {
    PsiClass parentClass = refClass.getContainingClass();
    if (parentClass != null) {
      JavaPsiFacade facade = JavaPsiFacade.getInstance(parentClass.getProject());
      final PsiResolveHelper resolveHelper = facade.getResolveHelper();
      if (resolveHelper.isAccessible(refClass, reference, null)
          && isSafeToShortenReference(reference.getReferenceName(), reference, refClass)) {
        return reference;
      }

      if (!CodeStyleSettingsManager.getSettings(reference.getProject())
          .INSERT_INNER_CLASS_IMPORTS) {
        final PsiElement qualifier = reference.getQualifier();
        if (qualifier instanceof PsiQualifiedReference) {
          return getClassReferenceToShorten(
              parentClass, addImports, (PsiQualifiedReference) qualifier);
        }
        return null;
      }
    }

    if (addImports && !((PsiImportHolder) reference.getContainingFile()).importClass(refClass))
      return null;
    if (!isSafeToShortenReference(reference, refClass)) return null;
    return reference;
  }
  /* Guesswork
   */
  @Nullable
  private static PsiSubstitutor getInheritorSubstitutorForNewExpression(
      final PsiClass baseClass,
      final PsiClass inheritor,
      final PsiSubstitutor baseSubstitutor,
      final PsiElement context) {
    final Project project = baseClass.getProject();
    JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final PsiResolveHelper resolveHelper = facade.getResolveHelper();
    PsiSubstitutor superSubstitutor =
        TypeConversionUtil.getClassSubstitutor(baseClass, inheritor, PsiSubstitutor.EMPTY);
    if (superSubstitutor == null) return null;
    PsiSubstitutor inheritorSubstitutor = PsiSubstitutor.EMPTY;
    for (PsiTypeParameter inheritorParameter : PsiUtil.typeParametersIterable(inheritor)) {
      for (PsiTypeParameter baseParameter : PsiUtil.typeParametersIterable(baseClass)) {
        final PsiType substituted = superSubstitutor.substitute(baseParameter);
        PsiType arg = baseSubstitutor.substitute(baseParameter);
        if (arg instanceof PsiWildcardType) arg = ((PsiWildcardType) arg).getBound();
        PsiType substitution =
            resolveHelper.getSubstitutionForTypeParameter(
                inheritorParameter, substituted, arg, true, PsiUtil.getLanguageLevel(context));
        if (PsiType.NULL.equals(substitution)) continue;
        if (substitution == null) {
          return facade.getElementFactory().createRawSubstitutor(inheritor);
        }
        inheritorSubstitutor = inheritorSubstitutor.put(inheritorParameter, substitution);
        break;
      }
    }

    return inheritorSubstitutor;
  }
  public AddModuleDependencyFix(
      Module currentModule, VirtualFile classVFile, PsiClass[] classes, PsiReference reference) {
    final PsiElement psiElement = reference.getElement();
    final Project project = psiElement.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    for (PsiClass aClass : classes) {
      if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue;
      PsiFile psiFile = aClass.getContainingFile();
      if (psiFile == null) continue;
      VirtualFile virtualFile = psiFile.getVirtualFile();
      if (virtualFile == null) continue;
      final Module classModule = fileIndex.getModuleForFile(virtualFile);
      if (classModule != null
          && classModule != currentModule
          && !ModuleRootManager.getInstance(currentModule).isDependsOn(classModule)) {
        myModules.add(classModule);
      }
    }
    myCurrentModule = currentModule;
    myClassVFile = classVFile;
    myClasses = classes;
    myReference = reference;
  }
  private static boolean canBePrivate(
      PsiMethod method,
      Collection<PsiReference> references,
      Collection<? extends PsiElement> deleted,
      final PsiElement[] allElementsToDelete) {
    final PsiClass containingClass = method.getContainingClass();
    if (containingClass == null) {
      return false;
    }

    PsiManager manager = method.getManager();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
    final PsiElementFactory factory = facade.getElementFactory();
    final PsiModifierList privateModifierList;
    try {
      final PsiMethod newMethod = factory.createMethod("x3", PsiType.VOID);
      privateModifierList = newMethod.getModifierList();
      privateModifierList.setModifierProperty(PsiModifier.PRIVATE, true);
    } catch (IncorrectOperationException e) {
      LOG.assertTrue(false);
      return false;
    }
    for (PsiReference reference : references) {
      final PsiElement element = reference.getElement();
      if (!isInside(element, allElementsToDelete)
          && !isInside(element, deleted)
          && !facade
              .getResolveHelper()
              .isAccessible(method, privateModifierList, element, null, null)) {
        return false;
      }
    }
    return true;
  }
 public FieldConflictsResolver(String name, PsiCodeBlock scope) {
   myScope = scope;
   if (myScope == null) {
     myField = null;
     myReferenceExpressions = null;
     return;
   }
   JavaPsiFacade facade = JavaPsiFacade.getInstance(myScope.getProject());
   final PsiVariable oldVariable =
       facade.getResolveHelper().resolveAccessibleReferencedVariable(name, myScope);
   myField = oldVariable instanceof PsiField ? (PsiField) oldVariable : null;
   if (!(oldVariable instanceof PsiField)) {
     myReferenceExpressions = null;
     return;
   }
   myReferenceExpressions = new ArrayList<PsiReferenceExpression>();
   for (PsiReference reference :
       ReferencesSearch.search(myField, new LocalSearchScope(myScope), false)) {
     final PsiElement element = reference.getElement();
     if (element instanceof PsiReferenceExpression) {
       final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) element;
       if (referenceExpression.getQualifierExpression() == null) {
         myReferenceExpressions.add(referenceExpression);
       }
     }
   }
   if (myField.hasModifierProperty(PsiModifier.STATIC)) {
     myQualifyingClass = myField.getContainingClass();
   }
 }
  private boolean checkAccessibility(final PsiClass aClass) {
    // We don't care about accessibility in javadoc
    if (JavaResolveUtil.isInJavaDoc(myPlace)) {
      return true;
    }

    if (PsiImplUtil.isInServerPage(aClass.getContainingFile())) {
      PsiFile file = FileContextUtil.getContextFile(myPlace);
      if (PsiImplUtil.isInServerPage(file)) {
        return true;
      }
    }

    boolean accessible = true;
    if (aClass instanceof PsiTypeParameter) {
      accessible = !myStaticContext;
    }

    PsiManager manager = aClass.getManager();
    if (aClass.hasModifierProperty(PsiModifier.PRIVATE)) {
      PsiElement parent = aClass.getParent();
      while (true) {
        PsiElement parentScope = parent.getParent();
        if (parentScope instanceof PsiJavaFile) break;
        parent = parentScope;
        if (!(parentScope instanceof PsiClass)) break;
      }
      if (parent instanceof PsiDeclarationStatement) {
        parent = parent.getParent();
      }
      accessible = false;
      for (PsiElement placeParent = myPlace;
          placeParent != null;
          placeParent = placeParent.getContext()) {
        if (manager.areElementsEquivalent(placeParent, parent)) accessible = true;
      }
    }
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
    if (aClass.hasModifierProperty(PsiModifier.PROTECTED)) {
      accessible = false;
      if (myPlace != null && facade.arePackagesTheSame(aClass, myPlace)) {
        accessible = true;
      } else {
        if (aClass.getContainingClass() != null) {
          accessible =
              myAccessClass == null
                  || myPlace != null
                      && facade.getResolveHelper().isAccessible(aClass, myPlace, myAccessClass);
        }
      }
    }
    if (aClass.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
      if (myPlace == null || !facade.arePackagesTheSame(aClass, myPlace)) {
        accessible = false;
      }
    }
    return accessible;
  }
  public static boolean variableNameResolvesToTarget(
      @NotNull String variableName, @NotNull PsiVariable target, @NotNull PsiElement context) {

    final Project project = context.getProject();
    final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    final PsiResolveHelper resolveHelper = psiFacade.getResolveHelper();
    final PsiVariable variable =
        resolveHelper.resolveAccessibleReferencedVariable(variableName, context);
    return target.equals(variable);
  }
 private static boolean isSafeToShortenReference(
     final String referenceText, final PsiElement psiReference, final PsiClass refClass) {
   final PsiManager manager = refClass.getManager();
   final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
   final PsiResolveHelper helper = facade.getResolveHelper();
   if (manager.areElementsEquivalent(
       refClass, helper.resolveReferencedClass(referenceText, psiReference))) {
     PsiElement parent = psiReference.getParent();
     if (parent instanceof PsiJavaCodeReferenceElement
         && parent.getParent() instanceof PsiNewExpression) return true;
     return helper.resolveReferencedVariable(referenceText, psiReference) == null;
   }
   return false;
 }
 public static boolean containsConflictingDeclarations(
     PsiCodeBlock block, PsiCodeBlock parentBlock) {
   final PsiStatement[] statements = block.getStatements();
   if (statements.length == 0) {
     return false;
   }
   final List<PsiCodeBlock> followingBlocks = new ArrayList<>();
   collectFollowingBlocks(block.getParent().getNextSibling(), followingBlocks);
   final Project project = block.getProject();
   final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
   final PsiResolveHelper resolveHelper = facade.getResolveHelper();
   for (final PsiStatement statement : statements) {
     if (!(statement instanceof PsiDeclarationStatement)) {
       continue;
     }
     final PsiDeclarationStatement declaration = (PsiDeclarationStatement) statement;
     final PsiElement[] variables = declaration.getDeclaredElements();
     for (PsiElement variable : variables) {
       if (!(variable instanceof PsiLocalVariable)) {
         continue;
       }
       final PsiLocalVariable localVariable = (PsiLocalVariable) variable;
       final String variableName = localVariable.getName();
       if (variableName == null) {
         continue;
       }
       final PsiVariable target =
           resolveHelper.resolveAccessibleReferencedVariable(variableName, parentBlock);
       if (target instanceof PsiLocalVariable) {
         return true;
       }
       for (PsiCodeBlock codeBlock : followingBlocks) {
         final PsiVariable target1 =
             resolveHelper.resolveAccessibleReferencedVariable(variableName, codeBlock);
         if (target1 instanceof PsiLocalVariable) {
           return true;
         }
       }
     }
   }
   return false;
 }
예제 #10
0
  /**
   * Adds import if it is needed.
   *
   * @return false when the FQ-name have to be used in code (e.g. when conflicting imports already
   *     exist)
   */
  public boolean addImport(@NotNull PsiJavaFile file, @NotNull PsiClass refClass) {
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(file.getProject());
    PsiElementFactory factory = facade.getElementFactory();
    PsiResolveHelper helper = facade.getResolveHelper();

    String className = refClass.getQualifiedName();
    if (className == null) return true;
    String packageName = getPackageOrClassName(className);
    String shortName = PsiNameHelper.getShortClassName(className);

    PsiClass conflictSingleRef = findSingleImportByShortName(file, shortName);
    if (conflictSingleRef != null) {
      return className.equals(conflictSingleRef.getQualifiedName());
    }

    PsiClass curRefClass = helper.resolveReferencedClass(shortName, file);
    if (file.getManager().areElementsEquivalent(refClass, curRefClass)) {
      return true;
    }

    boolean useOnDemand = true;
    if (packageName.length() == 0) {
      useOnDemand = false;
    }

    PsiElement conflictPackageRef = findImportOnDemand(file, packageName);
    if (conflictPackageRef != null) {
      useOnDemand = false;
    }

    List<PsiElement> classesToReimport = new ArrayList<PsiElement>();

    List<PsiJavaCodeReferenceElement> importRefs = getImportsFromPackage(file, packageName);
    if (useOnDemand) {
      if (mySettings.USE_SINGLE_CLASS_IMPORTS
          && importRefs.size() + 1 < mySettings.CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND
          && !mySettings.PACKAGES_TO_USE_IMPORT_ON_DEMAND.contains(packageName)) {
        useOnDemand = false;
      }
      // name of class we try to import is the same as of the class defined in this file
      if (curRefClass != null) {
        useOnDemand = true;
      }
      // check conflicts
      if (useOnDemand) {
        PsiElement[] onDemandRefs = file.getOnDemandImports(false, true);
        if (onDemandRefs.length > 0) {
          PsiPackage aPackage = facade.findPackage(packageName);
          if (aPackage != null) {
            PsiDirectory[] dirs = aPackage.getDirectories();
            for (PsiDirectory dir : dirs) {
              PsiFile[] files = dir.getFiles(); // do not iterate classes - too slow when not loaded
              for (PsiFile aFile : files) {
                if (aFile instanceof PsiJavaFile) {
                  String name = aFile.getVirtualFile().getNameWithoutExtension();
                  for (PsiElement ref : onDemandRefs) {
                    String refName =
                        ref instanceof PsiClass
                            ? ((PsiClass) ref).getQualifiedName()
                            : ((PsiPackage) ref).getQualifiedName();
                    String conflictClassName = refName + "." + name;
                    GlobalSearchScope resolveScope = file.getResolveScope();
                    PsiClass conflictClass = facade.findClass(conflictClassName, resolveScope);
                    if (conflictClass != null && helper.isAccessible(conflictClass, file, null)) {
                      String conflictClassName2 = aPackage.getQualifiedName() + "." + name;
                      PsiClass conflictClass2 = facade.findClass(conflictClassName2, resolveScope);
                      if (conflictClass2 != null
                          && helper.isAccessible(conflictClass2, file, null)) {
                        if (ReferencesSearch.search(
                                    conflictClass, new LocalSearchScope(file), false)
                                .findFirst()
                            != null) {
                          classesToReimport.add(conflictClass);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    try {
      PsiImportList importList = file.getImportList();
      PsiImportStatement statement;
      if (useOnDemand) {
        statement = factory.createImportStatementOnDemand(packageName);
      } else {
        statement = factory.createImportStatement(refClass);
      }
      importList.add(statement);
      if (useOnDemand) {
        for (PsiJavaCodeReferenceElement ref : importRefs) {
          LOG.assertTrue(ref.getParent() instanceof PsiImportStatement);
          if (!ref.isValid()) continue; // todo[dsl] Q?
          classesToReimport.add(ref.resolve());
          PsiImportStatement importStatement = (PsiImportStatement) ref.getParent();
          importStatement.delete();
        }
      }

      for (PsiElement aClassesToReimport : classesToReimport) {
        PsiClass aClass = (PsiClass) aClassesToReimport;
        if (aClass != null) {
          addImport(file, aClass);
        }
      }
    } catch (IncorrectOperationException e) {
      LOG.error(e);
    }
    return true;
  }
  protected void findUsages(@NotNull final List<FixableUsageInfo> usages) {
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(myProject);
    final PsiElementFactory elementFactory = facade.getElementFactory();
    final PsiResolveHelper resolveHelper = facade.getResolveHelper();

    ReferencesSearch.search(mySuperClass)
        .forEach(
            new Processor<PsiReference>() {
              public boolean process(final PsiReference reference) {
                final PsiElement element = reference.getElement();
                if (element instanceof PsiJavaCodeReferenceElement) {
                  final PsiImportStaticStatement staticImportStatement =
                      PsiTreeUtil.getParentOfType(element, PsiImportStaticStatement.class);
                  if (staticImportStatement != null) {
                    usages.add(
                        new ReplaceStaticImportUsageInfo(staticImportStatement, myTargetClasses));
                  } else {
                    final PsiImportStatement importStatement =
                        PsiTreeUtil.getParentOfType(element, PsiImportStatement.class);
                    if (importStatement != null) {
                      usages.add(new RemoveImportUsageInfo(importStatement));
                    } else {
                      final PsiElement parent = element.getParent();
                      if (parent instanceof PsiReferenceList) {
                        final PsiElement pparent = parent.getParent();
                        if (pparent instanceof PsiClass) {
                          final PsiClass inheritor = (PsiClass) pparent;
                          if (parent.equals(inheritor.getExtendsList())
                              || parent.equals(inheritor.getImplementsList())) {
                            usages.add(
                                new ReplaceExtendsListUsageInfo(
                                    (PsiJavaCodeReferenceElement) element,
                                    mySuperClass,
                                    inheritor));
                          }
                        }
                      } else {
                        final PsiClass targetClass = myTargetClasses[0];
                        final PsiClassType targetClassType =
                            elementFactory.createType(
                                targetClass,
                                TypeConversionUtil.getSuperClassSubstitutor(
                                    mySuperClass, targetClass, PsiSubstitutor.EMPTY));

                        if (parent instanceof PsiTypeElement) {
                          final PsiType superClassType = ((PsiTypeElement) parent).getType();
                          PsiSubstitutor subst =
                              getSuperClassSubstitutor(
                                  superClassType, targetClassType, resolveHelper, targetClass);
                          usages.add(
                              new ReplaceWithSubtypeUsageInfo(
                                  ((PsiTypeElement) parent),
                                  elementFactory.createType(targetClass, subst),
                                  myTargetClasses));
                        } else if (parent instanceof PsiNewExpression) {
                          final PsiClassType newType =
                              elementFactory.createType(
                                  targetClass,
                                  getSuperClassSubstitutor(
                                      ((PsiNewExpression) parent).getType(),
                                      targetClassType,
                                      resolveHelper,
                                      targetClass));
                          usages.add(
                              new ReplaceConstructorUsageInfo(
                                  ((PsiNewExpression) parent), newType, myTargetClasses));
                        } else if (parent instanceof PsiJavaCodeReferenceElement) {
                          usages.add(
                              new ReplaceReferenceUsageInfo(
                                  ((PsiJavaCodeReferenceElement) parent).getQualifier(),
                                  myTargetClasses));
                        }
                      }
                    }
                  }
                }
                return true;
              }
            });
    for (PsiClass targetClass : myTargetClasses) {
      for (MemberInfo memberInfo : myMemberInfos) {
        final PsiMember member = memberInfo.getMember();
        for (PsiReference reference : ReferencesSearch.search(member, member.getUseScope(), true)) {
          final PsiElement element = reference.getElement();
          if (element instanceof PsiReferenceExpression
              && ((PsiReferenceExpression) element).getQualifierExpression()
                  instanceof PsiSuperExpression
              && PsiTreeUtil.isAncestor(targetClass, element, false)) {
            usages.add(new RemoveQualifierUsageInfo((PsiReferenceExpression) element));
          }
        }
      }

      final PsiMethod[] superConstructors = mySuperClass.getConstructors();
      for (PsiMethod constructor : targetClass.getConstructors()) {
        final PsiCodeBlock constrBody = constructor.getBody();
        LOG.assertTrue(constrBody != null);
        final PsiStatement[] statements = constrBody.getStatements();
        if (statements.length > 0) {
          final PsiStatement firstConstrStatement = statements[0];
          if (firstConstrStatement instanceof PsiExpressionStatement) {
            final PsiExpression expression =
                ((PsiExpressionStatement) firstConstrStatement).getExpression();
            if (expression instanceof PsiMethodCallExpression) {
              final PsiReferenceExpression methodExpression =
                  ((PsiMethodCallExpression) expression).getMethodExpression();
              if (methodExpression.getText().equals(PsiKeyword.SUPER)) {
                final PsiMethod superConstructor =
                    ((PsiMethodCallExpression) expression).resolveMethod();
                if (superConstructor != null && superConstructor.getBody() != null) {
                  usages.add(new InlineSuperCallUsageInfo((PsiMethodCallExpression) expression));
                  continue;
                }
              }
            }
          }
        }

        // insert implicit call to super
        for (PsiMethod superConstructor : superConstructors) {
          if (superConstructor.getParameterList().getParametersCount() == 0) {
            final PsiExpression expression =
                JavaPsiFacade.getElementFactory(myProject)
                    .createExpressionFromText("super()", constructor);
            usages.add(
                new InlineSuperCallUsageInfo((PsiMethodCallExpression) expression, constrBody));
          }
        }
      }

      if (targetClass.getConstructors().length == 0) {
        // copy default constructor
        for (PsiMethod superConstructor : superConstructors) {
          if (superConstructor.getParameterList().getParametersCount() == 0) {
            usages.add(new CopyDefaultConstructorUsageInfo(targetClass, superConstructor));
            break;
          }
        }
      }
    }
  }
  private void replaceInnerTypeUsages() {
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(myProject);
    final PsiElementFactory elementFactory = facade.getElementFactory();
    final PsiResolveHelper resolveHelper = facade.getResolveHelper();
    final Map<UsageInfo, PsiElement> replacementMap = new HashMap<UsageInfo, PsiElement>();
    for (final PsiClass targetClass : myTargetClasses) {
      final PsiSubstitutor superClassSubstitutor =
          TypeConversionUtil.getSuperClassSubstitutor(
              mySuperClass, targetClass, PsiSubstitutor.EMPTY);
      final PsiClassType targetClassType =
          elementFactory.createType(targetClass, superClassSubstitutor);
      targetClass.accept(
          new JavaRecursiveElementWalkingVisitor() {
            @Override
            public void visitTypeElement(final PsiTypeElement typeElement) {
              super.visitTypeElement(typeElement);
              final PsiType superClassType = typeElement.getType();
              if (PsiUtil.resolveClassInType(superClassType) == mySuperClass) {
                PsiSubstitutor subst =
                    getSuperClassSubstitutor(
                        superClassType, targetClassType, resolveHelper, targetClass);
                replacementMap.put(
                    new UsageInfo(typeElement),
                    elementFactory.createTypeElement(
                        elementFactory.createType(targetClass, subst)));
              }
            }

            @Override
            public void visitNewExpression(final PsiNewExpression expression) {
              super.visitNewExpression(expression);
              final PsiType superClassType = expression.getType();
              if (PsiUtil.resolveClassInType(superClassType) == mySuperClass) {
                PsiSubstitutor subst =
                    getSuperClassSubstitutor(
                        superClassType, targetClassType, resolveHelper, targetClass);
                try {
                  replacementMap.put(
                      new UsageInfo(expression),
                      elementFactory.createExpressionFromText(
                          "new "
                              + elementFactory.createType(targetClass, subst).getCanonicalText()
                              + expression.getArgumentList().getText(),
                          expression));
                } catch (IncorrectOperationException e) {
                  LOG.error(e);
                }
              }
            }
          });
    }
    try {
      for (Map.Entry<UsageInfo, PsiElement> elementEntry : replacementMap.entrySet()) {
        final PsiElement element = elementEntry.getKey().getElement();
        if (element != null) {
          element.replace(elementEntry.getValue());
        }
      }
    } catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }
    public void run() {
      try {
        final boolean rebindNeeded2 = !myVariableName.equals(myFieldName) || myRebindNeeded;
        final PsiReference[] refs;
        if (rebindNeeded2) {
          refs =
              ReferencesSearch.search(myLocal, GlobalSearchScope.projectScope(myProject), false)
                  .toArray(new PsiReference[0]);
        } else {
          refs = null;
        }

        final JavaPsiFacade facade = JavaPsiFacade.getInstance(myProject);
        PsiVariable psiVariable =
            facade.getResolveHelper().resolveAccessibleReferencedVariable(myFieldName, myLocal);
        if (psiVariable != null && (!psiVariable.equals(myLocal))) {
          CommonRefactoringUtil.showErrorMessage(
              GosuIntroduceFieldHandler.REFACTORING_NAME,
              LOCAL_VARIABLE_WITH_THIS_NAME_ALREADY_EXISTS,
              HelpID.INTRODUCE_FIELD,
              myProject);
          return;
        }

        if (refs != null) {

          for (PsiReference occur : refs) {
            psiVariable =
                facade
                    .getResolveHelper()
                    .resolveAccessibleReferencedVariable(myFieldName, (PsiElement) occur);
            if (psiVariable != null && (psiVariable.getName().equals(myFieldName))) {
              CommonRefactoringUtil.showErrorMessage(
                  GosuIntroduceFieldHandler.REFACTORING_NAME,
                  LOCAL_VARIABLE_WITH_THIS_NAME_ALREADY_EXISTS,
                  HelpID.INTRODUCE_FIELD,
                  myProject);
              return;
            }
          }
        }

        final PsiMethod enclosingConstructor =
            GosuBaseExpressionToFieldHandler.getEnclosingConstructor(myDestinationClass, myLocal);
        myField =
            mySettings.isIntroduceEnumConstant()
                ? EnumConstantsUtil.createEnumConstant(myDestinationClass, myLocal, myFieldName)
                : createField(
                    myLocal,
                    mySettings.getForcedType(),
                    myFieldName,
                    myInitializerPlace == IN_FIELD_DECLARATION);
        myField = (PsiField) myDestinationClass.add(myField);
        GosuBaseExpressionToFieldHandler.setModifiers(myField, mySettings);
        if (!mySettings.isIntroduceEnumConstant()) {
          VisibilityUtil.fixVisibility(myOccurences, myField, mySettings.getFieldVisibility());
        }

        myLocal.normalizeDeclaration();
        //        PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)
        // myLocal.getParent();
        final GosuBaseExpressionToFieldHandler.InitializationPlace finalInitializerPlace;
        if (myLocal.getInitializer() == null) {
          finalInitializerPlace = IN_FIELD_DECLARATION;
        } else {
          finalInitializerPlace = myInitializerPlace;
        }
        final PsiElementFactory factory = JavaPsiFacade.getInstance(myProject).getElementFactory();

        switch (finalInitializerPlace) {
          case IN_FIELD_DECLARATION:
            //            declarationStatement.delete();
            myLocal.delete();
            break;

          case IN_CURRENT_METHOD:
            PsiStatement statement = createAssignment(myLocal, myFieldName, null);
            //            myAssignmentStatement = (PsiStatement)
            // declarationStatement.replace(statement);
            myAssignmentStatement = (PsiStatement) myLocal.replace(statement);
            GeneratedMarkerVisitor.markGenerated(myAssignmentStatement);
            break;

          case IN_CONSTRUCTOR:
            myAssignmentStatement =
                addInitializationToConstructors(myLocal, myField, enclosingConstructor, factory);
            GeneratedMarkerVisitor.markGenerated(myAssignmentStatement);
            break;
          case IN_SETUP_METHOD:
            //            myAssignmentStatement = addInitializationToSetUp(myLocal, myField,
            // factory);
        }

        if (enclosingConstructor != null && myInitializerPlace == IN_CONSTRUCTOR) {
          PsiStatement statement = createAssignment(myLocal, myFieldName, null);
          //          myAssignmentStatement = (PsiStatement)
          // declarationStatement.replace(statement);
          myAssignmentStatement = (PsiStatement) myLocal.replace(statement);
          GeneratedMarkerVisitor.markGenerated(myAssignmentStatement);
        }

        if (rebindNeeded2) {
          for (final PsiReference reference : refs) {
            if (reference != null) {
              // expr = GosuRefactoringUtil.outermostParenthesizedExpression(expr);
              GosuRefactoringUtil.replaceOccurenceWithFieldRef(
                  (PsiExpression) reference, myField, myDestinationClass);
              // replaceOccurenceWithFieldRef((PsiExpression)reference, field, aaClass);
            }
          }
          // GosuRefactoringUtil.renameVariableReferences(local, pPrefix + fieldName,
          // GlobalSearchScope.projectScope(myProject));
        }
        ClassLord.doImportAndStick(
            mySettings.getForcedType().getCanonicalText(), myDestinationClass.getContainingFile());
      } catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
예제 #14
0
  public static Processor<PsiClass> createInheritorsProcessor(
      final PsiElement context,
      final PsiClassType baseType,
      final int arrayDim,
      final boolean getRawSubtypes,
      final Consumer<PsiType> result,
      @NotNull final PsiClass baseClass,
      final PsiSubstitutor baseSubstitutor) {
    final PsiManager manager = context.getManager();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
    final PsiResolveHelper resolveHelper = facade.getResolveHelper();

    return new Processor<PsiClass>() {
      @Override
      public boolean process(final PsiClass inheritor) {
        ProgressManager.checkCanceled();

        return ApplicationManager.getApplication()
            .runReadAction(
                new Computable<Boolean>() {
                  @Override
                  public Boolean compute() {
                    if (!context.isValid()
                        || !inheritor.isValid()
                        || !facade.getResolveHelper().isAccessible(inheritor, context, null))
                      return true;

                    if (inheritor.getQualifiedName() == null
                        && !manager.areElementsEquivalent(
                            inheritor.getContainingFile(),
                            context.getContainingFile().getOriginalFile())) {
                      return true;
                    }

                    if (JavaCompletionUtil.isInExcludedPackage(inheritor, false)) return true;

                    PsiSubstitutor superSubstitutor =
                        TypeConversionUtil.getClassSubstitutor(
                            baseClass, inheritor, PsiSubstitutor.EMPTY);
                    if (superSubstitutor == null) return true;
                    if (getRawSubtypes) {
                      result.consume(
                          createType(
                              inheritor,
                              facade.getElementFactory().createRawSubstitutor(inheritor),
                              arrayDim));
                      return true;
                    }

                    PsiSubstitutor inheritorSubstitutor = PsiSubstitutor.EMPTY;
                    for (PsiTypeParameter inheritorParameter :
                        PsiUtil.typeParametersIterable(inheritor)) {
                      for (PsiTypeParameter baseParameter :
                          PsiUtil.typeParametersIterable(baseClass)) {
                        final PsiType substituted = superSubstitutor.substitute(baseParameter);
                        PsiType arg = baseSubstitutor.substitute(baseParameter);
                        if (arg instanceof PsiWildcardType) {
                          PsiType bound = ((PsiWildcardType) arg).getBound();
                          arg = bound != null ? bound : ((PsiWildcardType) arg).getExtendsBound();
                        }
                        PsiType substitution =
                            resolveHelper.getSubstitutionForTypeParameter(
                                inheritorParameter,
                                substituted,
                                arg,
                                true,
                                PsiUtil.getLanguageLevel(context));
                        if (PsiType.NULL.equals(substitution)
                            || substitution instanceof PsiWildcardType) continue;
                        if (substitution == null) {
                          result.consume(
                              createType(
                                  inheritor,
                                  facade.getElementFactory().createRawSubstitutor(inheritor),
                                  arrayDim));
                          return true;
                        }
                        inheritorSubstitutor =
                            inheritorSubstitutor.put(inheritorParameter, substitution);
                        break;
                      }
                    }

                    PsiType toAdd = createType(inheritor, inheritorSubstitutor, arrayDim);
                    if (baseType.isAssignableFrom(toAdd)) {
                      result.consume(toAdd);
                    }
                    return true;
                  }
                })
            .booleanValue();
      }
    };
  }
예제 #15
0
  @Nullable
  public static List<LocalQuickFix> registerFixes(
      @NotNull final QuickFixActionRegistrar registrar, @NotNull final PsiReference reference) {
    final PsiElement psiElement = reference.getElement();
    @NonNls
    final String shortReferenceName = reference.getRangeInElement().substring(psiElement.getText());

    Project project = psiElement.getProject();
    PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile == null) return null;

    final VirtualFile classVFile = containingFile.getVirtualFile();
    if (classVFile == null) return null;

    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final Module currentModule = fileIndex.getModuleForFile(classVFile);
    if (currentModule == null) return null;

    final List<LocalQuickFix> providedFixes =
        findFixes(
            new Function<MissingDependencyFixProvider, List<LocalQuickFix>>() {
              @Override
              public List<LocalQuickFix> fun(MissingDependencyFixProvider provider) {
                return provider.registerFixes(registrar, reference);
              }
            });
    if (providedFixes != null) {
      return providedFixes;
    }

    List<LocalQuickFix> result = new ArrayList<LocalQuickFix>();
    JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    String fullReferenceText = reference.getCanonicalText();
    for (ExternalLibraryResolver resolver : ExternalLibraryResolver.EP_NAME.getExtensions()) {
      final ExternalClassResolveResult resolveResult =
          resolver.resolveClass(shortReferenceName, isReferenceToAnnotation(psiElement));
      OrderEntryFix fix = null;
      if (resolveResult != null
          && psiFacade.findClass(
                  resolveResult.getQualifiedClassName(),
                  currentModule.getModuleWithDependenciesAndLibrariesScope(true))
              == null) {
        fix =
            new AddExternalLibraryToDependenciesQuickFix(
                currentModule,
                resolveResult.getLibrary(),
                reference,
                resolveResult.getQualifiedClassName());
      } else if (!fullReferenceText.equals(shortReferenceName)) {
        ExternalLibraryDescriptor descriptor = resolver.resolvePackage(fullReferenceText);
        if (descriptor != null) {
          fix =
              new AddExternalLibraryToDependenciesQuickFix(
                  currentModule, descriptor, reference, null);
        }
      }
      if (fix != null) {
        registrar.register(fix);
        result.add(fix);
      }
    }
    if (!result.isEmpty()) {
      return result;
    }

    Set<Object> librariesToAdd = new THashSet<Object>();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(psiElement.getProject());
    PsiClass[] classes =
        PsiShortNamesCache.getInstance(project)
            .getClassesByName(shortReferenceName, GlobalSearchScope.allScope(project));
    List<PsiClass> allowedDependencies = filterAllowedDependencies(psiElement, classes);
    if (allowedDependencies.isEmpty()) {
      return result;
    }
    classes = allowedDependencies.toArray(new PsiClass[allowedDependencies.size()]);
    OrderEntryFix moduleDependencyFix =
        new AddModuleDependencyFix(currentModule, classVFile, classes, reference);

    final PsiClass[] finalClasses = classes;
    final OrderEntryFix finalModuleDependencyFix = moduleDependencyFix;
    final OrderEntryFix providedModuleDependencyFix =
        provideFix(
            new Function<MissingDependencyFixProvider, OrderEntryFix>() {
              @Override
              public OrderEntryFix fun(MissingDependencyFixProvider provider) {
                return provider.getAddModuleDependencyFix(
                    reference, finalModuleDependencyFix, currentModule, classVFile, finalClasses);
              }
            });
    moduleDependencyFix = ObjectUtils.notNull(providedModuleDependencyFix, moduleDependencyFix);

    registrar.register(moduleDependencyFix);
    result.add(moduleDependencyFix);
    for (final PsiClass aClass : classes) {
      if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue;
      PsiFile psiFile = aClass.getContainingFile();
      if (psiFile == null) continue;
      VirtualFile virtualFile = psiFile.getVirtualFile();
      if (virtualFile == null) continue;
      ModuleFileIndex moduleFileIndex = ModuleRootManager.getInstance(currentModule).getFileIndex();
      for (OrderEntry orderEntry : fileIndex.getOrderEntriesForFile(virtualFile)) {
        if (orderEntry instanceof LibraryOrderEntry) {
          final LibraryOrderEntry libraryEntry = (LibraryOrderEntry) orderEntry;
          final Library library = libraryEntry.getLibrary();
          if (library == null) continue;
          VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
          if (files.length == 0) continue;
          final VirtualFile jar = files[0];

          if (jar == null
              || libraryEntry.isModuleLevel() && !librariesToAdd.add(jar)
              || !librariesToAdd.add(library)) continue;
          OrderEntry entryForFile = moduleFileIndex.getOrderEntryForFile(virtualFile);
          if (entryForFile != null
              && !(entryForFile instanceof ExportableOrderEntry
                  && ((ExportableOrderEntry) entryForFile).getScope() == DependencyScope.TEST
                  && !ModuleRootManager.getInstance(currentModule)
                      .getFileIndex()
                      .isInTestSourceContent(classVFile))) {
            continue;
          }
          final OrderEntryFix platformFix =
              new OrderEntryFix() {
                @Override
                @NotNull
                public String getText() {
                  return QuickFixBundle.message(
                      "orderEntry.fix.add.library.to.classpath", libraryEntry.getPresentableName());
                }

                @Override
                @NotNull
                public String getFamilyName() {
                  return QuickFixBundle.message("orderEntry.fix.family.add.library.to.classpath");
                }

                @Override
                public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
                  return !project.isDisposed()
                      && !currentModule.isDisposed()
                      && libraryEntry.isValid();
                }

                @Override
                public void invoke(
                    @NotNull final Project project, @Nullable final Editor editor, PsiFile file) {
                  OrderEntryUtil.addLibraryToRoots(libraryEntry, currentModule);
                  if (editor != null) {
                    DumbService.getInstance(project)
                        .withAlternativeResolveEnabled(
                            new Runnable() {
                              @Override
                              public void run() {
                                new AddImportAction(project, reference, editor, aClass).execute();
                              }
                            });
                  }
                }
              };

          final OrderEntryFix providedFix =
              provideFix(
                  new Function<MissingDependencyFixProvider, OrderEntryFix>() {
                    @Override
                    public OrderEntryFix fun(MissingDependencyFixProvider provider) {
                      return provider.getAddLibraryToClasspathFix(
                          reference, platformFix, currentModule, libraryEntry, aClass);
                    }
                  });
          final OrderEntryFix fix = ObjectUtils.notNull(providedFix, platformFix);
          registrar.register(fix);
          result.add(fix);
        }
      }
    }
    return result;
  }