openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1629505 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/util/GenericsUtil.java main/java/org/apache/webbeans/util/WebBeansUtil.java test/java/org/apache/webbeans/test/util/GenericsUtilTest.java
Date Sun, 05 Oct 2014 16:49:28 GMT
Author: rmannibucau
Date: Sun Oct  5 16:49:28 2014
New Revision: 1629505

URL: http://svn.apache.org/r1629505
Log:
OWB-1021 not perfect but at least let deltaspike starting

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/GenericsUtilTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java?rev=1629505&r1=1629504&r2=1629505&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
Sun Oct  5 16:49:28 2014
@@ -519,7 +519,7 @@ public final class GenericsUtil
      */
     public static Type resolveType(Class<?> subclass, Field field)
     {
-        return resolveType(field.getGenericType(), subclass);
+        return resolveType(field.getGenericType(), subclass, newSeenList());
     }
 
     /**
@@ -527,7 +527,7 @@ public final class GenericsUtil
      */
     public static Type resolveReturnType(Class<?> subclass, Method method)
     {
-        return resolveType(method.getGenericReturnType(), subclass);
+        return resolveType(method.getGenericReturnType(), subclass, newSeenList());
     }
 
     /**
@@ -551,10 +551,15 @@ public final class GenericsUtil
      */
     public static Type resolveType(Type type, Class<?> subclass, Member member)
     {
-        return resolveType(type, subclass);
+        return resolveType(type, subclass, newSeenList());
     }
 
-    public static Type resolveType(Type type, Type actualType)
+    public static Type resolveType(Type type, Class<?> subclass, Member member, Collection<TypeVariable<?>>
seen)
+    {
+        return resolveType(type, subclass, seen);
+    }
+
+    public static Type resolveType(Type type, Type actualType, Collection<TypeVariable<?>>
seen)
     {
         if (type instanceof Class)
         {
@@ -573,7 +578,7 @@ public final class GenericsUtil
             }
             else
             {
-                resolvedTypeArguments = resolveTypes(parameterizedType.getActualTypeArguments(),
actualType);
+                resolvedTypeArguments = resolveTypes(parameterizedType.getActualTypeArguments(),
actualType, seen);
 
             }
 
@@ -582,19 +587,19 @@ public final class GenericsUtil
         else if (type instanceof TypeVariable)
         {
             TypeVariable<?> variable = (TypeVariable<?>)type;
-            return resolveTypeVariable(variable, actualType);
+            return resolveTypeVariable(variable, actualType, seen);
         }
         else if (type instanceof WildcardType)
         {
             WildcardType wildcardType = (WildcardType)type;
-            Type[] upperBounds = resolveTypes(wildcardType.getUpperBounds(), actualType);
-            Type[] lowerBounds = resolveTypes(wildcardType.getLowerBounds(), actualType);
+            Type[] upperBounds = resolveTypes(wildcardType.getUpperBounds(), actualType,
seen);
+            Type[] lowerBounds = resolveTypes(wildcardType.getLowerBounds(), actualType,
seen);
             return new OwbWildcardTypeImpl(upperBounds, lowerBounds);
         }
         else if (type instanceof GenericArrayType)
         {
             GenericArrayType arrayType = (GenericArrayType)type;
-            return createArrayType(resolveType(arrayType.getGenericComponentType(), actualType));
+            return createArrayType(resolveType(arrayType.getGenericComponentType(), actualType,
seen));
         }
         else
         {
@@ -602,12 +607,26 @@ public final class GenericsUtil
         }
     }
     
+    public static Type[] resolveTypes(Type[] types, Type actualType, Collection<TypeVariable<?>>
seen)
+    {
+        Type[] resolvedTypeArguments = new Type[types.length];
+        for (int i = 0; i < types.length; i++)
+        {
+            final Type type = resolveType(types[i], actualType, seen);
+            if (type != null) // means a stackoverflow was avoided, just keep what we have
+            {
+                resolvedTypeArguments[i] = type;
+            }
+        }
+        return resolvedTypeArguments;
+    }
+
     public static Type[] resolveTypes(Type[] types, Type actualType)
     {
         Type[] resolvedTypeArguments = new Type[types.length];
         for (int i = 0; i < types.length; i++)
         {
-            resolvedTypeArguments[i] = resolveType(types[i], actualType);
+            resolvedTypeArguments[i] = resolveType(types[i], actualType, newSeenList());
         }
         return resolvedTypeArguments;
     }
