Java Aspectj Usage copyInstruction(Instruction i)

Here you can find the source of copyInstruction(Instruction i)

Description

Fix for Bugzilla #39479, #40109 patch contributed by Andy Clement Need to manually copy Select instructions - if we rely on the the 'fresh' object created by copy(), the InstructionHandle array 'targets' inside the Select object will not have been deep copied, so modifying targets in fresh will modify the original Select - not what we want !

License

Open Source License

Declaration

public static Instruction copyInstruction(Instruction i) 

Method Source Code

//package com.java2s;
/* *******************************************************************
 * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved. /*ww  w . j  ava 2 s.co m*/
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     PARC     initial implementation 
 * ******************************************************************/

import org.aspectj.apache.bcel.generic.Instruction;

import org.aspectj.apache.bcel.generic.InstructionHandle;

import org.aspectj.apache.bcel.generic.InstructionSelect;

import org.aspectj.apache.bcel.generic.SwitchBuilder;

public class Main {
    /**
     * Fix for Bugzilla #39479, #40109 patch contributed by Andy Clement
     * 
     * Need to manually copy Select instructions - if we rely on the the 'fresh' object created by copy(), the InstructionHandle
     * array 'targets' inside the Select object will not have been deep copied, so modifying targets in fresh will modify the
     * original Select - not what we want ! (It is a bug in BCEL to do with cloning Select objects).
     * 
     * <pre>
     * declare error:
     *     call(* Instruction.copy()) &amp;&amp; within(org.aspectj.weaver)
     *       &amp;&amp; !withincode(* Utility.copyInstruction(Instruction)):
     *     &quot;use Utility.copyInstruction to work-around bug in Select.copy()&quot;;
     * </pre>
     */
    public static Instruction copyInstruction(Instruction i) {
        if (i instanceof InstructionSelect) {
            InstructionSelect freshSelect = (InstructionSelect) i;

            // Create a new targets array that looks just like the existing one
            InstructionHandle[] targets = new InstructionHandle[freshSelect.getTargets().length];
            for (int ii = 0; ii < targets.length; ii++) {
                targets[ii] = freshSelect.getTargets()[ii];
            }

            // Create a new select statement with the new targets array

            return new SwitchBuilder(freshSelect.getMatchs(), targets, freshSelect.getTarget()).getInstruction();
        } else {
            return i.copy(); // Use clone for shallow copy...
        }
    }
}

Related

  1. arrayAsList(Object[] ra)
  2. classOrInterface(int access_flags)
  3. collectArguments(JoinPoint jp)
  4. combine(String[] one, String[] two)
  5. convertToFile(String path)
  6. createConstant(InstructionFactory fact, int value)
  7. createInstanceof(InstructionFactory fact, ReferenceType t)
  8. genPointcutDetails(Pointcut pcd)
  9. genSignature(IProgramElement node)