Skip to content

Commit

Permalink
support for @obtainable Instance, w/o tests
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@464 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
Gavin King authored and gavin.king@gmail.com committed Dec 8, 2008
1 parent dee0a5f commit 5e548b2
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 189 deletions.
79 changes: 79 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/FacadeImpl.java
@@ -0,0 +1,79 @@
package org.jboss.webbeans;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Observable;

import org.jboss.webbeans.util.Reflections;

public class FacadeImpl<T> {

protected final Set<? extends Annotation> bindingTypes;
protected final ManagerImpl manager;
protected final Class<T> type;

/**
* Validates the binding types
*
* Removes @Observable from the list
*
* @param annotations The annotations to validate
* @return A set of binding type annotations (minus @Observable, if it was
* present)
*/
protected static Set<Annotation> getBindingTypes(Annotation... annotations) {
Set<Annotation> result = new HashSet<Annotation>();
for (Annotation annotation : annotations)
{
if (!Reflections.isBindingType(annotation))
{
throw new IllegalArgumentException(annotation + " is not a binding type");
}
if (!annotation.annotationType().equals(Observable.class))
{
result.add(annotation);
}
}
return result;
}

protected FacadeImpl(ManagerImpl manager, Class<T> eventType, Annotation... bindingTypes) {
this.manager = manager;
this.type = eventType;
this.bindingTypes = getBindingTypes(bindingTypes);
}

/**
* Validates the binding types and checks for duplicates among the annotations.
*
* @param annotations The annotations to validate
* @return A set of unique binding type annotations
*/
protected Set<Annotation> checkBindingTypes(Annotation... annotations) {
Set<Annotation> result = new HashSet<Annotation>();
for (Annotation annotation : annotations)
{
if (!Reflections.isBindingType(annotation))
{
throw new IllegalArgumentException(annotation + " is not a binding type for " + this);
}
if (result.contains(annotation) || this.bindingTypes.contains(annotation))
{
throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list for " + this);
}
result.add(annotation);
}
return result;
}

protected Annotation[] mergeBindings(Annotation... bindingTypes) {
Set<Annotation> bindingParameters = checkBindingTypes(bindingTypes);
bindingParameters.addAll(this.bindingTypes);
Annotation[] bindings = bindingParameters.toArray(new Annotation[0]);
return bindings;
}

}
60 changes: 60 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/InstanceImpl.java
@@ -0,0 +1,60 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, 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.webbeans;

import java.lang.annotation.Annotation;

import javax.webbeans.Instance;

import org.jboss.webbeans.util.Strings;

/**
* Implementation of the Event interface
*
* @author David Allen
*
* @param <T>
* @see javax.webbeans.Event
*/
public class InstanceImpl<T> extends FacadeImpl<T> implements Instance<T>
{
/**
* Constructor
*
* @param bindingTypes The binding types
*/
public InstanceImpl(ManagerImpl manager, Class<T> type, Annotation... bindingTypes)
{
super(manager, type, bindingTypes);
}

public T get(Annotation... bindingTypes)
{
return manager.getInstanceByType(type, mergeBindings(bindingTypes));
}

@Override
public String toString()
{
StringBuilder buffer = new StringBuilder();
buffer.append("Obtainable Instance:\n");
buffer.append(Strings.collectionToString(" Bindings: ", bindingTypes));
return buffer.toString();
}

}
Expand Up @@ -26,7 +26,6 @@
import javax.webbeans.Destructor;
import javax.webbeans.Disposes;
import javax.webbeans.Initializer;
import javax.webbeans.Observable;
import javax.webbeans.Observes;
import javax.webbeans.Produces;
import javax.webbeans.Production;
Expand Down Expand Up @@ -113,17 +112,6 @@ public Set<AnnotatedField<Object>> getProducerFields()
{
return getAnnotatedItem().getAnnotatedFields(Produces.class);
}

/**
* Returns @Observer annotated fields.
*
* @return A set of observing fields. An empty set is returned if there are
* none present.
*/
public Set<AnnotatedField<Object>> getEventFields()
{
return getAnnotatedItem().getAnnotatedFields(Observable.class);
}

