/*
 * Copyright (c) 2009-2012 Panxiaobo
 * 
 * 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.
 */
package com.googlecode.dex2jar.asm;

import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * @author <a href="mailto:pxb1988@gmail.com">Panxiaobo</a>
 * @version $Rev$
 */
public class LdcOptimizeAdapter extends MethodAdapter implements Opcodes {

    /**
     * @param mv
     */
    public LdcOptimizeAdapter(MethodVisitor mv) {
        super(mv);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.objectweb.asm.MethodAdapter#visitLdcInsn(java.lang.Object)
     */
    @Override
    public void visitLdcInsn(Object cst) {
        if (cst == null) {
            this.visitInsn(ACONST_NULL);
        } else if (cst instanceof Integer) {
            int value = (Integer) cst;
            if (value >= 0 && value <= 5) {
                super.visitInsn(ICONST_0 + value);
            } else if (value <= Byte.MAX_VALUE && value >= Byte.MIN_VALUE) {
                super.visitIntInsn(BIPUSH, value);
            } else if (value <= Short.MAX_VALUE && value >= Short.MIN_VALUE) {
                super.visitIntInsn(SIPUSH, value);
            } else {
                super.visitLdcInsn(cst);
            }
        } else {
            super.visitLdcInsn(cst);
        }
    }

}

@@ -1829,24 +1829,1031 @@
          %7D%0D%0A
+        %7D else if (cst instanceof Long) %7B%0D%0A            long value = (Long) cst;%0D%0A            if (value == 0L %7C%7C value == 1L) %7B%0D%0A                super.visitInsn(LCONST_0 + ((int) value));%0D%0A            %7D else %7B%0D%0A                super.visitLdcInsn(cst);%0D%0A            %7D%0D%0A        %7D else if (cst instanceof Float) %7B%0D%0A            float value = (Float) cst;%0D%0A            if (value == 0.0F) %7B%0D%0A                super.visitInsn(FCONST_0);%0D%0A            %7D else if (value == 1.0F) %7B%0D%0A                super.visitInsn(FCONST_1);%0D%0A            %7D else if (value == 2.0F) %7B%0D%0A                super.visitInsn(FCONST_2);%0D%0A            %7D else %7B%0D%0A                super.visitLdcInsn(cst);%0D%0A            %7D%0D%0A        %7D else if (cst instanceof Double) %7B%0D%0A            double value = (Double) cst;%0D%0A            if (value == 0.0D) %7B%0D%0A                super.visitInsn(DCONST_0);%0D%0A            %7D else if (value == 1.0D) %7B%0D%0A                super.visitInsn(DCONST_1);%0D%0A            %7D else %7B%0D%0A                super.visitLdcInsn(cst);%0D%0A            %7D%0D%0A
         %7D el

use LCONST_0/1 FCONST_0/1/2 DCONST_0/1 is possible