diff --git a/CS2JLibrary/NetFramework/System/Type.xml b/CS2JLibrary/NetFramework/System/Type.xml index 11700d4..37e9d27 100644 --- a/CS2JLibrary/NetFramework/System/Type.xml +++ b/CS2JLibrary/NetFramework/System/Type.xml @@ -1,4 +1,4 @@ - + + + + CS2JNet.System.TypeCode + + TypeCode + System.TypeCode + + System.Enum + + + + ${this:16}.Empty + Empty + + + ${this:16}.Object + Object + + + ${this:16}.DBNull + DBNull + + + ${this:16}.Boolean + Boolean + + + ${this:16}.Char + Char + + + ${this:16}.SByte + SByte + + + ${this:16}.Byte + Byte + + + ${this:16}.Int16 + Int16 + + + ${this:16}.UInt16 + UInt16 + + + ${this:16}.Int32 + Int32 + + + ${this:16}.UInt32 + UInt32 + + + ${this:16}.Int64 + Int64 + + + ${this:16}.UInt64 + UInt64 + + + ${this:16}.Single + Single + + + ${this:16}.Double + Double + + + ${this:16}.Decimal + Decimal + + + ${this:16}.DateTime + DateTime + + + ${this:16}.String + String + + +mo6ChmPjcAlAo0NBWLLZbsocykw=RlUioSuamZxr9dmH+n+zDAoS31RAR3VRqGwbq0J8Ew28sRdZoXTjz2yJwWvSZIzMRNeMc0v8VZbLRMEM39bSKH51gWKuQ8TFyzTBwMzQDeDsNO5kzdxcE1DYGnZKKplPDKPM8n9mlpuDYginKU8dk3dA/CtPgAmc7X90AuPIhew= diff --git a/CS2JLibrary/src/CS2JNet/System/TypeCode.java b/CS2JLibrary/src/CS2JNet/System/TypeCode.java new file mode 100644 index 0000000..65a59bf --- /dev/null +++ b/CS2JLibrary/src/CS2JNet/System/TypeCode.java @@ -0,0 +1,46 @@ +/* + Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com) + + 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. + + Author(s): + + Kevin Glynn (kevin.glynn@twigletsoftware.com) +*/ + +package CS2JNet.System; + +/** + * @author keving + * + */ +public enum TypeCode { + Empty, // A null reference. + Object, // A general type representing any reference or value type not explicitly represented by another TypeCode. + DBNull, // A database null (column) value. + Boolean, // A simple type representing Boolean values of true or false. + Char, // An integral type representing unsigned 16-bit integers with values between 0 and 65535. The set of possible values for the Char type corresponds to the Unicode character set. + SByte, // An integral type representing signed 8-bit integers with values between -128 and 127. + Byte, // An integral type representing unsigned 8-bit integers with values between 0 and 255. + Int16, // An integral type representing signed 16-bit integers with values between -32768 and 32767. + UInt16, // An integral type representing unsigned 16-bit integers with values between 0 and 65535. + Int32, // An integral type representing signed 32-bit integers with values between -2147483648 and 2147483647. + UInt32, // An integral type representing unsigned 32-bit integers with values between 0 and 4294967295. + Int64, // An integral type representing signed 64-bit integers with values between -9223372036854775808 and 9223372036854775807. + UInt64, // An integral type representing unsigned 64-bit integers with values between 0 and 18446744073709551615. + Single, // A floating point type representing values ranging from approximately 1.5 x 10 -45 to 3.4 x 10 38 with a precision of 7 digits. + Double, // A floating point type representing values ranging from approximately 5.0 x 10 -324 to 1.7 x 10 308 with a precision of 15-16 digits. + Decimal, // A simple type representing values ranging from 1.0 x 10 -28 to approximately 7.9 x 10 28 with 28-29 significant digits. + DateTime, // A type representing a date and time value. + String // A sealed class type representing Unicode character strings. +} diff --git a/CS2JLibrary/src/CS2JNet/System/TypeSupport.java b/CS2JLibrary/src/CS2JNet/System/TypeSupport.java index 64bfccd..def20a0 100644 --- a/CS2JLibrary/src/CS2JNet/System/TypeSupport.java +++ b/CS2JLibrary/src/CS2JNet/System/TypeSupport.java @@ -32,6 +32,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; import java.text.ParseException; import java.util.ArrayList; +import java.util.Date; import CS2JNet.System.Reflection.BindingFlags; @@ -169,9 +170,86 @@ public class TypeSupport { } } - public static Field GetField(Class myType, - String fieldName) throws NoSuchFieldException, Exception { - return GetField(myType, fieldName, BindingFlags.getDefault()); - } - + public static Field GetField(Class myType, + String fieldName) throws NoSuchFieldException, Exception { + return GetField(myType, fieldName, BindingFlags.getDefault()); + } + + public static TypeCode GetTypeCode(Class myType) { + TypeCode ret = TypeCode.Object; + + if (myType.isPrimitive()) { + + if (myType.equals(Boolean.TYPE)) { + ret = TypeCode.Boolean; + } + else if (myType.equals(Character.TYPE)) { + ret = TypeCode.Char; + } + else if (myType.equals(Byte.TYPE)) { + ret = TypeCode.Byte; + } + else if (myType.equals(Short.TYPE)) { + ret = TypeCode.Int16; + } + else if (myType.equals(Integer.TYPE)) { + ret = TypeCode.Int32; + } + else if (myType.equals(Long.TYPE)) { + ret = TypeCode.Int64; + } + else if (myType.equals(Float.TYPE)) { + ret = TypeCode.Single; + } + else if (myType.equals(Double.TYPE)) { + ret = TypeCode.Double; + } + else if (myType.equals(Void.TYPE)) { + // No equivalent, return object I guess + } + } + else { + if (myType.isInstance(Boolean.FALSE)) { + ret = TypeCode.Boolean; + } + else if (myType.isInstance(Character.MAX_VALUE)) { + ret = TypeCode.Char; + } + else if (myType.isInstance(Byte.MAX_VALUE)) { + ret = TypeCode.Byte; + } + else if (myType.isInstance(Short.MAX_VALUE)) { + ret = TypeCode.Int16; + } + else if (myType.isInstance(Integer.MAX_VALUE)) { + ret = TypeCode.Int32; + } + else if (myType.isInstance(Long.MAX_VALUE)) { + ret = TypeCode.Int64; + } + else if (myType.isInstance(Float.MAX_VALUE)) { + ret = TypeCode.Single; + } + else if (myType.isInstance(Double.MAX_VALUE)) { + ret = TypeCode.Double; + } + else if (myType.isInstance(new Date())) { + ret = TypeCode.Double; + } + else if (myType.isInstance("")) { + ret = TypeCode.String; + } + } + return ret; + } + + public static void Testmain(String[] args) + { + Boolean b = true; + int i = 0; + System.out.println(TypeCode.Boolean); + System.out.println(GetTypeCode(b.getClass())); + System.out.println(GetTypeCode(((Integer)i).getClass())); + } + }