public Set<AnnotatedMethod<Object>> getObserverMethods()
{
Expand Down
96 changes: 4 additions & 92 deletions webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
Expand Up @@ -17,19 +17,11 @@

package org.jboss.webbeans.bean;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;

import javax.webbeans.DefinitionException;
import javax.webbeans.Dependent;
import javax.webbeans.Event;
import javax.webbeans.Production;
import javax.webbeans.Standard;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.event.EventImpl;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedItem;

/**
Expand All @@ -39,105 +31,25 @@
*
* @param <T>
*/
public class EventBean<T> extends AbstractBean<Event<T>, Field>
public class EventBean<T, S> extends FacadeBean<Event<T>, S, T>
{

// The underlying annotated item
private AnnotatedField<Event<T>> annotatedItem;

/**
* Constructor
*
* @param field The underlying field abstraction
*/
@SuppressWarnings("unchecked")
public EventBean(AnnotatedField<T> field, ManagerImpl manager)
{
super(manager);
this.annotatedItem = (AnnotatedField<Event<T>>) field;
init();
}

/**
* Initializes the bean
*
* Calls super method and validates the annotated item
*/
protected void init()
{
super.init();
checkAnnotatedItem();
}

/**
* Validates the annotated item
*/
private void checkAnnotatedItem()
public EventBean(AnnotatedItem<Event<T>, S> field, ManagerImpl manager)
{
Type[] actualTypeArguments = annotatedItem.getActualTypeArguments();
if (actualTypeArguments.length != 1)
{
throw new DefinitionException("Event must have type arguments");
}
if (!(actualTypeArguments[0] instanceof Class))
{
throw new DefinitionException("Event must have concrete type argument");
}
}

@Override
protected void initScopeType()
{
this.scopeType = Dependent.class;
}

@Override
protected void initDeploymentType()
{
this.deploymentType = Standard.class;
}

@Override
protected AnnotatedItem<Event<T>, Field> getAnnotatedItem()
{
return annotatedItem;
}

@Override
protected String getDefaultName()
{
return null;
}

@Override
protected void initType()
{
try
{
if (getAnnotatedItem() != null)
{
this.type = getAnnotatedItem().getType();
}
}
catch (ClassCastException e)
{
// TODO: Expand error
throw new IllegalArgumentException("Type mismatch");
}
super(field, manager);
}

@SuppressWarnings("unchecked")
@Override
public Event<T> create()
{
Class<T> eventType = (Class<T>) annotatedItem.getType().getTypeParameters()[0].getClass();
return new EventImpl<T>(manager, eventType, annotatedItem.getBindingTypesAsArray());
}

@Override
protected Class<? extends Annotation> getDefaultDeploymentType()
{
return Production.class;
return new EventImpl<T>(manager, getTypeParameter(), getBindingTypesArray());
}

}
98 changes: 98 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/bean/FacadeBean.java
@@ -0,0 +1,98 @@
package org.jboss.webbeans.bean;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.webbeans.DefinitionException;
import javax.webbeans.Dependent;
import javax.webbeans.Production;
import javax.webbeans.Standard;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.introspector.AnnotatedItem;

public abstract class FacadeBean<T, S, P> extends AbstractBean<T, S> {

protected AnnotatedItem<T, S> annotatedItem;

public FacadeBean(AnnotatedItem<T, S> field, ManagerImpl manager) {
super(manager);
this.annotatedItem = field;
init();
}

/**
* Initializes the bean
*
* Calls super method and validates the annotated item
*/
protected void init() {
super.init();
checkAnnotatedItem();
}

/**
* Validates the annotated item
*/
private void checkAnnotatedItem() {
Type[] actualTypeArguments = annotatedItem.getActualTypeArguments();
if (actualTypeArguments.length != 1)
{
throw new DefinitionException("Event must have type arguments");
}
if (!(actualTypeArguments[0] instanceof Class))
{
throw new DefinitionException("Event must have concrete type argument");
}
}

protected Annotation[] getBindingTypesArray() {
return annotatedItem.getBindingTypesAsArray();
}

protected Class<P> getTypeParameter() {
return (Class<P>) annotatedItem.getType().getTypeParameters()[0].getClass();
}

@Override
protected void initScopeType() {
this.scopeType = Dependent.class;
}

@Override
protected void initDeploymentType() {
this.deploymentType = Standard.class;
}

@Override
protected AnnotatedItem<T, S> getAnnotatedItem() {
return annotatedItem;
}

@Override
protected String getDefaultName() {
return null;
}

@Override
protected void initType() {
try
{
if (getAnnotatedItem() != null)
{
this.type = getAnnotatedItem().getType();
}
}
catch (ClassCastException e)
{
// TODO: Expand error
throw new IllegalArgumentException("Type mismatch");
}
}

@Override
protected Class<? extends Annotation> getDefaultDeploymentType() {
return Production.class;
}

}

0 comments on commit 5e548b2

Please sign in to comment.