Skip to content

Commit

Permalink
Allowing injecting in constructor's (FabricMC#30 FabricMC#40)
Browse files Browse the repository at this point in the history
* Allowing injecting in constructor

It is bad to assume that everyone does business logic outside of constructor. We don't want redirect conflict hell

* This should work

Signed-off-by: liach <liach@users.noreply.github.com>

* Add notes for easy portin in the future

Signed-off-by: liach <liach@users.noreply.github.com>

* Restore build number

* Adopt new versioning scheme

Signed-off-by: liach <liach@users.noreply.github.com>

Co-authored-by: liach <7806504+liach@users.noreply.github.com>
Co-authored-by: liach <liach@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 26, 2020
1 parent 279f1e6 commit 536170a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ public boolean checkPriority(int targetPriority, int mixinPriority) {
* @return restriction level
*/
public RestrictTargetLevel getTargetRestriction(IInjectionPointContext context) {
return RestrictTargetLevel.CONSTRUCTORS_AFTER_DELEGATE; // Fabric change: allow inject in constructors
}

/**
* Returns the target restriction level for this injection point's cancellation for
* {@literal @Inject} annotations. This level defines whether an injection point
* can declare {@code cancellable = true}.
*
* @param context injection-specific context
* @return restriction level
*/
// Fabric addition: prevent cancellation of inject in constructors
public RestrictTargetLevel getCancellationRestriction(IInjectionPointContext context) {
return RestrictTargetLevel.METHODS_ONLY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,16 @@ protected void addTargetNode(Target target, List<InjectionNode> myNodes, Abstrac
throw new InvalidInjectionException(this.info, String.format("%s selector %s", ip, ex.getMessage()));
}

// Fabric start: prevent cancellation in constructor injections
if (this.cancellable) {
try {
this.checkTargetForNode(target, injectionNode, ip.getCancellationRestriction(this.info));
} catch (InvalidInjectionException ex) {
throw new InvalidInjectionException(this.info, String.format("%s selector (cancellable = true) %s", ip, ex.getMessage()));
}
}
// Fabric end

String id = ip.getId();
if (Strings.isNullOrEmpty(id)) {
continue;
Expand Down

0 comments on commit 536170a

Please sign in to comment.