org.friz.transformers.IntShiftTransformer.java Source code

Java tutorial

Introduction

Here is the source code for org.friz.transformers.IntShiftTransformer.java

Source

/**
 * Copyright (c) 2015 Kyle Friz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.friz.transformers;

import java.util.Collection;
import java.util.Iterator;

import org.friz.Transformer;
import org.friz.bytecode.Container;
import org.friz.bytecode.element.ClassElement;
import org.friz.bytecode.element.MethodElement;
import org.friz.bytecode.insn.InsnMatcher;
import org.friz.bytecode.insn.InsnNodeUtility;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.IntInsnNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Kyle Friz
 * @since Apr 4, 2015
 */
public class IntShiftTransformer extends Transformer {

    /**
     * The {@link Logger} instance
     */
    private static Logger logger = LoggerFactory.getLogger(IntShiftTransformer.class);

    public IntShiftTransformer(Container container) {
        super(container);
    }

    /* (non-Javadoc)
     * @see org.friz.Transformer#transform(java.util.Collection)
     */
    @Override
    public void transform(Collection<ClassElement> elements) {
        for (ClassElement element : elements) {
            for (MethodElement method : element.methods()) {
                InsnMatcher matcher = new InsnMatcher(method.insn());
                for (Iterator<AbstractInsnNode[]> it = matcher.match("pushinstruction ((ISHL)|(ISHR)|(IUSHR))"); it
                        .hasNext();) {
                    AbstractInsnNode[] nodes = it.next();
                    AbstractInsnNode node = nodes[0];
                    if (node instanceof IntInsnNode || node instanceof LdcInsnNode
                            || (node instanceof InsnNode && (node.getOpcode() >= 2 && node.getOpcode() <= 8))) {
                        long push = InsnNodeUtility.getNumericPushValue(node);
                        long realpush = push & 0x1f;
                        if (push != realpush) {
                            method.insn().set(node, InsnNodeUtility.createNumericPushInsn(realpush));
                            pInc(0);
                        } else {
                            tInc(0);
                        }
                    }
                }
            }
        }
    }

    /* (non-Javadoc)
     * @see org.friz.Transformer#log()
     */
    @Override
    public void log() {
        logger.info("Fixed " + pointer(0) + " out of " + total(0) + " Integer Bit Shifts.");
    }
}