Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2014, 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.resolution;
 
 import static org.jboss.weld.util.Types.boxedClass;
 
 
Utility class that captures invariant Java assignability rules (used with generics). This class operates on all the possible Type subtypes: Class, ParameterizedType, TypeVariable, WildcardType, GenericArrayType. To make this class easier to understand and maintain, there is a separate isAssignableFrom method for each combination of possible types. Each of these methods compares two type instances and determines whether the first one is assignable from the other. Since Java wildcards are by definition covariant, this class does not operate on wildcards and instead delegates to CovariantTypes. TypeVariables are considered a specific unknown type restricted by the upper bound. No inference of type variables is performed.

Author(s):
Jozef Hartinger
 
 public class InvariantTypes {
 
     private InvariantTypes() {
     }
 
     public static boolean isAssignableFrom(Type type1Type type2) {
         if (type1 instanceof WildcardType || type2 instanceof WildcardType) {
             // Wildcards are by definition covariant
             return CovariantTypes.isAssignableFrom(type1type2);
         }
         if (type1 instanceof Class<?>) {
             if (type2 instanceof Class<?>) {
                 return isAssignableFrom((Class<?>) type1, (Class<?>) type2);
             }
             if (type2 instanceof ParameterizedType) {
                 return isAssignableFrom((Class<?>) type1, (ParameterizedTypetype2);
             }
             if (type2 instanceof TypeVariable<?>) {
                 return isAssignableFrom((Class<?>) type1, (TypeVariable<?>) type2);
             }
             if (type2 instanceof GenericArrayType) {
                 return isAssignableFrom((Class<?>) type1, (GenericArrayTypetype2);
             }
             throw ..unknownType(type2);
         }
         if (type1 instanceof ParameterizedType) {
             if (type2 instanceof Class<?>) {
                 return isAssignableFrom((ParameterizedTypetype1, (Class<?>) type2);
             }
             if (type2 instanceof ParameterizedType) {
                 return isAssignableFrom((ParameterizedTypetype1, (ParameterizedTypetype2);
             }
             if (type2 instanceof TypeVariable<?>) {
                 return isAssignableFrom((ParameterizedTypetype1, (TypeVariable<?>) type2);
             }
             if (type2 instanceof GenericArrayType) {
                 return isAssignableFrom((ParameterizedTypetype1, (GenericArrayTypetype2);
             }
             throw ..unknownType(type2);
         }
         if (type1 instanceof TypeVariable<?>) {
             if (type2 instanceof Class<?>) {
                 return isAssignableFrom((TypeVariable<?>) type1, (Class<?>) type2);
             }
             if (type2 instanceof ParameterizedType) {
                 return isAssignableFrom((TypeVariable<?>) type1, (ParameterizedTypetype2);
             }
             if (type2 instanceof TypeVariable<?>) {
                 return isAssignableFrom((TypeVariable<?>) type1, (TypeVariable<?>) type2);
             }
             if (type2 instanceof GenericArrayType) {
                 return isAssignableFrom((TypeVariable<?>) type1, (GenericArrayTypetype2);
             }
             throw ..unknownType(type2);
         }
        if (type1 instanceof GenericArrayType) {
            if (type2 instanceof Class<?>) {
                return isAssignableFrom((GenericArrayTypetype1, (Class<?>) type2);
            }
            if (type2 instanceof ParameterizedType) {
                return isAssignableFrom((GenericArrayTypetype1, (ParameterizedTypetype2);
            }
            if (type2 instanceof TypeVariable<?>) {
                return isAssignableFrom((GenericArrayTypetype1, (TypeVariable<?>) type2);
            }
            if (type2 instanceof GenericArrayType) {
                return isAssignableFrom((GenericArrayTypetype1, (GenericArrayTypetype2);
            }
            throw ..unknownType(type2);
        }
        throw ..unknownType(type1);
    }
    /*
     * Raw type
     */
    private static boolean isAssignableFrom(Class<?> type1Class<?> type2) {
        return boxedClass(type1).equals(boxedClass(type2));
    }
    private static boolean isAssignableFrom(Class<?> type1ParameterizedType type2) {
        return false;
    }
    private static boolean isAssignableFrom(Class<?> type1TypeVariable<?> type2) {
        return false;
    }
    private static boolean isAssignableFrom(Class<?> type1GenericArrayType type2) {
        return false;
    }
    /*
     * ParameterizedType
     */
    private static boolean isAssignableFrom(ParameterizedType type1Class<?> type2) {
        return false;
    }
    private static boolean isAssignableFrom(ParameterizedType type1ParameterizedType type2) {
        // first, raw types have to be equal
        if (!Reflections.getRawType(type1).equals(Reflections.getRawType(type2))) {
            return false;
        }
        final Type[] types1 = type1.getActualTypeArguments();
        final Type[] types2 = type2.getActualTypeArguments();
        if (types1.length != types2.length) {
            throw ..invalidTypeArgumentCombination(type1type2);
        }
        for (int i = 0; i < types1.lengthi++) {
            if (!isAssignableFrom(types1[i], types2[i])) {
                return false;
            }
        }
        return true;
    }
    private static boolean isAssignableFrom(ParameterizedType type1TypeVariable<?> type2) {
        return false;
    }
    private static boolean isAssignableFrom(ParameterizedType type1GenericArrayType type2) {
        return false;
    }
    /*
     * Type variable
     */
    private static boolean isAssignableFrom(TypeVariable<?> type1Class<?> type2) {
        return false;
    }
    private static boolean isAssignableFrom(TypeVariable<?> type1ParameterizedType type2) {
        return false;
    }
    private static boolean isAssignableFrom(TypeVariable<?> type1TypeVariable<?> type2) {
        return type1.equals(type2);
    }
    private static boolean isAssignableFrom(TypeVariable<?> type1GenericArrayType type2) {
        return false;
    }
    /*
     * GenericArrayType
     */
    private static boolean isAssignableFrom(GenericArrayType type1Class<?> type2) {
        return false;
    }
    private static boolean isAssignableFrom(GenericArrayType type1ParameterizedType type2) {
        return false;
    }
    private static boolean isAssignableFrom(GenericArrayType type1TypeVariable<?> type2) {
        return false;
    }
    private static boolean isAssignableFrom(GenericArrayType type1GenericArrayType type2) {
        return isAssignableFrom(type1.getGenericComponentType(), type2.getGenericComponentType());
    }
New to GrepCode? Check out our FAQ X