comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/IdentityConverter.d @ 88:9e0ab372d5d8

Revert from TypeInfo/ClassInfo to java.lang.Class
author Frank Benoit <benoit@tionex.de>
date Sun, 19 Apr 2009 11:10:09 +0200
parents 6be48cf9f95c
children
comparison
equal deleted inserted replaced
87:8594250b1d1c 88:9e0ab372d5d8
11 * Matt Carter - Character support completed (bug 197679) 11 * Matt Carter - Character support completed (bug 197679)
12 */ 12 */
13 module org.eclipse.core.internal.databinding.conversion.IdentityConverter; 13 module org.eclipse.core.internal.databinding.conversion.IdentityConverter;
14 14
15 import java.lang.all; 15 import java.lang.all;
16 import java.nonstandard.RuntimeTraits;
17 16
18 import org.eclipse.core.databinding.BindingException; 17 import org.eclipse.core.databinding.BindingException;
19 import org.eclipse.core.databinding.conversion.IConverter; 18 import org.eclipse.core.databinding.conversion.IConverter;
20 19
21 /** 20 /**
22 * TheIdentityConverter. Returns the source value (the identity function). 21 * TheIdentityConverter. Returns the source value (the identity function).
23 */ 22 */
24 public class IdentityConverter : IConverter { 23 public class IdentityConverter : IConverter {
25 24
26 private TypeInfo fromType; 25 private Class fromType;
27 26
28 private TypeInfo toType; 27 private Class toType;
29 28
30 /** 29 /**
31 * @param type 30 * @param type
32 */ 31 */
33 public this(TypeInfo type) { 32 public this(Class type) {
34 this.fromType = type; 33 this.fromType = type;
35 this.toType = type; 34 this.toType = type;
36 initPrimitiveMap(); 35 initPrimitiveMap();
37 } 36 }
38 37
39 /** 38 /**
40 * @param fromType 39 * @param fromType
41 * @param toType 40 * @param toType
42 */ 41 */
43 public this(TypeInfo fromType, TypeInfo toType) { 42 public this(Class fromType, Class toType) {
44 this.fromType = fromType; 43 this.fromType = fromType;
45 this.toType = toType; 44 this.toType = toType;
46 initPrimitiveMap(); 45 initPrimitiveMap();
47 } 46 }
48 47
49 private TypeInfo[][] primitiveMap; 48 private Class[][] primitiveMap;
50 49
51 private void initPrimitiveMap(){ 50 private void initPrimitiveMap(){
52 primitiveMap = [ 51 primitiveMap = [
53 [ cast(TypeInfo)Integer.TYPE, typeid(Integer) ], [ cast(TypeInfo)Short.TYPE, typeid(Short) ], 52 [ cast(Class)Integer.TYPE, Class.fromType!(Integer) ], [ cast(Class)Short.TYPE, Class.fromType!(Short) ],
54 [ cast(TypeInfo)Long.TYPE, typeid(Long) ], [ cast(TypeInfo)Double.TYPE, typeid(Double) ], 53 [ cast(Class)Long.TYPE, Class.fromType!(Long) ], [ cast(Class)Double.TYPE, Class.fromType!(Double) ],
55 [ cast(TypeInfo)Byte.TYPE, typeid(Byte) ], [ cast(TypeInfo)Float.TYPE, typeid(Float) ], 54 [ cast(Class)Byte.TYPE, Class.fromType!(Byte) ], [ cast(Class)Float.TYPE, Class.fromType!(Float) ],
56 [ cast(TypeInfo)Boolean.TYPE, typeid(Boolean) ], 55 [ cast(Class)Boolean.TYPE, Class.fromType!(Boolean) ],
57 [ cast(TypeInfo)Character.TYPE, typeid(Character) ] ]; 56 [ cast(Class)Character.TYPE, Class.fromType!(Character) ] ];
58 } 57 }
59 58
60 /* 59 /*
61 * (non-Javadoc) 60 * (non-Javadoc)
62 * 61 *
63 * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) 62 * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object)
64 */ 63 */
65 public Object convert(Object source) { 64 public Object convert(Object source) {
66 if ( isJavaPrimitive(toType)) { 65 if ( toType.isPrimitive()) {
67 if (source is null) { 66 if (source is null) {
68 throw new BindingException("Cannot convert null to a primitive"); //$NON-NLS-1$ 67 throw new BindingException("Cannot convert null to a primitive"); //$NON-NLS-1$
69 } 68 }
70 } 69 }
71 if (source !is null) { 70 if (source !is null) {
72 TypeInfo sourceClass = getTypeInfo(source.classinfo); 71 Class sourceClass = Class.fromObject(source);
73 if (isJavaPrimitive(toType) || isJavaPrimitive(sourceClass)) { 72 if (toType.isPrimitive() || sourceClass.isPrimitive()) {
74 if (sourceClass.opEquals(toType) 73 if (sourceClass.opEquals(toType)
75 || isPrimitiveTypeMatchedWithBoxed(sourceClass, toType)) { 74 || isPrimitiveTypeMatchedWithBoxed(sourceClass, toType)) {
76 return source; 75 return source;
77 } 76 }
78 throw new BindingException( 77 throw new BindingException(
79 "Boxed and unboxed types do not match"); //$NON-NLS-1$ 78 "Boxed and unboxed types do not match"); //$NON-NLS-1$
80 } 79 }
81 if (!isImplicitly(sourceClass, toType)) { 80 if (!toType.isAssignableFrom(sourceClass)) {
82 throw new BindingException(asClass(sourceClass).name 81 throw new BindingException(Class.fromObject(sourceClass).getName()
83 ~ " is not assignable to " ~ asClass(toType).name); //$NON-NLS-1$ 82 ~ " is not assignable to " ~ Class.fromObject(toType).getName()); //$NON-NLS-1$
84 } 83 }
85 } 84 }
86 return source; 85 return source;
87 } 86 }
88 87
91 * 90 *
92 * @param sourceClass 91 * @param sourceClass
93 * @param toClass 92 * @param toClass
94 * @return true if sourceClass and toType are matched primitive/boxed types 93 * @return true if sourceClass and toType are matched primitive/boxed types
95 */ 94 */
96 public bool isPrimitiveTypeMatchedWithBoxed(TypeInfo sourceClass, 95 public bool isPrimitiveTypeMatchedWithBoxed(Class sourceClass,
97 TypeInfo toClass) { 96 Class toClass) {
98 for (int i = 0; i < primitiveMap.length; i++) { 97 for (int i = 0; i < primitiveMap.length; i++) {
99 if (toClass.opEquals(primitiveMap[i][0]) 98 if (toClass.opEquals(primitiveMap[i][0])
100 && sourceClass.opEquals(primitiveMap[i][1])) { 99 && sourceClass.opEquals(primitiveMap[i][1])) {
101 return true; 100 return true;
102 } 101 }