private static string ConvertStringToJava(string value, bool ascii) { char[] arr = value.ToCharArray(); StringBuilder buffer = new StringBuilder(arr.Length); foreach (char c in arr) { switch (c) { case '\\': { // u005c: backslash \ buffer.Append("\\\\"); break; } case (char)unchecked ((int)(0x8)): { // "\\\\b"); // u0008: backspace BS buffer.Append("\\b"); break; } case (char)unchecked ((int)(0x9)): { //"\\\\t"); // u0009: horizontal tab HT buffer.Append("\\t"); break; } case (char)unchecked ((int)(0xA)): { //"\\\\n"); // u000a: linefeed LF buffer.Append("\\n"); break; } case (char)unchecked ((int)(0xC)): { //"\\\\f"); // u000c: form feed FF buffer.Append("\\f"); break; } case (char)unchecked ((int)(0xD)): { //"\\\\r"); // u000d: carriage return CR buffer.Append("\\r"); break; } case (char)unchecked ((int)(0x22)): { //"\\\\\""); // u0022: double quote " buffer.Append("\\\""); break; } default: { //case 0x27: //"\\\\'"); // u0027: single quote ' // buffer.append("\\\'"); // break; if (IsPrintableAscii(c) || !ascii && TextUtil.IsPrintableUnicode(c)) { buffer.Append(c); } else { buffer.Append(TextUtil.CharToUnicodeLiteral(c)); } break; } } } return(buffer.ToString()); }
public override TextBuffer ToJava(int indent, BytecodeMappingTracer tracer) { bool literal = DecompilerContext.GetOption(IFernflowerPreferences.Literals_As_Is ); bool ascii = DecompilerContext.GetOption(IFernflowerPreferences.Ascii_String_Characters ); tracer.AddMapping(bytecode); if (constType.type != ICodeConstants.Type_Null && value == null) { return(new TextBuffer(ExprProcessor.GetCastTypeName(constType))); } switch (constType.type) { case ICodeConstants.Type_Boolean: { return(new TextBuffer(((int)value != 0).ToString())); } case ICodeConstants.Type_Char: { int val = (int)value; string ret = Char_Escapes.GetOrNull(val); if (ret == null) { char c = (char)val; if (IsPrintableAscii(c) || !ascii && TextUtil.IsPrintableUnicode(c)) { ret = c.ToString(); } else { ret = TextUtil.CharToUnicodeLiteral(c); } } return(new TextBuffer(ret).Enclose("'", "'")); } case ICodeConstants.Type_Byte: case ICodeConstants.Type_Bytechar: case ICodeConstants.Type_Short: case ICodeConstants.Type_Shortchar: case ICodeConstants.Type_Int: { int intVal = (int)value; if (!literal) { if (intVal == int.MaxValue) { return(new FieldExprent("MAX_VALUE", "java/lang/Integer", true, null, FieldDescriptor .Integer_Descriptor, bytecode).ToJava(0, tracer)); } else if (intVal == int.MinValue) { return(new FieldExprent("MIN_VALUE", "java/lang/Integer", true, null, FieldDescriptor .Integer_Descriptor, bytecode).ToJava(0, tracer)); } } return(new TextBuffer(value.ToString())); } case ICodeConstants.Type_Long: { long longVal = (long)value; if (!literal) { if (longVal == long.MaxValue) { return(new FieldExprent("MAX_VALUE", "java/lang/Long", true, null, FieldDescriptor .Long_Descriptor, bytecode).ToJava(0, tracer)); } else if (longVal == long.MinValue) { return(new FieldExprent("MIN_VALUE", "java/lang/Long", true, null, FieldDescriptor .Long_Descriptor, bytecode).ToJava(0, tracer)); } } return(new TextBuffer(value.ToString()).Append('L')); } case ICodeConstants.Type_Float: { float floatVal = (float)value; if (!literal) { if (float.IsNaN(floatVal)) { return(new FieldExprent("NaN", "java/lang/Float", true, null, FieldDescriptor.Float_Descriptor , bytecode).ToJava(0, tracer)); } else if (floatVal == float.PositiveInfinity) { return(new FieldExprent("POSITIVE_INFINITY", "java/lang/Float", true, null, FieldDescriptor .Float_Descriptor, bytecode).ToJava(0, tracer)); } else if (floatVal == float.NegativeInfinity) { return(new FieldExprent("NEGATIVE_INFINITY", "java/lang/Float", true, null, FieldDescriptor .Float_Descriptor, bytecode).ToJava(0, tracer)); } else if (floatVal == float.MaxValue) { return(new FieldExprent("MAX_VALUE", "java/lang/Float", true, null, FieldDescriptor .Float_Descriptor, bytecode).ToJava(0, tracer)); } else if (floatVal == float.MinValue) { return(new FieldExprent("MIN_VALUE", "java/lang/Float", true, null, FieldDescriptor .Float_Descriptor, bytecode).ToJava(0, tracer)); } } else if (float.IsNaN(floatVal)) { return(new TextBuffer("0.0F / 0.0")); } else if (floatVal == float.PositiveInfinity) { return(new TextBuffer("1.0F / 0.0")); } else if (floatVal == float.NegativeInfinity) { return(new TextBuffer("-1.0F / 0.0")); } return(new TextBuffer(value.ToString()).Append('F')); } case ICodeConstants.Type_Double: { double doubleVal = (double)value; if (!literal) { if (double.IsNaN(doubleVal)) { return(new FieldExprent("NaN", "java/lang/Double", true, null, FieldDescriptor.Double_Descriptor , bytecode).ToJava(0, tracer)); } else if (doubleVal == double.PositiveInfinity) { return(new FieldExprent("POSITIVE_INFINITY", "java/lang/Double", true, null, FieldDescriptor .Double_Descriptor, bytecode).ToJava(0, tracer)); } else if (doubleVal == double.NegativeInfinity) { return(new FieldExprent("NEGATIVE_INFINITY", "java/lang/Double", true, null, FieldDescriptor .Double_Descriptor, bytecode).ToJava(0, tracer)); } else if (doubleVal == double.MaxValue) { return(new FieldExprent("MAX_VALUE", "java/lang/Double", true, null, FieldDescriptor .Double_Descriptor, bytecode).ToJava(0, tracer)); } else if (doubleVal == double.MinValue) { return(new FieldExprent("MIN_VALUE", "java/lang/Double", true, null, FieldDescriptor .Double_Descriptor, bytecode).ToJava(0, tracer)); } } else if (double.IsNaN(doubleVal)) { return(new TextBuffer("0.0D / 0.0")); } else if (doubleVal == double.PositiveInfinity) { return(new TextBuffer("1.0D / 0.0")); } else if (doubleVal == double.NegativeInfinity) { return(new TextBuffer("-1.0D / 0.0")); } return(new TextBuffer(value.ToString()).Append('D')); } case ICodeConstants.Type_Null: { return(new TextBuffer("null")); } case ICodeConstants.Type_Object: { if (constType.Equals(VarType.Vartype_String)) { return(new TextBuffer(ConvertStringToJava(value.ToString(), ascii)).Enclose("\"", "\"")); } else if (constType.Equals(VarType.Vartype_Class)) { string stringVal = value.ToString(); VarType type = new VarType(stringVal, !stringVal.StartsWith("[")); return(new TextBuffer(ExprProcessor.GetCastTypeName(type)).Append(".class")); } break; } } throw new Exception("invalid constant type: " + constType); }