@@ -693,7 +712,7 @@ public final class GenericsUtil
         {
            return;
         }
-        Type resolvedType = GenericsUtil.resolveType(type, actualType);
+        Type resolvedType = GenericsUtil.resolveType(type, actualType, newSeenList());
         set.add(resolvedType);
         Class<?> resolvedClass = GenericsUtil.getRawType(resolvedType, actualType);
         if (resolvedClass.getSuperclass() != null)
@@ -706,6 +725,11 @@ public final class GenericsUtil
         }
     }
 
+    private static Collection<TypeVariable<?>> newSeenList()
+    {
+        return new ArrayList<TypeVariable<?>>();
+    }
+
     public static boolean hasTypeParameters(Type type)
     {
         if (type instanceof Class)
@@ -829,7 +853,7 @@ public final class GenericsUtil
         return classTypes.toArray(new Class[classTypes.size()]);
     }
 
-    private static Type resolveTypeVariable(TypeVariable<?> variable, Type actualType)
+    private static Type resolveTypeVariable(TypeVariable<?> variable, Type actualType,
Collection<TypeVariable<?>> seen)
     {
         if (actualType == null)
         {
@@ -839,14 +863,14 @@ public final class GenericsUtil
         Class<?> actualClass = getRawType(actualType);
         if (actualClass == declaringClass)
         {
-            return resolveTypeVariable(variable, variable.getGenericDeclaration(), getParameterizedType(actualType));
+            return resolveTypeVariable(variable, variable.getGenericDeclaration(), getParameterizedType(actualType),
seen);
         }
         else if (actualClass.isAssignableFrom(declaringClass))
         {
             Class<?> directSubclass = getDirectSubclass(declaringClass, actualClass);
             Type[] typeArguments = resolveTypeArguments(directSubclass, actualType);
             Type directSubtype = new OwbParametrizedTypeImpl(directSubclass.getDeclaringClass(),
directSubclass, typeArguments);
-            return resolveTypeVariable(variable, directSubtype);
+            return resolveTypeVariable(variable, directSubtype, seen);
         }
         else // if (declaringClass.isAssignableFrom(actualClass))
         { 
@@ -867,7 +891,7 @@ public final class GenericsUtil
                 Type[] typeArguments = resolveTypeArguments(getParameterizedType(actualType),
genericSupertype);
                 genericSuperclass = new OwbParametrizedTypeImpl(genericSupertype.getOwnerType(),
genericSupertype.getRawType(), typeArguments);
             }
-            Type resolvedType = resolveTypeVariable(variable, genericSuperclass);
+            Type resolvedType = resolveTypeVariable(variable, genericSuperclass, seen);
             if (resolvedType instanceof TypeVariable)
             {
                 TypeVariable<?> resolvedTypeVariable = (TypeVariable<?>)resolvedType;
@@ -901,7 +925,8 @@ public final class GenericsUtil
         }
     }
 
-    private static Type resolveTypeVariable(TypeVariable<?> variable, GenericDeclaration
declaration, ParameterizedType type)
+    private static Type resolveTypeVariable(TypeVariable<?> variable, GenericDeclaration
declaration, ParameterizedType type,
+                                            Collection<TypeVariable<?>> seen)
     {
         int index = getIndex(declaration, variable);
         if (declaration instanceof Class)
@@ -921,7 +946,13 @@ public final class GenericsUtil
         }
         else
         {
-            Type[] resolvedBounds = resolveTypes(declaration.getTypeParameters()[index].getBounds(),
type);
+            if (seen.contains(variable))
+            {
+                return null;
+            }
+            seen.add(variable);
+
+            Type[] resolvedBounds = resolveTypes(declaration.getTypeParameters()[index].getBounds(),
type, seen);
             return OwbTypeVariableImpl.createTypeVariable(variable, resolvedBounds);
         }
         return variable;
@@ -1087,4 +1118,9 @@ public final class GenericsUtil
             return new OwbGenericArrayTypeImpl(componentType);
         }
     }
+
+    public static Type resolveType(ParameterizedType parameterizedType, Type metadataType)
+    {
+        return resolveType(parameterizedType, metadataType, newSeenList());
+    }
 }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=1629505&r1=1629504&r2=1629505&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
