rmuller / infomas-asl

Open sourced code of the INFOMAS PIM Application Suite
Apache License 2.0
121 stars 37 forks source link
annotation-detector bytecode-parser java

infomas-asl

Build Status Reference Status Maven Central

INFOMAS ASL contains all open sourced code from the INFOMAS PIM Application Suite. All code is licensed by the Apache License, Version 2.0, so it can be used by both open source and commercial projects.

The INFOMAS PIM Application Suite is a commercial Product Information Management Application. For more information, visit http://www.xiam.nl.

Warning: API Change!!

Starting version 3.1.0-SNAPSHOT the API has been changed considerably and several new features have been added. Do not worry, we did not sacrifice the unique selling points of this library: simple API, small footprint and great performance. Performance is even improved by about 10%! The library size increased from 16 kB to 20 kB. This version is not (yet) released to Maven Central, so if you disagree with some changes or have better ideas, please let us know!

See (git) branche "release 3.1".

Changes (3.1.0-SNAPSHOT):

Quick Facts

Modules

Currently INFOMAS ASL contains the following modules:

annotation-detector

This library can be used to scan (part of) the class path for annotated classes, methods or instance variables. Main advantages of this library compared with similar solutions are: light weight (no dependencies, simple API, 20 kb jar file) and very fast (fastest annotation detection library as far as I know).

Maven configuration:

<dependency>
   <groupId>eu.infomas</groupId>
   <artifactId>annotation-detector</artifactId>
   <version>3.0.6</version>
</dependency>

Example Usage (3.0.x versions):

Put the annotation-detector-3.0.x.jar on the class path. No other dependencies are required! You can either scan the complete class path or only scan specified packages or Files (see JavaDoc for more details).

// Scan all .class files on the class path
// Report all .class files, with org.junit.Test annotated methods
final MethodReporter reporter = new MethodReporter() {

    @SuppressWarnings("unchecked")
    @Override
    public Class<? extends Annotation>[] annotations() {
        return new Class[]{Test.class};
    }

    @Override
    public void reportMethodAnnotation(Class<? extends Annotation> annotation,
        String className, String methodName) {
        // do something
    }

};
final AnnotationDetector cf = new AnnotationDetector(reporter);
cf.detect();

That's all!

Example Usage (3.1.x versions):

Put the annotation-detector-3.1.x.jar on the class path. No other dependencies are required! You can either scan the complete class path or only scan specified packages or Files (see JavaDoc for more details).

// Get a List with all classes annotated with @RuntimeVisibleTestAnnotation (Java 8 syntax)
List<Class<?>> types = AnnotationDetector.scanClassPath()
    .forAnnotations(RuntimeVisibleTestAnnotation.class)
    .collect(AnnotationDetector::getType);

    assertEquals(1, types.size());
    assertSame(NewApiTest.class, types.get(0));

// Get a List with all methods annotated with @RuntimeVisibleTestAnnotation, excluding
// files ending with "Test.class" in the "eu.infomas" package and subpackages (Java 8 syntax)
List<Method> methods = AnnotationDetector.scanClassPath("eu.infomas") // or: scanFiles(File... files)
    .forAnnotations(RuntimeVisibleTestAnnotation.class) // one or more annotations
    .on(ElementType.METHOD) // optional, default ElementType.TYPE. One ore more element types
    .filter((File dir, String name) -> !name.endsWith("Test.class")) // optional, default all *.class files
    .collect(AnnotationDetector::getMethod);

    assertEquals(1, methods.size());
    assertEquals(void.class, methods.get(0).getReturnType());

Even simpler, isn't it?

License

Copyright (c) 2011 - 2016 XIAM Solutions B.V.

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

Ohloh profile for ronaldmuller