-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…@bean(destroyMethod) Example: ``` @factory final class MyFactory { @bean MyComponent myComponent() { ... } @PreDestroy(priority = 3000) void destroyMyComponent(MyComponent bean) { ... } ``` - ONLY valid on factory components - An alternative to @bean(destroyMethod) - The @PreDestroy method matches to a @bean by type only (ignores qualifiers, only 1 argument to the destroy method)
- Loading branch information
Showing
11 changed files
with
196 additions
and
20 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
77 changes: 77 additions & 0 deletions
77
inject-generator/src/main/java/io/avaje/inject/generator/DestroyMethods.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,77 @@ | ||
package io.avaje.inject.generator; | ||
|
||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.ExecutableElement; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Holds extra PreDestroy methods for a factory. | ||
* <p> | ||
* These methods are expected to relate back to a {@code @Bean} method | ||
* on the same factory. | ||
*/ | ||
final class DestroyMethods { | ||
|
||
private final Map<String, DestroyMethod> methods = new HashMap<>(); | ||
private final Set<String> matchedTypes = new HashSet<>(); | ||
|
||
void add(ExecutableElement element) { | ||
Integer priority = PreDestroyPrism.getOptionalOn(element) | ||
.map(PreDestroyPrism::priority) | ||
.orElse(null); | ||
|
||
var destroyMethod = new DestroyMethod(element, priority); | ||
methods.put(destroyMethod.matchType, destroyMethod); | ||
} | ||
|
||
DestroyMethod match(String returnTypeRaw) { | ||
var match = methods.get(returnTypeRaw); | ||
if (match != null) { | ||
matchedTypes.add(returnTypeRaw); | ||
} | ||
return match; | ||
} | ||
|
||
/** | ||
* Return PreDestroy methods that were not matched to a {@code @Bean} method | ||
* on the same factory. | ||
*/ | ||
List<DestroyMethod> unmatched() { | ||
return methods.values() | ||
.stream() | ||
.filter(entry -> !matchedTypes.contains(entry.matchType())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
static final class DestroyMethod { | ||
|
||
private final String method; | ||
private final Integer priority; | ||
private final String matchType; | ||
private final ExecutableElement element; | ||
|
||
DestroyMethod(ExecutableElement element, Integer priority) { | ||
this.element = element; | ||
this.method = element.getSimpleName().toString(); | ||
this.matchType = element.getParameters().get(0).asType().toString(); | ||
this.priority = priority; | ||
} | ||
|
||
String method() { | ||
return method; | ||
} | ||
|
||
Integer priority() { | ||
return priority; | ||
} | ||
|
||
String matchType() { | ||
return matchType; | ||
} | ||
|
||
Element element() { | ||
return element; | ||
} | ||
} | ||
} |
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