(9);
+ @SuppressWarnings({"PublicField"})
+ public boolean insideTryAllowed = false;
+
static {
- ContainerUtil.addAll(creationMethodNameSet, creationMethodName);
+ ContainerUtil.addAll(creationMethodNameSet, creationMethodName);
}
+ @Override
@NotNull
- public String getID(){
+ public String getID() {
return "JDBCResourceOpenedButNotSafelyClosed";
}
+ @Override
@NotNull
- public String getDisplayName(){
+ public String getDisplayName() {
return InspectionGadgetsBundle.message(
"jdbc.resource.opened.not.closed.display.name");
}
+ @Override
@NotNull
- public String buildErrorString(Object... infos){
+ public String buildErrorString(Object... infos) {
final PsiExpression expression = (PsiExpression) infos[0];
final PsiType type = expression.getType();
assert type != null;
@@ -83,60 +91,83 @@ public class JDBCResourceInspection extends ResourceInspection{
"jdbc.resource.opened.not.closed.problem.descriptor", text);
}
- public BaseInspectionVisitor buildVisitor(){
+ @Override
+ public JComponent createOptionsPanel() {
+ final JComponent panel = new JPanel(new GridBagLayout());
+ final CheckBox checkBox = new CheckBox(
+ InspectionGadgetsBundle.message(
+ "allow.resource.to.be.opened.inside.a.try.block"),
+ this, "insideTryAllowed");
+
+ final GridBagConstraints constraints = new GridBagConstraints();
+ constraints.anchor = GridBagConstraints.FIRST_LINE_START;
+ constraints.gridx = 0;
+ constraints.gridy = 0;
+ constraints.insets.left = 4;
+ constraints.insets.right = 4;
+ constraints.weightx = 1.0;
+ constraints.weighty = 1.0;
+ constraints.fill = GridBagConstraints.HORIZONTAL;
+ panel.add(checkBox, constraints);
+ return panel;
+ }
+
+ @Override
+ public BaseInspectionVisitor buildVisitor() {
return new JDBCResourceVisitor();
}
- private static class JDBCResourceVisitor extends BaseInspectionVisitor{
+ private class JDBCResourceVisitor extends BaseInspectionVisitor {
- @Override public void visitMethodCallExpression(
- @NotNull PsiMethodCallExpression expression){
+ @Override
+ public void visitMethodCallExpression(
+ @NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
- if(!isJDBCResourceCreation(expression)){
+ if (!isJDBCResourceCreation(expression)) {
return;
}
final PsiElement parent = getExpressionParent(expression);
- if(parent instanceof PsiReturnStatement){
+ if (parent instanceof PsiReturnStatement) {
return;
}
final PsiVariable boundVariable = getVariable(parent);
- if(isSafelyClosed(boundVariable, expression)){
+ if (isSafelyClosed(boundVariable, expression, insideTryAllowed)) {
return;
}
- if(isResourceEscapedFromMethod(boundVariable, expression)){
+ if (isResourceEscapedFromMethod(boundVariable, expression)) {
return;
}
registerError(expression, expression);
}
- private static boolean isJDBCResourceCreation(
- PsiMethodCallExpression expression){
+ private boolean isJDBCResourceCreation(
+ PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
final String name = methodExpression.getReferenceName();
- if(name == null){
+ if (name == null) {
return false;
}
- if(!creationMethodNameSet.contains(name)){
+ if (!creationMethodNameSet.contains(name)) {
return false;
}
final PsiMethod method = expression.resolveMethod();
- if(method == null){
+ if (method == null) {
return false;
}
- for(int i = 0; i < creationMethodName.length; i++){
- if(!name.equals(creationMethodName[i])){
+ for (int i = 0; i < creationMethodName.length; i++) {
+ if (!name.equals(creationMethodName[i])) {
continue;
}
final PsiClass containingClass = method.getContainingClass();
- if(containingClass == null){
+ if (containingClass == null) {
return false;
}
final String className = containingClass.getQualifiedName();
- if(className == null){
+ if (className == null) {
return false;
}
- if(className.equals(creationMethodClassName[i])){
+ if (className.equals(creationMethodClassName[i])) {
return true;
}
}
diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/resources/JNDIResourceInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/resources/JNDIResourceInspection.java
index 1d5d3d491780..d79432ab2b52 100644
--- a/plugins/InspectionGadgets/src/com/siyeh/ig/resources/JNDIResourceInspection.java
+++ b/plugins/InspectionGadgets/src/com/siyeh/ig/resources/JNDIResourceInspection.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
+ * Copyright 2003-2010 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.
@@ -19,24 +19,34 @@ import com.intellij.psi.*;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TypeUtils;
+import com.siyeh.ig.ui.CheckBox;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
+import javax.swing.*;
+import java.awt.*;
+
public class JNDIResourceInspection extends ResourceInspection {
+ @SuppressWarnings({"PublicField"})
+ public boolean insideTryAllowed = false;
+
+ @Override
@NotNull
- public String getID(){
+ public String getID() {
return "JNDIResourceOpenedButNotSafelyClosed";
}
+ @Override
@NotNull
- public String getDisplayName(){
+ public String getDisplayName() {
return InspectionGadgetsBundle.message(
"jndi.resource.opened.not.closed.display.name");
}
+ @Override
@NotNull
- public String buildErrorString(Object... infos){
+ public String buildErrorString(Object... infos) {
final PsiExpression expression = (PsiExpression) infos[0];
final PsiType type = expression.getType();
assert type != null;
@@ -45,41 +55,46 @@ public class JNDIResourceInspection extends ResourceInspection {
"resource.opened.not.closed.problem.descriptor", text);
}
- public BaseInspectionVisitor buildVisitor(){
+ @Override
+ public JComponent createOptionsPanel() {
+ final JComponent panel = new JPanel(new GridBagLayout());
+ final CheckBox checkBox = new CheckBox(
+ InspectionGadgetsBundle.message(
+ "allow.resource.to.be.opened.inside.a.try.block"),
+ this, "insideTryAllowed");
+
+ final GridBagConstraints constraints = new GridBagConstraints();
+ constraints.anchor = GridBagConstraints.FIRST_LINE_START;
+ constraints.gridx = 0;
+ constraints.gridy = 0;
+ constraints.insets.left = 4;
+ constraints.insets.right = 4;
+ constraints.weightx = 1.0;
+ constraints.weighty = 1.0;
+ constraints.fill = GridBagConstraints.HORIZONTAL;
+ panel.add(checkBox, constraints);
+ return panel;
+ }
+
+ @Override
+ public BaseInspectionVisitor buildVisitor() {
return new JNDIResourceVisitor();
}
- private static class JNDIResourceVisitor extends BaseInspectionVisitor{
+ private class JNDIResourceVisitor extends BaseInspectionVisitor {
- @NonNls private static final String LIST = "list";
- @NonNls private static final String LIST_BINDING = "listBindings";
- @NonNls private static final String GET_ALL = "getAll";
+ @NonNls
+ private static final String LIST = "list";
+ @NonNls
+ private static final String LIST_BINDING = "listBindings";
+ @NonNls
+ private static final String GET_ALL = "getAll";
- @Override public void visitMethodCallExpression(
- @NotNull PsiMethodCallExpression expression){
+ @Override
+ public void visitMethodCallExpression(
+ @NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
- if(!isJNDIFactoryMethod(expression)){
- return;
- }
- final PsiElement parent = getExpressionParent(expression);
- if(parent instanceof PsiReturnStatement){
- return;
- }
- final PsiVariable boundVariable = getVariable(parent);
- if(isSafelyClosed(boundVariable, expression)){
- return;
- }
- if(isResourceEscapedFromMethod(boundVariable, expression)){
- return;
- }
- registerError(expression, expression);
- }
-
-
- @Override public void visitNewExpression(
- @NotNull PsiNewExpression expression){
- super.visitNewExpression(expression);
- if(!isJNDIResource(expression)){
+ if (!isJNDIFactoryMethod(expression)) {
return;
}
final PsiElement parent = getExpressionParent(expression);
@@ -87,22 +102,44 @@ public class JNDIResourceInspection extends ResourceInspection {
return;
}
final PsiVariable boundVariable = getVariable(parent);
- if (isSafelyClosed(boundVariable, expression)) {
+ if (isSafelyClosed(boundVariable, expression, insideTryAllowed)) {
return;
}
- if(isResourceEscapedFromMethod(boundVariable, expression)){
+ if (isResourceEscapedFromMethod(boundVariable, expression)) {
return;
}
registerError(expression, expression);
}
- private static boolean isJNDIResource(PsiNewExpression expression){
- return TypeUtils.expressionHasTypeOrSubtype(expression,
- "javax.naming.InitialContext");
+
+ @Override
+ public void visitNewExpression(
+ @NotNull PsiNewExpression expression) {
+ super.visitNewExpression(expression);
+ if (!isJNDIResource(expression)) {
+ return;
+ }
+ final PsiElement parent = getExpressionParent(expression);
+ if (parent instanceof PsiReturnStatement) {
+ return;
+ }
+ final PsiVariable boundVariable = getVariable(parent);
+ if (isSafelyClosed(boundVariable, expression, insideTryAllowed)) {
+ return;
+ }
+ if (isResourceEscapedFromMethod(boundVariable, expression)) {
+ return;
+ }
+ registerError(expression, expression);
}
- private static boolean isJNDIFactoryMethod(
- PsiMethodCallExpression expression){
+ private boolean isJNDIResource(PsiNewExpression expression) {
+ return TypeUtils.expressionHasTypeOrSubtype(expression,
+ "javax.naming.InitialContext");
+ }
+
+ private boolean isJNDIFactoryMethod(
+ PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
final String methodName = methodExpression.getReferenceName();
diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/resources/ResourceInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/resources/ResourceInspection.java
index cfd7641fd1d5..b5ed6b87fba5 100644
--- a/plugins/InspectionGadgets/src/com/siyeh/ig/resources/ResourceInspection.java
+++ b/plugins/InspectionGadgets/src/com/siyeh/ig/resources/ResourceInspection.java
@@ -59,7 +59,8 @@ public abstract class ResourceInspection extends BaseInspection {
}
protected static boolean isSafelyClosed(@Nullable PsiVariable variable,
- PsiElement context) {
+ PsiElement context,
+ boolean insideTryAllowed) {
if (variable == null) {
return false;
}
@@ -71,6 +72,23 @@ public abstract class ResourceInspection extends BaseInspection {
PsiStatement nextStatement =
PsiTreeUtil.getNextSiblingOfType(statement,
PsiStatement.class);
+ if (insideTryAllowed) {
+ PsiStatement parentStatement =
+ PsiTreeUtil.getParentOfType(statement, PsiStatement.class);
+ while (parentStatement != null &&
+ !(parentStatement instanceof PsiTryStatement)) {
+ parentStatement =
+ PsiTreeUtil.getParentOfType(statement,
+ PsiStatement.class);
+ }
+ if (parentStatement != null) {
+ final PsiTryStatement tryStatement =
+ (PsiTryStatement) parentStatement;
+ if (isResourceClosedInFinally(tryStatement, variable)) {
+ return true;
+ }
+ }
+ }
while (nextStatement == null) {
statement = PsiTreeUtil.getParentOfType(statement,
PsiStatement.class, true);
@@ -90,10 +108,10 @@ public abstract class ResourceInspection extends BaseInspection {
return isResourceClose(nextStatement, variable);
}
final PsiTryStatement tryStatement = (PsiTryStatement) nextStatement;
- return resourceIsClosedInFinally(tryStatement, variable);
+ return isResourceClosedInFinally(tryStatement, variable);
}
- protected static boolean resourceIsClosedInFinally(
+ protected static boolean isResourceClosedInFinally(
@NotNull PsiTryStatement tryStatement,
@NotNull PsiVariable variable) {
final PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
@@ -126,16 +144,16 @@ public abstract class ResourceInspection extends BaseInspection {
}
protected static boolean isResourceEscapedFromMethod(
- PsiVariable boundVariable, PsiElement context){
+ PsiVariable boundVariable, PsiElement context) {
// poor man dataflow
final PsiMethod method =
PsiTreeUtil.getParentOfType(context, PsiMethod.class, true,
PsiMember.class);
- if(method == null){
+ if (method == null) {
return false;
}
final PsiCodeBlock body = method.getBody();
- if(body == null){
+ if (body == null) {
return false;
}
final EscapeVisitor visitor = new EscapeVisitor(boundVariable);
@@ -274,40 +292,43 @@ public abstract class ResourceInspection extends BaseInspection {
}
}
- private static class EscapeVisitor extends JavaRecursiveElementVisitor{
+ private static class EscapeVisitor extends JavaRecursiveElementVisitor {
private final PsiVariable boundVariable;
private boolean escaped = false;
- public EscapeVisitor(PsiVariable boundVariable){
+ public EscapeVisitor(PsiVariable boundVariable) {
this.boundVariable = boundVariable;
}
- @Override public void visitAnonymousClass(PsiAnonymousClass aClass){}
+ @Override
+ public void visitAnonymousClass(PsiAnonymousClass aClass) {
+ }
@Override
- public void visitElement(PsiElement element){
- if(escaped){
+ public void visitElement(PsiElement element) {
+ if (escaped) {
return;
}
super.visitElement(element);
}
- @Override public void visitReturnStatement(
- PsiReturnStatement statement){
+ @Override
+ public void visitReturnStatement(
+ PsiReturnStatement statement) {
PsiExpression value = statement.getReturnValue();
value = PsiUtil.deparenthesizeExpression(value);
- if (value instanceof PsiReferenceExpression){
+ if (value instanceof PsiReferenceExpression) {
final PsiReferenceExpression referenceExpression =
(PsiReferenceExpression) value;
final PsiElement target = referenceExpression.resolve();
- if(target == boundVariable){
+ if (target != null && target.equals(boundVariable)) {
escaped = true;
}
}
}
- public boolean isEscaped(){
+ public boolean isEscaped() {
return escaped;
}
}
diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/resources/SocketResourceInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/resources/SocketResourceInspection.java
index 6c475f33b604..273825c1431c 100644
--- a/plugins/InspectionGadgets/src/com/siyeh/ig/resources/SocketResourceInspection.java
+++ b/plugins/InspectionGadgets/src/com/siyeh/ig/resources/SocketResourceInspection.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
+ * Copyright 2003-2010 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.
@@ -19,27 +19,34 @@ import com.intellij.psi.*;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TypeUtils;
+import com.siyeh.ig.ui.CheckBox;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
+import javax.swing.*;
+import java.awt.*;
+
public class SocketResourceInspection extends ResourceInspection {
- @Override
- @NotNull
- public String getID(){
- return "SocketOpenedButNotSafelyClosed";
- }
+ @SuppressWarnings({"PublicField"})
+ public boolean insideTryAllowed = false;
@Override
@NotNull
- public String getDisplayName(){
+ public String getID() {
+ return "SocketOpenedButNotSafelyClosed";
+ }
+
+ @Override
+ @NotNull
+ public String getDisplayName() {
return InspectionGadgetsBundle.message(
"socket.opened.not.closed.display.name");
}
@Override
@NotNull
- public String buildErrorString(Object... infos){
+ public String buildErrorString(Object... infos) {
final PsiExpression expression = (PsiExpression) infos[0];
final PsiType type = expression.getType();
assert type != null;
@@ -49,71 +56,95 @@ public class SocketResourceInspection extends ResourceInspection {
}
@Override
- public BaseInspectionVisitor buildVisitor(){
+ public JComponent createOptionsPanel() {
+ final JComponent panel = new JPanel(new GridBagLayout());
+ final CheckBox checkBox = new CheckBox(
+ InspectionGadgetsBundle.message(
+ "allow.resource.to.be.opened.inside.a.try.block"),
+ this, "insideTryAllowed");
+
+ final GridBagConstraints constraints = new GridBagConstraints();
+ constraints.anchor = GridBagConstraints.FIRST_LINE_START;
+ constraints.gridx = 0;
+ constraints.gridy = 0;
+ constraints.insets.left = 4;
+ constraints.insets.right = 4;
+ constraints.weightx = 1.0;
+ constraints.weighty = 1.0;
+ constraints.fill = GridBagConstraints.HORIZONTAL;
+ panel.add(checkBox, constraints);
+ return panel;
+ }
+
+
+ @Override
+ public BaseInspectionVisitor buildVisitor() {
return new SocketResourceVisitor();
}
- private static class SocketResourceVisitor extends BaseInspectionVisitor{
+ private class SocketResourceVisitor extends BaseInspectionVisitor {
- @Override public void visitMethodCallExpression(
- @NotNull PsiMethodCallExpression expression){
+ @Override
+ public void visitMethodCallExpression(
+ @NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
- if(!isSocketFactoryMethod(expression)){
+ if (!isSocketFactoryMethod(expression)) {
return;
}
final PsiElement parent = getExpressionParent(expression);
- if(parent instanceof PsiReturnStatement){
+ if (parent instanceof PsiReturnStatement) {
return;
}
final PsiVariable boundVariable = getVariable(parent);
- if(isSafelyClosed(boundVariable, expression)){
+ if (isSafelyClosed(boundVariable, expression, insideTryAllowed)) {
return;
}
- if(isResourceEscapedFromMethod(boundVariable, expression)){
+ if (isResourceEscapedFromMethod(boundVariable, expression)) {
return;
}
registerError(expression, expression);
}
- @Override public void visitNewExpression(
- @NotNull PsiNewExpression expression){
+ @Override
+ public void visitNewExpression(
+ @NotNull PsiNewExpression expression) {
super.visitNewExpression(expression);
- if(!isSocketResource(expression)){
+ if (!isSocketResource(expression)) {
return;
}
final PsiElement parent = getExpressionParent(expression);
- if(parent instanceof PsiReturnStatement){
+ if (parent instanceof PsiReturnStatement) {
return;
}
final PsiVariable boundVariable = getVariable(parent);
- if(isSafelyClosed(boundVariable, expression)){
+ if (isSafelyClosed(boundVariable, expression, insideTryAllowed)) {
return;
}
- if(isResourceEscapedFromMethod(boundVariable, expression)){
+ if (isResourceEscapedFromMethod(boundVariable, expression)) {
return;
}
registerError(expression, expression);
}
- private static boolean isSocketResource(PsiNewExpression expression){
+ private boolean isSocketResource(PsiNewExpression expression) {
return TypeUtils.expressionHasTypeOrSubtype(expression,
"java.net.Socket",
"java.net.DatagramSocket",
"java.net.ServerSocket") != null;
}
- private static boolean isSocketFactoryMethod(
- PsiMethodCallExpression expression){
+ private boolean isSocketFactoryMethod(
+ PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
@NonNls final String methodName =
methodExpression.getReferenceName();
- if(!"accept".equals(methodName)) {
+ if (!"accept".equals(methodName)) {
return false;
}
final PsiExpression qualifier =
methodExpression.getQualifierExpression();
- if(qualifier == null) {
+ if (qualifier == null) {
return false;
}
return TypeUtils.expressionHasTypeOrSubtype(qualifier,
diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ChannelResource.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ChannelResource.html
index 678bc60f37c2..a48dcbaa166f 100644
--- a/plugins/InspectionGadgets/src/inspectionDescriptions/ChannelResource.html
+++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ChannelResource.html
@@ -7,5 +7,9 @@ front of a try block and closed in the corre
if an exception is thrown before the resource is closed. Channel resources reported
by this inspection include any instances created by calling
getChannel() on a file or socket resource.
+
+Use the checkbox below to specify if a Channel is allowed to be opened inside a try
+block. This style is less desirable because it is more verbose than opening a Channel
+in front of a try block.
| Powered by InspectionGadgets |
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/HibernateResource.html b/plugins/InspectionGadgets/src/inspectionDescriptions/HibernateResource.html
index 216f4dd7f4ec..3178ada063ff 100644
--- a/plugins/InspectionGadgets/src/inspectionDescriptions/HibernateResource.html
+++ b/plugins/InspectionGadgets/src/inspectionDescriptions/HibernateResource.html
@@ -6,5 +6,9 @@ This inspection reports any Hibernate resource which is not opened in a finally block. Such resources may
be inadvertently leaked if an exception is thrown before the resource is closed. Hibernate resources reported
by this inspection include any instances of org.hibernate.Session.
+
+Use the checkbox below to specify if a Hibernate resource is allowed to be opened inside a try
+block. This style is less desirable because it is more verbose than opening a resource
+in front of a try block.
| Powered by InspectionGadgets |