/*
 * 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ig.resources;

import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class ChannelResourceInspection extends ResourceInspection {

    @SuppressWarnings({"PublicField"})
    public boolean insideTryAllowed = false;

    @Override
    @NotNull
    public String getID() {
        return "ChannelOpenedButNotSafelyClosed";
    }

    @Override
    @NotNull
    public String getDisplayName() {
        return InspectionGadgetsBundle.message(
                "channel.opened.not.closed.display.name");
    }

    @Override
    @NotNull
    public String buildErrorString(Object... infos) {
        final PsiExpression expression = (PsiExpression) infos[0];
        final PsiType type = expression.getType();
        assert type != null;
        final String text = type.getPresentableText();
        return InspectionGadgetsBundle.message(
                "channel.opened.not.closed.problem.descriptor", text);
    }

    @Override
    public JComponent createOptionsPanel() {
        return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message(
                "allow.resource.to.be.opened.inside.a.try.block"),
                this, "insideTryAllowed");
    }

    @Override
    public BaseInspectionVisitor buildVisitor() {
        return new ChannelResourceVisitor();
    }

    private class ChannelResourceVisitor extends BaseInspectionVisitor {

        @Override
        public void visitMethodCallExpression(
                @NotNull PsiMethodCallExpression expression) {
            super.visitMethodCallExpression(expression);
            if (!isChannelFactoryMethod(expression)) {
                return;
            }
            final PsiElement parent = getExpressionParent(expression);
            if (parent instanceof PsiReturnStatement) {
                return;
            }
            final PsiVariable boundVariable = getVariable(parent);
            if (isSafelyClosed(boundVariable, expression, insideTryAllowed)) {
                return;
            }
            if (isChannelFactoryClosedInFinally(expression)) {
                return;
            }
            if (isResourceEscapedFromMethod(boundVariable, expression)) {
                return;
            }
            registerError(expression, expression);
        }

        private boolean isChannelFactoryClosedInFinally(
                PsiMethodCallExpression expression) {
            final PsiReferenceExpression methodExpression =
                    expression.getMethodExpression();
            final PsiExpression qualifier =
                    methodExpression.getQualifierExpression();
            if (!(qualifier instanceof PsiReferenceExpression)) {
                return false;
            }
            final PsiReferenceExpression referenceExpression =
                    (PsiReferenceExpression) qualifier;
            final PsiElement target = referenceExpression.resolve();
            if (!(target instanceof PsiVariable)) {
                return false;
            }
            final PsiVariable variable = (PsiVariable) target;
            PsiTryStatement tryStatement =
                    PsiTreeUtil.getParentOfType(expression,
                            PsiTryStatement.class, true, PsiMember.class);
            if (tryStatement == null) {
                return false;
            }
            while (!isResourceClosedInFinally(tryStatement, variable)) {
                tryStatement =
                        PsiTreeUtil.getParentOfType(tryStatement,
                                PsiTryStatement.class, true, PsiMember.class);
                if (tryStatement == null) {
                    return false;
                }
            }
            return true;
        }

        private boolean isChannelFactoryMethod(
                PsiMethodCallExpression expression) {
            final PsiReferenceExpression methodExpression =
                    expression.getMethodExpression();
            final String methodName = methodExpression.getReferenceName();
            if (!HardcodedMethodConstants.GET_CHANNEL.equals(methodName)) {
                return false;
            }
            final PsiExpression qualifier =
                    methodExpression.getQualifierExpression();
            if (qualifier == null) {
                return false;
            }
            return TypeUtils.expressionHasTypeOrSubtype(qualifier,
                    "java.net.Socket",
                    "java.net.DatagramSocket",
                    "java.net.ServerSocket",
                    "java.io.FileInputStream",
                    "java.io.FileOutputStream",
                    "java.io.RandomAccessFile",
                    "com.sun.corba.se.pept.transport.EventHandler",
                    "sun.nio.ch.InheritedChannel") != null;
        }
    }
}
@@ -2709,16 +2709,77 @@
 tatement
+ %7C%7C%0A                    parent instanceof PsiResourceVariable
 ) %7B%0A    
@@ -5802,8 +5802,9 @@
 %0A    %7D%0A%7D
+%0A

fix "Socket opened but not safely closed" inspection in the presence of try-with-resources