Giter Site home page Giter Site logo

warn-closeable's Introduction

                                    __                         __    __
.--.--.--.---.-.----.-----.___.----|  |-----.-----.-----.---.-|  |--|  |-----.
|  |  |  |  _  |   _|     |___|  __|  |  _  |__ --|  -__|  _  |  _  |  |  -__|
|________|___._|__| |__|__|   |____|__|_____|_____|_____|___._|_____|__|_____|

A simple linter for Clojure namespaces that detects possibly unclosed instances of (Auto)Closeable objects. A similar feature exists for the Eclipse IDE.

This project will optimally be included as an Eastwood linter.

I am currently waiting for clarification regarding type information from tools.analyzer.jvm, but the core functionality is mostly complete.

Usage

Available as a Leiningen plugin or as a library for use from a REPL.

  • Leiningen plugin:
;; ~/.lein/profiles.clj
;; {:user {:plugins [[lein-warn-closeable "0.1.0"]]}}
"NOT AVAILABLE; INSTALL FROM SOURCE"

Run lein warn-closeable from your project root.

  • REPL usage
;; ~/.lein/profiles.clj
;; {:user {:dependencies [[com.sungpae/warn-closeable "0.1.0"]]}}
"NOT AVAILABLE; INSTALL FROM SOURCE"

Then:

(require '[com.sungpae.warn-closeable :as wc])

(wc/closeable-warnings *ns*) ; Lint current namespace
(wc/warn-closeable!)         ; Lint all namespaces, same as `lein warn-closeable`

Rationale

The Closeable interface (and the AutoCloseable interface in JRE 1.7+) represents a resource that must be manually closed, like open files and network sockets.

While the .close method of (Auto)Closeable objects are often called in the object finalizer, there is no guarantee that objects will ever be finalized. Therefore, resources must be explicitly closed to avoid resource leaks, which in turn may lead to undesirable exceptions like:

java.io.FileNotFoundException: (Too many open files)

A common convention in JVM languages is to close an (Auto)Closeable object in a finally block after opening the resource:

(let [s (java.net.Socket.)]
  (try
    …
    (finally
      (.close s))))

This is usually written in Clojure with the with-open macro, which expands to the same code as above:

(with-open [s (java.net.Socket.)]
  …)

warn-closeable and lein-warn-closeable, leveraging the power of tools.analyzer, can detect when an (Auto)Closeable object has been created without an associated .close call in a finally block.

MORE DOCUMENTATION HERE

warn-closeable's People

Contributors

guns avatar

Stargazers

Michael Hanson avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.