• Stars
    star
    506
  • Rank 87,236 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run queries on your Java code to check if it meets the criteria suggested by the book Effective Java. And some others.

effectivejava

Build Status

Effective java is a tool to examine your Java codebase. You can use it in three different ways:

  • as a Java linter. Just tells it which directory you want to examine, it will spit out a set of warnings and suggestions to improve your code
  • run queries from the command line. It could tell you which type of Singleton you are using in your codebase or how many constructors have 10 or more parameters. This modality can be easily integrated with other tools
  • run queries interactively. It permits to poke your codebase, parsing it once and running different queries to find out interesting facts about it.

The project is named effectivejava because many queries/checks derive from reading the book Effective Java. Others will be implemented as well (feel free to suggest your favorite ones!).

While reading that book I thought that yes, many principles are well known, but they are rarely applied to a large codebase. I thought that applying them in practice is much harder than it seems, and a tool like this one could help in improving constantly a codebase.

Which is the easiest way to install it?

Download the standalone jar from the releases page. No deps needed, everything is packed inside the jar. Feel free to rename it (effectivejava-0.1.0-SNAPSHOT-standalone.jar is a mouthful...)

Linting mode: how to use it

Just run:

# this generate a jar file
lein jar
# note that 0.1.3 is the current version it could change in the future
java -jar effectivejava-0.1.3-SNAPSHOT-standalone.jar -l -d "<myJavaProjectDir>"

You can expect a set of lines like this one:

org.springframework.jdbc.core.SqlInOutParameter : This class has too many constructors (7). Consider using static factory methods or the Builder pattern

If you run this command from the root of your codebase you can avoid the -d option.

CLI mode: how to use it

Now, suppose you want to know which classes has 5 or more constructor; you can run this command:

java -jar effectivejava-0.1.3-SNAPSHOT-standalone.jar -q mc -d "<myJavaProjectDir>" -t 5

You can expect a similar output:

Considering 109 Java files
japa.parser.ast.expr.ArrayCreationExpr  :  5
japa.parser.ast.body.MethodDeclaration  :  5
japa.parser.ast.body.BaseParameter  :  5
japa.parser.ast.body.FieldDeclaration  :  5

Interactive mode: how to use it (Work in progress!)

You can launch interactive mode with the -i option.

java -jar effectivejava-0.1.3-SNAPSHOT-standalone.jar -i

A typical interaction could be this one:

> load "."
Loading .
Java files loaded: 440
> mc th 5
Command not implemented:  :MC

As you can read from the last line, while the main logic for the interactive mode is there we still miss a few bits :) It will be corrected soon.

What queries can you run

I am just getting started so I implemented only a few queries for now:

  • mc=many constructors: find the classes which contain the same number of constructors as the given threshold, or more
  • mcp=many constructor parameters: find the constructors which contain the same number of parameters as the given threshold, or more
  • st=singleton type: find if a type implements the singleton pattern and distinguish between the three types (public field, static factory, singleton enum)
  • u=utils classes: find classes having only static methods and verify they have exactly one private constructor taking no parameters

Effective Java (the book) items implemented

Item Status
Item 1 Done
item 2 TODO
item 3 Done
item 4 Done
item 5 TODO
item 6 TODO
item 7 Done
item 8 Planned for v0.2
item 9 Planned for v0.2
item 10 Done
item 11 TODO
...item 78 TODO

Dev info

The project is written in Clojure using a java library called JavaParser.

You will need also Leiningen, the build tool for Clojure. It should download also Clojure for you.

Dev guidelines

To monitor code quality we use kibit and eastwood.

When running eastwood exclude the check for unlimited use of namespaces:

lein eastwood "{:exclude-linters [:unlimited-use]}"

To verify the code is correctly formatted cljfmt is used. You can run it like this:

# to verify possible style issues
lein cljfmt check
# to automatically fix them
lein cljfmt fix

We use lein-ancient to verify our dependencies are up-to-date.

What is the link with the book?

I am reading this book and many advices seem sort of obvious in theory but I guess there are some violations lurking in the large codebase I am working with. I was curious to assess how many violations there were and I needed a way to find them out automatically. And I wanted to learn Clojure. And I had a free sunday. So...

What else

Hope you enjoy this small project of mine. Feel free to open issues and ask questions!

Contributors

David Ortiz is a regular contributor: he started fixing bugs, setting up Travis and it is contributing many other improvements.

More Repositories

1

LangSandbox

