Author: pete.muir(a)jboss.org
Date: 2009-10-29 15:01:24 -0400 (Thu, 29 Oct 2009)
New Revision: 4412
Added:
api/trunk/cdi/src/main/java/javax/enterprise/util/
api/trunk/cdi/src/main/java/javax/enterprise/util/AnnotationLiteral.java
api/trunk/cdi/src/main/java/javax/enterprise/util/Nonbinding.java
api/trunk/cdi/src/main/java/javax/enterprise/util/TypeLiteral.java
Removed:
api/trunk/cdi/src/main/java/javax/enterprise/inject/AnnotationLiteral.java
api/trunk/cdi/src/main/java/javax/enterprise/inject/Nonbinding.java
api/trunk/cdi/src/main/java/javax/enterprise/inject/TypeLiteral.java
Modified:
api/trunk/cdi/src/main/java/javax/enterprise/event/Event.java
api/trunk/cdi/src/main/java/javax/enterprise/inject/Instance.java
Log:
create util package
Modified: api/trunk/cdi/src/main/java/javax/enterprise/event/Event.java
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/event/Event.java 2009-10-29 18:57:24 UTC
(rev 4411)
+++ api/trunk/cdi/src/main/java/javax/enterprise/event/Event.java 2009-10-29 19:01:24 UTC
(rev 4412)
@@ -19,7 +19,7 @@
import java.lang.annotation.Annotation;
-import javax.enterprise.inject.TypeLiteral;
+import javax.enterprise.util.TypeLiteral;
/**
* <p>Allows the application to fire events of a particular type.</p>
@@ -113,7 +113,7 @@
* additional required qualifiers.</p>
*
* @param <U> the specified type
- * @param subtype a {@link javax.enterprise.inject.TypeLiteral} representing the
specified type
+ * @param subtype a {@link javax.enterprise.util.TypeLiteral} representing the
specified type
* @param qualifiers the additional specified qualifiers
* @return the child <tt>Event</tt>
* @throws IllegalArgumentException if passed two instances of the
Deleted: api/trunk/cdi/src/main/java/javax/enterprise/inject/AnnotationLiteral.java
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/inject/AnnotationLiteral.java 2009-10-29
18:57:24 UTC (rev 4411)
+++ api/trunk/cdi/src/main/java/javax/enterprise/inject/AnnotationLiteral.java 2009-10-29
19:01:24 UTC (rev 4412)
@@ -1,196 +0,0 @@
-/*
-* 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 javax.enterprise.inject;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-import java.util.Arrays;
-
-
-/**
- * <p>Supports inline instantiation of annotation type instances.</p>
- *
- * <p>An instance of an annotation type may be obtained by subclassing
- * <tt>AnnotationLiteral</tt>.</p>
- *
- * <pre>
- * public abstract class PayByQualifier
- * extends AnnotationLiteral<PayBy>
- * implements PayBy {}
- * </pre>
- *
- * <pre>
- * PayBy payby = new PayByQualifier() { public value() { return CHEQUE; } };
- * </pre>
- *
- * @author Pete Muir
- * @author Gavin King
- *
- * @param <T> the annotation type
- *
- * @see javax.enterprise.inject.Instance#select(Annotation...)
- * @see javax.enterprise.event.Event#select(Annotation...)
- *
- */
-public abstract class AnnotationLiteral<T extends Annotation> implements
- Annotation
-{
-
- private Class<T> annotationType;
- private Method[] members;
-
- protected AnnotationLiteral()
- {
- Class<?> annotationLiteralSubclass =
getAnnotationLiteralSubclass(this.getClass());
- if (annotationLiteralSubclass == null)
- {
- throw new RuntimeException(getClass() + "is not a subclass of
AnnotationLiteral ");
- }
-
- annotationType = getTypeParameter(annotationLiteralSubclass);
-
- if (annotationType == null)
- {
- throw new RuntimeException(getClass() + " is missing type parameter in
AnnotationLiteral");
- }
-
- this.members = annotationType.getDeclaredMethods();
- }
-
- private static Class<?> getAnnotationLiteralSubclass(Class<?> clazz)
- {
- Class<?> superclass = clazz.getSuperclass();
- if (superclass.equals(AnnotationLiteral.class))
- {
- return clazz;
- }
- else if (superclass.equals(Object.class))
- {
- return null;
- }
- else
- {
- return (getAnnotationLiteralSubclass(superclass));
- }
- }
-
- @SuppressWarnings("unchecked")
- private static <T> Class<T> getTypeParameter(Class<?>
annotationLiteralSuperclass)
- {
- Type type = annotationLiteralSuperclass.getGenericSuperclass();
- if (type instanceof ParameterizedType)
- {
- ParameterizedType parameterizedType = (ParameterizedType) type;
- if (parameterizedType.getActualTypeArguments().length == 1)
- {
- return (Class<T>) parameterizedType
- .getActualTypeArguments()[0];
- }
- }
- return null;
- }
-
- public Class<? extends Annotation> annotationType()
- {
- return annotationType;
- }
-
- @Override
- public String toString()
- {
- String string = "@" + annotationType().getName() + "(";
- for (int i = 0; i < members.length; i++)
- {
- string += members[i].getName() + "=";
- string += invoke(members[i], this);
- if (i < members.length - 1)
- {
- string += ",";
- }
- }
- return string + ")";
- }
-
- @Override
- public boolean equals(Object other)
- {
- if (other instanceof Annotation)
- {
- Annotation that = (Annotation) other;
- if (this.annotationType().equals(that.annotationType()))
- {
- for (Method member : members)
- {
- Object thisValue = invoke(member, this);
- Object thatValue = invoke(member, that);
- if (thisValue.getClass().isArray() &&
thatValue.getClass().isArray())
- {
- if (!Arrays.equals(Object[].class.cast(thisValue),
Object[].class.cast(thatValue)))
- {
- return false;
- }
- }
- else if (!thisValue.equals(thatValue))
- {
- return false;
- }
- }
- return true;
- }
- }
- return false;
- }
-
- @Override
- public int hashCode()
- {
- int hashCode = 0;
- for (Method member : members)
- {
- int memberNameHashCode = 127 * member.getName().hashCode();
- Object value = invoke(member, this);
- int memberValueHashCode = value.getClass().isArray() ?
Arrays.hashCode(Object[].class.cast(value)) : value.hashCode();
- hashCode += memberNameHashCode ^ memberValueHashCode;
- }
- return hashCode;
- }
-
- private static Object invoke(Method method, Object instance)
- {
- try
- {
- method.setAccessible(true);
- return method.invoke(instance);
- }
- catch (IllegalArgumentException e)
- {
- throw new RuntimeException("Error checking value of member method " +
method.getName() + " on " + method.getDeclaringClass(), e);
- }
- catch (IllegalAccessException e)
- {
- throw new RuntimeException("Error checking value of member method " +
method.getName() + " on " + method.getDeclaringClass(), e);
- }
- catch (InvocationTargetException e)
- {
- throw new RuntimeException("Error checking value of member method " +
method.getName() + " on " + method.getDeclaringClass(), e);
- }
- }
-}
Modified: api/trunk/cdi/src/main/java/javax/enterprise/inject/Instance.java
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/inject/Instance.java 2009-10-29 18:57:24
UTC (rev 4411)
+++ api/trunk/cdi/src/main/java/javax/enterprise/inject/Instance.java 2009-10-29 19:01:24
UTC (rev 4412)
@@ -19,6 +19,7 @@
import java.lang.annotation.Annotation;
+import javax.enterprise.util.TypeLiteral;
import javax.inject.Provider;
@@ -95,8 +96,8 @@
*
* @see javax.inject.Provider#get()
* @see java.lang.Iterable#iterator()
- * @see javax.enterprise.inject.AnnotationLiteral
- * @see javax.enterprise.inject.TypeLiteral
+ * @see javax.enterprise.util.AnnotationLiteral
+ * @see javax.enterprise.util.TypeLiteral
*
* @author Gavin King
*
@@ -137,7 +138,7 @@
* additional required qualifiers.</p>
*
* @param <U> the required type
- * @param subtype a {@link javax.enterprise.inject.TypeLiteral} representing the
required type
+ * @param subtype a {@link javax.enterprise.util.TypeLiteral} representing the
required type
* @param qualifiers the additional required qualifiers
* @return the child <tt>Instance</tt>
* @throws IllegalArgumentException if passed two instances of the
Deleted: api/trunk/cdi/src/main/java/javax/enterprise/inject/Nonbinding.java
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/inject/Nonbinding.java 2009-10-29
18:57:24 UTC (rev 4411)
+++ api/trunk/cdi/src/main/java/javax/enterprise/inject/Nonbinding.java 2009-10-29
19:01:24 UTC (rev 4412)
@@ -1,53 +0,0 @@
-/*
- * 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 javax.enterprise.inject;
-
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-/**
- * <p>Excludes a member of an annotation type (such as a
- * {@linkplain javax.inject.Qualifier qualifier type} or
- * {@linkplain javax.interceptor interceptor binding type})
- * from consideration when the container compares two
- * annotation instances.</p>
- *
- * <pre>
- * @Qualifier
- * @Retention(RUNTIME)
- * @Target({METHOD, FIELD, PARAMETER, TYPE})
- * public @interface PayBy {
- * PaymentMethod value();
- * @Nonbinding String comment();
- * }
- * </pre>
- *
- * @author Gavin King
- *
- * @see javax.inject.Qualifier @Qualifier
- * @see javax.interceptor.InterceptorBinding @InterceptorBinding
- *
- */
-@Retention(RUNTIME)
-@Target(METHOD)
-public @interface Nonbinding
-{
-}
Deleted: api/trunk/cdi/src/main/java/javax/enterprise/inject/TypeLiteral.java
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/inject/TypeLiteral.java 2009-10-29
18:57:24 UTC (rev 4411)
+++ api/trunk/cdi/src/main/java/javax/enterprise/inject/TypeLiteral.java 2009-10-29
19:01:24 UTC (rev 4412)
@@ -1,127 +0,0 @@
-/*
-* 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 javax.enterprise.inject;
-
-import java.lang.reflect.GenericArrayType;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-
-/**
- * <p>Supports inline instantiation of objects that represent parameterized types
- * with actual type parameters.</p>
- *
- * <p>An object that represents any parameterized type may be obtained by
- * subclassing <tt>TypeLiteral</tt>.</p>
- *
- * <pre>
- * TypeLiteral<List<String>> type = new
TypeLiteral<List<String>>() {};
- * </pre>
- *
- * @author Gavin King
- * @author Pete Muir
- *
- * @param <T> the type, including all actual type parameters
- *
- * @see javax.enterprise.inject.Instance#select(TypeLiteral, Annotation...)
- * @see javax.enterprise.event.Event#select(TypeLiteral, Annotation...)
- *
- */
-public abstract class TypeLiteral<T>
-{
-
- private Type actualType;
-
- protected TypeLiteral()
- {
- Class<?> typeLiteralSubclass = getTypeLiteralSubclass(this.getClass());
- if (typeLiteralSubclass == null)
- {
- throw new RuntimeException(getClass() + " is not a subclass of
TypeLiteral");
- }
- actualType = getTypeParameter(typeLiteralSubclass);
- if (actualType == null)
- {
- throw new RuntimeException(getClass() + " is missing type parameter in
TypeLiteral");
- }
- }
-
- /**
- * @return the actual type represented by type literal
- */
- public final Type getType()
- {
- return actualType;
- }
-
- /**
- * @return the raw type represented by the type literal
- */
- @SuppressWarnings("unchecked")
- public final Class<T> getRawType() {
- Type type = getType();
- if (type instanceof Class)
- {
- return (Class<T>) type;
- }
- else if (type instanceof ParameterizedType)
- {
- return (Class<T>) ((ParameterizedType) type).getRawType();
- }
- else if (type instanceof GenericArrayType)
- {
- return (Class<T>) Object[].class;
- }
- else
- {
- throw new RuntimeException("Illegal type");
- }
- }
-
- private static Class<?> getTypeLiteralSubclass(Class<?> clazz)
- {
- Class<?> superclass = clazz.getSuperclass();
- if (superclass.equals(TypeLiteral.class))
- {
- return clazz;
- }
- else if (superclass.equals(Object.class))
- {
- return null;
- }
- else
- {
- return (getTypeLiteralSubclass(superclass));
- }
- }
-
- private static Type getTypeParameter(Class<?> superclass)
- {
- Type type = superclass.getGenericSuperclass();
- if (type instanceof ParameterizedType)
- {
- ParameterizedType parameterizedType = (ParameterizedType) type;
- if (parameterizedType.getActualTypeArguments().length == 1)
- {
- return parameterizedType.getActualTypeArguments()[0];
- }
- }
- return null;
- }
-
- // TODO: equals(), hashCode()
-}
Copied: api/trunk/cdi/src/main/java/javax/enterprise/util/AnnotationLiteral.java (from rev
4406, api/trunk/cdi/src/main/java/javax/enterprise/inject/AnnotationLiteral.java)
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/util/AnnotationLiteral.java
(rev 0)
+++ api/trunk/cdi/src/main/java/javax/enterprise/util/AnnotationLiteral.java 2009-10-29
19:01:24 UTC (rev 4412)
@@ -0,0 +1,198 @@
+/*
+* 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 javax.enterprise.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Arrays;
+
+import javax.enterprise.inject.Instance;
+
+
+/**
+ * <p>Supports inline instantiation of annotation type instances.</p>
+ *
+ * <p>An instance of an annotation type may be obtained by subclassing
+ * <tt>AnnotationLiteral</tt>.</p>
+ *
+ * <pre>
+ * public abstract class PayByQualifier
+ * extends AnnotationLiteral<PayBy>
+ * implements PayBy {}
+ * </pre>
+ *
+ * <pre>
+ * PayBy payby = new PayByQualifier() { public value() { return CHEQUE; } };
+ * </pre>
+ *
+ * @author Pete Muir
+ * @author Gavin King
+ *
+ * @param <T> the annotation type
+ *
+ * @see javax.enterprise.inject.Instance#select(Annotation...)
+ * @see javax.enterprise.event.Event#select(Annotation...)
+ *
+ */
+public abstract class AnnotationLiteral<T extends Annotation> implements
+ Annotation
+{
+
+ private Class<T> annotationType;
+ private Method[] members;
+
+ protected AnnotationLiteral()
+ {
+ Class<?> annotationLiteralSubclass =
getAnnotationLiteralSubclass(this.getClass());
+ if (annotationLiteralSubclass == null)
+ {
+ throw new RuntimeException(getClass() + "is not a subclass of
AnnotationLiteral ");
+ }
+
+ annotationType = getTypeParameter(annotationLiteralSubclass);
+
+ if (annotationType == null)
+ {
+ throw new RuntimeException(getClass() + " is missing type parameter in
AnnotationLiteral");
+ }
+
+ this.members = annotationType.getDeclaredMethods();
+ }
+
+ private static Class<?> getAnnotationLiteralSubclass(Class<?> clazz)
+ {
+ Class<?> superclass = clazz.getSuperclass();
+ if (superclass.equals(AnnotationLiteral.class))
+ {
+ return clazz;
+ }
+ else if (superclass.equals(Object.class))
+ {
+ return null;
+ }
+ else
+ {
+ return (getAnnotationLiteralSubclass(superclass));
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T> Class<T> getTypeParameter(Class<?>
annotationLiteralSuperclass)
+ {
+ Type type = annotationLiteralSuperclass.getGenericSuperclass();
+ if (type instanceof ParameterizedType)
+ {
+ ParameterizedType parameterizedType = (ParameterizedType) type;
+ if (parameterizedType.getActualTypeArguments().length == 1)
+ {
+ return (Class<T>) parameterizedType
+ .getActualTypeArguments()[0];
+ }
+ }
+ return null;
+ }
+
+ public Class<? extends Annotation> annotationType()
+ {
+ return annotationType;
+ }
+
+ @Override
+ public String toString()
+ {
+ String string = "@" + annotationType().getName() + "(";
+ for (int i = 0; i < members.length; i++)
+ {
+ string += members[i].getName() + "=";
+ string += invoke(members[i], this);
+ if (i < members.length - 1)
+ {
+ string += ",";
+ }
+ }
+ return string + ")";
+ }
+
+ @Override
+ public boolean equals(Object other)
+ {
+ if (other instanceof Annotation)
+ {
+ Annotation that = (Annotation) other;
+ if (this.annotationType().equals(that.annotationType()))
+ {
+ for (Method member : members)
+ {
+ Object thisValue = invoke(member, this);
+ Object thatValue = invoke(member, that);
+ if (thisValue.getClass().isArray() &&
thatValue.getClass().isArray())
+ {
+ if (!Arrays.equals(Object[].class.cast(thisValue),
Object[].class.cast(thatValue)))
+ {
+ return false;
+ }
+ }
+ else if (!thisValue.equals(thatValue))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int hashCode = 0;
+ for (Method member : members)
+ {
+ int memberNameHashCode = 127 * member.getName().hashCode();
+ Object value = invoke(member, this);
+ int memberValueHashCode = value.getClass().isArray() ?
Arrays.hashCode(Object[].class.cast(value)) : value.hashCode();
+ hashCode += memberNameHashCode ^ memberValueHashCode;
+ }
+ return hashCode;
+ }
+
+ private static Object invoke(Method method, Object instance)
+ {
+ try
+ {
+ method.setAccessible(true);
+ return method.invoke(instance);
+ }
+ catch (IllegalArgumentException e)
+ {
+ throw new RuntimeException("Error checking value of member method " +
method.getName() + " on " + method.getDeclaringClass(), e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new RuntimeException("Error checking value of member method " +
method.getName() + " on " + method.getDeclaringClass(), e);
+ }
+ catch (InvocationTargetException e)
+ {
+ throw new RuntimeException("Error checking value of member method " +
method.getName() + " on " + method.getDeclaringClass(), e);
+ }
+ }
+}
Property changes on:
api/trunk/cdi/src/main/java/javax/enterprise/util/AnnotationLiteral.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied: api/trunk/cdi/src/main/java/javax/enterprise/util/Nonbinding.java (from rev 4406,
api/trunk/cdi/src/main/java/javax/enterprise/inject/Nonbinding.java)
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/util/Nonbinding.java
(rev 0)
+++ api/trunk/cdi/src/main/java/javax/enterprise/util/Nonbinding.java 2009-10-29 19:01:24
UTC (rev 4412)
@@ -0,0 +1,53 @@
+/*
+ * 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 javax.enterprise.util;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * <p>Excludes a member of an annotation type (such as a
+ * {@linkplain javax.inject.Qualifier qualifier type} or
+ * {@linkplain javax.interceptor interceptor binding type})
+ * from consideration when the container compares two
+ * annotation instances.</p>
+ *
+ * <pre>
+ * @Qualifier
+ * @Retention(RUNTIME)
+ * @Target({METHOD, FIELD, PARAMETER, TYPE})
+ * public @interface PayBy {
+ * PaymentMethod value();
+ * @Nonbinding String comment();
+ * }
+ * </pre>
+ *
+ * @author Gavin King
+ *
+ * @see javax.inject.Qualifier @Qualifier
+ * @see javax.interceptor.InterceptorBinding @InterceptorBinding
+ *
+ */
+@Retention(RUNTIME)
+@Target(METHOD)
+public @interface Nonbinding
+{
+}
Property changes on: api/trunk/cdi/src/main/java/javax/enterprise/util/Nonbinding.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied: api/trunk/cdi/src/main/java/javax/enterprise/util/TypeLiteral.java (from rev 4406,
api/trunk/cdi/src/main/java/javax/enterprise/inject/TypeLiteral.java)
===================================================================
--- api/trunk/cdi/src/main/java/javax/enterprise/util/TypeLiteral.java
(rev 0)
+++ api/trunk/cdi/src/main/java/javax/enterprise/util/TypeLiteral.java 2009-10-29 19:01:24
UTC (rev 4412)
@@ -0,0 +1,129 @@
+/*
+* 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 javax.enterprise.util;
+
+import java.lang.reflect.GenericArrayType;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+import javax.enterprise.inject.Instance;
+
+/**
+ * <p>Supports inline instantiation of objects that represent parameterized types
+ * with actual type parameters.</p>
+ *
+ * <p>An object that represents any parameterized type may be obtained by
+ * subclassing <tt>TypeLiteral</tt>.</p>
+ *
+ * <pre>
+ * TypeLiteral<List<String>> type = new
TypeLiteral<List<String>>() {};
+ * </pre>
+ *
+ * @author Gavin King
+ * @author Pete Muir
+ *
+ * @param <T> the type, including all actual type parameters
+ *
+ * @see javax.enterprise.inject.Instance#select(TypeLiteral, Annotation...)
+ * @see javax.enterprise.event.Event#select(TypeLiteral, Annotation...)
+ *
+ */
+public abstract class TypeLiteral<T>
+{
+
+ private Type actualType;
+
+ protected TypeLiteral()
+ {
+ Class<?> typeLiteralSubclass = getTypeLiteralSubclass(this.getClass());
+ if (typeLiteralSubclass == null)
+ {
+ throw new RuntimeException(getClass() + " is not a subclass of
TypeLiteral");
+ }
+ actualType = getTypeParameter(typeLiteralSubclass);
+ if (actualType == null)
+ {
+ throw new RuntimeException(getClass() + " is missing type parameter in
TypeLiteral");
+ }
+ }
+
+ /**
+ * @return the actual type represented by type literal
+ */
+ public final Type getType()
+ {
+ return actualType;
+ }
+
+ /**
+ * @return the raw type represented by the type literal
+ */
+ @SuppressWarnings("unchecked")
+ public final Class<T> getRawType() {
+ Type type = getType();
+ if (type instanceof Class)
+ {
+ return (Class<T>) type;
+ }
+ else if (type instanceof ParameterizedType)
+ {
+ return (Class<T>) ((ParameterizedType) type).getRawType();
+ }
+ else if (type instanceof GenericArrayType)
+ {
+ return (Class<T>) Object[].class;
+ }
+ else
+ {
+ throw new RuntimeException("Illegal type");
+ }
+ }
+
+ private static Class<?> getTypeLiteralSubclass(Class<?> clazz)
+ {
+ Class<?> superclass = clazz.getSuperclass();
+ if (superclass.equals(TypeLiteral.class))
+ {
+ return clazz;
+ }
+ else if (superclass.equals(Object.class))
+ {
+ return null;
+ }
+ else
+ {
+ return (getTypeLiteralSubclass(superclass));
+ }
+ }
+
+ private static Type getTypeParameter(Class<?> superclass)
+ {
+ Type type = superclass.getGenericSuperclass();
+ if (type instanceof ParameterizedType)
+ {
+ ParameterizedType parameterizedType = (ParameterizedType) type;
+ if (parameterizedType.getActualTypeArguments().length == 1)
+ {
+ return parameterizedType.getActualTypeArguments()[0];
+ }
+ }
+ return null;
+ }
+
+ // TODO: equals(), hashCode()
+}
Property changes on: api/trunk/cdi/src/main/java/javax/enterprise/util/TypeLiteral.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native