@Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) {
   super.visitMethodCallExpression(call);
   final PsiReferenceExpression methodExpression = call.getMethodExpression();
   final PsiElement qualifier = methodExpression.getQualifier();
   if (!(qualifier instanceof PsiReferenceExpression)) {
     return;
   }
   final PsiMethod method = call.resolveMethod();
   if (method == null) {
     return;
   }
   if (!method.hasModifierProperty(PsiModifier.STATIC)) {
     return;
   }
   final PsiElement referent = ((PsiReference) qualifier).resolve();
   if (!(referent instanceof PsiClass)) {
     return;
   }
   final PsiClass referencedClass = (PsiClass) referent;
   final PsiClass declaringClass = method.getContainingClass();
   if (declaringClass == null) {
     return;
   }
   if (declaringClass.equals(referencedClass)) {
     return;
   }
   final PsiClass containingClass = ClassUtils.getContainingClass(call);
   if (!ClassUtils.isClassVisibleFromClass(containingClass, declaringClass)) {
     return;
   }
   registerMethodCallError(call, declaringClass, referencedClass);
 }
 @Nullable
 private static PsiClass getContainingContextClass(PsiElement element) {
   final PsiClass aClass = ClassUtils.getContainingClass(element);
   if (aClass instanceof PsiAnonymousClass) {
     final PsiAnonymousClass anonymousClass = (PsiAnonymousClass) aClass;
     final PsiExpressionList args = anonymousClass.getArgumentList();
     if (args != null && PsiTreeUtil.isAncestor(args, element, true)) {
       return ClassUtils.getContainingClass(aClass);
     }
   }
   return aClass;
 }
 @Override
 public void visitMethod(@NotNull PsiMethod method) {
   super.visitMethod(method);
   final PsiCodeBlock body = method.getBody();
   if (body == null) {
     return;
   }
   if (method.getNameIdentifier() == null) {
     return;
   }
   final PsiMethod leastConcreteSuperMethod = getLeastConcreteSuperMethod(method);
   if (leastConcreteSuperMethod == null) {
     return;
   }
   final PsiClass objectClass = ClassUtils.findObjectClass(method);
   final PsiMethod[] superMethods = method.findSuperMethods(objectClass);
   if (superMethods.length > 0) {
     return;
   }
   if (ignoreEmptySuperMethods) {
     final PsiMethod superMethod = (PsiMethod) leastConcreteSuperMethod.getNavigationElement();
     if (MethodUtils.isTrivial(superMethod, true)) {
       return;
     }
   }
   if (onlyReportWhenAnnotated) {
     if (!AnnotationUtil.isAnnotated(leastConcreteSuperMethod, annotations)) {
       return;
     }
   }
   if (containsSuperCall(body, leastConcreteSuperMethod)) {
     return;
   }
   registerMethodError(method);
 }
 @Override
 public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
   if (FileTypeUtils.isInServerPageFile(expression)) {
     // disable for jsp files IDEADEV-12957
     return;
   }
   super.visitReferenceExpression(expression);
   if (expression.getQualifierExpression() == null) {
     return;
   }
   final PsiElement referenceNameElement = expression.getReferenceNameElement();
   if (referenceNameElement == null) {
     return;
   }
   final PsiElement containingClass = getContainingContextClass(expression);
   if (containingClass == null) {
     return;
   }
   final PsiElement element = expression.resolve();
   if (!(element instanceof PsiMethod || element instanceof PsiField)) {
     return;
   }
   final PsiMember member = (PsiMember) element;
   if (!member.hasModifierProperty(PsiModifier.PRIVATE)) {
     return;
   }
   final PsiClass memberClass = ClassUtils.getContainingClass(member);
   if (memberClass == null) {
     return;
   }
   if (memberClass.equals(containingClass)) {
     return;
   }
   registerError(referenceNameElement, memberClass, member);
 }
 @Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   final PsiReferenceExpression methodExpression = expression.getMethodExpression();
   final String methodName = methodExpression.getReferenceName();
   @NonNls final String signal = "signal";
   if (!signal.equals(methodName)) {
     return;
   }
   final PsiExpressionList argumentList = expression.getArgumentList();
   if (argumentList.getExpressions().length != 0) {
     return;
   }
   final PsiMethod method = expression.resolveMethod();
   if (method == null) {
     return;
   }
   final PsiClass containingClass = method.getContainingClass();
   if (containingClass == null) {
     return;
   }
   if (!ClassUtils.isSubclass(containingClass, "java.util.concurrent.locks.Condition")) {
     return;
   }
   registerMethodCallError(expression);
 }
 @Override
 public void visitNewExpression(@NotNull PsiNewExpression expression) {
   if (!PsiUtil.isLanguageLevel5OrHigher(expression)) {
     return;
   }
   super.visitNewExpression(expression);
   final PsiType type = expression.getType();
   if (type == null) {
     return;
   }
   final String canonicalText = type.getCanonicalText();
   if (!cachedNumberTypes.contains(canonicalText)) {
     return;
   }
   final PsiClass aClass = ClassUtils.getContainingClass(expression);
   if (aClass != null && cachedNumberTypes.contains(aClass.getQualifiedName())) {
     return;
   }
   final PsiExpressionList argumentList = expression.getArgumentList();
   if (argumentList == null) {
     return;
   }
   final PsiExpression[] arguments = argumentList.getExpressions();
   if (arguments.length != 1) {
     return;
   }
   final PsiExpression argument = arguments[0];
   final PsiType argumentType = argument.getType();
   if (argumentType == null || argumentType.equalsToText("java.lang.String")) {
     return;
   }
   registerError(expression, expression);
 }
 @Override
 public void visitLiteralExpression(@NotNull PsiLiteralExpression expression) {
   super.visitLiteralExpression(expression);
   final PsiType type = expression.getType();
   if (!ClassUtils.isPrimitiveNumericType(type)) {
     return;
   }
   if (PsiType.CHAR.equals(type)) {
     return;
   }
   if (isSpecialCaseLiteral(expression)) {
     return;
   }
   if (ExpressionUtils.isDeclaredConstant(expression)) {
     return;
   }
   if (ignoreInHashCode) {
     final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class);
     if (MethodUtils.isHashCode(containingMethod)) {
       return;
     }
   }
   if (ignoreInTestCode && TestUtils.isInTestCode(expression)) {
     return;
   }
   final PsiElement parent = expression.getParent();
   if (parent instanceof PsiPrefixExpression) {
     registerError(parent);
   } else {
     registerError(expression);
   }
 }
