Skip to content

Commit

Permalink
convert producer method tests to @artifact (one broken) and fix a bug…
Browse files Browse the repository at this point in the history
… in the RI reflection layer

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1616 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Feb 20, 2009
1 parent d73bb8b commit 676ea96
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 100 deletions.
Expand Up @@ -158,8 +158,7 @@ protected void init()
*/
protected void initTypes()
{
types = new HashSet<Type>();
Reflections.getTypeHierachy(getType(), types);
types = new Reflections.HierarchyDiscovery<Type>(getAnnotatedItem().getUnderlyingType()).getFlattenedTypes();
}

/**
Expand Down
Expand Up @@ -116,10 +116,7 @@ protected void initType()
{
try
{
if (getAnnotatedItem() != null)
{
this.type = getAnnotatedItem().getType();
}
this.type = getAnnotatedItem().getType();
}
catch (ClassCastException e)
{
Expand Down
Expand Up @@ -126,6 +126,8 @@ public interface AnnotatedItem<T, S>
* @return The type of the element
*/
public Class<T> getType();

public Type getUnderlyingType();

/**
* Extends Java Class assignability such that actual type parameters are also
Expand Down
Expand Up @@ -118,6 +118,11 @@ public Class<T> getType()
{
return delegate().getType();
}

public Type getUnderlyingType()
{
return delegate().getUnderlyingType();
}

/**
* @see org.jboss.webbeans.introspector.AnnotatedItem
Expand Down
Expand Up @@ -18,6 +18,7 @@
package org.jboss.webbeans.introspector.jlr;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Set;
Expand Down Expand Up @@ -52,7 +53,10 @@ interface WrappableAnnotatedItem<T, S> extends AnnotatedItem<T, S>

}


public Type getUnderlyingType()
{
return getType();
}

// Cached string representation
private String toString;
Expand Down Expand Up @@ -166,9 +170,16 @@ public boolean isAssignableFrom(Set<Type> types)
return true;
}
}
else
else if (type instanceof ParameterizedType)
{

ParameterizedType parameterizedType = (ParameterizedType) type;
if (parameterizedType.getRawType() instanceof Class)
{
if (isAssignableFrom((Class<?>) parameterizedType.getRawType(), parameterizedType.getActualTypeArguments()))
{
return true;
}
}
}
}
return false;
Expand Down
Expand Up @@ -48,6 +48,8 @@ public class AnnotatedMethodImpl<T> extends AbstractAnnotatedMember<T, Method> i

// The actual type arguments
private final Type[] actualTypeArguments;
private final Type underlyingType;
private final Class<T> type;
// The underlying method
private final Method method;

Expand Down Expand Up @@ -87,12 +89,15 @@ protected AnnotatedMethodImpl(Method method, AnnotatedType<?> declaringClass)
this.method = method;
this.method.setAccessible(true);
this.declaringClass = declaringClass;
this.type = (Class<T>) method.getReturnType();
if (method.getGenericReturnType() instanceof ParameterizedType)
{
this.actualTypeArguments = ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments();
this.underlyingType = method.getGenericReturnType();
this.actualTypeArguments = ((ParameterizedType) underlyingType).getActualTypeArguments();
}
else
{
this.underlyingType = type;
this.actualTypeArguments = new Type[0];
}

Expand Down Expand Up @@ -132,58 +137,33 @@ protected AnnotatedMethodImpl(Method method, AnnotatedType<?> declaringClass)
}
}

/**
* Gets the annotated method
*
* @return The method
*/
public Method getAnnotatedMethod()
{
return method;
}

/**
* Gets the delegate
*
* @return The delegate
*/
public Method getDelegate()
{
return method;
}

/**
* Gets the type of the method
*
* @return The return type of the method
*/
@SuppressWarnings("unchecked")
public Class<T> getType()
{
return (Class<T>) method.getReturnType();
return type;
}

@Override
public Type getUnderlyingType()
{
return underlyingType;
}

/**
* Gets the actual type arguments
*
* @return The actual type arguments
*
* @see org.jboss.webbeans.introspector.AnnotatedMethod#getActualTypeArguments()
*/
public Type[] getActualTypeArguments()
{
return actualTypeArguments;
}

/**
* Gets the annotated parameters
*
* If the parameters are null, they are initialized first
*
* @return The annotated parameters
*
* @see org.jboss.webbeans.introspector.AnnotatedMethod#getParameters()
*/
public List<AnnotatedParameter<?>> getParameters()
{
return Collections.unmodifiableList(parameters);
Expand All @@ -194,25 +174,11 @@ public Class<?>[] getParameterTypesAsArray()
return method.getParameterTypes();
}

