public void testRemoveLocalVariables() throws Exception { final MethodGen mg = getMethod(Foo.class, "bar"); final LocalVariableGen lv = mg.getLocalVariables()[1]; assertEquals("variable name", "a", lv.getName()); final InstructionHandle start = lv.getStart(); final 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()); }
public void replaceCheckMethod() { ClassGen cg = cgs.get(AuthClass); Method method = cg.getMethods()[0]; MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool()); InstructionList list = new InstructionList(); list.append(new ICONST(1)); list.append(new IRETURN()); mg.removeExceptionHandlers(); mg.removeLineNumbers(); mg.removeLocalVariables(); mg.removeExceptions(); mg.setInstructionList(list); mg.setMaxLocals(); mg.setMaxStack(); cg.replaceMethod(method, mg.getMethod()); }
private void updateLocalVariableTable(final LocalVariableTable a) { final LocalVariable[] lv = a.getLocalVariableTable(); removeLocalVariables(); for (final LocalVariable l : lv) { InstructionHandle start = il.findHandle(l.getStartPC()); final InstructionHandle end = il.findHandle(l.getStartPC() + l.getLength()); // Repair malformed handles if (null == start) { start = il.getStart(); } // end == null => live to end of method // Since we are recreating the LocalVaraible, we must // propagate the orig_index to new copy. addLocalVariable(l.getName(), Type.getType(l.getSignature()), l .getIndex(), start, end, l.getOrigIndex()); } }
LocalVariable[] lv = ((LocalVariableTable)a).getLocalVariableTable(); removeLocalVariables();