@Override
 public void visitReferenceExpression(final PsiReferenceExpression reference) {
   if (myLineRange.intersects(reference.getTextRange())) {
     final PsiElement psiElement = reference.resolve();
     if (psiElement instanceof PsiVariable) {
       final PsiVariable var = (PsiVariable) psiElement;
       if (var instanceof PsiField) {
         if (myCollectExpressions
             && !DebuggerUtils.hasSideEffectsOrReferencesMissingVars(
                 reference, myVisibleLocals)) {
           /*
           if (var instanceof PsiEnumConstant && reference.getQualifier() == null) {
             final PsiClass enumClass = ((PsiEnumConstant)var).getContainingClass();
             if (enumClass != null) {
               final PsiExpression expression = JavaPsiFacade.getInstance(var.getProject()).getParserFacade().createExpressionFromText(enumClass.getName() + "." + var.getName(), var);
               final PsiReference ref = expression.getReference();
               if (ref != null) {
                 ref.bindToElement(var);
                 myExpressions.add(new TextWithImportsImpl(expression));
               }
             }
           }
           else {
             myExpressions.add(new TextWithImportsImpl(reference));
           }
           */
           final PsiModifierList modifierList = var.getModifierList();
           boolean isConstant =
               (var instanceof PsiEnumConstant)
                   || (modifierList != null
                       && modifierList.hasModifierProperty(PsiModifier.STATIC)
                       && modifierList.hasModifierProperty(PsiModifier.FINAL));
           if (!isConstant) {
             myExpressions.add(new TextWithImportsImpl(reference));
           }
         }
       } else {
         if (myVisibleLocals.contains(var.getName())) {
           myVars.add(var.getName());
         } else {
           // fix for variables used in inner classes
           if (!Comparing.equal(
               PsiTreeUtil.getParentOfType(reference, PsiClass.class),
               PsiTreeUtil.getParentOfType(var, PsiClass.class))) {
             myExpressions.add(new TextWithImportsImpl(reference));
           }
         }
       }
     }
   }
   super.visitReferenceExpression(reference);
 }
Exemple #2
0
    public PsiReturnStatement addReturnForMethod(final PsiFile file, final PsiMethod method) {
      final PsiModifierList modifiers = method.getModifierList();
      if (modifiers.hasModifierProperty(PsiModifier.ABSTRACT) || method.getBody() == null) {
        return null;
      }

      try {
        final ConvertReturnStatementsVisitor visitor =
            new ConvertReturnStatementsVisitor(factory, method, myTargetType);

        ControlFlow controlFlow;
        try {
          controlFlow = HighlightControlFlowUtil.getControlFlowNoConstantEvaluate(method.getBody());
        } catch (AnalysisCanceledException e) {
          return null; // must be an error
        }
        PsiReturnStatement returnStatement;
        if (controlFlow != null && ControlFlowUtil.processReturns(controlFlow, visitor)) {
          // extra return statement not needed
          // get latest modified return statement and select...
          returnStatement = visitor.getLatestReturn();
        } else {
          returnStatement = visitor.createReturnInLastStatement();
        }
        if (method.getContainingFile() != file) {
          UndoUtil.markPsiFileForUndo(file);
        }
        return returnStatement;
      } catch (IncorrectOperationException e) {
        LOG.error(e);
      }

      return null;
    }
 private boolean checkModifiers(
     PsiModifierList modifiers, String modifier, Visibility visibility) {
   JavaDocSettings configuration = getSettings().getConfiguration();
   return modifiers != null
       && modifiers.hasModifierProperty(modifier)
       && configuration != null
       && configuration.getGeneralSettings().getVisibilities().contains(visibility);
 }
