diff --git a/weld/src/main/java/org/jboss/weld/experimental/ExperimentalAnnotated.java b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalAnnotated.java new file mode 100644 index 00000000..16682e8e --- /dev/null +++ b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalAnnotated.java @@ -0,0 +1,80 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2014, 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.experimental; + +import java.lang.annotation.Annotation; +import java.lang.annotation.Repeatable; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Objects; +import java.util.Set; + +import javax.enterprise.inject.spi.Annotated; + +/** + * This API is experimental and will change! All the methods declared by this interface are supposed to be moved to {@link Annotated}. + * + * @author Jozef Hartinger + * + * @see WELD-1743 + * + * @param + */ +public interface ExperimentalAnnotated extends Annotated { + + /** + * Equivalent of {@link AnnotatedElement#getAnnotationsByType(Class)}. + * + * @param annotationClass the Class object corresponding to the annotation type + * @return all this element's annotations for the specified annotation type if associated with this element, else an array of length zero + */ + @SuppressWarnings("unchecked") + default Set getAnnotationsByType(Class annotationClass) { + Objects.requireNonNull(annotationClass, "annotationClass"); + // first, delegate to getAnnotation() + final T annotation = getAnnotation(annotationClass); + if (annotation != null) { + return Collections.singleton(annotation); + } + // second, check if this annotation is repeatable + final Repeatable repeatable = annotationClass.getAnnotation(Repeatable.class); + if (repeatable != null) { + Class containerClass = repeatable.value(); + Annotation container = getAnnotation(containerClass); + if (container != null) { + // we found a container, extract the values + Method value; + try { + value = containerClass.getMethod("value"); + } catch (NoSuchMethodException | SecurityException e) { + throw new RuntimeException(e); // TODO + } + Set result = new LinkedHashSet<>(); + try { + Collections.addAll(result, (T[]) value.invoke(container)); + return result; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new RuntimeException(e); // TODO + } + } + } + return Collections.emptySet(); + } +} diff --git a/weld/src/main/java/org/jboss/weld/experimental/ExperimentalAnnotatedParameter.java b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalAnnotatedParameter.java new file mode 100644 index 00000000..d3a8f1dd --- /dev/null +++ b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalAnnotatedParameter.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2014, 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.experimental; + +import java.lang.reflect.Executable; +import java.lang.reflect.Member; +import java.lang.reflect.Parameter; + +import javax.enterprise.inject.spi.AnnotatedParameter; + +/** + * This API is experimental and will change! All the methods declared by this interface are supposed to be moved to {@link AnnotatedParameter}. + * + * All the methods declared by this interface should be moved to AnnotatedParameter. + * + * @author Jozef Hartinger + * @see CDI-481 + * + * @param + */ +public interface ExperimentalAnnotatedParameter extends AnnotatedParameter { + + /** + * Get the underlying {@link Parameter}. + * + * @return the {@link Parameter} + */ + default Parameter getJavaParameter() { + Member member = getDeclaringCallable().getJavaMember(); + if (!(member instanceof Executable)) { + throw new IllegalStateException("Parameter does not belong to an executable " + member); + } + Executable executable = (Executable) member; + return executable.getParameters()[getPosition()]; + } +} diff --git a/weld/src/main/java/org/jboss/weld/experimental/ExperimentalInvocationContext.java b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalInvocationContext.java new file mode 100644 index 00000000..ca3fd116 --- /dev/null +++ b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalInvocationContext.java @@ -0,0 +1,48 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2014, 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.experimental; + +import java.lang.annotation.Annotation; +import java.util.Set; + +import javax.interceptor.Interceptors; +import javax.interceptor.InvocationContext; + +/** + * This API is experimental and will change! All the methods declared by this interface are supposed to be moved to {@link InvocationContext}. + * + * @author Martin Kouba + * @see CDI-468 + * + */ +public interface ExperimentalInvocationContext extends InvocationContext { + + /** + * The returning set may be empty if only interceptors using the {@link Interceptors} annotation are associated. + * + * @return a set of interceptor bindings + */ + Set getInterceptorBindingsByType(Class annotationType); + + /** + * The returning set may be empty if only interceptors using the {@link Interceptors} annotation are associated. + * + * @return a set of interceptor bindings + */ + Set getInterceptorBindings(); + +} \ No newline at end of file diff --git a/weld/src/main/java/org/jboss/weld/experimental/ExperimentalObserverMethod.java b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalObserverMethod.java new file mode 100644 index 00000000..ead7179e --- /dev/null +++ b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalObserverMethod.java @@ -0,0 +1,46 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2014, 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.experimental; + +import javax.enterprise.inject.spi.ObserverMethod; +import javax.interceptor.Interceptor; + +/** + * This API is experimental and will change! All the methods declared by this interface are supposed to be moved to {@link ObserverMethod}. + * + * @author Jozef Hartinger + * @see WELD-1728 + * + * @param the event type + */ +public interface ExperimentalObserverMethod extends ObserverMethod, Prioritized { + + /** + * Default priority for observer methods that do not define priority. Currently the default priority is set to be in the middle + * of the "APPLICATION" range (see {@link Interceptor.Priority} for details). + * + * WARNING: This concept of default priority of an observer method is preliminary and subject to change. + */ + int DEFAULT_PRIORITY = 2500; + + /** + * The priority of this observer method + */ + default int getPriority() { + return DEFAULT_PRIORITY; + } +} diff --git a/weld/src/main/java/org/jboss/weld/experimental/ExperimentalProcessObserverMethod.java b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalProcessObserverMethod.java new file mode 100644 index 00000000..64246cde --- /dev/null +++ b/weld/src/main/java/org/jboss/weld/experimental/ExperimentalProcessObserverMethod.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2014, 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.experimental; + +import javax.enterprise.inject.spi.ObserverMethod; +import javax.enterprise.inject.spi.ProcessObserverMethod; + +/** + * This API is experimental and will change! All the methods declared by this interface are supposed to be moved to {@link ProcessObserverMethod}. + * + * @author Jozef Hartinger + * @see WELD-1749 + * + * @param The type of the event being observed + * @param The bean type containing the observer method + */ +public interface ExperimentalProcessObserverMethod extends ProcessObserverMethod { + + /** + * Replaces the observer method. + * + * @param observer + */ + void setObserverMethod(ObserverMethod observerMethod); + + /** + * Forces the container to ignore the observer method. + */ + void veto(); + + /** + * Temporarilly overriden return type + */ + @Override + ExperimentalObserverMethod getObserverMethod(); +} diff --git a/weld/src/main/java/org/jboss/weld/experimental/Prioritized.java b/weld/src/main/java/org/jboss/weld/experimental/Prioritized.java new file mode 100644 index 00000000..4890412e --- /dev/null +++ b/weld/src/main/java/org/jboss/weld/experimental/Prioritized.java @@ -0,0 +1,32 @@ +/* + * 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.experimental; + +/** + * This API is experimental and will change! + * + * An optional interface - we don't want to force users to specify priority if not needed. + */ +public interface Prioritized { + + /** + * + * @return the priority + */ + int getPriority(); + +} \ No newline at end of file diff --git a/weld/src/main/java/org/jboss/weld/experimental/Priority.java b/weld/src/main/java/org/jboss/weld/experimental/Priority.java new file mode 100644 index 00000000..9900ff01 --- /dev/null +++ b/weld/src/main/java/org/jboss/weld/experimental/Priority.java @@ -0,0 +1,27 @@ +package org.jboss.weld.experimental; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * This API is experimental and will change! + * + * This annotation duplicates {@link javax.annotation.Priority}. Unlike {@link javax.annotation.Priority}, this annotation can also be applied on parameters + * which makes it usable for specifying the priority of observer methods. + * + * In the future, {@link javax.annotation.Priority} will be updated and this annotation will be removed. + * + * @author Jozef Hartinger + * + */ +@Target({ TYPE, PARAMETER }) +@Retention(RUNTIME) +@Documented +public @interface Priority { + int value(); +} \ No newline at end of file diff --git a/weld/src/main/java/org/jboss/weld/experimental/util/ForwardingExperimentalObserverMethod.java b/weld/src/main/java/org/jboss/weld/experimental/util/ForwardingExperimentalObserverMethod.java new file mode 100644 index 00000000..2125532d --- /dev/null +++ b/weld/src/main/java/org/jboss/weld/experimental/util/ForwardingExperimentalObserverMethod.java @@ -0,0 +1,105 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2014, 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.experimental.util; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Set; + +import javax.enterprise.event.Reception; +import javax.enterprise.event.TransactionPhase; +import javax.enterprise.inject.spi.ObserverMethod; + +import org.jboss.weld.experimental.ExperimentalObserverMethod; + +/** + * This API is experimental and will change! + * + * This is a forwarding implementation of {@link ExperimentalObserverMethod}. + * + * @author Jozef Hartinger + * + */ +public class ForwardingExperimentalObserverMethod implements ExperimentalObserverMethod { + + private final ObserverMethod delegate; + + public ForwardingExperimentalObserverMethod(ObserverMethod delegate) { + this.delegate = delegate; + } + + protected ObserverMethod delegate() { + return delegate; + } + + @Override + public Class getBeanClass() { + return delegate().getBeanClass(); + } + + @Override + public Type getObservedType() { + return delegate().getObservedType(); + } + + @Override + public Set getObservedQualifiers() { + return delegate().getObservedQualifiers(); + } + + @Override + public Reception getReception() { + return delegate().getReception(); + } + + @Override + public TransactionPhase getTransactionPhase() { + return delegate().getTransactionPhase(); + } + + @Override + public void notify(T event) { + delegate().notify(event); + } + + @Override + public int getPriority() { + if (delegate() instanceof ExperimentalObserverMethod) { + return ((ExperimentalObserverMethod) delegate()).getPriority(); + } + return ExperimentalObserverMethod.DEFAULT_PRIORITY; + } + + @Override + public int hashCode() { + return delegate().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof ForwardingExperimentalObserverMethod) { + obj = ((ForwardingExperimentalObserverMethod) obj).delegate(); + } + return delegate().equals(obj); + } + + @Override + public String toString() { + return delegate().toString(); + } + +}