@Override
 public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) {
   super.visitBinaryExpression(expression);
   final PsiExpression rhs = expression.getROperand();
   if (!(rhs != null)) {
     return;
   }
   if (!ComparisonUtils.isComparison(expression)) {
     return;
   }
   if (ComparisonUtils.isEqualityComparison(expression)) {
     return;
   }
   final PsiExpression lhs = expression.getLOperand();
   if (!isCharacter(lhs)) {
     return;
   }
   if (!isCharacter(rhs)) {
     return;
   }
   if (NonNlsUtils.isNonNlsAnnotated(lhs) || NonNlsUtils.isNonNlsAnnotated(rhs)) {
     return;
   }
   registerError(expression);
 }
 @Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   final PsiExpressionList argumentList = expression.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   if (arguments.length == 0) {
     return;
   }
   final PsiExpression xpathArgument = arguments[0];
   if (!ExpressionUtils.hasStringType(xpathArgument)) {
     return;
   }
   if (!PsiUtil.isConstantExpression(xpathArgument)) {
     return;
   }
   final PsiType type = xpathArgument.getType();
   if (type == null) {
     return;
   }
   final String value = (String) ConstantExpressionUtil.computeCastTo(xpathArgument, type);
   if (value == null) {
     return;
   }
   if (!callTakesXPathExpression(expression)) {
     return;
   }
   final XPathFactory xpathFactory = XPathFactory.newInstance();
   final XPath xpath = xpathFactory.newXPath();
   //noinspection UnusedCatchParameter,ProhibitedExceptionCaught
   try {
     xpath.compile(value);
   } catch (XPathExpressionException ignore) {
     registerError(xpathArgument);
   }
 }
 @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 visitForStatement(PsiForStatement statement) {
   super.visitForStatement(statement);
   final PsiExpression condition = statement.getCondition();
   final PsiStatement body = statement.getBody();
   checkCondition(condition, body);
 }
 @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 visitIfStatement(PsiIfStatement statement) {
   super.visitIfStatement(statement);
   final PsiExpression condition = statement.getCondition();
   final PsiStatement body = statement.getThenBranch();
   checkCondition(condition, body);
 }
 @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 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);
 }
 @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 visitMethodCallExpression(PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   final PsiReferenceExpression methodExpression = expression.getMethodExpression();
   @NonNls final String referenceName = methodExpression.getReferenceName();
   if (!"toString".equals(referenceName)
       || ExpressionUtils.isConversionToStringNecessary(expression)) {
     return;
   }
   final PsiExpressionList argumentList = expression.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   if (arguments.length != 0) {
     return;
   }
   final PsiExpression qualifier = methodExpression.getQualifierExpression();
   if (qualifier != null) {
     if (qualifier.getType() instanceof PsiArrayType) {
       // do not warn on nonsensical code
       return;
     } else if (qualifier instanceof PsiSuperExpression) {
       return;
     }
   }
   registerMethodCallError(expression, calculateReplacementText(qualifier));
 }
 @Override
 public void visitMethodCallExpression(PsiMethodCallExpression methodCallExpression) {
   super.visitMethodCallExpression(methodCallExpression);
   if (!MethodCallUtils.isSimpleCallToMethod(
           methodCallExpression,
           "javax.servlet.http.HttpSession",
           PsiType.VOID,
           "putValue",
           "java.lang.String",
           "java.lang.Object")
       && !MethodCallUtils.isSimpleCallToMethod(
           methodCallExpression,
           "javax.servlet.http.HttpSession",
           PsiType.VOID,
           "setAttribute",
           "java.lang.String",
           "java.lang.Object")) {
     return;
   }
   final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   if (arguments.length != 2) {
     return;
   }
   final PsiExpression argument = arguments[1];
   final PsiType argumentType = argument.getType();
   if (argumentType == null) {
     return;
   }
   if (SerializationUtils.isProbablySerializable(argumentType)) {
     return;
   }
   registerError(argument);
 }
 @Override
 public void visitLambdaExpression(PsiLambdaExpression lambdaExpression) {
   super.visitLambdaExpression(lambdaExpression);
   if (lambdaExpression.getBody() instanceof PsiExpression) {
     registerError(lambdaExpression);
   }
 }
 @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);
   }
 }
 @Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   final PsiReferenceExpression methodExpression = expression.getMethodExpression();
   @NonNls final String methodName = methodExpression.getReferenceName();
   if (methodName == null) {
     return;
   }
   if (!methodName.startsWith("get") && !methodName.startsWith("update")) {
     return;
   }
   final PsiExpressionList argumentList = expression.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   if (arguments.length == 0) {
     return;
   }
   final PsiExpression argument = arguments[0];
   if (!TypeUtils.expressionHasType(argument, PsiKeyword.INT)) {
     return;
   }
   if (!PsiUtil.isConstantExpression(argument)) {
     return;
   }
   final Integer val = (Integer) ConstantExpressionUtil.computeCastTo(argument, PsiType.INT);
   if (val == null || val.intValue() != 0) {
     return;
   }
   final PsiExpression qualifier = methodExpression.getQualifierExpression();
   if (!TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.ResultSet")) {
     return;
   }
   registerError(argument);
 }
 @Override
 public void visitNewExpression(PsiNewExpression expression) {
   super.visitNewExpression(expression);
   final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
   if (classReference == null) {
     return;
   }
   final String name = classReference.getReferenceName();
   if (!"BigDecimal".equals(name)) {
     return;
   }
   final PsiMethod constructor = expression.resolveConstructor();
   if (constructor == null) {
     return;
   }
   final PsiParameterList parameterList = constructor.getParameterList();
   final int length = parameterList.getParametersCount();
   if (length != 1 && length != 2) {
     return;
   }
   final PsiParameter[] parameters = parameterList.getParameters();
   final PsiParameter firstParameter = parameters[0];
   final PsiType type = firstParameter.getType();
   if (type != PsiType.DOUBLE) {
     return;
   }
   registerNewExpressionError(expression);
 }
 @Override
 public void visitMethodCallExpression(PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   if (!isCallToRegexMethod(expression)) {
     return;
   }
   registerMethodCallError(expression);
 }
 @Override
 public void visitReturnStatement(@NotNull PsiReturnStatement statement) {
   super.visitReturnStatement(statement);
   if (!ControlFlowUtils.isInFinallyBlock(statement)) {
     return;
   }
   registerStatementError(statement);
 }
 @Override
 public void visitMethod(@NotNull PsiMethod method) {
   super.visitMethod(method);
   final String name = method.getName();
   if (!PsiKeyword.ASSERT.equals(name)) {
     return;
   }
   registerMethodError(method);
 }
 @Override
 public void visitVariable(@NotNull PsiVariable variable) {
   super.visitVariable(variable);
   final String variableName = variable.getName();
   if (!PsiKeyword.ASSERT.equals(variableName)) {
     return;
   }
   registerVariableError(variable);
 }
 @Override
 public void visitTypeParameter(PsiTypeParameter parameter) {
   super.visitTypeParameter(parameter);
   final String name = parameter.getName();
   if (!PsiKeyword.ASSERT.equals(name)) {
     return;
   }
   registerTypeParameterError(parameter);
 }
 @Override
 public void visitElement(PsiElement element) {
   if (element.getLanguage() != JavaLanguage.INSTANCE) {
     return;
   }
   if (!PsiUtil.isLanguageLevel5OrHigher(element)) {
     return;
   }
   super.visitElement(element);
 }
 @Override
 public void visitField(@NotNull PsiField field) {
   super.visitField(field);
   final PsiType type = field.getType();
   if (!type.equalsToText(CommonClassNames.JAVA_LANG_STRING_BUFFER)
       && !type.equalsToText(CommonClassNames.JAVA_LANG_STRING_BUILDER)) {
     return;
   }
   registerFieldError(field, type);
 }
 @Override
 public void visitThrowStatement(PsiThrowStatement statement) {
   super.visitThrowStatement(statement);
   final PsiCatchSection catchSection =
       PsiTreeUtil.getParentOfType(statement, PsiCatchSection.class, true, PsiClass.class);
   if (catchSection == null) {
     return;
   }
   final PsiParameter parameter = catchSection.getParameter();
   if (parameter == null) {
     return;
   }
   @NonNls final String parameterName = parameter.getName();
   if (PsiUtil.isIgnoredName(parameterName)) {
     return;
   }
   final PsiExpression exception = statement.getException();
   if (exception == null) {
     return;
   }
   if (ignoreCantWrap) {
     final PsiType thrownType = exception.getType();
     if (thrownType instanceof PsiClassType) {
       final PsiClassType classType = (PsiClassType) thrownType;
       final PsiClass exceptionClass = classType.resolve();
       if (exceptionClass != null) {
         final PsiMethod[] constructors = exceptionClass.getConstructors();
         final PsiClassType throwableType =
             TypeUtils.getType(CommonClassNames.JAVA_LANG_THROWABLE, statement);
         boolean canWrap = false;
         outer:
         for (PsiMethod constructor : constructors) {
           final PsiParameterList parameterList = constructor.getParameterList();
           final PsiParameter[] parameters = parameterList.getParameters();
           for (PsiParameter constructorParameter : parameters) {
             final PsiType type = constructorParameter.getType();
             if (throwableType.equals(type)) {
               canWrap = true;
               break outer;
             }
           }
         }
         if (!canWrap) {
           return;
         }
       }
     }
   }
   final ReferenceFinder visitor = new ReferenceFinder(parameter);
   exception.accept(visitor);
   if (visitor.usesParameter()) {
     return;
   }
   registerStatementError(statement);
 }
 @Override
 public void visitPolyadicExpression(PsiPolyadicExpression expression) {
   super.visitPolyadicExpression(expression);
   if (!ExpressionUtils.hasStringType(expression)) {
     return;
   }
   final PsiExpression[] operands = expression.getOperands();
   for (PsiExpression operand : operands) {
     checkExpression(operand);
   }
 }
 @Override
 public void visitMethod(@NotNull PsiMethod method) {
   super.visitMethod(method);
   if (!method.hasModifierProperty(PsiModifier.STATIC)) {
     return;
   }
   final String name = method.getName();
   if (isValid(name)) {
     return;
   }
   registerMethodError(method, name);
 }
 @Override
 public void visitEnumConstant(PsiEnumConstant constant) {
   super.visitEnumConstant(constant);
   final String name = constant.getName();
   if (name == null) {
     return;
   }
   if (isValid(name)) {
     return;
   }
   registerFieldError(constant, name);
 }
 @Override
 public void visitThrowStatement(PsiThrowStatement statement) {
   super.visitThrowStatement(statement);
   final PsiExpression exception = ParenthesesUtils.stripParentheses(statement.getException());
   if (!(exception instanceof PsiLiteralExpression)) {
     return;
   }
   final PsiType type = exception.getType();
   if (!PsiType.NULL.equals(type)) {
     return;
   }
   registerError(exception);
 }
 @Override
 public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) {
   super.visitBinaryExpression(expression);
   if (!ComparisonUtils.isComparison(expression)) {
     return;
   }
   final PsiExpression lhs = expression.getLOperand();
   final PsiExpression rhs = expression.getROperand();
   if (rhs == null || !isConstantExpression(lhs) || isConstantExpression(rhs)) {
     return;
   }
   registerError(expression);
 }
 @Override
 public void visitAssignmentExpression(@NotNull PsiAssignmentExpression expression) {
   super.visitAssignmentExpression(expression);
   final IElementType tokenType = expression.getOperationTokenType();
   if (!tokenType.equals(JavaTokenType.PLUSEQ)) {
     return;
   }
   final PsiExpression lhs = expression.getLExpression();
   if (!ExpressionUtils.hasStringType(lhs)) {
     return;
   }
   final PsiExpression rhs = expression.getRExpression();
   checkExpression(rhs);
 }
Exemplo n.º 30
0
 @Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   if (!MethodCallUtils.isCallToMethod(
           expression, "java.lang.Thread", PsiType.VOID, "sleep", PsiType.LONG)
       && !MethodCallUtils.isCallToMethod(
           expression, "java.lang.Thread", PsiType.VOID, "sleep", PsiType.LONG, PsiType.INT)) {
     return;
   }
   if (!ControlFlowUtils.isInLoop(expression)) {
     return;
   }
   registerMethodCallError(expression);
 }