Exemple #4
0
 @NotNull
 public static Set<String> modifiersListToModifiersSet(@Nullable PsiModifierList modifierList) {
   Set<String> modifiersSet = new HashSet<String>();
   if (modifierList != null) {
     if (modifierList.hasExplicitModifier(PsiModifier.ABSTRACT))
       modifiersSet.add(Modifier.ABSTRACT);
     if (modifierList.hasModifierProperty(PsiModifier.FINAL)) modifiersSet.add(Modifier.FINAL);
     if (modifierList.hasModifierProperty(PsiModifier.STATIC)) modifiersSet.add(Modifier.STATIC);
     if (modifierList.hasExplicitModifier(PsiModifier.PUBLIC)) modifiersSet.add(Modifier.PUBLIC);
     if (modifierList.hasExplicitModifier(PsiModifier.PROTECTED))
       modifiersSet.add(Modifier.PROTECTED);
     if (modifierList.hasExplicitModifier(PsiModifier.PACKAGE_LOCAL))
       modifiersSet.add(Modifier.INTERNAL);
     if (modifierList.hasExplicitModifier(PsiModifier.PRIVATE)) modifiersSet.add(Modifier.PRIVATE);
   }
   return modifiersSet;
 }
 @SuppressWarnings("MagicConstant")
 private static void parseModifiers(
     @Nullable PsiModifierList modifierList, @NotNull JavaElementArrangementEntry entry) {
   if (modifierList == null) {
     return;
   }
   for (String modifier : PsiModifier.MODIFIERS) {
     if (modifierList.hasModifierProperty(modifier)) {
       ArrangementSettingsToken arrangementModifier = MODIFIERS.get(modifier);
       if (arrangementModifier != null) {
         entry.addModifier(arrangementModifier);
       }
     }
   }
   if (modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
     entry.addModifier(PACKAGE_PRIVATE);
   }
 }
  /**
   * Create a new {@link FieldElement} object.
   *
   * @param field the {@link com.intellij.psi.PsiField} to get the information from.
   * @return a new {@link FieldElement} object.
   */
  public static FieldElement newFieldElement(PsiField field, boolean useAccessor) {
    FieldElement fe = new FieldElement();
    fe.setName(field.getName());
    final PsiMethod getterForField = useAccessor ? PropertyUtil.findGetterForField(field) : null;
    fe.setAccessor(getterForField != null ? getterForField.getName() + "()" : field.getName());

    if (PsiAdapter.isConstantField(field)) fe.setConstant(true);
    if (PsiAdapter.isEnumField(field)) fe.setEnum(true);
    PsiModifierList modifiers = field.getModifierList();
    if (modifiers != null) {
      if (modifiers.hasModifierProperty(PsiModifier.TRANSIENT)) fe.setModifierTransient(true);
      if (modifiers.hasModifierProperty(PsiModifier.VOLATILE)) fe.setModifierVolatile(true);
    }

    PsiElementFactory factory = JavaPsiFacade.getInstance(field.getProject()).getElementFactory();
    PsiType type = field.getType();
    setElementInfo(fe, factory, type, modifiers);

    return fe;
  }
 @Nullable
 private static PsiField getReferencedFinalField(final PsiExpression argument) {
   if (argument instanceof PsiReferenceExpression) {
     final PsiElement element = ((PsiReferenceExpression) argument).resolve();
     if (element instanceof PsiField) {
       final PsiField field = (PsiField) element;
       final PsiModifierList modifierList = field.getModifierList();
       if (modifierList != null && modifierList.hasModifierProperty(PsiModifier.FINAL)) {
         return field;
       }
     }
   }
   return null;
 }
  /**
   * Creates a new {@link MethodElement} object.
   *
   * @param method the PSI method object.
   * @return a new {@link MethodElement} object.
   * @since 2.15
   */
  public static MethodElement newMethodElement(PsiMethod method) {
    MethodElement me = new MethodElement();
    PsiType type = method.getReturnType();
    PsiModifierList modifiers = method.getModifierList();

    // if something is wrong:
    // http://www.intellij.net/forums/thread.jsp?nav=false&forum=18&thread=88676&start=0&msRange=15
    if (type == null) {
      log.warn(
          "This method does not have a valid return type: "
              + method.getName()
              + ", returnType="
              + type);
      return me;
    }
    PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory();
    setElementInfo(me, factory, type, modifiers);

    // names
    String fieldName = PsiAdapter.getGetterFieldName(method);
    me.setName(fieldName == null ? method.getName() : fieldName);
    me.setFieldName(fieldName);
    me.setMethodName(method.getName());

    // getter
    me.setGetter(PsiAdapter.isGetterMethod(method));

    // misc
    me.setDeprecated(method.isDeprecated());
    me.setReturnTypeVoid(PsiAdapter.isTypeOfVoid(method.getReturnType()));

    // modifiers
    if (modifiers.hasModifierProperty(PsiModifier.ABSTRACT)) me.setModifierAbstract(true);
    if (modifiers.hasModifierProperty(PsiModifier.SYNCHRONIZED)) me.setModifierSynchronized(true);

    return me;
  }
  @Override
  public String getAccessModifier(PsiModifierListOwner psiElement) {
    if (psiElement instanceof PsiParameter) return PsiModifier.PACKAGE_LOCAL;

    PsiModifierList list = psiElement.getModifierList();
    String result = PsiModifier.PACKAGE_LOCAL;

    if (list != null) {
      if (list.hasModifierProperty(PsiModifier.PRIVATE)) {
        result = PsiModifier.PRIVATE;
      } else if (list.hasModifierProperty(PsiModifier.PROTECTED)) {
        result = PsiModifier.PROTECTED;
      } else if (list.hasModifierProperty(PsiModifier.PUBLIC)) {
        result = PsiModifier.PUBLIC;
      } else if (psiElement.getParent() instanceof PsiClass) {
        PsiClass ownerClass = (PsiClass) psiElement.getParent();
        if (ownerClass.isInterface()) {
          result = PsiModifier.PUBLIC;
        }
      }
    }

    return result;
  }
  /**
   * Sets the basic element information from the given type.
   *
   * @param element the element to set information from the type
   * @param factory
   * @param type the type
   * @param modifiers modifier list
   * @since 2.15
   */
  private static void setElementInfo(
      AbstractElement element, PsiElementFactory factory, PsiType type, PsiModifierList modifiers) {

    // type names
    element.setTypeName(PsiAdapter.getTypeClassName(type));
    element.setTypeQualifiedName(PsiAdapter.getTypeQualifiedClassName(type));
    element.setType(type.getCanonicalText());

    // arrays, collections and maps types
    if (PsiAdapter.isObjectArrayType(type)) {
      element.setObjectArray(true);
      element.setArray(true);

      // additional specify if the element is a string array
      if (PsiAdapter.isStringArrayType(type)) element.setStringArray(true);

    } else if (PsiAdapter.isPrimitiveArrayType(type)) {
      element.setPrimitiveArray(true);
      element.setArray(true);
    }
    if (PsiAdapter.isCollectionType(factory, type)) element.setCollection(true);
    if (PsiAdapter.isListType(factory, type)) element.setList(true);
    if (PsiAdapter.isSetType(factory, type)) element.setSet(true);
    if (PsiAdapter.isMapType(factory, type)) element.setMap(true);

    // other types
    if (PsiAdapter.isPrimitiveType(type)) element.setPrimitive(true);
    if (PsiAdapter.isObjectType(factory, type)) element.setObject(true);
    if (PsiAdapter.isStringType(factory, type)) element.setString(true);
    if (PsiAdapter.isNumericType(factory, type)) element.setNumeric(true);
    if (PsiAdapter.isDateType(factory, type)) element.setDate(true);
    if (PsiAdapter.isCalendarType(factory, type)) element.setCalendar(true);
    if (PsiAdapter.isBooleanType(factory, type)) element.setBoolean(true);
    if (PsiType.VOID.equals(type)) element.setVoid(true);
    if (PsiType.LONG.equals(type)) element.setLong(true);
    if (PsiType.FLOAT.equals(type)) element.setFloat(true);
    if (PsiType.DOUBLE.equals(type)) element.setDouble(true);
    if (PsiType.BYTE.equals(type)) element.setByte(true);
    if (PsiType.CHAR.equals(type)) element.setChar(true);
    if (PsiType.SHORT.equals(type)) element.setShort(true);
    element.setNestedArray(PsiAdapter.isNestedArray(type));
    // modifiers
    if (modifiers != null) {
      if (modifiers.hasModifierProperty(PsiModifier.STATIC)) element.setModifierStatic(true);
      if (modifiers.hasModifierProperty(PsiModifier.FINAL)) element.setModifierFinal(true);
      if (modifiers.hasModifierProperty(PsiModifier.PUBLIC)) {
        element.setModifierPublic(true);
      } else if (modifiers.hasModifierProperty(PsiModifier.PROTECTED)) {
        element.setModifierProtected(true);
      } else if (modifiers.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
        element.setModifierPackageLocal(true);
      } else if (modifiers.hasModifierProperty(PsiModifier.PRIVATE))
        element.setModifierPrivate(true);
    }
  }
    @Override
    public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      if (!FileModificationService.getInstance()
          .preparePsiElementForWrite(descriptor.getPsiElement())) return;
      final PsiModifierListOwner element =
          PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiModifierListOwner.class);
      if (element != null) {
        RefElement refElement = null;
        if (myManager != null) {
          refElement = myManager.getReference(element);
        }
        try {
          if (element instanceof PsiVariable) {
            ((PsiVariable) element).normalizeDeclaration();
          }

          PsiModifierList list = element.getModifierList();

          LOG.assertTrue(list != null);

          if (element instanceof PsiMethod) {
            PsiMethod psiMethod = (PsiMethod) element;
            PsiClass containingClass = psiMethod.getContainingClass();
            if (containingClass != null
                && containingClass.getParent() instanceof PsiFile
                && myHint == PsiModifier.PRIVATE
                && list.hasModifierProperty(PsiModifier.FINAL)) {
              list.setModifierProperty(PsiModifier.FINAL, false);
            }
          }

          list.setModifierProperty(myHint, true);
          if (refElement instanceof RefJavaElement) {
            RefJavaUtil.getInstance().setAccessModifier((RefJavaElement) refElement, myHint);
          }
        } catch (IncorrectOperationException e) {
          LOG.error(e);
        }
      }
    }
  private static void checkSuperclassMembers(
      PsiClass superClass,
      MemberInfoBase<? extends PsiMember>[] infos,
      MultiMap<PsiElement, String> conflictsList) {
    for (MemberInfoBase<? extends PsiMember> info : infos) {
      PsiMember member = info.getMember();
      boolean isConflict = false;
      if (member instanceof PsiField) {
        String name = member.getName();

        isConflict = superClass.findFieldByName(name, false) != null;
      } else if (member instanceof PsiMethod) {
        PsiSubstitutor superSubstitutor =
            TypeConversionUtil.getSuperClassSubstitutor(
                superClass, member.getContainingClass(), PsiSubstitutor.EMPTY);
        MethodSignature signature = ((PsiMethod) member).getSignature(superSubstitutor);
        final PsiMethod superClassMethod =
            MethodSignatureUtil.findMethodBySignature(superClass, signature, false);
        isConflict = superClassMethod != null;
      }

      if (isConflict) {
        String message =
            RefactoringBundle.message(
                "0.already.contains.a.1",
                RefactoringUIUtil.getDescription(superClass, false),
                RefactoringUIUtil.getDescription(member, false));
        message = CommonRefactoringUtil.capitalize(message);
        conflictsList.putValue(superClass, message);
      }

      if (member instanceof PsiMethod) {
        final PsiMethod method = (PsiMethod) member;
        final PsiModifierList modifierList = method.getModifierList();
        if (!modifierList.hasModifierProperty(PsiModifier.PRIVATE)) {
          for (PsiClass subClass : ClassInheritorsSearch.search(superClass)) {
            if (method.getContainingClass() != subClass) {
              MethodSignature signature =
                  ((PsiMethod) member)
                      .getSignature(
                          TypeConversionUtil.getSuperClassSubstitutor(
                              superClass, subClass, PsiSubstitutor.EMPTY));
              final PsiMethod wouldBeOverriden =
                  MethodSignatureUtil.findMethodBySignature(subClass, signature, false);
              if (wouldBeOverriden != null
                  && VisibilityUtil.compare(
                          VisibilityUtil.getVisibilityModifier(wouldBeOverriden.getModifierList()),
                          VisibilityUtil.getVisibilityModifier(modifierList))
                      > 0) {
                conflictsList.putValue(
                    wouldBeOverriden,
                    CommonRefactoringUtil.capitalize(
                        RefactoringUIUtil.getDescription(method, true)
                            + " in super class would clash with local method from "
                            + RefactoringUIUtil.getDescription(subClass, true)));
              }
            }
          }
        }
      }
    }
  }
 public static boolean modifierListsAreEquivalent(
     @Nullable PsiModifierList list1, @Nullable PsiModifierList list2) {
   if (list1 == null) {
     return list2 == null;
   } else if (list2 == null) {
     return false;
   }
   final PsiAnnotation[] annotations = list1.getAnnotations();
   for (PsiAnnotation annotation : annotations) {
     final String qualifiedName = annotation.getQualifiedName();
     if (qualifiedName == null) {
       return false;
     }
     if (list2.findAnnotation(qualifiedName) == null) {
       return false;
     }
   }
   if (list1.hasModifierProperty(PsiModifier.ABSTRACT)
       && !list2.hasModifierProperty(PsiModifier.ABSTRACT)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.FINAL)
       && !list2.hasModifierProperty(PsiModifier.FINAL)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.NATIVE)
       && !list2.hasModifierProperty(PsiModifier.NATIVE)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)
       && !list2.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.PRIVATE)
       && !list2.hasModifierProperty(PsiModifier.PRIVATE)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.PROTECTED)
       && !list2.hasModifierProperty(PsiModifier.PROTECTED)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.PUBLIC)
       && !list2.hasModifierProperty(PsiModifier.PUBLIC)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.STATIC)
       && !list2.hasModifierProperty(PsiModifier.STATIC)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.STRICTFP)
       && !list2.hasModifierProperty(PsiModifier.STRICTFP)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.SYNCHRONIZED)
       && !list2.hasModifierProperty(PsiModifier.SYNCHRONIZED)) {
     return false;
   }
   if (list1.hasModifierProperty(PsiModifier.TRANSIENT)
       && !list2.hasModifierProperty(PsiModifier.TRANSIENT)) {
     return false;
   }
   return !(list1.hasModifierProperty(PsiModifier.VOLATILE)
       && !list2.hasModifierProperty(PsiModifier.VOLATILE));
 }
 private boolean isStaticFinal(PsiField field) {
   final PsiModifierList modifierList = field.getModifierList();
   logger.assertTrue(modifierList != null);
   return modifierList.hasModifierProperty(PsiModifier.STATIC)
       && modifierList.hasModifierProperty(PsiModifier.FINAL);
 }
 public boolean hasModifierProperty(@NonNls @NotNull String name) {
   PsiModifierList list = getModifierList();
   assert list != null;
   return list.hasModifierProperty(name);
 }
 @Override
 public boolean hasModifierProperty(@PsiModifier.ModifierConstant @NonNls @NotNull String name) {
   final PsiModifierList list = getModifierList();
   return list != null && list.hasModifierProperty(name);
 }