diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index cd91310754d6..696bf197e64c 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -155,6 +155,7 @@ result.of.object.allocation.ignored.display.name=Result of object allocation ign result.of.object.allocation.ignored.problem.descriptor=Result of new #ref() is ignored. #loc use.0index.in.jdbc.resultset.display.name=Use of index 0 in JDBC ResultSet use.0index.in.jdbc.resultset.problem.descriptor=Use of index '0' in JDBC ResultSet #loc +use.0index.in.jdbc.prepared.statement.problem.descriptor=Use of index '0' in JDBC PreparedStatement #loc return.of.null.display.name=Return of 'null' return.of.null.problem.descriptor=Return of #ref #loc return.of.null.arrays.option=Report methods that return arrays diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultSetIndexZeroInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultSetIndexZeroInspection.java index f9b75e62a611..e6d52f074474 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultSetIndexZeroInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultSetIndexZeroInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,11 @@ package com.siyeh.ig.bugs; import com.intellij.psi.*; -import com.intellij.psi.util.ConstantExpressionUtil; import com.intellij.psi.util.PsiUtil; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.psiutils.ExpressionUtils; import com.siyeh.ig.psiutils.TypeUtils; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -36,15 +36,17 @@ public class ResultSetIndexZeroInspection extends BaseInspection { @Override @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "use.0index.in.jdbc.resultset.display.name"); + return InspectionGadgetsBundle.message("use.0index.in.jdbc.resultset.display.name"); } @Override @NotNull public String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "use.0index.in.jdbc.resultset.problem.descriptor"); + if (((Boolean)infos[0]).booleanValue()) { + return InspectionGadgetsBundle.message("use.0index.in.jdbc.resultset.problem.descriptor"); + } else { + return InspectionGadgetsBundle.message("use.0index.in.jdbc.prepared.statement.problem.descriptor"); + } } @Override @@ -52,22 +54,22 @@ public class ResultSetIndexZeroInspection extends BaseInspection { return new ResultSetIndexZeroVisitor(); } - private static class ResultSetIndexZeroVisitor - extends BaseInspectionVisitor { + private static class ResultSetIndexZeroVisitor extends BaseInspectionVisitor { @Override - public void visitMethodCallExpression( - @NotNull PsiMethodCallExpression expression) { + public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); - final PsiReferenceExpression methodExpression = - expression.getMethodExpression(); - @NonNls final String methodName = - methodExpression.getReferenceName(); + final PsiReferenceExpression methodExpression = expression.getMethodExpression(); + @NonNls final String methodName = methodExpression.getReferenceName(); if (methodName == null) { return; } - if (!methodName.startsWith("get") && - !methodName.startsWith("update")) { + final boolean resultSet; + if (methodName.startsWith("get") || methodName.startsWith("update")) { + resultSet = true; + } else if (methodName.startsWith("set")) { + resultSet = false; + } else { return; } final PsiExpressionList argumentList = expression.getArgumentList(); @@ -82,19 +84,20 @@ public class ResultSetIndexZeroInspection extends BaseInspection { if (!PsiUtil.isConstantExpression(argument)) { return; } - final Integer val = - (Integer)ConstantExpressionUtil.computeCastTo(argument, - PsiType.INT); - if (val == null || val.intValue() != 0) { + final Object val = ExpressionUtils.computeConstantExpression(argument); + if (!(val instanceof Integer) || ((Integer)val).intValue() != 0) { return; } - final PsiExpression qualifier = - methodExpression.getQualifierExpression(); - if (!TypeUtils.expressionHasTypeOrSubtype(qualifier, - "java.sql.ResultSet")) { - return; + final PsiExpression qualifier = methodExpression.getQualifierExpression(); + if (resultSet) { + if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.ResultSet")) { + registerError(argument, Boolean.valueOf(resultSet)); + } + } else { + if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.PreparedStatement")) { + registerError(argument, Boolean.valueOf(resultSet)); + } } - registerError(argument); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ResultSetIndexZero.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ResultSetIndexZero.html index 73f598072fcc..30f9a01422b8 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/ResultSetIndexZero.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ResultSetIndexZero.html @@ -1,7 +1,7 @@ -This inspection reports any attempts to access column 0 of a java.sql.ResultSet. For historical -reasons, columns of java.sql.ResultSets are numbered beginning with 1, rather than +This inspection reports any attempts to access column 0 of a java.sql.ResultSet or java.sql.PreparedStatement. For historical +reasons columns of java.sql.ResultSets and java.sql.PreparedStatements are numbered beginning with 1, rather than 0, and accessing column 0 is a common error in JDBC programming.

Powered by InspectionGadgets