Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.util;
 
 import java.util.Set;
 
 
Utilities for working with Javassist proxies

Author(s):
Nicklas Karlsson
Pete Muir
Tomaz Cerar
Ales Justin
 
 @SuppressWarnings({"ThrowableResultOfMethodCallIgnored""NullableProblems"})
 public class Proxies {
 
     public static class TypeInfo {
 
         private final Set<Class<?>> interfaces;
         private final Set<Class<?>> classes;
 
         private TypeInfo() {
             super();
             this. = new LinkedHashSet<Class<?>>();
             this. = new LinkedHashSet<Class<?>>();
         }
 
         public Class<?> getSuperClass() {
             if (.isEmpty()) {
                 return Object.class;
             }
             Iterator<Class<?>> it = .iterator();
             Class<?> superclass = it.next();
             while (it.hasNext()) {
                 Class<?> clazz = it.next();
                 if (superclass.isAssignableFrom(clazz)) {
                     superclass = clazz;
                 }
             }
             return superclass;
         }
 
         public Class<?> getSuperInterface() {
             if (.isEmpty()) {
                 return null;
             }
             Iterator<Class<?>> it = .iterator();
             Class<?> superclass = it.next();
             while (it.hasNext()) {
                 Class<?> clazz = it.next();
                 if (superclass.isAssignableFrom(clazz)) {
                     superclass = clazz;
                 }
             }
             return superclass;
         }
 
         private TypeInfo add(Type type) {
             if (type instanceof Class<?>) {
                 Class<?> clazz = (Class<?>) type;
                 if (clazz.isInterface()) {
                     .add(clazz);
                 } else {
                     .add(clazz);
                 }
            } else if (type instanceof ParameterizedType) {
                add(((ParameterizedTypetype).getRawType());
            } else {
                throw ..cannotProxyNonClassType(type);
            }
            return this;
        }
        public Set<Class<?>> getClasses() {
            return Collections.unmodifiableSet();
        }
        public Set<Class<?>> getInterfaces() {
            return Collections.unmodifiableSet();
        }
        public static TypeInfo of(Set<? extends Typetypes) {
            TypeInfo typeInfo = new TypeInfo();
            for (Type type : types) {
                typeInfo.add(type);
            }
            return typeInfo;
        }
    }
    private Proxies() {
    }

    
Indicates if a class is proxyable

Parameters:
type The class to test
Returns:
True if proxyable, false otherwise
    public static boolean isTypeProxyable(Type typeServiceRegistry services) {
        return getUnproxyableTypeException(typeservices) == null;
    }
        return getUnproxyableTypeException(typenullservices);
    }

    
Indicates if a set of types are all proxyable

Parameters:
declaringBean with types to test
Returns:
True if proxyable, false otherwise
    public static boolean isTypesProxyable(Bean<?> declaringBeanServiceRegistry services) {
        return getUnproxyableTypesException(declaringBeanservices) == null;
    }

    
Indicates if a set of types are all proxyable

Parameters:
types The types to test
Returns:
True if proxyable, false otherwise
    public static boolean isTypesProxyable(Iterable<? extends TypetypesServiceRegistry services) {
        return getUnproxyableTypesException(typesservices) == null;
    }
    public static UnproxyableResolutionException getUnproxyableTypesException(Bean<?> declaringBeanServiceRegistry services) {
        if (declaringBean == null) {
            throw new java.lang.IllegalArgumentException("Null declaring bean!");
        }
        return getUnproxyableTypesExceptionInt(declaringBean.getTypes(), declaringBeanservices);
    }
    public static UnproxyableResolutionException getUnproxyableTypesException(Iterable<? extends TypetypesServiceRegistry services) {
        return getUnproxyableTypesExceptionInt(typesnullservices);
    }
    public static UnproxyableResolutionException getUnproxyableTypeException(Type typeBean<?> declaringBeanServiceRegistry services) {
        if (type instanceof Class<?>) {
            return getUnproxyableClassException((Class<?>) typedeclaringBeanservices);
        } else if (type instanceof ParameterizedType) {
            Type rawType = ((ParameterizedTypetype).getRawType();
            if (rawType instanceof Class<?>) {
                return getUnproxyableClassException((Class<?>) rawTypedeclaringBeanservices);
            }
        }
        return ..notProxyableUnknown(typegetDeclaringBeanInfo(declaringBean));
    }
    // --- private
    private static UnproxyableResolutionException getUnproxyableTypesExceptionInt(Iterable<? extends TypetypesBean<?> declaringBeanServiceRegistry services) {
        for (Type apiType : types) {
            if (Object.class.equals(apiType)) {
                continue;
            }
            UnproxyableResolutionException e = getUnproxyableTypeException(apiTypedeclaringBeanservices);
            if (e != null) {
                return e;
            }
        }
        return null;
    }
    private static UnproxyableResolutionException getUnproxyableClassException(Class<?> clazzBean<?> declaringBeanServiceRegistry services) {
        if (clazz.isInterface()) {
            return null;
        }
        Constructor<?> constructor;
        try {
            constructor = AccessController.doPrivileged(GetDeclaredConstructorAction.of(clazz));
        } catch (PrivilegedActionException e) {
            InstantiatorFactory factory = services.get(InstantiatorFactory.class);
            if (factory == null || !(factory.useInstantiators())) {
                return ..notProxyableNoConstructor(clazzgetDeclaringBeanInfo(declaringBean));
            } else {
                return null;
            }
        }
        if (constructor == null) {
            return ..notProxyableNoConstructor(clazzgetDeclaringBeanInfo(declaringBean));
        } else if (Modifier.isPrivate(constructor.getModifiers())) {
            InstantiatorFactory factory = services.get(InstantiatorFactory.class);
            if (factory == null || !(factory.useInstantiators())) {
                return new UnproxyableResolutionException(..notProxyablePrivateConstructor(clazzconstructorgetDeclaringBeanInfo(declaringBean)));
            } else {
                return null;
            }
        } else if (Reflections.isTypeOrAnyMethodFinal(clazz)) {
            return ..notProxyableFinalTypeOrMethod(clazz, Reflections.getNonPrivateFinalMethodOrType(clazz), getDeclaringBeanInfo(declaringBean));
        } else if (clazz.isPrimitive()) {
            return ..notProxyablePrimitive(clazzgetDeclaringBeanInfo(declaringBean));
        } else if (Reflections.isArrayType(clazz)) {
            return ..notProxyableArrayType(clazzgetDeclaringBeanInfo(declaringBean));
        } else {
            return null;
        }
    }
    private static Object getDeclaringBeanInfo(Bean<?> bean) {
        return (bean != null) ? bean : "<unknown javax.enterprise.inject.spi.Bean instance>";
    }
New to GrepCode? Check out our FAQ X