Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .checkstyle

This file was deleted.

9 changes: 0 additions & 9 deletions .github/dependabot.yml

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ A small Java library that contains several helpful utility classes.
[![Java Development Kit 17](https://img.shields.io/badge/JDK-17-green.svg)](https://openjdk.java.net/projects/jdk/17/)

## Versions
- 0.14.0 = See [release-notes](release-notes.md)
- [0.15.0](release-notes.md#0150)
- [0.14.0](release-notes.md#0140)
- 0.13.x (or later) = **Java 17**
- 0.12.0 = **Java 11** with new **jakarta** namespace
- 0.11.x = **Java 11** before namespace change from 'javax' to 'jakarta'
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<artifactId>utils4j</artifactId>
<packaging>bundle</packaging>
<version>0.14.0</version>
<version>0.15.0-SNAPSHOT</version>
<description>A small Java library that contains several helpful utility classes.</description>
<url>http://www.fuin.org/utils4j/</url>

Expand Down
4 changes: 4 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 0.15.0
- Added new [JandexUtils](src/main/java/org/fuin/utils4j/jandex/JandexUtils.java) methods
- Added new [TechnicalId](src/main/java/org/fuin/utils4j/TechnicalId.java) tag interface.

## 0.14.0

### General
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/fuin/utils4j/TechnicalId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (C) 2015 Michael Schnell. All rights reserved.
* http://www.fuin.org/
* <p>
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option) any
* later version.
* <p>
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see http://www.gnu.org/licenses/.
*/
package org.fuin.utils4j;

/**
* Technical identifier that is only used internally and never shown to a user.
*/
public interface TechnicalId {

}
60 changes: 60 additions & 0 deletions src/main/java/org/fuin/utils4j/jandex/JandexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@
package org.fuin.utils4j.jandex;

import org.fuin.utils4j.Utils4J;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.CompositeIndex;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -84,6 +91,21 @@ public static Index indexDir(final File dir) {
return indexer.complete();
}

/**
* Indexes all classes in multiple directories and their subdirectories.
*
* @param dirs Classes directories to analyze.
* @return Index of all classes in the directories.
*/
public static Index indexDirs(final File... dirs) {
final Indexer indexer = new Indexer();
final List<File> knownClassFiles = new ArrayList<>();
for (final File dir : dirs) {
indexDir(indexer, knownClassFiles, dir);
}
return indexer.complete();
}

/**
* Indexes all classes in a directory or it's subdirectories.
*
Expand Down Expand Up @@ -194,5 +216,43 @@ public static Class<?> loadClass(DotName name) {
return Utils4J.loadClass(name.toString());
}

/**
* Locates (non-abstract, non-interface) classes that implement a given interface in directories.
*
* @param intf Interface to find implementors for.
* @param classesDirs Directories with class files to scan.
* @return List of classes.
*
* @param <T> Expected interface type.
*/
@SuppressWarnings("unchecked")
public static <T> List<Class<? extends T>> findImplementors(final Class<T> intf, final File... classesDirs) {
final List<IndexView> indexes = new ArrayList<>();
indexes.add(new JandexIndexFileReader.Builder().addDefaultResource().build().loadR());
indexes.add(indexDirs(classesDirs));
return findImplementors(intf, CompositeIndex.create(indexes));
}

/**
* Locates (non-abstract, non-interface) classes that implement a given interface in an index.
*
* @param intf Interface to find implementors for.
* @param index Index with known classes.
* @return List of classes.
*
* @param <T> Expected interface type.
*/
@SuppressWarnings("unchecked")
public static <T> List<Class<? extends T>> findImplementors(final Class<T> intf, final IndexView index) {
List<Class<? extends T>> implementors = new ArrayList<>();
final Collection<ClassInfo> implementingClasses = index.getAllKnownImplementors(DotName.createSimple(intf));
for (final ClassInfo classInfo : implementingClasses) {
if (!Modifier.isAbstract(classInfo.flags()) && !Modifier.isInterface(classInfo.flags())) {
final Class<? extends T> implementor = (Class<? extends T>) JandexUtils.loadClass(classInfo.name());
implementors.add(implementor);
}
}
return implementors;
}

}
34 changes: 34 additions & 0 deletions src/test/java/org/fuin/utils4j/jandex/JandexUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package org.fuin.utils4j.jandex;

import org.fuin.utils4j.Utils4J;
import org.fuin.utils4j.filter.AndFilter;
import org.fuin.utils4j.filter.Filter;
import org.fuin.utils4j.jandex.JandexUtils;
import org.fuin.utils4j.jaxb.CDataXmlAdapter;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;
Expand Down Expand Up @@ -73,6 +76,22 @@ public final void testIndexDirSimple() {

}

@Test
public final void testIndexDirs() {

// PREPEARE
final File filterDir = new File(TARGET_DIR, "classes/org/fuin/utils4j/filter");
final File jaxbDir = new File(TARGET_DIR, "classes/org/fuin/utils4j/jaxb");

// TEST
final Index index = JandexUtils.indexDirs(filterDir, jaxbDir);

// VERIFY
assertThat(index.getClassByName(DotName.createSimple(AndFilter.class.getName()))).isNotNull();
assertThat(index.getClassByName(DotName.createSimple(CDataXmlAdapter.class.getName()))).isNotNull();

}

@Test
public final void testIndexDir() {

Expand Down Expand Up @@ -145,5 +164,20 @@ public final void loadClassFailure() {
.hasMessageContaining("Failed to load class");
}

@Test
public final void findImplementors() {

// PREPEARE
final File filterDir = new File(TARGET_DIR, "classes/org/fuin/utils4j/filter");

// TEST
final List<Class<? extends Filter>> classes = JandexUtils.findImplementors(Filter.class, filterDir);

// VERIFY
assertThat(classes).isNotEmpty();
assertThat(classes).contains(AndFilter.class);

}

}
// CHECKSTYLE:ON