Comments (12)
FYI - nasty reflection was:
package com.google.inject.internal;
import com.google.inject.internal.Annotations.AnnotationChecker;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import java.lang.annotation.Annotation;
import java.util.Collections;
public class ConfigPropertyOrAnnotationChecker extends AnnotationChecker {
private final AnnotationChecker delegate;
/**
* Constructs a new checker that looks for annotations of the given types.
*
* @param delegate delegate checker
*/
public ConfigPropertyOrAnnotationChecker(Object delegate) {
super(Collections.singleton(ConfigProperty.class));
this.delegate = (AnnotationChecker) delegate;
}
@Override
boolean hasAnnotations(Class<? extends Annotation> annotated) {
return annotated == ConfigProperty.class || delegate.hasAnnotations(annotated);
}
}
and
try {
Field bindingAnnotationCheckerField = Annotations.class.getDeclaredField("bindingAnnotationChecker");
bindingAnnotationCheckerField.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(
bindingAnnotationCheckerField, bindingAnnotationCheckerField.getModifiers() & ~Modifier.FINAL);
Object bindingAnnotationChecker = bindingAnnotationCheckerField.get(Annotations.class);
bindingAnnotationCheckerField.set(
Annotations.class, new ConfigPropertyOrAnnotationChecker(bindingAnnotationChecker));
bindingAnnotationCheckerField.setAccessible(false);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new ConfigurationException(Collections.singleton(new Message(e.getLocalizedMessage())));
}
from guice.
Field + Construct interceptors will be a much better option here, Qualifier should already be there - Strictly speaking this should actually be more than do-able
I'll join you on this one
from guice.
TypeListeners seem to be the best route
from guice.
TypeListeners seem to be the best route
Sorry not really sure how they help... the point is to make things annotated with ConfigProperty injectable - do listeners allow not having a binding but influencing how to inject the value?
Or were you suggesting writing an extension that listened to type and then added a binding - the challenge there is that you can't create a key with any annotation, which doesn't contain javax.inject.Qualifier or com.google.inject.BindingAnnotation, which ConfigProperty does not
from guice.
Yep - I got pretty well through it but very busy time work wise for me right now -
Intercepting on the injection with a TypeListener and pushing the value into the configuration seems to be bringing some success for me, support for 3rd party annotations generally goes this route to cater for these scenarios,
Whats also pretty nice is the config library can also be bound as singletons or eager singletons as well which I'm playing with
Reflection probably not going to get away from, in one way or the other, but the binding mechanism :) I need it for something i'm working on during the 9-5 as well, but have a lot in front before I get there :)
I'm doing an approach very similar to the jsr250 implementation
bindListener(Matchers.any(), new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
}
});
from guice.
Your work is amazing and inspiring and makes me wonder - "how on earth did you figure out how to do that?" The documentation doesn't really go into much detail about extending or plugging in, and from what experimentation I've tried - I can't see how only having the TypeListener is able to do the injection and communicate to Guice internals that it's already taken care of... if I try with a simple listener and add a memberInjector and just set the value, Guice still complains. Is there some magic communication that happens when you ask the "encounter" for the Injector and then ask it for a provider? I see the key is rewritten to look for Named but how does Guice know that you've done that for it?
Ultimately, I would like to not have to prebind anything - and this mechanism looks really great if I can just get the Config object and get the value (which is easy) then inject it, it would be quite nice to use - but squelching Guice's lookup failures is the part I can't make work.
from guice.
Ohhhhhh… This doesn't work if you have @Inject
AND @Resource
(or some other non-injectible thing) - for some reason, I thought that you had figured out the magic to let listeners adjust the bindings, but, in fact, your clients would have to NOT declare @Inject
- I'll have to think about what that means - it doesn't seem quite right… in CDI world, @ConfigProperty
needs @Inject
so it seems like you'd want that regardless of whether SE with Guice, or EE with CDI/Weld.
from guice.
OK that is strange I've already done this on a few of my Guiced-EE implementations and that wasn't the case - I will check it and get back to you, definitely supposed to override any @Inject settings hmm - To make sure though, this best works with PrivateModules with expose clauses - sorry about the delay the holidays in April were a few too many put quite a bit of the side-work on hold getting back into work xD
I'm going a bit overboard on the impl though, doing a main MP implementation, not just for config, let's see if I can keep them loosely coupled xD
from guice.
@GedMarc - Here's a draft PR to show you what I haven't been able to make work - but in the context of your system.
And reference showing that the desired behavior for ConfigProfile should include @Inject
@Inject
@ConfigProperty(name = "my.long.property")
private Long injectedLongValue;
from guice.
Duplicate of #1383
from guice.
Related Issues (20)
- Deadlock in multi-threading enviroment (Guice 5.1)
- enable @Inject for records HOT 4
- InternalProvisionException.errorInUserCode discards important context about user errors HOT 1
- Annotations @Provides and @Singleton do not work together HOT 2
- HiddenClassDefiner incorrectly assumes Unsafe.staticFieldBase(Field) returns a real Object HOT 8
- Bob Lee,God bless You
- where are the installation instructions? HOT 4
- java.lang.ClassCastException: class com.google.inject.servlet.GuiceFilter cannot be cast to class jakarta.servlet.Filter (com.google.inject.servlet.GuiceFilter is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @e383572; jakarta.servlet.Filter is in unnamed module of loader java.net.URLClassLoader @2ef1e4fa) HOT 1
- Changed behaviour in Guice 7 HOT 3
- Struts 6.x.x not working with Guice HOT 9
- Possible leak using Guice HOT 5
- Performance Issue in getJustInTimeBinding Method HOT 1
- Misleading exception message for nested Guice injector failure
- 8 Flaky tests in the module extensions/throwingproviders
- Request/Question: Provide non-jarjar'd builds
- Guice AOP is not compatible with Kotlin Coroutines HOT 2
- Enable to define a custom "@Inject" marker (ie add some extension point for reflection doing getAnnotation/isAnnotationPresent on this marker)
- Implement a functionality similar to Spring Boot’s fully automatic configuration dependency injection
- Replace Guava with standard Java (where possible) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guice.