Coder Social home page Coder Social logo

sonar-java-academic-plugin's Introduction

sonar-java-academic-plugin

Build Status codecov

Sonar plugin that can detect academic code smells in Java applications

Currently only Java language is supported.

Supported code smells

Current list of supported code smells:

Code smell Description
Data class Description can be found here
Message chain Description can be found here. This rule has configurable method chain length. This can be configured with property sonar.academic.plugin.message.chain.length
Long method Detects methods that are longer than X lines (supports if statements, do while loops, for loops, synchronized blocks, try blocks (catch + finally), while loops). Configuratin property to configure method length is sonar.academic.plugin.long.method.length, with default value 8
Switch statement Detects usage of switch statements
Shotgun surgery Detect methods that are overused by other classes
Lazy class Detect classes with no methods, low complexity methods or with high coupling with other classes
Long parameter list Detect methods with more than X parameters
Blob class
Refused Bequest
Comments
Cyclic dependencies
Tradition breaker
Divergent change
Feature envy
Data clumps
Parallel inheritance hierarchies
Speculative generality (interfaces)
Speculative generality (methods)
Middle man
Primitive obsession
Brain method
Inappropriate intimacy
Alternate classes with different interfaces
God class
Intensive coupling
Swiss Army Knife
Missing template method
Unstable dependencies
SAPBreakers

How does it work?

All of the examples can be found in the test resources directory of the project.

Data class

By definition, data class is something that only contains data and no other business logic methods.

So, in Java we can define data class as follows:

  • A class that has only getters and/or setters
  • A class that only has public variables

This narrows down the definition to the following: if class has no custom methods with any other logic that getters/setters, it is a data class code smell.

Plugin analyses a class and nested classes, and looks for getters/setters and then substracts the number of getters + setters from the total method count. If this number is zero, issue is reported to the context of the class.

Message chains

By definition message chain is a delegation of method calls.

This can be described as a pattern a -> b -> c -> d etc.

Plugin analyses method invocations and measures the depth of the calls.

Long method

Investigate how many lines does the method have.

Since code follows tree-like structure, we traverse recursively into the tree and count the number of expressions in the tree recursively.

If number of expressions is larger than X we report an issue.

Switch statement

Find all usages of switch statements.

Report all usages of switch statements as an issue.

Shotgun surgery

Detects methods of a class that are overused by external classes.

Lazy class

Detects classes that have either:

  • 0 methods
  • high number of low complexity methods
  • tight coupling with external classes and deep inheritance tree

Long parameter list

Detects all methods with more than X parameters

Blob class

Refused bequest

Comments

Cyclic dependencies

Tradition breaker

Divergent change

Feature envy

Data clumps

Parallel inheritance hierarchies

Speculative generality (interfaces)

Speculative generality (methods)

Middle man

Primitive obsession

Brain method

Inappropriate intimacy

God class

Alternate classes with different interfaces

Intensive coupling

Swiss Army Knife

Missing template method

Unstable dependencies

SAPBreakers

sonar-java-academic-plugin's People

Contributors

zukkari avatar

Stargazers

James Pether Sörling avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

sonar-java-academic-plugin's Issues

Add Sensor

Add sensor class so that the check can be run on sonar lint side

Issues when running Sonar analysis

All issues found during run with the tool should go here.

  • Fix annotations
  • Add default constructor
Caused by: java.lang.IllegalStateException: Fail to instantiate class class io.github.zukkari.checks.LongMethodRule for rule sonar-academic-repository:LongMethodRule
        at org.sonar.api.batch.rule.Checks.failToInstantiateCheck(Checks.java:170)
        at org.sonar.api.batch.rule.Checks.instantiate(Checks.java:165)
        at org.sonar.api.batch.rule.Checks.addAnnotatedChecks(Checks.java:136)
        at org.sonar.java.SonarComponents.registerCheckClasses(SonarComponents.java:217)
        at org.sonar.java.SonarComponents.<init>(SonarComponents.java:131)
        at org.picocontainer.injectors.AbstractInjector.newInstance(AbstractInjector.java:145)
        at org.picocontainer.injectors.ConstructorInjector$1.run(ConstructorInjector.java:342)
        at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
        at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
        at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
        at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
        at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
        at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
        at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
        at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:63)
        ... 84 more
Caused by: java.lang.InstantiationException: io.github.zukkari.checks.LongMethodRule
        at org.sonar.api.batch.rule.Checks.instantiate(Checks.java:160)
        ... 97 more
Caused by: java.lang.NoSuchMethodException: io.github.zukkari.checks.LongMethodRule.<init>()
        ... 98 more

Figure out what to do with declaration limitation

From Sonar javadoc:

/**
   * Declaration node of this symbol. Currently, only works for declaration within the same file.
   * @return the Tree of the declaration of this symbol. Null if declaration does not occur in the currently analyzed file.
   */
  @Nullable
  Tree declaration();

So as we can see, limitation is that declaration is accessible only for symbols in the same class.
Should figure out how to work around this limitation or at least find out where there is a plan to implement it.

Figure out library differences

It looks like there are some library version differences when running in the server.

This causes for example Message chain rule to act in a weird way since it depends on classOf checks.

Add docker support

Add a Dockerfile so it would be easy to test the plugin on a new system

Plugin should rely on fully qualified class names

Currently the plugin uses simple names of classes which is an issue if project has classes with non-unique names. To solve this issue, we should use fully qualified class names where comparing class names.

Class cast exceptions due to different classloaders

During plugin testing found an issue that during sensor analysis we run into class cast exceptions, due to the fact that we have to use some internal API and bring the whole plugin library with us.

To fix this, we can try to create dynamic proxies that would delegate to Sonar classes via reflection.

Move logging into SyncIO

Now that cats-effects is in use, we can create custom logger factory and wrap logging calls into SyncIO for purity.

Implement nice descriptions

Currently we are using dummy placeholders.

Implement nice descriptions that describe rules and compliant/non compliant solutions.

Issues found during analysis

Description of issues found during test analysis and fixes:

  • Lazy class: NPE @ 87
  • Intensive coupling: added case for SwitchExpressionTree
  • Refused bequest: NPE @ 98
  • Shotgun surgery: NPE @ 29
  • Middleman: division by 0 @ 48

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.