Sun Oct  5 16:49:28 2014
@@ -56,13 +56,11 @@ import org.apache.webbeans.config.OwbWil
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.container.AnnotatedTypeWrapper;
 import org.apache.webbeans.container.BeanManagerImpl;
-import org.apache.webbeans.container.ExternalScope;
 import org.apache.webbeans.container.InjectionResolver;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 
 
 import org.apache.webbeans.inject.AlternativesManager;
-import org.apache.webbeans.logger.WebBeansLoggerFacade;
 import org.apache.webbeans.plugins.PluginLoader;
 import org.apache.webbeans.portable.AbstractProducer;
 import org.apache.webbeans.portable.InjectionTargetImpl;
@@ -85,7 +83,6 @@ import org.apache.webbeans.spi.plugins.O
 
 import javax.decorator.Decorator;
 import javax.enterprise.context.Dependent;
-import javax.enterprise.context.NormalScope;
 import javax.enterprise.context.spi.Contextual;
 import javax.enterprise.inject.Alternative;
 import javax.enterprise.inject.Decorated;
@@ -150,7 +147,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-import java.util.logging.Logger;
 
 /**
  * Contains some utility methods used in the all project.
@@ -158,8 +154,6 @@ import java.util.logging.Logger;
 @SuppressWarnings("unchecked")
 public final class WebBeansUtil
 {
-    private static final Logger logger = WebBeansLoggerFacade.getLogger(WebBeansUtil.class);
-
     private final WebBeansContext webBeansContext;
 
     // cache to skip some validations
@@ -414,33 +408,6 @@ public final class WebBeansUtil
         return false;
     }
 
-    private boolean isNormalScoped(final AnnotatedType<?> type)
-    {
-        final Set<Annotation> annotations = type.getAnnotations();
-        BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
-        if (annotations != null)
-        {
-            for (final Annotation a : annotations)
-            {
-                if (AnnotationUtil.hasMetaAnnotation(a.annotationType().getAnnotations(),
NormalScope.class)
-                    || AnnotationUtil.hasAnnotation(a.annotationType().getAnnotations(),
NormalScope.class))
-                {
-                    return true;
-                }
-                for (ExternalScope externalScope : beanManager.getAdditionalScopes())
-                {
-                    if (externalScope.isNormal() && externalScope.getScope().equals(a))
-                    {
-                        return true;
-                    }
-                }
-            }
-        }
-
-        return false;
-    }
-
-
     public <T> Bean<T> createNewComponent(Class<T> type)
     {
         Asserts.nullCheckForClass(type);

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/GenericsUtilTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/GenericsUtilTest.java?rev=1629505&r1=1629504&r2=1629505&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/GenericsUtilTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/GenericsUtilTest.java
Sun Oct  5 16:49:28 2014
@@ -18,21 +18,35 @@
  */
 package org.apache.webbeans.test.util;
 
+import static java.util.Arrays.asList;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.GenericArrayType;
+import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Set;
 
+import org.apache.webbeans.config.OwbParametrizedTypeImpl;
 import org.apache.webbeans.util.GenericsUtil;
 import org.junit.Assert;
 import org.junit.Test;
 
 public class GenericsUtilTest {
+    @Test
+    public void stackOverFlowProtection() throws Exception
+    {
+        final Type type = new OwbParametrizedTypeImpl(null, ByDefaultIllDoAStackOverFlowIngenericUtils.class,
ByDefaultIllDoAStackOverFlowIngenericUtils.class.getTypeParameters());
+        final Collection<Method> methods = new ArrayList<Method>(asList(ByDefaultIllDoAStackOverFlowIngenericUtils.class.getMethods()));
+        methods.removeAll(asList(Object.class.getMethods()));
+        final Type actualType = methods.iterator().next().getTypeParameters()[0];
+        GenericsUtil.resolveTypes(new Type[] { actualType }, type);
+    }
 
     @Test
     public void resolveType() throws NoSuchFieldException {
@@ -157,4 +171,13 @@ public class GenericsUtilTest {
             };
         }
     }
+
+    public static class ByDefaultIllDoAStackOverFlowIngenericUtils<C, R>
+    {
+        public <P extends Comparable<? super P>> ByDefaultIllDoAStackOverFlowIngenericUtils<C,
R> foo(
+                final ByDefaultIllDoAStackOverFlowIngenericUtils<? super C, P> att,
P value)
+        {
+            return this;
+        }
+    }
 }



Mime
View raw message