src/share/classes/com/sun/tools/javac/util/Constants.java

Print this page




  66         value = decode(value, type);
  67         switch (type.tag) {
  68         case BYTE:      return formatByte((Byte) value);
  69         case LONG:      return formatLong((Long) value);
  70         case FLOAT:     return formatFloat((Float) value);
  71         case DOUBLE:    return formatDouble((Double) value);
  72         case CHAR:      return formatChar((Character) value);
  73         }
  74         if (value instanceof String)
  75             return formatString((String) value);
  76         return value + "";
  77     }
  78 
  79     /**
  80      * Returns a string representation of a constant value (given in
  81      * standard wrapped representation), quoted and formatted as in
  82      * Java source.
  83      */
  84     public static String format(Object value) {
  85         if (value instanceof Byte)      return formatByte((Byte) value);

  86         if (value instanceof Long)      return formatLong((Long) value);
  87         if (value instanceof Float)     return formatFloat((Float) value);
  88         if (value instanceof Double)    return formatDouble((Double) value);
  89         if (value instanceof Character) return formatChar((Character) value);
  90         if (value instanceof String)    return formatString((String) value);
  91         return value + "";







  92     }
  93 
  94     private static String formatByte(byte b) {
  95         return String.format("0x%02x", b);




  96     }
  97 
  98     private static String formatLong(long lng) {
  99         return lng + "L";
 100     }
 101 
 102     private static String formatFloat(float f) {
 103         if (Float.isNaN(f))
 104             return "0.0f/0.0f";
 105         else if (Float.isInfinite(f))
 106             return (f < 0) ? "-1.0f/0.0f" : "1.0f/0.0f";
 107         else
 108             return f + "f";
 109     }
 110 
 111     private static String formatDouble(double d) {
 112         if (Double.isNaN(d))
 113             return "0.0/0.0";
 114         else if (Double.isInfinite(d))
 115             return (d < 0) ? "-1.0/0.0" : "1.0/0.0";


  66         value = decode(value, type);
  67         switch (type.tag) {
  68         case BYTE:      return formatByte((Byte) value);
  69         case LONG:      return formatLong((Long) value);
  70         case FLOAT:     return formatFloat((Float) value);
  71         case DOUBLE:    return formatDouble((Double) value);
  72         case CHAR:      return formatChar((Character) value);
  73         }
  74         if (value instanceof String)
  75             return formatString((String) value);
  76         return value + "";
  77     }
  78 
  79     /**
  80      * Returns a string representation of a constant value (given in
  81      * standard wrapped representation), quoted and formatted as in
  82      * Java source.
  83      */
  84     public static String format(Object value) {
  85         if (value instanceof Byte)      return formatByte((Byte) value);
  86         if (value instanceof Short)     return formatShort((Short) value);
  87         if (value instanceof Long)      return formatLong((Long) value);
  88         if (value instanceof Float)     return formatFloat((Float) value);
  89         if (value instanceof Double)    return formatDouble((Double) value);
  90         if (value instanceof Character) return formatChar((Character) value);
  91         if (value instanceof String)    return formatString((String) value);
  92         if (value instanceof Integer ||
  93             value instanceof Boolean)   return value.toString();
  94         else
  95             throw new IllegalArgumentException("Argument is not a primitive type or a string; it " +
  96                                                ((value == null) ?
  97                                                 "is a null value." :
  98                                                 "has class " +
  99                                                 value.getClass().getName()) + "." );
 100     }
 101 
 102     private static String formatByte(byte b) {
 103         return String.format("(byte)0x%02x", b);
 104     }
 105 
 106     private static String formatShort(short s) {
 107         return String.format("(short)%d", s);
 108     }
 109 
 110     private static String formatLong(long lng) {
 111         return lng + "L";
 112     }
 113 
 114     private static String formatFloat(float f) {
 115         if (Float.isNaN(f))
 116             return "0.0f/0.0f";
 117         else if (Float.isInfinite(f))
 118             return (f < 0) ? "-1.0f/0.0f" : "1.0f/0.0f";
 119         else
 120             return f + "f";
 121     }
 122 
 123     private static String formatDouble(double d) {
 124         if (Double.isNaN(d))
 125             return "0.0/0.0";
 126         else if (Double.isInfinite(d))
 127             return (d < 0) ? "-1.0/0.0" : "1.0/0.0";