Skip to content
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

Weld 2335 Having different types between InterceptionFactory and instance to intercept should fail #1599

Closed
wants to merge 1 commit into from

Conversation

antoinesd
Copy link
Contributor

As the idea behind InterceptionFactory is to customise the underlying AnnotatedType to add interceptor binding on class or method, it doesn't make sense to pass an instance of a different type than the parameter type used to defined the InterceptionFactory.

@mkouba
Copy link
Member

mkouba commented Feb 20, 2017

Well, sometimes it might make sense to pass a subtype instance - consider this dummy producer:

@Produces
void Number produceNumber(InterceptionFactory<Number> factory) {
   // MyInterceprotBinding has @Inherited
   factory.configure().add(MyInterceprotBinding.Literal.INSTANCE);
   return factory.createInterceptedInstance(isDummyConditionTrue ? Long.valueOf("1") : Short.valueOf());
}

@manovotn
Copy link
Contributor

manovotn commented Feb 20, 2017

First of all, I have to say I feel like this functionality is very limiting. Not being able to use interfaces sounds like a bad practice. Not to mention it's completely counter-intuitive, so we should at the very least point this out somewhere in docs.

Ignore the text below, got bad import in the test, sorry for the fuss :-/
Anyhow, here is another test case in which the graceful failure fails - you get NoSuchElementException there. It is a plain bean implementing an interface and I tried using InterceptionFactory on the interface.

@antoinesd
Copy link
Contributor Author

antoinesd commented Feb 20, 2017

@manovotn this feature is here to apply interceptor binding on an instance dynamically.
When you use interceptors in the classical way, applying an interceptor binding to an interface doesn't produce anything, so failing here is somehow compliant with interceptor spec.
Will look at your new test.

I agree that the question of supporting parent class is open, even if interceptor binding are only applied on subclass if the annotation is @Inherited and applied on method if they are not overloaded in subclass...

@antoinesd
Copy link
Contributor Author

@mkouba I update the PR to support creation of intercepted instance on child class instance.

Technically we could support interfaces (by creating a new AnnotatedType and copying annotations), but is it worth?

@mkouba
Copy link
Member

mkouba commented Feb 21, 2017

@antoinesd I think it is more a spec issue/question for EG. My opinion is that interfaces should not be supported. And I don't think it a bad practice or counter-intuitive - InterceptionFactory is a helper for a produced instance - implementing class. The bean type could be an interface.

public void testListAdd(BeanManager bm) {
// resolving via Instance just to make the NPE exception human-readable (direct method injection will blow up with Arq. stack)
// Invalid producer using an InterceptionFactory for List (interface) and applying it to ArrayList
List<Object> list = bm.createInstance().select(new TypeLiteral<List<Object>>() {}, Produced.Literal.INSTANCE).get();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor thing... why no use @Produced Instance<List<Object>> isntance parameter instead of BeanManager bm?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it was the example test I had on my branch and I did it this is way to make the exception human-readable (as the comments suggest). Otherwise you got an NPE coming from arquillian.
I agree it should be either changed now, or at least the comment is now useless.

// resolving via Instance just to make the NPE exception human-readable (direct method injection will blow up with Arq. stack)
// Invalid producer using an InterceptionFactory for List (interface) and applying it to ArrayList
List<Object> list = bm.createInstance().select(new TypeLiteral<List<Object>>() {}, Produced.Literal.INSTANCE).get();
Producer.reset();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines should be replaced with Assert.fail()...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, since this is now deeded as expected failure

@@ -112,6 +112,10 @@ public T createInterceptedInstance(T instance) {
}
used = true;

if(annotatedType.getJavaClass().isInterface()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the predefined formatting style...

@mkouba
Copy link
Member

mkouba commented Feb 21, 2017

@antoinesd could you please squash the last 3 commits please?

add a test on type to have it fails gracefully
@mkouba
Copy link
Member

mkouba commented Feb 22, 2017

Merged, thanks!

@mkouba mkouba closed this Feb 22, 2017
@antoinesd antoinesd deleted the weld_2335 branch February 22, 2017 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants