Fixed getXConstant() for Enums
authorJan Vrany <jan.vrany@fit.cvut.cz>
Thu, 13 Nov 2014 00:25:54 +0100
changeset 3272 7a2b5c67bc0f
parent 3271 34baed88b792
child 3273 31b5a49df0e0
Fixed getXConstant() for Enums For Enum constants, create EnumConstantSignature just like standard Eclipse ClassReader.
tools/java/src/stx/libjava/tools/environment/ReflectiveUtils.java
--- a/tools/java/src/stx/libjava/tools/environment/ReflectiveUtils.java	Tue Nov 11 21:02:07 2014 +0100
+++ b/tools/java/src/stx/libjava/tools/environment/ReflectiveUtils.java	Thu Nov 13 00:25:54 2014 +0100
@@ -1,5 +1,6 @@
 package stx.libjava.tools.environment;
 
+import org.eclipse.jdt.internal.compiler.env.EnumConstantSignature;
 import org.eclipse.jdt.internal.compiler.impl.BooleanConstant;
 import org.eclipse.jdt.internal.compiler.impl.ByteConstant;
 import org.eclipse.jdt.internal.compiler.impl.CharConstant;
@@ -17,39 +18,41 @@
 	 * value. Works for primitive-wrapper classes (like Byte, Short, Float,...),
 	 * String and arrays of such classes. Returns null for any other class.
 	 * 
-	 * @param type an object to be converted to its constant alternative
+	 * @param value an object to be converted to its constant alternative
 	 * @return constant form of the given object or null if the given objects' 
 	 * type isn't supported
 	 */
-	public static Object getXConstant(Object type) {
-		if (type == null)
+	public static Object getXConstant(Object value) {
+		if (value == null)
 			return null;
 		
-		if (type.getClass().isArray()) {
-			Object[] array = (Object[]) type;
+		if (value.getClass().isArray()) {
+			Object[] array = (Object[]) value;
 			Object[] arrayConstant = new Object[array.length];
 			for (int i = 0; i < array.length; i++) {
 				arrayConstant[i] = getXConstant(array[i]);
 			}
 			return arrayConstant;
-		} else if (type instanceof Byte) {
-			return ByteConstant.fromValue(((Byte) type).byteValue());
-		} else if (type instanceof Short) {
-			return ShortConstant.fromValue(((Short) type).shortValue());
-		} else if (type instanceof Integer) {
-			return IntConstant.fromValue(((Integer) type).intValue());
-		} else if (type instanceof Long) {
-			return LongConstant.fromValue(((Long) type).longValue());
-		} else if (type instanceof Float) {
-			return FloatConstant.fromValue(((Float) type).floatValue());
-		} else if (type instanceof Double) {
-			return DoubleConstant.fromValue(((Double) type).doubleValue());
-		} else if (type instanceof Character) {
-			return CharConstant.fromValue(((Character) type).charValue());
-		} else if (type instanceof Boolean) {
-			return BooleanConstant.fromValue(((Boolean) type).booleanValue());
-		} else if (type instanceof String) {
-			return StringConstant.fromValue((String) type);
+		} else if (value instanceof Byte) {
+			return ByteConstant.fromValue(((Byte) value).byteValue());
+		} else if (value instanceof Short) {
+			return ShortConstant.fromValue(((Short) value).shortValue());
+		} else if (value instanceof Integer) {
+			return IntConstant.fromValue(((Integer) value).intValue());
+		} else if (value instanceof Long) {
+			return LongConstant.fromValue(((Long) value).longValue());
+		} else if (value instanceof Float) {
+			return FloatConstant.fromValue(((Float) value).floatValue());
+		} else if (value instanceof Double) {
+			return DoubleConstant.fromValue(((Double) value).doubleValue());
+		} else if (value instanceof Character) {
+			return CharConstant.fromValue(((Character) value).charValue());
+		} else if (value instanceof Boolean) {
+			return BooleanConstant.fromValue(((Boolean) value).booleanValue());
+		} else if (value instanceof String) {
+			return StringConstant.fromValue((String) value);
+		} else if (value.getClass().isEnum()) {
+			return new EnumConstantSignature(getSigFromName(value.getClass().getName()).toCharArray(), ((Enum)value).name().toCharArray());
 		}
 		return null;
 	}