use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class ExceptionSoftening method collectExceptions.
/**
* collects all the valid exception objects (ones where start and finish are before the target) and with a catch type
*
* @param exceptions
* the exceptions from the class file
* @return the filtered exceptions keyed by catch end pc
*/
private static LinkedHashMap<Integer, CodeException> collectExceptions(CodeException... exceptions) {
List<CodeException> filteredEx = new ArrayList<>();
for (CodeException ce : exceptions) {
if ((ce.getCatchType() != 0) && (ce.getStartPC() < ce.getEndPC()) && (ce.getEndPC() <= ce.getHandlerPC())) {
filteredEx.add(ce);
}
}
LinkedHashMap<Integer, CodeException> handlers = new LinkedHashMap<>();
for (CodeException ex : filteredEx) {
handlers.put(Integer.valueOf(ex.getEndPC()), ex);
}
return handlers;
}
use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class PossiblyRedundantMethodCalls method visitCode.
/**
* implements the visitor to reset the stack, and method call maps for new method Note: that when collecting branch targets, it's unfortunately not good
* enough to just collect the handler pcs, as javac plays fast and loose, and will sometimes jam code below the end pc and before the first handler pc,
* which gets executed. So we need to clear our maps if we go past the end pc as well.
*
* @param obj
* the context object of the currently parsed code block
*/
@Override
public void visitCode(Code obj) {
stack.resetForMethodEntry(this);
localMethodCalls.clear();
fieldMethodCalls.clear();
staticMethodCalls.clear();
branchTargets.clear();
CodeException[] codeExceptions = obj.getExceptionTable();
for (CodeException codeEx : codeExceptions) {
// adding the end pc seems silly, but it is need because javac may repeat
// part of the finally block in the try block, at times.
branchTargets.set(codeEx.getEndPC());
branchTargets.set(codeEx.getHandlerPC());
}
super.visitCode(obj);
}
use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class PresizeCollections method addExceptionRanges.
/**
* adds optionalRanges for all try/catch blocks
*
* @param c
* the currently parsed code object
*/
private void addExceptionRanges(Code c) {
Map<CodeRange, List<Integer>> ranges = new HashMap<>();
CodeException[] ces = c.getExceptionTable();
if (ces != null) {
for (CodeException ce : ces) {
CodeRange range = new CodeRange(ce.getStartPC(), ce.getEndPC(), false);
List<Integer> handlers = ranges.get(range);
if (handlers == null) {
handlers = new ArrayList<>(6);
ranges.put(range, handlers);
}
handlers.add(ce.getHandlerPC());
}
}
for (Map.Entry<CodeRange, List<Integer>> entry : ranges.entrySet()) {
optionalRanges.add(entry.getKey());
List<Integer> handlers = entry.getValue();
Collections.sort(handlers);
for (int h = 0; h < (handlers.size() - 1); h++) {
optionalRanges.add(new CodeRange(handlers.get(h), handlers.get(h + 1), false));
}
}
}
use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class UnnecessaryStoreBeforeReturn method visitCode.
/**
* implements the visitor to make sure method returns a value, and then clears the targets
*
* @param obj
* the context object of the currently parsed code block
*/
@Override
public void visitCode(Code obj) {
Method m = getMethod();
String sig = m.getSignature();
if (!Values.SIG_VOID.equals(SignatureUtils.getReturnSignature(sig))) {
state = State.SEEN_NOTHING;
branchTargets.clear();
CodeException[] ces = obj.getExceptionTable();
catchTargets.clear();
stack.resetForMethodEntry(this);
for (CodeException ce : ces) {
if (ce.getCatchType() != 0) {
catchTargets.set(ce.getHandlerPC());
}
}
super.visitCode(obj);
}
}
use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class UseTryWithResources method prescreen.
private boolean prescreen(Code obj) {
if (getMethod().isNative()) {
return false;
}
CodeException[] ces = obj.getExceptionTable();
if (CollectionUtils.isEmpty(ces)) {
return false;
}
boolean hasFinally = false;
for (CodeException ce : ces) {
if (ce.getCatchType() == 0) {
finallyBlocks.put(Integer.valueOf(ce.getHandlerPC()), new TryBlock(ce.getStartPC(), ce.getEndPC(), ce.getHandlerPC(), obj.getCode().length - 1));
hasFinally = true;
}
}
return hasFinally;
}
Aggregations