001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.format;
032:
033: import java.math.BigInteger;
034:
035: /**
036: * @author Taras Puchko
037: */
038: abstract class NondecimalIntegralConversion extends NumericConversion {
039:
040: public void format(FormatContext context) {
041: context.checkWidth();
042: context.assertNoPrecision();
043: context.assertNoFlag(',');
044: context.checkFlags();
045: Object argument = context.getArgument();
046: if (argument instanceof Byte) {
047: printf(context, (Byte) argument, Byte.SIZE);
048: } else if (argument instanceof Short) {
049: printf(context, (Short) argument, Short.SIZE);
050: } else if (argument instanceof Integer) {
051: printf(context, (Integer) argument, Integer.SIZE);
052: } else if (argument instanceof Long) {
053: printf(context, (Long) argument, Long.SIZE);
054: } else if (argument instanceof BigInteger) {
055: printf(context, (BigInteger) argument);
056: } else if (argument == null) {
057: context.writePadded(String.valueOf(argument));
058: } else {
059: throw context.getConversionException();
060: }
061: }
062:
063: private void printf(FormatContext context, long argument, int size) {
064: context.assertNoFlag('(');
065: context.assertNoFlag(' ');
066: context.assertNoFlag('+');
067: long value = argument >= 0 || size == Long.SIZE ? argument
068: : argument + (1L << size);
069: printf(context, false, toUnsignedString(value));
070: }
071:
072: private void printf(FormatContext context, BigInteger argument) {
073: printf(context, argument.signum() < 0, toSignedString(argument
074: .abs()));
075: }
076:
077: private void printf(FormatContext context, boolean negative,
078: String value) {
079: String prefix = context.isFlag('#') ? getRadixIndicator()
080: : null;
081: printNumber(context, negative, prefix,
082: new StringBuilder(value), context.getSymbols(false));
083: }
084:
085: protected abstract String getRadixIndicator();
086:
087: protected abstract String toUnsignedString(long value);
088:
089: protected abstract String toSignedString(BigInteger value);
090:
091: public static class OctalConversion extends
092: NondecimalIntegralConversion {
093:
094: protected String getRadixIndicator() {
095: return "0";
096: }
097:
098: protected String toUnsignedString(long value) {
099: return Long.toOctalString(value);
100: }
101:
102: protected String toSignedString(BigInteger value) {
103: return value.toString(8);
104: }
105: }
106:
107: public static class HexadecimalConversion extends
108: NondecimalIntegralConversion {
109:
110: protected String getRadixIndicator() {
111: return "0x";
112: }
113:
114: protected String toUnsignedString(long value) {
115: return Long.toHexString(value);
116: }
117:
118: protected String toSignedString(BigInteger value) {
119: return value.toString(16);
120: }
121: }
122:
123: }
|