Skip to content

Commit

Permalink
Build type closures lazily where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas authored and pmuir committed Nov 16, 2010
1 parent 28379af commit b9bfac4
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 33 deletions.
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
Expand Up @@ -324,7 +324,7 @@ protected ManagedBean(WeldClass<T> type, String idSuffix, BeanManagerImpl beanMa
initTypes();
initQualifiers();
initConstructor();
this.proxiable = Proxies.isTypeProxyable(type.getBaseType());
this.proxiable = Proxies.isTypesProxyable(type.getTypeClosure());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/ProducerField.java
Expand Up @@ -82,7 +82,7 @@ protected ProducerField(WeldField<T, ? super X> field, AbstractClassBean<X> decl
initTypes();
initQualifiers();
initStereotypes();
this.proxiable = Proxies.isTypeProxyable(field.getBaseType());
this.proxiable = Proxies.isTypesProxyable(field.getTypeClosure());
}

protected static String createId(WeldField<?, ?> field, AbstractClassBean<?> declaringBean)
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java
Expand Up @@ -86,7 +86,7 @@ protected ProducerMethod(WeldMethod<T, ? super X> method, AbstractClassBean<X> d
this.id = createId(method, declaringBean);
initStereotypes();
initProducerMethodInjectableParameters();
this.proxiable = Proxies.isTypeProxyable(method.getBaseType());
this.proxiable = Proxies.isTypesProxyable(method.getTypeClosure());
}

protected String createId(WeldMethod<T, ? super X> method, AbstractClassBean<X> declaringBean)
Expand Down
@@ -0,0 +1,61 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., 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.weld.introspector;

import java.lang.reflect.Type;
import java.util.Set;

import org.jboss.weld.util.LazyValueHolder;
import org.jboss.weld.util.reflection.HierarchyDiscovery;

/**
* {@link LazyValueHolder} that calculates a type closue. In many cases this
* will not be needed, so computing it on demand saves memory and startup time.
*
* @author Stuart Douglas
*
*/
public class TypeClosureLazyValueHolder extends LazyValueHolder<Set<Type>>
{

private final Type type;

private final Set<Type> types;

public TypeClosureLazyValueHolder(Type type)
{
this.type = type;
this.types = null;
}

public TypeClosureLazyValueHolder(Set<Type> types)
{
this.type = null;
this.types = types;
}

@Override
protected Set<Type> computeValue()
{
if (types != null)
{
return types;
}
return new HierarchyDiscovery(type).getTypeClosure();
}

}
Expand Up @@ -36,6 +36,7 @@
import org.jboss.weld.literal.DefaultLiteral;
import org.jboss.weld.metadata.TypeStore;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.LazyValueHolder;
import org.jboss.weld.util.collections.ArraySet;
import org.jboss.weld.util.collections.ArraySetMultimap;
import org.jboss.weld.util.collections.Arrays2;
Expand Down Expand Up @@ -128,7 +129,7 @@ private static void addMetaAnnotation(ArraySetMultimap<Class<? extends Annotatio
private final Class<T> rawType;
private final Type[] actualTypeArguments;
private final Type type;
private final Set<Type> typeClosure;
private final LazyValueHolder<Set<Type>> typeClosure;

/**
* Constructor
Expand All @@ -139,7 +140,7 @@ private static void addMetaAnnotation(ArraySetMultimap<Class<? extends Annotatio
* @param annotationMap A map of annotation to register
*
*/
public AbstractWeldAnnotated(Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer, Class<T> rawType, Type type, Set<Type> typeClosure)
public AbstractWeldAnnotated(Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer, Class<T> rawType, Type type, final LazyValueHolder<Set<Type>> typeClosure)
{
if (annotationMap == null)
{
Expand Down Expand Up @@ -168,7 +169,7 @@ public AbstractWeldAnnotated(Map<Class<? extends Annotation>, Annotation> annota
{
this.actualTypeArguments = new Type[0];
}
this.typeClosure = Collections.unmodifiableSet(new ArraySet<Type>(typeClosure));
this.typeClosure = typeClosure;
}

protected AbstractWeldAnnotated(Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, TypeStore typeStore)
Expand Down Expand Up @@ -239,7 +240,7 @@ public Type getBaseType()

public Set<Type> getTypeClosure()
{
return typeClosure;
return typeClosure.get();
}

public Set<Annotation> getAnnotations()
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.jboss.weld.introspector.WeldCallable;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.LazyValueHolder;

/**
* @author pmuir
Expand All @@ -33,7 +34,7 @@
public abstract class AbstractWeldCallable<T, X, S extends Member> extends AbstractWeldMember<T, X, S> implements WeldCallable<T, X, S>
{

protected AbstractWeldCallable(Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer, Member member, Class<T> rawType, Type type, Set<Type> typeClosure, WeldClass<X> declaringType)
protected AbstractWeldCallable(Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer, Member member, Class<T> rawType, Type type, LazyValueHolder<Set<Type>> typeClosure, WeldClass<X> declaringType)
{
super(annotationMap, declaredAnnotationMap, classTransformer, member, rawType, type, typeClosure, declaringType);
}
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldMember;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.LazyValueHolder;
import org.jboss.weld.util.reflection.Reflections;

/**
Expand All @@ -50,7 +51,7 @@ public abstract class AbstractWeldMember<T, X, S extends Member> extends Abstrac
*
* @param annotationMap The annotation map
*/
protected AbstractWeldMember(Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer, Member member, Class<T> rawType, Type type, Set<Type> typeClosure, WeldClass<X> declaringType)
protected AbstractWeldMember(Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer, Member member, Class<T> rawType, Type type, LazyValueHolder<Set<Type>> typeClosure, WeldClass<X> declaringType)
{
super(annotationMap, declaredAnnotationMap, classTransformer, rawType, type, typeClosure);
this.declaringType = declaringType;
Expand Down
Expand Up @@ -25,11 +25,11 @@
import java.util.Map;
import java.util.Set;

import org.jboss.weld.introspector.TypeClosureLazyValueHolder;
import org.jboss.weld.introspector.WeldAnnotation;
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.collections.HashSetSupplier;
import org.jboss.weld.util.reflection.HierarchyDiscovery;
import org.jboss.weld.util.reflection.SecureReflections;

import com.google.common.collect.Multimaps;
Expand Down Expand Up @@ -75,7 +75,7 @@ public static <A extends Annotation> WeldAnnotation<A> of(Class<A> annotationTyp
*/
protected WeldAnnotationImpl(Class<T> annotationType, Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer)
{
super(annotationType, annotationType, null, new HierarchyDiscovery(annotationType).getTypeClosure(), annotationMap, declaredAnnotationMap, classTransformer);
super(annotationType, annotationType, null, new TypeClosureLazyValueHolder(annotationType), annotationMap, declaredAnnotationMap, classTransformer);
this.clazz = annotationType;
members = new HashSet<WeldMethod<?, ?>>();
annotatedMembers = Multimaps.newSetMultimap(new HashMap<Class<? extends Annotation>, Collection<WeldMethod<?, ?>>>(), HashSetSupplier.<WeldMethod<?, ?>>instance());
Expand Down
Expand Up @@ -42,15 +42,16 @@
import org.jboss.weld.introspector.ConstructorSignature;
import org.jboss.weld.introspector.ExternalAnnotatedType;
import org.jboss.weld.introspector.MethodSignature;
import org.jboss.weld.introspector.TypeClosureLazyValueHolder;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldConstructor;
import org.jboss.weld.introspector.WeldField;
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.LazyValueHolder;
import org.jboss.weld.util.collections.ArraySet;
import org.jboss.weld.util.collections.ArraySetMultimap;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.util.reflection.HierarchyDiscovery;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;

Expand Down Expand Up @@ -109,20 +110,20 @@ public class WeldClassImpl<T> extends AbstractWeldAnnotated<T, Class<T>> impleme

public static <T> WeldClass<T> of(Class<T> clazz, ClassTransformer classTransformer)
{
return new WeldClassImpl<T>(clazz, clazz, null, new HierarchyDiscovery(clazz).getTypeClosure(), buildAnnotationMap(clazz.getAnnotations()), buildAnnotationMap(clazz.getDeclaredAnnotations()), classTransformer);
return new WeldClassImpl<T>(clazz, clazz, null, new TypeClosureLazyValueHolder(clazz), buildAnnotationMap(clazz.getAnnotations()), buildAnnotationMap(clazz.getDeclaredAnnotations()), classTransformer);
}

public static <T> WeldClass<T> of(AnnotatedType<T> annotatedType, ClassTransformer classTransformer)
{
return new WeldClassImpl<T>(annotatedType.getJavaClass(), annotatedType.getBaseType(), annotatedType, annotatedType.getTypeClosure(), buildAnnotationMap(annotatedType.getAnnotations()), buildAnnotationMap(annotatedType.getAnnotations()), classTransformer);
return new WeldClassImpl<T>(annotatedType.getJavaClass(), annotatedType.getBaseType(), annotatedType, new TypeClosureLazyValueHolder(annotatedType.getTypeClosure()), buildAnnotationMap(annotatedType.getAnnotations()), buildAnnotationMap(annotatedType.getAnnotations()), classTransformer);
}

public static <T> WeldClass<T> of(Class<T> rawType, Type type, ClassTransformer classTransformer)
{
return new WeldClassImpl<T>(rawType, type, null, new HierarchyDiscovery(type).getTypeClosure(), buildAnnotationMap(rawType.getAnnotations()), buildAnnotationMap(rawType.getDeclaredAnnotations()), classTransformer);
return new WeldClassImpl<T>(rawType, type, null, new TypeClosureLazyValueHolder(type), buildAnnotationMap(rawType.getAnnotations()), buildAnnotationMap(rawType.getDeclaredAnnotations()), classTransformer);
}

protected WeldClassImpl(Class<T> rawType, Type type, AnnotatedType<T> annotatedType, Set<Type> typeClosure, Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer)
protected WeldClassImpl(Class<T> rawType, Type type, AnnotatedType<T> annotatedType, LazyValueHolder<Set<Type>> typeClosure, Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer)
{
super(annotationMap, declaredAnnotationMap, classTransformer, rawType, type, typeClosure);

Expand Down
Expand Up @@ -33,14 +33,15 @@

import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.introspector.ConstructorSignature;
import org.jboss.weld.introspector.TypeClosureLazyValueHolder;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldConstructor;
import org.jboss.weld.introspector.WeldParameter;
import org.jboss.weld.logging.messages.ReflectionMessage;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.LazyValueHolder;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.util.reflection.HierarchyDiscovery;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;

Expand All @@ -66,12 +67,12 @@ public class WeldConstructorImpl<T> extends AbstractWeldCallable<T, T, Construct

public static <T> WeldConstructor<T> of(Constructor<T> constructor, WeldClass<T> declaringClass, ClassTransformer classTransformer)
{
return new WeldConstructorImpl<T>(constructor, constructor.getDeclaringClass(), constructor.getDeclaringClass(), null, new HierarchyDiscovery(constructor.getDeclaringClass()).getTypeClosure(), buildAnnotationMap(constructor.getAnnotations()), buildAnnotationMap(constructor.getDeclaredAnnotations()), declaringClass, classTransformer);
return new WeldConstructorImpl<T>(constructor, constructor.getDeclaringClass(), constructor.getDeclaringClass(), null, new TypeClosureLazyValueHolder(constructor.getDeclaringClass()), buildAnnotationMap(constructor.getAnnotations()), buildAnnotationMap(constructor.getDeclaredAnnotations()), declaringClass, classTransformer);
}

public static <T> WeldConstructor<T> of(AnnotatedConstructor<T> annotatedConstructor, WeldClass<T> declaringClass, ClassTransformer classTransformer)
{
return new WeldConstructorImpl<T>(annotatedConstructor.getJavaMember(), annotatedConstructor.getJavaMember().getDeclaringClass(), annotatedConstructor.getBaseType(), annotatedConstructor, annotatedConstructor.getTypeClosure(), buildAnnotationMap(annotatedConstructor.getAnnotations()), buildAnnotationMap(annotatedConstructor.getAnnotations()), declaringClass, classTransformer);
return new WeldConstructorImpl<T>(annotatedConstructor.getJavaMember(), annotatedConstructor.getJavaMember().getDeclaringClass(), annotatedConstructor.getBaseType(), annotatedConstructor, new TypeClosureLazyValueHolder(annotatedConstructor.getTypeClosure()), buildAnnotationMap(annotatedConstructor.getAnnotations()), buildAnnotationMap(annotatedConstructor.getAnnotations()), declaringClass, classTransformer);
}

/**
Expand All @@ -82,7 +83,7 @@ public static <T> WeldConstructor<T> of(AnnotatedConstructor<T> annotatedConstru
* @param constructor The constructor method
* @param declaringClass The declaring class
*/
private WeldConstructorImpl(Constructor<T> constructor, final Class<T> rawType, final Type type, AnnotatedConstructor<T> annotatedConstructor, Set<Type> typeClosure, Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, WeldClass<T> declaringClass, ClassTransformer classTransformer)
private WeldConstructorImpl(Constructor<T> constructor, final Class<T> rawType, final Type type, AnnotatedConstructor<T> annotatedConstructor, LazyValueHolder<Set<Type>> typeClosure, Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, WeldClass<T> declaringClass, ClassTransformer classTransformer)
{
super(annotationMap, declaredAnnotationMap, classTransformer, constructor, rawType, type, typeClosure, declaringClass);
this.constructor = constructor;
Expand Down
Expand Up @@ -17,7 +17,6 @@
package org.jboss.weld.introspector.jlr;

import static org.jboss.weld.logging.messages.UtilMessage.ACCESS_ERROR_ON_FIELD;
import static org.jboss.weld.util.reflection.Reflections.cast;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
Expand All @@ -28,11 +27,12 @@
import javax.enterprise.inject.spi.AnnotatedField;

import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.introspector.TypeClosureLazyValueHolder;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldField;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.LazyValueHolder;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.util.reflection.HierarchyDiscovery;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;

Expand All @@ -53,12 +53,12 @@ public class WeldFieldImpl<T, X> extends AbstractWeldMember<T, X, Field> impleme

public static <T, X> WeldFieldImpl<T, X> of(Field field, WeldClass<X> declaringClass, ClassTransformer classTransformer)
{
return new WeldFieldImpl<T, X>(field, Reflections.<Class<T>>cast(field.getType()), field.getGenericType(), new HierarchyDiscovery(field.getGenericType()).getTypeClosure(), buildAnnotationMap(field.getAnnotations()), buildAnnotationMap(field.getDeclaredAnnotations()), declaringClass, classTransformer);
return new WeldFieldImpl<T, X>(field, Reflections.<Class<T>> cast(field.getType()), field.getGenericType(), new TypeClosureLazyValueHolder(field.getGenericType()), buildAnnotationMap(field.getAnnotations()), buildAnnotationMap(field.getDeclaredAnnotations()), declaringClass, classTransformer);
}

public static <X> WeldFieldImpl<?, X> of(AnnotatedField<? super X> annotatedField, WeldClass<X> declaringClass, ClassTransformer classTransformer)
{
return new WeldFieldImpl<Object, X>(annotatedField.getJavaMember(), Reflections.<Class<Object>>cast(annotatedField.getJavaMember().getType()), annotatedField.getBaseType(), annotatedField.getTypeClosure(), buildAnnotationMap(annotatedField.getAnnotations()), buildAnnotationMap(annotatedField.getAnnotations()), declaringClass, classTransformer);
return new WeldFieldImpl<Object, X>(annotatedField.getJavaMember(), Reflections.<Class<Object>> cast(annotatedField.getJavaMember().getType()), annotatedField.getBaseType(), new TypeClosureLazyValueHolder(annotatedField.getTypeClosure()), buildAnnotationMap(annotatedField.getAnnotations()), buildAnnotationMap(annotatedField.getAnnotations()), declaringClass, classTransformer);
}

/**
Expand All @@ -70,7 +70,7 @@ public static <X> WeldFieldImpl<?, X> of(AnnotatedField<? super X> annotatedFiel
* @param field The actual field
* @param declaringClass The abstraction of the declaring class
*/
private WeldFieldImpl(Field field, final Class<T> rawType, final Type type, Set<Type> typeClosure, Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, WeldClass<X> declaringClass, ClassTransformer classTransformer)
private WeldFieldImpl(Field field, final Class<T> rawType, final Type type, LazyValueHolder<Set<Type>> typeClosure, Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, WeldClass<X> declaringClass, ClassTransformer classTransformer)
{
super(annotationMap, declaredAnnotationMap, classTransformer, field, rawType, type, typeClosure, declaringClass);
this.field = field;
Expand Down

0 comments on commit b9bfac4

Please sign in to comment.