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 732 #32

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -219,7 +219,7 @@ protected <X> void createProducerFields(AbstractClassBean<X> declaringBean, Weld

protected <X> void createObserverMethods(RIBean<X> declaringBean, WeldClass<X> annotatedClass)
{
for (WeldMethod<?, ? super X> method : annotatedClass.getDeclaredWeldMethodsWithAnnotatedParameters(Observes.class))
for (WeldMethod<?, ? super X> method : annotatedClass.getWeldMethodsWithAnnotatedParameters(Observes.class))
{
createObserverMethod(declaringBean, method);
}
Expand Down
11 changes: 10 additions & 1 deletion impl/src/main/java/org/jboss/weld/introspector/WeldClass.java
Expand Up @@ -158,14 +158,23 @@ public interface WeldClass<T> extends WeldAnnotated<T, Class<T>>, AnnotatedType<
public WeldMethod<?, ?> getDeclaredWeldMethod(Method method);

/**
* Gets all with parameters annotated with annotationType
* Gets declared with parameters annotated with annotationType
*
* @param annotationType The annotation to match
* @return A set of abstracted methods with the given annotation. Returns an
* empty set if there are no matches
*/
public Collection<WeldMethod<?, ? super T>> getDeclaredWeldMethodsWithAnnotatedParameters(Class<? extends Annotation> annotationType);

/**
* Gets all with parameters annotated with annotationType
*
* @param annotationType The annotation to match
* @return A set of abstracted methods with the given annotation. Returns an
* empty set if there are no matches
*/
public Collection<WeldMethod<?, ? super T>> getWeldMethodsWithAnnotatedParameters(Class<? extends Annotation> annotationType);

/**
* Gets the superclass.
*
Expand Down
Expand Up @@ -547,6 +547,20 @@ public WeldConstructor<T> getNoArgsWeldConstructor()
return Collections.unmodifiableCollection(declaredMethodsByAnnotatedParameters.get(annotationType));
}

public Collection<WeldMethod<?, ? super T>> getWeldMethodsWithAnnotatedParameters(Class<? extends Annotation> annotationType)
{
// TODO should be cached
ArrayList<WeldMethod<?, ? super T>> methods = new ArrayList<WeldMethod<?, ? super T>>();
for (WeldMethod<?, ?> method : getWeldMethods())
{
if (!method.getWeldParameters(annotationType).isEmpty())
{
methods.add((WeldMethod<?, ? super T>) method);
}
}
return Collections.unmodifiableCollection(methods);
}

public WeldMethod<?, ?> getWeldMethod(Method methodDescriptor)
{
// TODO Should be cached
Expand Down Expand Up @@ -651,7 +665,7 @@ public boolean isPublic()
{
return Modifier.isFinal(getJavaClass().getModifiers());
}

public boolean isGeneric()
{
return getJavaClass().getTypeParameters().length > 0;
Expand Down
@@ -0,0 +1,23 @@
package org.jboss.weld.tests.event.observer.superclass;

import javax.enterprise.event.Observes;

public abstract class AbstractTestObserver
{
private TestEvent testEvent;

public TestEvent getTestEvent()
{
return testEvent;
}

void observe(@Observes TestEvent event)
{
this.testEvent = event;
}

public void reset()
{
testEvent = null;
}
}
@@ -0,0 +1,12 @@
package org.jboss.weld.tests.event.observer.superclass;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface Disabled {

}
@@ -0,0 +1,12 @@
package org.jboss.weld.tests.event.observer.superclass;

@Disabled
public class DisabledTestObserver extends TestObserver {

@Override
// observation disabled because this overrides the observer method without
// the @Observes
void observe(TestEvent event) {
super.observe(event);
}
}
@@ -0,0 +1,12 @@
package org.jboss.weld.tests.event.observer.superclass;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface ReEnabled {

}
@@ -0,0 +1,12 @@
package org.jboss.weld.tests.event.observer.superclass;

import javax.enterprise.event.Observes;

@ReEnabled
public class ReEnabledTestObserver extends DisabledTestObserver {
@Override
// reenables observation by overriding and adding @Observes
void observe(@Observes TestEvent event) {
super.observe(event);
}
}
@@ -0,0 +1,79 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.event.observer.superclass;

import javax.enterprise.event.Event;
import javax.inject.Inject;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class SuperclassObserversTest {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(BeanArchive.class).addPackage(
SuperclassObserversTest.class.getPackage());
}

@Inject
Event<TestEvent> event;

@Inject
private TestObserver observer;

@Inject
@Disabled
private DisabledTestObserver disabled;

@Inject
@ReEnabled
private ReEnabledTestObserver reenabled;

@Test
public void testObserverMethodFromSuperclassInvoked() {
observer.reset();

assert observer.getTestEvent() == null;
event.fire(new TestEvent());
assert observer.getTestEvent() != null;
}

@Test
public void testObserverMethodOnOverridesWithoutAnnotNotInvoked() {
disabled.reset();

assert disabled.getTestEvent() == null;
event.fire(new TestEvent());
assert disabled.getTestEvent() == null;
}

@Test
public void testObserverMethodOnOverridesWithAnnotAreInvoked() {
reenabled.reset();

assert reenabled.getTestEvent() == null;
event.fire(new TestEvent());
assert reenabled.getTestEvent() != null;
}

}
@@ -0,0 +1,6 @@
package org.jboss.weld.tests.event.observer.superclass;

public class TestEvent
{

}
@@ -0,0 +1,9 @@
package org.jboss.weld.tests.event.observer.superclass;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class TestObserver extends AbstractTestObserver
{

}