forked from smallrye/smallrye-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
smallrye#316: power annotations scanner and tck (and utils)
Signed-off-by: Rüdiger zu Dohna <[email protected]>
- Loading branch information
Showing
49 changed files
with
3,266 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.smallrye</groupId> | ||
<artifactId>smallrye-power-annotations-parent</artifactId> | ||
<version>1.0.18-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>power-annotations-scanner</artifactId> | ||
<name>Power Annotations Jandex Runtime Scanner</name> | ||
<description>Build a Jandex index at runtime from the classpath and resolve power annotations</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jboss</groupId> | ||
<artifactId>jandex</artifactId> | ||
<version>2.2.2.Final</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.smallrye</groupId> | ||
<artifactId>power-annotations-jandex-common</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>5.7.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.18.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
53 changes: 53 additions & 0 deletions
53
...nnotations/scanner/src/main/java/com/github/t1/powerannotations/scanner/IndexBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.github.t1.powerannotations.scanner; | ||
|
||
import static java.util.logging.Level.FINE; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Optional; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.jboss.jandex.IndexReader; | ||
import org.jboss.jandex.IndexView; | ||
|
||
public class IndexBuilder { | ||
public static IndexView loadOrScan() { | ||
IndexView jandex = load().orElseGet(Scanner::scan); | ||
if (LOG.isLoggable(LEVEL)) { | ||
LOG.log(LEVEL, "------------------------------------------------------------"); | ||
jandex.getKnownClasses() | ||
.forEach(classInfo -> LOG.log(LEVEL, classInfo.name() + " :: " + classInfo.classAnnotations().stream() | ||
.filter(instance -> !instance.name().toString().equals("kotlin.Metadata")) // contains binary | ||
.map(Object::toString).collect(joining(", ")))); | ||
LOG.log(LEVEL, "------------------------------------------------------------"); | ||
} | ||
return jandex; | ||
} | ||
|
||
@SuppressWarnings("UnusedReturnValue") | ||
private static Optional<IndexView> load() { | ||
try (InputStream inputStream = getClassLoader().getResourceAsStream("META-INF/jandex.idx")) { | ||
return Optional.ofNullable(inputStream).map(IndexBuilder::load); | ||
} catch (RuntimeException | IOException e) { | ||
throw new RuntimeException("can't read index file", e); | ||
} | ||
} | ||
|
||
private static IndexView load(InputStream inputStream) { | ||
try { | ||
return new IndexReader(inputStream).read(); | ||
} catch (RuntimeException | IOException e) { | ||
throw new RuntimeException("can't read Jandex input stream", e); | ||
} | ||
} | ||
|
||
private static ClassLoader getClassLoader() { | ||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
return (classLoader == null) ? ClassLoader.getSystemClassLoader() : classLoader; | ||
} | ||
|
||
private static final Logger LOG = Logger.getLogger(IndexBuilder.class.getName()); | ||
private static final Level LEVEL = FINE; | ||
} |
54 changes: 54 additions & 0 deletions
54
...notations/scanner/src/main/java/com/github/t1/powerannotations/scanner/IndexerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.github.t1.powerannotations.scanner; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Stream; | ||
|
||
class IndexerConfig { | ||
private final List<String> exclude = new ArrayList<>(); | ||
|
||
IndexerConfig() { | ||
this("META-INF/jandex.properties"); | ||
} | ||
|
||
IndexerConfig(String resource) { | ||
try (InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(resource)) { | ||
if (inputStream == null) | ||
return; | ||
Properties properties = new Properties(); | ||
properties.load(inputStream); | ||
loadExcludeConfig(properties); | ||
} catch (IOException e) { | ||
throw new RuntimeException("can't load " + resource, e); | ||
} | ||
} | ||
|
||
private void loadExcludeConfig(Properties properties) { | ||
String excludeString = properties.getProperty("exclude", null); | ||
if (excludeString != null) { | ||
try { | ||
Stream.of(excludeString.split("\\s+")) | ||
.map(this::gavToRegex) | ||
.forEach(this.exclude::add); | ||
} catch (Exception e) { | ||
throw new RuntimeException("can't parse exclude config", e); | ||
} | ||
} | ||
} | ||
|
||
private String gavToRegex(String groupArtifact) { | ||
Matcher matcher = Pattern.compile("(?<group>[^:]+):(?<artifact>[^:]+)").matcher(groupArtifact); | ||
if (!matcher.matches()) | ||
throw new RuntimeException("expect `group:artifact` but found `" + groupArtifact + "`"); | ||
return ".*/" + matcher.group("group") + "/.*/" + matcher.group("artifact") + "-.*\\.jar"; | ||
} | ||
|
||
public Stream<String> excludes() { | ||
return exclude.stream(); | ||
} | ||
} |
Oops, something went wrong.