/**
* Gets the parameter abstractions with a given annotation type
*
* If the parameter abstractions are null, they are initialized first
*
* @param annotationType The annotation type to match
* @return The list of abstracted parameters with given annotation type
* present. An empty list is returned if there are no matches
*/
public List<AnnotatedParameter<?>> getAnnotatedParameters(Class<? extends Annotation> annotationType)
{
return Collections.unmodifiableList(annotatedParameters.get(annotationType));
}

/**
* Compares two annotated methods (delegates)
*
* @return True if equals, false otherwise
*/
@Override
public boolean equals(Object other)
{
Expand Down Expand Up @@ -252,35 +218,16 @@ public T invoke(Object instance, Object... parameters) throws IllegalArgumentExc
return result;
}

/**
* Gets the name of the property
*
* @return The name
*
* @see org.jboss.webbeans.introspector.AnnotatedMethod#getPropertyName()
*/
public String getPropertyName()
{
return propertyName;
}

/**
* Gets the declaring class
*
* @return The declaring class
*
* @see org.jboss.webbeans.introspector.AnnotatedMethod#getDeclaringClass()
*/
public AnnotatedType<?> getDeclaringClass()
{
return declaringClass;
}

/**
* Gets a string representation of the method
*
* @return A string representation
*/
@Override
public String toString()
{
Expand Down
19 changes: 18 additions & 1 deletion webbeans-ri/src/main/java/org/jboss/webbeans/util/Proxies.java
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.webbeans.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -142,7 +143,23 @@ public static ProxyFactory getProxyFactory(Set<Type> types)
*/
public static boolean isTypeProxyable(Type type)
{
Class<?> clazz = (Class<?>) type;
if (type instanceof Class)
{
return isClassProxyable((Class<?>) type);
}
else if (type instanceof ParameterizedType)
{
Type rawType = ((ParameterizedType) type).getRawType();
if (rawType instanceof Class)
{
return isClassProxyable((Class<?>) rawType);
}
}
return false;
}

private static boolean isClassProxyable(Class<?> clazz)
{
if (clazz.isInterface())
{
return true;
Expand Down
87 changes: 64 additions & 23 deletions webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.java
Expand Up @@ -43,6 +43,66 @@
*/
public class Reflections
{

public static class HierarchyDiscovery<T extends Type>
{

private final T type;

private Set<T> types;

public HierarchyDiscovery(T type)
{
this.type = type;
}

protected void add(T type)
{
types.add(type);
}

public Set<T> getFlattenedTypes()
{
if (types == null)
{
this.types = new HashSet<T>();
discoverTypes(type);
}
return types;
}


private void discoverTypes(T type)
{
if (type != null)
{
add(type);
if (type instanceof Class)
{
discoverFromClass((Class<?>) type);
}
else if (type instanceof ParameterizedType)
{
Type rawType = ((ParameterizedType) type).getRawType();
if (rawType instanceof Class)
{
discoverFromClass((Class<?>) rawType);
}
}
}
}

@SuppressWarnings("unchecked")
private void discoverFromClass(Class<?> clazz)
{
discoverTypes((T) clazz.getSuperclass());
for (Class<?> c : clazz.getInterfaces())
{
discoverTypes((T) c);
}
}

}

/**
* Gets the property name from a getter method
Expand Down Expand Up @@ -336,35 +396,16 @@ public static boolean isProxy(Object instance)
* reached. For each steps, adds all interfaces of the class to the set.
* Since the data structure is a set, duplications are eliminated
*
*
* @deprecated see {@link HierarchyDiscovery}
* @param clazz The class to examine
* @return The set of classes and interfaces in the hierarchy
* @see #getTypeHierachy(Class, Set)
*/
@Deprecated
public static Set<Class<?>> getTypeHierachy(Class<?> clazz)
{
Set<Class<?>> classes = new HashSet<Class<?>>();
getTypeHierachy(clazz, classes);
return classes;
}

/**
* Gets the flattened type hierarchy for a class, including all super classes
* and the entire interface type hierarchy
*
* @param clazz the class to examine
* @param classes the set of types
*/
public static void getTypeHierachy(Class<?> clazz, Set<? super Class<?>> classes)
{
if (clazz != null)
{
classes.add(clazz);
getTypeHierachy(clazz.getSuperclass(), classes);
for (Class<?> c : clazz.getInterfaces())
{
getTypeHierachy(c, classes);
}
}
return new HierarchyDiscovery<Class<?>>(clazz).getFlattenedTypes();
}

/**
Expand Down
Expand Up @@ -8,7 +8,7 @@
import org.jboss.webbeans.test.unit.AbstractTest;
import org.testng.annotations.Test;

public class Tests extends AbstractTest
public class ExampleTest extends AbstractTest
{
@Test
public void testGameGenerator() throws Exception {
Expand Down

0 comments on commit 676ea96

Please sign in to comment.