Method Counter

Simple counter of method calls through AOP

Лицензия

Лицензия

Группа

Группа

io.github.isharipov
Идентификатор

Идентификатор

method-counter
Последняя версия

Последняя версия

0.0.7
Дата

Дата

Тип

Тип

pom
Описание

Описание

Method Counter
Simple counter of method calls through AOP
Ссылка на сайт

Ссылка на сайт

https://github.com/iSharipov/method-counter
Система контроля версий

Система контроля версий

https://github.com/iSharipov/method-counter

Скачать method-counter

Имя Файла Размер
method-counter-0.0.7.pom 4 KB
Обзор

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/io.github.isharipov/method-counter/ -->
<dependency>
    <groupId>io.github.isharipov</groupId>
    <artifactId>method-counter</artifactId>
    <version>0.0.7</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/io.github.isharipov/method-counter/
implementation 'io.github.isharipov:method-counter:0.0.7'
// https://jarcasting.com/artifacts/io.github.isharipov/method-counter/
implementation ("io.github.isharipov:method-counter:0.0.7")
'io.github.isharipov:method-counter:pom:0.0.7'
<dependency org="io.github.isharipov" name="method-counter" rev="0.0.7">
  <artifact name="method-counter" type="pom" />
</dependency>
@Grapes(
@Grab(group='io.github.isharipov', module='method-counter', version='0.0.7')
)
libraryDependencies += "io.github.isharipov" % "method-counter" % "0.0.7"
[io.github.isharipov/method-counter "0.0.7"]

Зависимости

compile (2)

Идентификатор библиотеки Тип Версия
org.slf4j : slf4j-api jar 1.8.0-beta2
ch.qos.logback : logback-classic jar 1.3.0-alpha4

Модули Проекта

  • method-counter-core
  • method-counter-spring-boot-autoconfigure
  • method-counter-spring-boot-starter

Method counter

Simple counter of method calls through AOP.

Counter works based on CTW (compile-time weaving) principle.

You can use the library for both cases:

  • Simple libraries (Testing purposes)

  • With Spring Framework

Getting Started

Dependencies

Gradle

You can see a detailed example at the Gradle example project.

To add library to your project without Spring, add core dependency

build.gradle
compile group: 'io.github.isharipov', name: 'method-counter-core', version: '0.0.3

If you doesnt have aspect support in your project you have to setting up an gradle aspectj plugin and add core library as aspectpath

build.gradle
buildscript {
    dependencies {
        classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
    }
}

ext{
    aspectjVersion = '1.9.2'
}

dependencies {
    implementation "io.github.isharipov:method-counter-core:0.0.3"
    aspectpath "io.github.isharipov:method-counter-core:0.0.3"
}

Maven

You can see a detailed example at the Maven example project.

pom.xml
<dependency>
    <groupId>io.github.isharipov</groupId>
    <artifactId>method-counter-core</artifactId>
    <version>0.0.3</version>
</dependency>

If you doesnt have aspect support in your project you have to setting up an aspectj maven plugin and add core library as aspectLibrary

pom.xml
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.11</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.2</version>
        </dependency>
    </dependencies>
    <configuration>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
        <showWeaveInfo>true</showWeaveInfo>
        <verbose>true</verbose>
        <Xlint>ignore</Xlint>
        <encoding>UTF-8</encoding>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>io.github.isharipov</groupId>
                <artifactId>method-counter-core</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
            </configuration>
        <execution>
    </executions>
</plugin>

Project Lombok Support

Gradle

If you have Project Lombok in your current Gradle project you have to use ant directly

You can see a detailed example at the Gradle Lombok example project.

build.gradle
configurations {
    ajc
    aspects
    compile {
        extendsFrom aspects
    }
}

dependencies {
    ajc "org.aspectj:aspectjtools:1.9.2"

    implementation "io.github.isharipov:method-counter-core:0.0.6"
    aspects "io.github.isharipov:method-counter-core:0.0.6"

    compileOnly "org.projectlombok:lombok:1.18.4"
    annotationProcessor "org.projectlombok:lombok:1.18.4"

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

def aspectj = { destDir, aspectPath, inpath, classpath ->
    ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
            classpath: configurations.ajc.asPath)

    ant.iajc(
            maxmem: "1024m", fork: "true", Xlint: "ignore",
            destDir: destDir,
            aspectPath: aspectPath,
            inpath: inpath,
            classpath: classpath,
            source: project.sourceCompatibility,
            target: project.targetCompatibility
    )
}

compileJava {
    doLast {
        aspectj project.sourceSets.main.output.classesDir.absolutePath,
                configurations.aspects.asPath,
                project.sourceSets.main.output.classesDir.absolutePath,
                project.sourceSets.main.runtimeClasspath.asPath
    }
}

Additional explanation you can find in the article.

Maven

You can see a detailed example at the Maven Lombok example project.

pom.xml
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>io.github.isharipov.method.counter.maven.lombok.example.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <complianceLevel>1.8</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>io.github.isharipov</groupId>
                            <artifactId>method-counter-core</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <forceAjcCompile>true</forceAjcCompile>
                    <sources/>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                    </weaveDirectories>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <id>aspectj-compile</id>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

Spring Boot Support

To use method counter with Spring Boot, there is Method counter Spring Boot Starter project

To use it in your Spring Boot Project just add dependency

Maven

pom.xml
<dependency>
    <groupId>io.github.isharipov</groupId>
    <artifactId>method-counter-spring-boot-starter</artifactId>
    <version>0.0.3</version>
</dependency>

And setting up aspectj maven plugin .pom.xml

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.11</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.2</version>
        </dependency>
    </dependencies>
    <configuration>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
        <showWeaveInfo>true</showWeaveInfo>
        <verbose>true</verbose>
        <Xlint>ignore</Xlint>
        <encoding>UTF-8</encoding>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>io.github.isharipov</groupId>
                <artifactId>method-counter-core</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
            </configuration>
        </execution>
    </executions>
</plugin>

Basic Functionality

The Counter is an annotation @Counter

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Inherited
public @interface Counter {

    String name() default "";

    Class<? extends CounterType> type() default DefaultCounterType.class;

    Class<? extends Behaviour> behaviour() default Success.class;

    boolean timer() default false;
}

By default it has a set of parameters:

  • name - ""

  • type - DefaultCounterType.class

  • behaviour - Success.class

  • timer - false

Out of the box Counter has three behaviours:

  • Success - count only if successful method execution

  • Always - count anyway

  • Failure - count only if method ended with an exception

Версии библиотеки

Версия
0.0.7
0.0.6
0.0.3
0.0.2