Пример #8
0
 public boolean isAccepted(PsiClass aClass) {
   for (String ancestorClass : ancestorClasses) {
     if (ClassUtils.isSubclass(aClass, ancestorClass)) {
       return true;
     }
   }
   return false;
 }
 @Override
 public void visitField(@NotNull PsiField field) {
   if (field.hasModifierProperty(PsiModifier.STATIC)) {
     return;
   }
   if (field.getInitializer() != null) {
     return;
   }
   final PsiAnnotation annotation = AnnotationUtil.findAnnotation(field, annotationNames);
   if (annotation != null) {
     return;
   }
   if (m_ignorePrimitives) {
     final PsiType fieldType = field.getType();
     if (ClassUtils.isPrimitive(fieldType)) {
       return;
     }
   }
   final PsiClass aClass = field.getContainingClass();
   if (aClass == null) {
     return;
   }
   for (ImplicitUsageProvider provider :
       Extensions.getExtensions(ImplicitUsageProvider.EP_NAME)) {
     if (provider.isImplicitWrite(field)) {
       return;
     }
   }
   final UninitializedReadCollector uninitializedReadsCollector =
       new UninitializedReadCollector();
   if (!isInitializedInInitializer(field, uninitializedReadsCollector)) {
     final PsiMethod[] constructors = aClass.getConstructors();
     for (final PsiMethod constructor : constructors) {
       final PsiCodeBlock body = constructor.getBody();
       uninitializedReadsCollector.blockAssignsVariable(body, field);
     }
   }
   final PsiExpression[] badReads = uninitializedReadsCollector.getUninitializedReads();
   for (PsiExpression expression : badReads) {
     registerError(expression);
   }
 }
 private void checkExpression(@NotNull PsiExpression expression) {
   if (expression.getParent() instanceof PsiParenthesizedExpression) {
     return;
   }
   final PsiType expressionType = expression.getType();
   if (expressionType == null
       || expressionType.equals(PsiType.VOID)
       || !TypeConversionUtil.isPrimitiveAndNotNull(expressionType)) {
     return;
   }
   final PsiPrimitiveType primitiveType = (PsiPrimitiveType) expressionType;
   final PsiClassType boxedType = primitiveType.getBoxedType(expression);
   if (boxedType == null) {
     return;
   }
   final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, false);
   if (expectedType == null || ClassUtils.isPrimitive(expectedType)) {
     return;
   }
   if (!expectedType.isAssignableFrom(boxedType)) {
     // JLS 5.2 Assignment Conversion
     // check if a narrowing primitive conversion is applicable
     if (!(expectedType instanceof PsiClassType) || !PsiUtil.isConstantExpression(expression)) {
       return;
     }
     final PsiClassType classType = (PsiClassType) expectedType;
     final String className = classType.getCanonicalText();
     if (!convertableBoxedClassNames.contains(className)) {
       return;
     }
     if (!PsiType.BYTE.equals(expressionType)
         && !PsiType.CHAR.equals(expressionType)
         && !PsiType.SHORT.equals(expressionType)
         && !PsiType.INT.equals(expressionType)) {
       return;
     }
   }
   if (ignoreAddedToCollection && isAddedToCollection(expression)) {
     return;
   }
   registerError(expression);
 }
 @Override
 public void visitClass(@NotNull PsiClass aClass) {
   if (!aClass.isEnum()) {
     return;
   }
   if (!ClassUtils.isInnerClass(aClass)) {
     return;
   }
   if (!aClass.hasModifierProperty(PsiModifier.STATIC)) {
     return;
   }
   final PsiModifierList modifiers = aClass.getModifierList();
   if (modifiers == null) {
     return;
   }
   final PsiElement[] children = modifiers.getChildren();
   for (final PsiElement child : children) {
     final String text = child.getText();
     if (PsiModifier.STATIC.equals(text)) {
       registerError(child, child, aClass);
     }
   }
 }
    @Override
    protected void doFix(Project project, ProblemDescriptor descriptor) {
      final PsiElement element = descriptor.getPsiElement();
      final PsiClass aClass = ClassUtils.getContainingStaticClass(element);
      if (aClass == null) {
        return;
      }
      final PsiElement parent = element.getParent();
      if (!(parent instanceof PsiReferenceExpression)) {
        return;
      }
      final PsiReferenceExpression methodExpression = (PsiReferenceExpression) parent;
      final PsiElement grandParent = methodExpression.getParent();
      if (!(grandParent instanceof PsiMethodCallExpression)) {
        return;
      }
      final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) grandParent;
      final PsiExpressionList list = methodCallExpression.getArgumentList();
      final PsiExpression[] expressions = list.getExpressions();
      @NonNls
      final StringBuilder fieldText =
          new StringBuilder(
              "private static final java.util.regex.Pattern PATTERN = java.util.regex.Pattern.compile(");
      final int expressionsLength = expressions.length;
      if (expressionsLength > 0) {
        fieldText.append(expressions[0].getText());
      }
      @NonNls final String methodName = methodExpression.getReferenceName();
      final boolean literalReplacement = "replace".equals(methodName);
      if (literalReplacement) {
        fieldText.append(", java.util.regex.Pattern.LITERAL");
      }
      fieldText.append(");");

      @NonNls final StringBuilder expressionText = new StringBuilder("PATTERN.");
      final PsiExpression qualifier = methodExpression.getQualifierExpression();
      @NonNls final String qualifierText = (qualifier == null) ? "this" : qualifier.getText();
      if ("split".equals(methodName)) {
        expressionText.append(methodName);
        expressionText.append('(');
        expressionText.append(qualifierText);
        for (int i = 1; i < expressionsLength; i++) {
          expressionText.append(',').append(expressions[i].getText());
        }
        expressionText.append(')');
      } else {
        expressionText.append("matcher(").append(qualifierText).append(").");
        if (literalReplacement) {
          expressionText.append("replaceAll");
        } else {
          expressionText.append(methodName);
        }
        expressionText.append('(');
        if (literalReplacement) {
          expressionText.append("java.util.regex.Matcher.quoteReplacement(");
        }
        if (expressionsLength > 1) {
          expressionText.append(expressions[1].getText());
          for (int i = 2; i < expressionsLength; i++) {
            expressionText.append(',').append(expressions[i].getText());
          }
        }
        if (literalReplacement) {
          expressionText.append(')');
        }
        expressionText.append(')');
      }

      final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
      final PsiField newField = factory.createFieldFromText(fieldText.toString(), element);
      final PsiElement field = aClass.add(newField);
      final PsiExpression newExpression =
          factory.createExpressionFromText(expressionText.toString(), element);
      PsiMethodCallExpression newMethodCallExpression =
          (PsiMethodCallExpression) methodCallExpression.replace(newExpression);
      newMethodCallExpression =
          CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(newMethodCallExpression);
      final PsiReferenceExpression reference = getReference(newMethodCallExpression);
      HighlightUtils.showRenameTemplate(aClass, (PsiNameIdentifierOwner) field, reference);
    }