anthavio-aspects

Anthavio AspectJ aspect library

License

License

Categories

Categories

Ant Build Tools Net
GroupId

GroupId

net.anthavio
ArtifactId

ArtifactId

anthavio-aspects
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

anthavio-aspects
Anthavio AspectJ aspect library
Source Code Management

Source Code Management

https://github.com/anthavio/anthavio-aspects

Download anthavio-aspects

How to add to project

<!-- https://jarcasting.com/artifacts/net.anthavio/anthavio-aspects/ -->
<dependency>
    <groupId>net.anthavio</groupId>
    <artifactId>anthavio-aspects</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/net.anthavio/anthavio-aspects/
implementation 'net.anthavio:anthavio-aspects:1.0.0'
// https://jarcasting.com/artifacts/net.anthavio/anthavio-aspects/
implementation ("net.anthavio:anthavio-aspects:1.0.0")
'net.anthavio:anthavio-aspects:jar:1.0.0'
<dependency org="net.anthavio" name="anthavio-aspects" rev="1.0.0">
  <artifact name="anthavio-aspects" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.anthavio', module='anthavio-aspects', version='1.0.0')
)
libraryDependencies += "net.anthavio" % "anthavio-aspects" % "1.0.0"
[net.anthavio/anthavio-aspects "1.0.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.aspectj : aspectjrt jar 1.7.4
org.slf4j : slf4j-api jar 1.7.5

test (3)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.0.13
org.testng : testng jar 6.8.7
org.easytesting : fest-assert-core jar 2.0M10

Project Modules

There are no modules declared in this project.

AspectJ collection

All aspects are used in following Example

public class Example {

	private String important;

	public Example(@NullCheck String important) {
		this.important = important;
	}

	@Logged
	public String myMethod(String param, int number) {
		System.out.println("Nasty forgotten sysout: " + number);
		return important + " " + param;
	}

	public static void main(String[] args) {
		new Example("something").myMethod("works", 5);
		new Example(null); //IllegalArgumentException
	}

}

Execution will output

18:35:05.115 [main] TRACE example.Example - >>myMethod(works,5)>>
18:35:05.116 [main] WARN  net.anthavio.aspect.ApiPolicyAspect - PrintStream java.lang.System.out acces at Example.java:24
Nasty forgotten sysout: 5
18:35:05.116 [main] TRACE example.Example - <<myMethod: something works<< 1ms
Exception in thread "main" java.lang.IllegalArgumentException: Null String argument on position 1 of constructor example.Example(String)
	at net.anthavio.aspect.NullCheckAspect.checkConstructorArguments(NullCheckAspect.java:135)
	at example.Example.<init>(Example.java:18)
	at example.Example.main(Example.java:30)

You can see entries for

  • (LogAspect) step into myMethod with parameter value
  • (ApiPolicyAspect) warning about nasty System.out.println usage
  • (LogAspect) exit from myMethod with return value and execution time
  • (NullCheckAspect) IllegalArgumentException because of null argument passed to Example class constructor

LogAspect

Yes! Yet another logging aspect, but this is quite advanced one. On debug log level prints parameter value, but on info level prints only parameter type. Works nicely with Hibernate lazy loaded fields. @Logged markin annotation can be placed on method, constructor or even class to log all nonprivate methods.

NullCheckAspect

Checks @NullCheck annotated parameters and throws IllegalArgumentException when null value is found. Saves you lots of lines of code!

ApiPolicyAspect

Helps you to locate bad APIs usage. Find occurences of System.out, Throwable.printStackTrace, Runtime.exit,... calls and prints warning with class and code line.

How to incorporate in you project

Add dependency

	<dependency>
		<groupId>net.anthavio</groupId>
		<artifactId>anthavio-aspects</artifactId>
		<version>1.0.0</version>
	</dependency>

Configure aspectj-maven-plugin to compile with anthavio-aspects library

  <build>
    <plugins>
  		<plugin>
  			<groupId>org.codehaus.mojo</groupId>
  			<artifactId>aspectj-maven-plugin</artifactId>
  			<executions>
  				<execution>
  					<goals>
  						<goal>compile</goal>
  					</goals>
  				</execution>
  			</executions>
  			<configuration>
  				<aspectLibraries>
  					<aspectLibrary>
  						<groupId>net.anthavio</groupId>
  						<artifactId>anthavio-aspects</artifactId>
  					</aspectLibrary>
  				</aspectLibraries>
  			</configuration>
  		</plugin>
		</plugins>
  </build>

Versions

Version
1.0.0