Java Reflection Assignable isCompatible(Class fromClass, Class toClass)

Here you can find the source of isCompatible(Class fromClass, Class toClass)

Description

is Compatible

License

Open Source License

Declaration

public static boolean isCompatible(Class<?> fromClass, Class<?> toClass) 

Method Source Code

//package com.java2s;
/* // w w  w  .ja  v  a 2 s.  co  m
  Copyright (C) 2013 Raquel Pau and Albert Coroleu.
     
 Walkmod is free software: you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
     
 Walkmod is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Lesser General Public License for more details.
     
 You should have received a copy of the GNU Lesser General Public License
 along with Walkmod.  If not, see <http://www.gnu.org/licenses/>.*/

import java.util.Map;

public class Main {
    private static Map<String, Integer> matrixTypePosition;
    private static boolean[][] compatibilityMatrix;

    public static boolean isCompatible(Class<?> fromClass, Class<?> toClass) {
        if (fromClass == null || toClass == null) {
            return true;
        }
        if (matrixTypePosition.containsKey(fromClass.getName())
                && matrixTypePosition.containsKey(toClass.getName())) {
            return compatibilityMatrix[matrixTypePosition.get(fromClass.getName())][matrixTypePosition
                    .get(toClass.getName())];
        } else {
            return toClass.isAssignableFrom(fromClass);
        }
    }

    public static boolean isCompatible(Class<?>[] fromClasses, Class<?>[] toClasses) {
        if (fromClasses.length == toClasses.length) {
            boolean assignable = true;
            for (int i = 0; i < fromClasses.length && assignable; i++) {
                assignable = isCompatible(fromClasses[i], toClasses[i]);
            }
            return assignable;
        }
        return false;
    }
}

Related

  1. isAssignableValue(Class type, Object value)
  2. isAssignableValue(Class type, Object value)
  3. isClassAssignableFrom(Class formal, Class actual)
  4. isCompatible(Class actualType, Class parameterType)
  5. isCompatible(Class parameterType, Class parameterClass)
  6. isCompatible(Class type0, Class type1)
  7. isPromotableFrom(Class assigneeClass, Class valueClass)