| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.core.versions; |
| 17 | |
|
| 18 | |
import org.kuali.rice.core.versions.annotations.DeprecatedSince; |
| 19 | |
import org.kuali.rice.core.versions.annotations.SupportedSince; |
| 20 | |
|
| 21 | |
import java.lang.annotation.Annotation; |
| 22 | |
import java.lang.reflect.AnnotatedElement; |
| 23 | |
import java.lang.reflect.Field; |
| 24 | |
import java.lang.reflect.Method; |
| 25 | |
import java.util.ArrayList; |
| 26 | |
import java.util.Arrays; |
| 27 | |
import java.util.List; |
| 28 | |
|
| 29 | 0 | public class VersionHelper { |
| 30 | |
public static boolean areVersionsCompatible(Object source, Object target) { |
| 31 | 0 | double sourceVer = getHighestVersion(source); |
| 32 | 0 | double targetVer = getHighestVersion(target); |
| 33 | 0 | return targetVer <= sourceVer; |
| 34 | |
} |
| 35 | |
|
| 36 | |
public static boolean isVersionCompatible(Object obj, double ver) { |
| 37 | 0 | return ver <= getHighestVersion(obj); |
| 38 | |
} |
| 39 | |
|
| 40 | |
public static double getHighestVersion(Object obj) { |
| 41 | |
|
| 42 | 0 | Class clazz = obj.getClass(); |
| 43 | 0 | Method[] methods = clazz.getMethods(); |
| 44 | 0 | Field[] fields = clazz.getFields(); |
| 45 | |
List<AnnotatedElement> elements; |
| 46 | 0 | elements = new ArrayList<AnnotatedElement>(methods.length + fields.length + 1); |
| 47 | 0 | elements.add(clazz); |
| 48 | 0 | elements.addAll(Arrays.asList(methods)); |
| 49 | 0 | elements.addAll(Arrays.asList(fields)); |
| 50 | 0 | return getHighestVersion(elements); |
| 51 | |
} |
| 52 | |
|
| 53 | |
protected static double getHighestVersion(List<AnnotatedElement> elements) { |
| 54 | 0 | double highestVersion = 1.0; |
| 55 | 0 | for ( AnnotatedElement element : elements ) { |
| 56 | 0 | DeprecatedSince deprecated = element.getAnnotation(DeprecatedSince.class); |
| 57 | 0 | SupportedSince supported = element.getAnnotation(SupportedSince.class); |
| 58 | 0 | double compareVersion = getHighestVersion( |
| 59 | |
new Annotation[] {deprecated, supported}); |
| 60 | 0 | if ( highestVersion < compareVersion ) |
| 61 | 0 | highestVersion = compareVersion; |
| 62 | 0 | } |
| 63 | 0 | return highestVersion; |
| 64 | |
} |
| 65 | |
|
| 66 | |
protected static double getHighestVersion(Annotation[] annotations) { |
| 67 | 0 | double highestVersion = 1.0; |
| 68 | 0 | for ( Annotation annotation : annotations ) { |
| 69 | 0 | double compareVersion = 1.0; |
| 70 | 0 | if ( annotation instanceof DeprecatedSince ) |
| 71 | 0 | compareVersion = ((DeprecatedSince)annotation).version(); |
| 72 | 0 | else if ( annotation instanceof DeprecatedSince ) |
| 73 | 0 | compareVersion = ((SupportedSince)annotation).version(); |
| 74 | 0 | if ( highestVersion < compareVersion ) |
| 75 | 0 | highestVersion = compareVersion; |
| 76 | |
} |
| 77 | 0 | return highestVersion; |
| 78 | |
} |
| 79 | |
|
| 80 | |
public static boolean isMethodSupported(Object obj, String methodName, |
| 81 | |
double ver) { |
| 82 | 0 | Method[] methods = obj.getClass().getMethods(); |
| 83 | 0 | for ( Method method : methods ) { |
| 84 | 0 | if ( method.getName().equals(methodName) ) { |
| 85 | |
|
| 86 | |
|
| 87 | 0 | return isMethodSupported(method, ver); |
| 88 | |
} |
| 89 | |
} |
| 90 | 0 | return false; |
| 91 | |
} |
| 92 | |
|
| 93 | |
public static boolean isMethodSupported(Method method, double ver) { |
| 94 | 0 | SupportedSince supported = method.getAnnotation(SupportedSince.class); |
| 95 | 0 | DeprecatedSince deprecated = method.getAnnotation(DeprecatedSince.class); |
| 96 | 0 | return isVersionValid(supported, deprecated, ver); |
| 97 | |
} |
| 98 | |
|
| 99 | |
public static boolean isFieldSupported(Object obj, String fieldName, |
| 100 | |
double ver) { |
| 101 | |
try { |
| 102 | 0 | Field field = obj.getClass().getField(fieldName); |
| 103 | 0 | return isFieldSupported(field, ver); |
| 104 | |
} |
| 105 | 0 | catch ( NoSuchFieldException e ) { |
| 106 | 0 | return false; |
| 107 | |
} |
| 108 | |
} |
| 109 | |
|
| 110 | |
public static boolean isFieldSupported(Field field, double ver) { |
| 111 | 0 | SupportedSince supported = field.getAnnotation(SupportedSince.class); |
| 112 | 0 | DeprecatedSince deprecated = field.getAnnotation(DeprecatedSince.class); |
| 113 | 0 | return isVersionValid(supported, deprecated, ver); |
| 114 | |
} |
| 115 | |
|
| 116 | |
public static boolean isClassSupported(Object obj, double ver) { |
| 117 | 0 | SupportedSince supported = obj.getClass().getAnnotation(SupportedSince.class); |
| 118 | 0 | DeprecatedSince deprecated = obj.getClass().getAnnotation(DeprecatedSince.class); |
| 119 | 0 | return isVersionValid(supported, deprecated, ver); |
| 120 | |
} |
| 121 | |
|
| 122 | |
protected static boolean isVersionValid(SupportedSince supported, |
| 123 | |
DeprecatedSince deprecated, |
| 124 | |
double ver) { |
| 125 | 0 | double supportedVersion = supported != null ? supported.version() : 1.0; |
| 126 | 0 | if ( supported == null || supported.version() <= ver ) |
| 127 | 0 | return ( deprecated == null || ver < deprecated.version() ); |
| 128 | |
else |
| 129 | 0 | return false; |
| 130 | |
} |
| 131 | |
} |