annoscan

A reflection based annotation scanner.

License

License

GroupId

GroupId

com.siirush
ArtifactId

ArtifactId

annoscan
Last Version

Last Version

0.1.5
Release Date

Release Date

Type

Type

jar
Description

Description

annoscan
A reflection based annotation scanner.
Project URL

Project URL

http://github.com/mrdanpsmith/annoscan
Source Code Management

Source Code Management

https://github.com/mrdanpsmith/annoscan

Download annoscan

How to add to project

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

Dependencies

compile (1)

Group / Artifact Type Version
javax.inject : javax.inject jar 1

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

Annoscan

Reflective annotation scanner for Java.

Why another annotation scanner?

After scouring the web a few times, I could not find an easy way to do a simple task: take a class and scan for annotated properties.

As part of my development efforts on another project, I wrote such an annotation scanner.

I often found myself needing to do this same task; so, I decided to publish the code to make it accessible to others.

Installation

Annoscan is in maven central. Simply add the following dependency:

<dependency>
    <groupId>com.siirush</groupId>
    <artifactId>annoscan</artifactId>
    <version>0.1.5</version>
</dependency>

Configuration

AnnoScan can be configured using a variety of methods.

It relies on constructor injection and is annotated in compliance with JSR-330 containers (Guice, Spring 3, etc).

In addition, it can be handwired manually or obtained using the provider singleton.

Handwired:

AnnoScan annoScan = new AnnoScanImpl(new GetterLocatorImpl());

Provider:

AnnoScan annoScan = AnnoScanProvider.instance.get();

Usage

To scan a class for annotated properties, simply call one of the methods with your class and annotation class.

In unspecified order:

properties = annoScan.getAnnotatedProperties(TestClass.class,TestAnnotation.class);

In sorted order:

sortedProperties = annoScan.getAnnotatedPropertiesSorted(TestClassWithSort.class, TestAnnotation.class, new TestAnnotationComparator());

Then iterate through all of the properties and get the annotation or the value of the field:

for (AnnotatedProperty<TestAnnotation> property: properties) {
	TestAnnotation annotation = property.getAnnotation();
	Object fieldValue = property.getPropertyAccessor().getValue(annotatedObject);
}

Additional Notes

The default AnnoScan and GetterLocator implementations are stateless and can be instantiated prototypically with minimal overhead, or as singletons.

The default provider instance get method call demonstrated above returns a singleton.

Versions

Version
0.1.5
0.1.4