Project to illustrate how to build a programming language
Kotlin
694
star
2

kanvas

A truly hackable editor: simple, lightweight, understandable
Kotlin
144
star
3

civs

Civ simulator
Clojure
141
star
4

analyze-java-code-examples

Some examples of code extracting information from Java source files using JavaParser
Java
99
star
5

turin-programming-language

A static language for the JVM with sensible defaults
Java
96
star
6

kllvm

Kotlin library to work with LLVM
Kotlin
54
star
7

kotlin-wasm-examples

JavaScript
46
star
8

DriveInvoicing

Use Google Drive and the Google Execution API for generating PDF invoices
Python
46
star
9

python-ast

Python parser built using ANTLR
ANTLR
41
star
10

erd-web-server

A web server to generate ER diagrams
JavaScript
35
star
11

langgen

Language generator
Python
28
star
12

antlr-web-example

JavaScript
28
star
13

wasmcompilerkit

Support for manipulation WebAssembly binary files
Kotlin
21
star
14

javadoc-extractor

Java
20
star
15

antlr-kotlin

Support for Kotlin as a target for ANTLR
Kotlin
15
star
16

JavaCC2ANTLR

Java
15
star
17

simple-web-editor

JavaScript
14
star
18

antlr-plus

A complement to ANTLR to get a model from your AST and transform it
ANTLR
14
star
19

antlr4-c3-kotlin

Code completion for ANTLR
Kotlin
11
star
20

civs-browser

A web application to visualize the history files produced by csv
Clojure
10
star
21

ofx-java

Parsing OFX data
Java
9
star
22

redmine-reactive

JavaScript
8
star
23

EmailSchedulingRules

Kotlin
8
star
24

spoon-examples

Java
7
star
25

SketchModel

Recognize sketches and translate them into models
Java
7
star
26

ANTLR_graph

Generate graphs for ANTLR grammars
Kotlin
6
star
27

JavaIncrementalParser

Let's try to build a Java incremental parser
Java
6
star
28

NetModelingFramework

A framework to support meta-modeling and modeling under .NET (à la EMF).
C#
6
star
29

codemirror_plantuml

CodeMirror mode for PlantUML
JavaScript
5
star
30

java-symbol-solver-examples

Java
5
star
31

turin-maven-plugin

A Maven plugin to compile Turin files
Java
4
star
32

procedurality-lands

An engine to generate worlds
Java
4
star
33

antlr-kotlin-runtime

Kotlin runtime for ANTLR
Kotlin
4
star
34

mps-antlr

ANTLR
4
star
35

civs-haskell

Writing a civs simulator in haskell
Haskell
3
star
36

java-formatter

A tool to format Java Code
Shell
3
star
37

MpsByteCode

3
star
38

turin-intellij-plugin

Java
3
star
39

semreview

Text classification taking advantage of the Semantic Web
Java
3
star
40

plantuml-antlr4

Parser for PlantUML
Java
3
star
41

jlibav

Automatically exported from code.google.com/p/jlibav
Java
2
star
42

smlang-example

Kotlin
2
star
43

MpsAccounting

Java
2
star
44

hplatec

Wrapper for platec in Haskell
Haskell
2
star
45

JavaModel

A set of utilities to parse, refactor and generate Java code
Java
2
star
46

namegen-haskell

A name generator written in Haskell
Haskell
2
star
47

codemodels

Ruby
1
star
48

world-explorer

Experiments with OpenGL in Haskell
Haskell
1
star
49

namegen-data

List of names usable as input for name generators
Haskell
1
star
50

parsingjava

Kotlin
1
star
51

worldengine-viewer

OpenGL navigator of a worldengine viewer
GLSL
1
star
52

procedural-content-generation-examples

Python
1
star
53

javamatchers

Java
1
star
54

java-advent-with-spark-and-javaslang

Java
1
star
55

java-cst-model-extractor

Kotlin
1
star
56

pydiffparser

Python lib to parse diff files
Python
1
star
57

hplatetectonics

Plate tectonics simulation in Haskell
Haskell
1
star
58

codemodels-ruby

An EMF metamodel for ruby and a parser to build EMF models of ruby code
Ruby
1
star
59

haskell-diamond-square

Haskell implementation of the Diamond-Square algorithm for heightmap generation
Haskell
1
star
60

rgen_ext

Extensions of general use to RGen
Ruby
1
star
61

QuantDSL

Kotlin
1
star
62

antlr-kotlin-target

Kotlin
1
star