Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for @obtainable Instance, w/o tests
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
Showing
10 changed files
with
354 additions
and
189 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
webbeans-ri/src/main/java/org/jboss/webbeans/FacadeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
60
webbeans-ri/src/main/java/org/jboss/webbeans/InstanceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
webbeans-ri/src/main/java/org/jboss/webbeans/bean/FacadeBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
Oops, something went wrong.