Skip to content

Plugin Aspects #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 17, 2023
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.avaje.inject.generator;

import static io.avaje.inject.generator.ProcessingContext.logDebug;
import static io.avaje.inject.generator.ProcessingContext.logWarn;

import java.util.Iterator;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
Expand Down Expand Up @@ -30,13 +33,17 @@ private static boolean moduleCP() {

static void registerModuleProvidedTypes(Set<String> providedTypes) {
if (!injectAvailable) {
logWarn(
"Unable to detect Avaje Inject in Annotation Processor Class Path, use the Avaje Inject Maven/Gradle plugin for detecting external dependencies");
return;
}

Iterator<Module> iterator = ServiceLoader.load(Module.class, ExternalProvider.class.getClassLoader()).iterator();
Iterator<Module> iterator =
ServiceLoader.load(Module.class, ExternalProvider.class.getClassLoader()).iterator();
while (iterator.hasNext()) {
try {
Module module = iterator.next();
var module = iterator.next();
logDebug("Loaded External Module %s", module.getClass().getCanonicalName());
for (final Class<?> provide : module.provides()) {
providedTypes.add(provide.getCanonicalName());
}
Expand All @@ -61,9 +68,13 @@ static void registerPluginProvidedTypes(ScopeInfo defaultScope) {
return;
}
for (final Plugin plugin : ServiceLoader.load(Plugin.class, Processor.class.getClassLoader())) {
logDebug("Loaded Plugin %s", plugin.getClass().getCanonicalName());
for (final Class<?> provide : plugin.provides()) {
defaultScope.pluginProvided(provide.getCanonicalName());
}
for (final Class<?> provide : plugin.providesAspects()) {
defaultScope.pluginProvided(Util.wrapAspect(provide.getCanonicalName()));
}
}
}
}
2 changes: 1 addition & 1 deletion inject-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repositories {
}

dependencies {
implementation 'io.avaje:avaje-inject:9.0'
implementation 'io.avaje:avaje-inject:9.4-SNAPSHOT'
implementation gradleApi()

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.gradle.api.*;

import io.avaje.inject.spi.Plugin;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand Down Expand Up @@ -54,13 +56,22 @@ private FileWriter createFileWriter(String dir, String file) throws IOException
}

private void writeProvidedPlugins(ClassLoader cl, FileWriter pluginWriter) throws IOException {
for (final io.avaje.inject.spi.Plugin plugin : ServiceLoader.load(io.avaje.inject.spi.Plugin.class, cl)) {
for (final Class<?> providedType : plugin.provides()) {
pluginWriter.write(providedType.getCanonicalName());
pluginWriter.write("\n");
}
}
}
final Set<String> providedTypes = new HashSet<>();

for (final var plugin : ServiceLoader.load(Plugin.class, newClassLoader)) {
for (final Class<?> provide : plugin.provides()) {
providedTypes.add(provide.getCanonicalName());
}
for (final Class<?> provide : plugin.providesAspects()) {
providedTypes.add(wrapAspect(provide.getCanonicalName()));
}
}

for (final var providedType : providedTypes) {
pluginWriter.write(providedType);
pluginWriter.write("\n");
}
}

private void writeProvidedModules(ClassLoader classLoader, FileWriter moduleWriter) throws IOException {
final Set<String> providedTypes = new HashSet<>();
Expand Down
2 changes: 1 addition & 1 deletion inject-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>8.12-RC4</version>
<version>9.4-SNAPSHOT</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,28 @@ private FileWriter createFileWriter(String string) throws IOException {

private void writeProvidedPlugins(URLClassLoader newClassLoader, FileWriter pluginWriter)
throws IOException {
for (final var plugin : ServiceLoader.load(Plugin.class, newClassLoader)) {
for (final Class<?> providedType : plugin.provides()) {
pluginWriter.write(providedType.getCanonicalName());
pluginWriter.write("\n");
}
}
}
final Set<String> providedTypes = new HashSet<>();

for (final var plugin : ServiceLoader.load(Plugin.class, newClassLoader)) {
for (final Class<?> provide : plugin.provides()) {
providedTypes.add(provide.getCanonicalName());
}
for (final Class<?> provide : plugin.providesAspects()) {
providedTypes.add(wrapAspect(provide.getCanonicalName()));
}
}

for (final var providedType : providedTypes) {
pluginWriter.write(providedType);
pluginWriter.write("\n");
}
}

private void writeProvidedModules(URLClassLoader newClassLoader, FileWriter moduleWriter)
throws IOException {
final Set<String> providedTypes = new HashSet<>();

for (final Module module : ServiceLoader.load(Module.class, newClassLoader)) {
for (final var module : ServiceLoader.load(Module.class, newClassLoader)) {
for (final Class<?> provide : module.provides()) {
providedTypes.add(provide.getCanonicalName());
}
Expand Down
8 changes: 6 additions & 2 deletions inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.*;

/**
* Build a bean scope with options for shutdown hook and supplying test doubles.
Expand Down Expand Up @@ -236,7 +236,11 @@ public BeanScope build() {
" Review IntelliJ Settings / Build / Build tools / Gradle - 'Build and run using' value and set that to 'Gradle'. " +
" Refer to https://avaje.io/inject#gradle");
}
log.log(DEBUG, "building with modules {0}", moduleNames);

log.log(
propertyRequiresPlugin.contains("printModules") ? INFO : DEBUG,
"building with modules {0}",
moduleNames);

final Builder builder = Builder.newBuilder(propertyRequiresPlugin, suppliedBeans, enrichBeans, parent, parentOverride);
for (final Module factory : factoryOrder.factories()) {
Expand Down
2 changes: 1 addition & 1 deletion inject/src/main/java/io/avaje/inject/spi/GenericType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Represents a full type including generics declaration, to avoid information loss due to type erasure.
* <p>
* This is a cut down version of Helidon GenericType Apache 2 licence.
* This is a cut down version of Helidon GenericType Apache 2 license.
*
* @param <T> the generic type parameter
*/
Expand Down
25 changes: 15 additions & 10 deletions inject/src/main/java/io/avaje/inject/spi/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@

/**
* A Plugin that can be applied when creating a bean scope.
* <p>
* Typically, a plugin might provide a default dependency via {@link BeanScopeBuilder#provideDefault(Type, java.util.function.Supplier)}.
*
* <p>Typically, a plugin might provide a default dependency via {@link
* BeanScopeBuilder#provideDefault(Type, java.util.function.Supplier)}.
*/
public interface Plugin {

/**
* Return the classes that the plugin provides.
*/
/** Apply the plugin to the scope builder. */
void apply(BeanScopeBuilder builder);

/** Empty array of classes. */
Class<?>[] EMPTY_CLASSES = {};

/** Return the classes that the plugin provides. */
default Class<?>[] provides() {
return new Class<?>[]{};
return EMPTY_CLASSES;
}

/**
* Apply the plugin to the scope builder.
*/
void apply(BeanScopeBuilder builder);
/** Return the aspect classes that the plugin provides. */
default Class<?>[] providesAspects() {
return EMPTY_CLASSES;
}
}