Author: ebourg Date: Fri Feb 20 14:52:39 2015 New Revision: 1661134 URL: http://svn.apache.org/r1661134 Log: MethodGen.removeLocalVariable/s now remove the associated targetters. Thanks to Mark Roberts (BCEL-207)
Added: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/ commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java (with props) Modified: commons/proper/bcel/trunk/RELEASE-NOTES.txt commons/proper/bcel/trunk/src/changes/changes.xml commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/LocalVariableGen.java commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java Modified: commons/proper/bcel/trunk/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/RELEASE-NOTES.txt?rev=1661134&r1=1661133&r2=1661134&view=diff ============================================================================== --- commons/proper/bcel/trunk/RELEASE-NOTES.txt (original) +++ commons/proper/bcel/trunk/RELEASE-NOTES.txt Fri Feb 20 14:52:39 2015 @@ -98,6 +98,7 @@ Bug fixes from 5.2 [BCEL-181] ClassLoaderRepository.loadClass(String) leaks input streams [BCEL-194] LocalVariableGen hashCode() function is incorrrect [BCEL-197] Add support for TypeVariables to Utility.signatureToString() +[BCEL-207] MethodGen.removeLocalVariable(s) doesn't remove the associated Targetters Feedback Modified: commons/proper/bcel/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/changes/changes.xml?rev=1661134&r1=1661133&r2=1661134&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/changes/changes.xml (original) +++ commons/proper/bcel/trunk/src/changes/changes.xml Fri Feb 20 14:52:39 2015 @@ -63,6 +63,10 @@ The <action> type attribute can be add,u <body> <release version="6.0" date="TBA" description="Major release with Java 7 and 8 support"> + <action issue="BCEL-207" type="fix" due-to="Mark Roberts"> + MethodGen.removeLocalVariable now properly unreference the removed variable + from the targetters of the instruction handlers delimiting the scope of the variable. + </action> <action issue="BCEL-197" type="fix" due-to="Mark Roberts"> Utility.signatureToString() no longer throws a ClassFormatException on TypeVariables found in generic signatures. Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/LocalVariableGen.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/LocalVariableGen.java?rev=1661134&r1=1661133&r2=1661134&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/LocalVariableGen.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/LocalVariableGen.java Fri Feb 20 14:52:39 2015 @@ -163,6 +163,13 @@ public class LocalVariableGen implements } } + /** + * Clear the references from and to this variable when it's removed. + */ + void dispose() { + setStart(null); + setEnd(null); + } /** * @return true, if ih is target of this variable Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java?rev=1661134&r1=1661133&r2=1661134&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java Fri Feb 20 14:52:39 2015 @@ -335,6 +335,7 @@ public class MethodGen extends FieldGenO * with an explicit index argument. */ public void removeLocalVariable( LocalVariableGen l ) { + l.dispose(); variable_vec.remove(l); } @@ -343,6 +344,9 @@ public class MethodGen extends FieldGenO * Remove all local variables. */ public void removeLocalVariables() { + for (LocalVariableGen lv : variable_vec) { + lv.dispose(); + } variable_vec.clear(); } Added: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java?rev=1661134&view=auto ============================================================================== --- commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java (added) +++ commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java Fri Feb 20 14:52:39 2015 @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.bcel.generic; + +import java.util.Arrays; + +import junit.framework.TestCase; +import org.apache.bcel.Repository; +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.classfile.Method; + +public class MethodGenTestCase extends TestCase { + + public static class Foo { + public void bar() { + int a = 1; + } + } + + private MethodGen getMethod(Class cls, String name) throws ClassNotFoundException { + JavaClass jc = Repository.lookupClass(cls); + ConstantPoolGen cp = new ConstantPoolGen(jc.getConstantPool()); + for (Method method : jc.getMethods()) { + if (method.getName().equals(name)) { + return new MethodGen(method, jc.getClassName(), cp); + } + } + + fail("Method " + name + " not found in class " + cls); + return null; + } + + public void testRemoveLocalVariable() throws Exception { + MethodGen mg = getMethod(Foo.class, "bar"); + + LocalVariableGen lv = mg.getLocalVariables()[1]; + assertEquals("variable name", "a", lv.getName()); + InstructionHandle start = lv.getStart(); + InstructionHandle end = lv.getEnd(); + assertNotNull("scope start", start); + assertNotNull("scope end", end); + assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv)); + assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv)); + + // now let's remove the local variable + mg.removeLocalVariable(lv); + + assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv)); + assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv)); + assertNull("scope start", lv.getStart()); + assertNull("scope end", lv.getEnd()); + } + + public void testRemoveLocalVariables() throws Exception { + MethodGen mg = getMethod(Foo.class, "bar"); + + LocalVariableGen lv = mg.getLocalVariables()[1]; + assertEquals("variable name", "a", lv.getName()); + InstructionHandle start = lv.getStart(); + InstructionHandle end = lv.getEnd(); + assertNotNull("scope start", start); + assertNotNull("scope end", end); + assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv)); + assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv)); + + // now let's remove the local variables + mg.removeLocalVariables(); + + assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv)); + assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv)); + assertNull("scope start", lv.getStart()); + assertNull("scope end", lv.getEnd()); + } +} Propchange: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL