/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache BCEL" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache BCEL", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*/
/**
* Template class for building up a method. This is done by defining exception
* handlers, adding thrown exceptions, local variables and attributes, whereas
* the `LocalVariableTable' and `LineNumberTable' attributes will be set
* automatically for the code. Use stripAttributes() if you don't like this.
*
* While generating code it may be necessary to insert NOP operations. You can
* use the `removeNOPs' method to get rid off them.
* The resulting method object can be obtained via the `getMethod()' method.
*
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
* @author <A HREF="http://www.vmeng.com/beard">Patrick C. Beard</A> [setMaxStack()]
* @see InstructionList
* @see Method
*/
private int max_locals;
private int max_stack;
private boolean strip_attributes;
/**
* Declare method. If the method is non-static the constructor
* automatically declares a local variable `$this' in slot 0. The
* actual code is contained in the `il' parameter, which may further
* manipulated by the user. But he must take care not to remove any
* instruction (handles) that are still referenced from this object.
*
* For example one may not add a local variable and later remove the
* instructions it refers to without causing havoc. It is safe
* however if you remove that local variable, too.
*
* @param access_flags access qualifiers
* @param return_type method type
* @param arg_types argument types
* @param arg_names argument names (if this is null, default names will be provided
* for them)
* @param method_name name of method
* @param class_name class name containing this method (may be null, if you don't care)
* @param il instruction list associated with this method, may be null only for
* abstract or native methods
* @param cp constant pool
*/
if(!abstract_) {
/* Add local variables, namely the implicit `this' and the arguments
*/
}
}
for(int i=0; i < size; i++) {
throw new ClassGenException("'void' is an illegal argument type for a method");
}
}
throw new ClassGenException("Mismatch in argument array lengths: " +
} else { // Give them dummy names
for(int i=0; i < size; i++)
arg_names[i] = "arg" + i;
}
if(!abstract_) {
for(int i=0; i < size; i++) {
}
}
}
}
/**
* Instantiate from existing method.
*
* @param m method
* @param class_name class name containing this method
* @param cp constant pool
*/
m.getName(), class_name,
cp);
Attribute a = attributes[i];
if(a instanceof Code) {
setMaxStack(c.getMaxStack());
setMaxLocals(c.getMaxLocals());
if(type > 0) {
}
} else {
}
}
}
a = c_attributes[j];
if(a instanceof LineNumberTable) {
LineNumber l = ln[k];
}
} else if(a instanceof LocalVariableTable) {
LocalVariable l = lv[k];
// Repair malformed handles
}
}
}
} else if (a instanceof LocalVariableTypeTable) {
LocalVariable l = lv[k];
// Repair malformed handles
}
}
}
} else
addCodeAttribute(a);
}
} else if(a instanceof ExceptionTable) {
addException(names[j]);
} else
addAttribute(a);
}
}
/**
* Adds a local variable to this method.
*
* @param name variable name
* @param type variable type
* @param slot the index of the local variable, if type is long or double, the next available
* index is slot+2
* @param start from where the variable is valid
* @param end until where the variable is valid
* @return new local variable object
* @see LocalVariable
*/
int i;
variable_vec.set(i, l);
else
variable_vec.add(l);
return l;
} else {
" as type for local variable");
}
}
/**
* Adds a local variable to this method and assigns an index automatically.
*
* @param name variable name
* @param type variable type
* @param start from where the variable is valid, if this is null,
* it is valid from the start
* @param end until where the variable is valid, if this is null,
* it is valid to the end
* @return new local variable object
* @see LocalVariable
*/
}
/**
* Remove a local variable, its slot will not be reused, if you do not use addLocalVariable
* with an explicit index argument.
*/
variable_vec.remove(l);
}
/**
* Remove all local variables.
*/
public void removeLocalVariables() {
}
/**
* Sort local variables by index
*/
int i = l, j = r;
do {
if(i <= j) {
i++; j--;
}
} while(i <= j);
}
/*
* If the range of the variable has not been set yet, it will be set to be valid from
* the start to the end of the instruction list.
*
* @return array of declared local variables sorted by index
*/
for(int i=0; i < size; i++) {
}
if(size > 1)
return lg;
}
/**
* @return `LocalVariableTable' attribute of all the local variables of this method.
*/
for(int i=0; i < size; i++)
}
/**
* Give an instruction a line number corresponding to the source code line.
*
* @param ih instruction to tag
* @return new line number object
* @see LineNumber
*/
line_number_vec.add(l);
return l;
}
/**
* Remove a line number.
*/
}
/**
* Remove all line numbers.
*/
public void removeLineNumbers() {
}
/*
* @return array of line numbers
*/
return lg;
}
/**
* @return `LineNumberTable' attribute of all the local variables of this method.
*/
try {
for(int i=0; i < size; i++)
} catch(ArrayIndexOutOfBoundsException e) {} // Never occurs
}
/**
* Add an exception handler, i.e., specify region where a handler is active and an
* instruction where the actual handling is done.
*
* @param start_pc Start of region (inclusive)
* @param end_pc End of region (inclusive)
* @param handler_pc Where handling is done
* @param catch_type class type of handled exception or null if any
* exception is handled
* @return new exception handler object
*/
throw new ClassGenException("Exception handler target is null instruction");
exception_vec.add(c);
return c;
}
/**
* Remove an exception handler.
*/
exception_vec.remove(c);
}
/**
* Remove all line numbers.
*/
public void removeExceptionHandlers() {
}
/*
* @return array of declared exception handlers
*/
return cg;
}
/**
* @return code exceptions for `Code' attribute
*/
try {
for(int i=0; i < size; i++) {
}
} catch(ArrayIndexOutOfBoundsException e) {}
return c_exc;
}
/**
* Add an exception possibly thrown by this method.
*
* @param class_name (fully qualified) name of exception
*/
}
/**
* Remove an exception.
*/
throws_vec.remove(c);
}
/**
* Remove all exceptions.
*/
public void removeExceptions() {
throws_vec.clear();
}
/*
* @return array of thrown exceptions
*/
throws_vec.toArray(e);
return e;
}
/**
* @return `Exceptions' attribute of all the exceptions thrown by this method.
*/
try {
for(int i=0; i < size; i++)
} catch(ArrayIndexOutOfBoundsException e) {}
}
/**
* Add an attribute to the code. Currently, the JVM knows about the
* LineNumberTable, LocalVariableTable and StackMap attributes,
* where the former two will be generated automatically and the
* latter is used for the MIDP only. Other attributes will be
* ignored by the JVM but do no harm.
*
* @param a attribute to be added
*/
/**
* Remove a code attribute.
*/
/**
* Remove all code attributes.
*/
public void removeCodeAttributes() {
}
/**
* @return all attributes of this method.
*/
return attributes;
}
/**
* Get method object. Never forget to call setMaxStack() or setMaxStack(max), respectively,
* before calling this method (the same applies for max locals).
*
* @return method object
*/
/* Also updates positions of instructions, i.e., their indices
*/
/* Create LocalVariableTable and LineNumberTable attributes (for debuggers, e.g.)
*/
/* Each attribute causes 6 additional header bytes
*/
int attrs_len = 0;
// Remove any stale code attribute
Attribute a = attributes[i];
if(a instanceof Code)
removeAttribute(a);
}
cp.getConstantPool());
}
// Undo effects of adding attributes
return m;
}
/**
* Remove all NOPs from the instruction list (if possible) and update every
* object refering to them, i.e., branch instructions, local variables and
* exception handlers.
*/
public void removeNOPs() {
/* Check branch instructions.
*/
try {
} catch(TargetLostException e) {
}
}
}
}
}
}
/**
* Set maximum number of local variables.
*/
/**
* Set maximum stack size for this method.
*/
/** @return class that contains this method
*/
}
/**
* Computes max. stack size by performing control flow analysis.
*/
public void setMaxStack() {
else
max_stack = 0;
}
/**
* Compute maximum number of local variables.
*/
public void setMaxLocals() {
if((ins instanceof LocalVariableInstruction) ||
{
}
}
max_locals = max;
} else
max_locals = 0;
}
* LocalVariableTable, like javac -O
*/
static final class BranchTarget {
int stackDepth;
this.stackDepth = stackDepth;
}
}
static final class BranchStack {
return;
}
if(!branchTargets.empty()) {
return bt;
}
return null;
}
return bt;
}
}
}
/**
* Computes stack usage of an instruction list by performing control flow analysis.
*
* @return maximum stack depth used by method
*/
/* Initially, populate the branch stack with the exception
* handlers, because these aren't (necessarily) branched to
* explicitly. in each case, the stack will have depth 1,
* containing the exception object.
*/
if (handler_pc != null)
}
stackDepth += delta;
if(stackDepth > maxStackDepth)
// choose the next instruction based on whether current is a branch.
if(instruction instanceof BranchInstruction) {
if(instruction instanceof Select) {
// explore all of the select's targets. the default target is handled below.
// nothing to fall through to.
} else if(!(branch instanceof IfInstruction)) {
// if an instruction that comes back to following PC,
// push next instruction, with stack depth reduced by 1.
}
// for all branches, the target of the branch is pushed on the branch stack.
// conditional branches have a fall through case, selects don't, and
} else {
// check for instructions that terminate the method.
}
// normal case, go to the next instruction.
// if we have no more instructions, see if there are any deferred branches to explore.
}
}
}
return maxStackDepth;
}
/** Add observer for this object.
*/
}
/** Remove observer for this object.
*/
}
/** Call notify() method on all observers. This method is not called
* automatically whenever the state has changed, but has to be
* called by the user after he has finished editing the object.
*/
public void update() {
}
/**
* Return string representation close to declaration format,
* `public static void _main(String[]) throws IOException', e.g.
*
* @return String representation of the method.
*/
true, getLocalVariableTable(cp));
}
}
/** @return deep copy of this method
*/
}
return mg;
}
}