org.apache.bcel.generic.InstructionHandle Java Examples

The following examples show how to use org.apache.bcel.generic.InstructionHandle. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CFG.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Location getPreviousLocation(Location loc) {
    InstructionHandle handle = loc.getHandle();

    BasicBlock basicBlock = loc.getBasicBlock();
    if (basicBlock.getFirstInstruction().equals(handle)) {
        BasicBlock prevBlock = basicBlock;

        while (true) {
            prevBlock = getPredecessorWithEdgeType(prevBlock, EdgeTypes.FALL_THROUGH_EDGE);
            if (prevBlock == null) {
                return loc;
            }

            handle = prevBlock.getLastInstruction();
            if (handle != null) {
                return new Location(handle, prevBlock);
            }
        }

    } else {
        handle = handle.getPrev();
        return new Location(handle, basicBlock);

    }

}
 
Example #2
Source File: VisitorSet.java    From contribution with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @see org.apache.bcel.classfile.Visitor#visitCode
 */
public void visitCode(Code aCode)
{   
    for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
        IDeepVisitor visitor = (IDeepVisitor) iter.next();
        Visitor v = visitor.getClassFileVisitor();
        aCode.accept(v);
    }
    
    // perform a deep visit
    final byte[] code = aCode.getCode();
    final InstructionList list = new InstructionList(code);
    final Iterator it = list.iterator();
    for (Iterator iter = list.iterator(); iter.hasNext();) {
        InstructionHandle instruction = (InstructionHandle) iter.next();
        visitInstructionHandle(instruction);
    }
}
 
Example #3
Source File: TestReturn01Creator.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
private void createMethod_1() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS,
          new String[] {  }, "foo", TEST_PACKAGE+".TestReturn01", il, _cp);

  final InstructionHandle ih_0 = il.append(_factory.createNew("java.lang.Object"));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(InstructionConst.DUP);
  il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
  il.append(InstructionConst.NOP);
  final InstructionHandle ih_8 = il.append(InstructionFactory.createReturn(Type.OBJECT));
  Assert.assertNotNull(ih_8); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example #4
Source File: BetterCFGBuilder2.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Get the basic block in the subroutine for the given instruction. If
 * the block doesn't exist yet, it is created, and a work list item is
 * added which will populate it. Note that if start is an exception
 * thrower, the block returned will be its ETB.
 *
 * @param start
 *            the start instruction for the block
 * @return the basic block for the instruction
 */
public BasicBlock getBlock(InstructionHandle start) {
    BasicBlock block = blockMap.get(start);
    if (block == null) {
        block = allocateBasicBlock();
        blockMap.put(start, block);

        // Block is an exception handler?
        CodeExceptionGen exceptionGen = exceptionHandlerMap.getHandlerForStartInstruction(start);
        if (exceptionGen != null) {
            block.setExceptionGen(null, exceptionGen);
        }

        addItem(new WorkListItem(start, block));
    }
    return block;
}
 
Example #5
Source File: ObligationAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, StateSet fact)
        throws DataflowAnalysisException {
    Collection<ObligationPolicyDatabaseAction> actionList = actionCache.getActions(basicBlock, handle);
    if (actionList.isEmpty()) {
        return;
    }
    if (DEBUG) {
        System.out.println("Applying actions at " + handle + " to " + fact);
    }
    for (ObligationPolicyDatabaseAction action : actionList) {
        if (DEBUG) {
            System.out.print("  " + action + "...");
        }
        action.apply(fact, basicBlock.getLabel());
        if (DEBUG) {
            System.out.println(fact);
        }
    }
}
 
Example #6
Source File: FinallyDuplicatesInfoFactory.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public List<Edge> getDuplicates(CFG cfg, Edge edge) {
    InstructionHandle ih = edge.getSource().getLastInstruction();
    if (ih == null) {
        return Collections.emptyList();
    }
    BitSet duplicates = getDuplicates(ih.getPosition());
    if (duplicates.isEmpty()) {
        return Collections.emptyList();
    }
    List<Edge> result = new ArrayList<>();
    for (Iterator<Edge> edgeIterator = cfg.edgeIterator(); edgeIterator.hasNext();) {
        Edge next = edgeIterator.next();
        if (next.getType() != edge.getType()) {
            continue;
        }
        InstructionHandle lastInst = next.getSource().getLastInstruction();
        if (lastInst != null && lastInst.getPosition() >= 0 && duplicates.get(lastInst.getPosition())) {
            result.add(next);
        }
    }
    return result;
}
 
Example #7
Source File: BetterCFGBuilder2.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @param handle instruction handle which loads the object for further GETFIELD/PUTFIELD operation
 * @return true if this object is known to be non-null
 */
private boolean isSafeFieldSource(InstructionHandle handle) {
    while (handle != null && handle.getInstruction().getOpcode() == Const.DUP) {
        // Some compilers generate DUP for field increment code like
        // ALOAD_0 / DUP / GETFIELD x / ICONST_1 / IADD / PUTFIELD x
        handle = handle.getPrev();
    }
    if (handle == null) {
        return false;
    }
    Instruction inst = handle.getInstruction();
    if (inst.getOpcode() == Const.ALOAD_0) {
        return true;
    }
    return inst instanceof GETFIELD && ((GETFIELD) inst).getFieldName(cpg).startsWith("this$");
}
 
Example #8
Source File: RegisterReceiverPermissionDetector.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected InjectionPoint getInjectionPoint(InvokeInstruction invoke, ConstantPoolGen cpg,
                                           InstructionHandle handle) {
    assert invoke != null && cpg != null;

    String method = invoke.getMethodName(cpg);
    String sig    = invoke.getSignature(cpg);

    if(method.equals("registerReceiver")){
        if(sig.contains("Ljava/lang/String;")){
            if(sig.contains(";I)")){
                return new InjectionPoint(new int[]{2}, ANDROID_REGISTER_RECEIVER_TYPE);
            }else{
                return new InjectionPoint(new int[]{1}, ANDROID_REGISTER_RECEIVER_TYPE);
            }
        }
    }
    return InjectionPoint.NONE;
}
 
Example #9
Source File: WebViewLoadDataDetector.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
    protected InjectionPoint getInjectionPoint(InvokeInstruction invoke, ConstantPoolGen cpg,
                                               InstructionHandle handle) {
        assert invoke != null && cpg != null;

        String method = invoke.getMethodName(cpg);
        String sig    = invoke.getSignature(cpg);
//        System.out.println(invoke.getClassName(cpg));
        if(sig.contains("Ljava/lang/String;")) {
            if("loadUrl".equals(method)){
                if(sig.contains("Ljava/util/Map;")){
                    return new InjectionPoint(new int[]{1}, WEBVIEW_LOAD_DATA_URL_TYPE);
                }else{
                    return new InjectionPoint(new int[]{0}, WEBVIEW_LOAD_DATA_URL_TYPE);
                }
            }else if("loadData".equals(method)){
                return new InjectionPoint(new int[]{2}, WEBVIEW_LOAD_DATA_URL_TYPE);
            }else if("loadDataWithBaseURL".equals(method)){
                //BUG
                return new InjectionPoint(new int[]{4}, WEBVIEW_LOAD_DATA_URL_TYPE);
            }
        }
        return InjectionPoint.NONE;
    }
 
Example #10
Source File: Subroutines.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the leaving RET instruction. Must be invoked after all instructions are added.
 * Must not be invoked for top-level 'subroutine'.
 */
void setLeavingRET() {
    if (localVariable == UNSET) {
        throw new AssertionViolatedException(
            "setLeavingRET() called for top-level 'subroutine' or forgot to set local variable first.");
    }
    InstructionHandle ret = null;
    for (final InstructionHandle actual : instructions) {
        if (actual.getInstruction() instanceof RET) {
            if (ret != null) {
                throw new StructuralCodeConstraintException(
                    "Subroutine with more then one RET detected: '"+ret+"' and '"+actual+"'.");
            }
            ret = actual;
        }
    }
    if (ret == null) {
        throw new StructuralCodeConstraintException("Subroutine without a RET detected.");
    }
    if (((RET) ret.getInstruction()).getIndex() != localVariable) {
        throw new StructuralCodeConstraintException(
            "Subroutine uses '"+ret+"' which does not match the correct local variable '"+localVariable+"'.");
    }
    theRET = ret;
}
 
Example #11
Source File: RedundantConditions.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @param methodGen method
 * @param start instruction to scan
 * @return instruction which consumes value which was on top of stack before start instruction
 * or null if cannot be determined
 */
private InstructionHandle getConsumer(MethodGen methodGen, InstructionHandle start) {
    int depth = 1;
    InstructionHandle cur = start;
    while (cur != null) {
        Instruction inst = cur.getInstruction();
        depth -= inst.consumeStack(methodGen.getConstantPool());
        if (depth <= 0) {
            return cur;
        }
        depth += inst.produceStack(methodGen.getConstantPool());
        if (inst instanceof BranchInstruction) {
            if (inst instanceof GotoInstruction) {
                cur = ((GotoInstruction) inst).getTarget();
                continue;
            }
            if (!(inst instanceof IfInstruction)) {
                return null;
            }
        }
        cur = cur.getNext();
    }
    return null;
}
 
Example #12
Source File: BlockTypeAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void transfer(BasicBlock basicBlock, @CheckForNull InstructionHandle end, BlockType start, BlockType result)
        throws DataflowAnalysisException {
    result.copyFrom(start);

    if (start.isValid() && basicBlock.isExceptionHandler()) {
        CodeExceptionGen exceptionGen = basicBlock.getExceptionGen();
        ObjectType catchType = exceptionGen.getCatchType();
        if (catchType == null) {
            // Probably a finally block, or a synchronized block
            // exception-compensation catch block.
            result.pushFinally();
        } else {
            // Catch type was explicitly specified:
            // this is probably a programmer-written catch block
            result.pushCatch();
        }
    }
}
 
Example #13
Source File: ObligationAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Type nullCheck(short opcode, Edge edge, InstructionHandle last, BasicBlock sourceBlock)
        throws DataflowAnalysisException {
    if (DEBUG_NULL_CHECK) {
        System.out.println("checking for nullcheck on edge " + edge);
    }
    Type type = null;
    if ((opcode == Const.IFNULL && edge.getType() == EdgeTypes.IFCMP_EDGE)
            || (opcode == Const.IFNONNULL && edge.getType() == EdgeTypes.FALL_THROUGH_EDGE)) {
        Location location = new Location(last, sourceBlock);
        TypeFrame typeFrame = typeDataflow.getFactAtLocation(location);
        if (typeFrame.isValid()) {
            type = typeFrame.getTopValue();
            if (DEBUG_NULL_CHECK) {
                System.out.println("ifnull comparison of " + type + " to null at " + last);
            }
        }
    }
    return type;
}
 
Example #14
Source File: VisitorSet.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @see org.apache.bcel.classfile.Visitor#visitCode
 */
public void visitCode(Code aCode)
{   
    for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
        IDeepVisitor visitor = (IDeepVisitor) iter.next();
        Visitor v = visitor.getClassFileVisitor();
        aCode.accept(v);
    }
    
    // perform a deep visit
    final byte[] code = aCode.getCode();
    final InstructionList list = new InstructionList(code);
    final Iterator it = list.iterator();
    for (Iterator iter = list.iterator(); iter.hasNext();) {
        InstructionHandle instruction = (InstructionHandle) iter.next();
        visitInstructionHandle(instruction);
    }
}
 
Example #15
Source File: FindInconsistentSync2.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void addAccess(MethodDescriptor method, InstructionHandle handle, boolean isLocked) {
    if (!interesting) {
        return;
    }
    if (!SYNC_ACCESS && isLocked) {
        return;
    }

    if (!servletField && !isLocked && syncAccessList.size() == 0 && unsyncAccessList.size() > 6) {
        interesting = false;
        syncAccessList = null;
        unsyncAccessList = null;
        return;
    }
    FieldAccess fa = new FieldAccess(method, handle.getPosition());
    if (isLocked) {
        syncAccessList = Util.addTo(syncAccessList, fa);
    } else {
        unsyncAccessList = Util.addTo(unsyncAccessList, fa);
    }
}
 
Example #16
Source File: NoiseNullDeref.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static boolean isThrower(BasicBlock target) {
    InstructionHandle ins = target.getFirstInstruction();
    int maxCount = 7;
    while (ins != null) {
        if (maxCount-- <= 0) {
            break;
        }
        Instruction i = ins.getInstruction();
        if (i instanceof ATHROW) {
            return true;
        }
        if (i instanceof InstructionTargeter || i instanceof ReturnInstruction) {
            return false;
        }
        ins = ins.getNext();
    }
    return false;
}
 
Example #17
Source File: Stream.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public boolean isStreamOpen(BasicBlock basicBlock, InstructionHandle handle, ConstantPoolGen cpg, ResourceValueFrame frame) {
    if (isOpenOnCreation) {
        return false;
    }

    Instruction ins = handle.getInstruction();
    if (!(ins instanceof INVOKESPECIAL)) {
        return false;
    }

    // Does this instruction open the stream?
    INVOKESPECIAL inv = (INVOKESPECIAL) ins;

    return frame.isValid() && getInstanceValue(frame, inv, cpg).isInstance()
            && matchMethod(inv, cpg, this.getResourceClass(), Const.CONSTRUCTOR_NAME);
}
 
Example #18
Source File: Monitorenter.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public MatchResult match(InstructionHandle handle, ConstantPoolGen cpg, ValueNumberFrame before, ValueNumberFrame after,
        BindingSet bindingSet) throws DataflowAnalysisException {

    // Instruction must be MONITORENTER.
    Instruction ins = handle.getInstruction();
    if (!(ins instanceof MONITORENTER)) {
        return null;
    }

    // Ensure the object being locked matches any previous
    // instructions which bound our variable name to a value.
    Variable lock = new LocalVariable(before.getTopValue());
    return addOrCheckDefinition(lock, bindingSet);
}
 
Example #19
Source File: FindUnreleasedLock.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Lock isResourceCreation(BasicBlock basicBlock, InstructionHandle handle, ConstantPoolGen cpg)
        throws DataflowAnalysisException {

    InvokeInstruction inv = toInvokeInstruction(handle.getInstruction());
    if (inv == null) {
        return null;
    }

    String className = inv.getClassName(cpg);
    String methodName = inv.getName(cpg);
    String methodSig = inv.getSignature(cpg);

    try {
        if ("lock".equals(methodName) && "()V".equals(methodSig)
                && Hierarchy.isSubtype(className, "java.util.concurrent.locks.Lock")) {

            Location location = new Location(handle, basicBlock);
            ValueNumberFrame frame = vnaDataflow.getFactAtLocation(location);
            ValueNumber lockValue = frame.getTopValue();
            if (DEBUG) {
                System.out.println("Lock value is " + lockValue.getNumber() + ", frame=" + frame.toString());
            }
            if (DEBUG) {
                ++numAcquires;
            }
            return new Lock(location, className, lockValue);
        }
    } catch (ClassNotFoundException e) {
        lookupFailureCallback.reportMissingClass(e);
    }
    return null;
}
 
Example #20
Source File: BCELPerfTest.java    From annotation-tools with MIT License 5 votes vote down vote up
byte[] nullAdaptClass(final InputStream is, final String name)
        throws Exception
{
    JavaClass jc = new ClassParser(is, name + ".class").parse();
    ClassGen cg = new ClassGen(jc);
    ConstantPoolGen cp = cg.getConstantPool();
    Method[] ms = cg.getMethods();
    for (int j = 0; j < ms.length; ++j) {
        MethodGen mg = new MethodGen(ms[j], cg.getClassName(), cp);
        boolean lv = ms[j].getLocalVariableTable() == null;
        boolean ln = ms[j].getLineNumberTable() == null;
        if (lv) {
            mg.removeLocalVariables();
        }
        if (ln) {
            mg.removeLineNumbers();
        }
        mg.stripAttributes(skipDebug);
        InstructionList il = mg.getInstructionList();
        if (il != null) {
            InstructionHandle ih = il.getStart();
            while (ih != null) {
                ih = ih.getNext();
            }
            if (compute) {
                mg.setMaxStack();
                mg.setMaxLocals();
            }
        }
        cg.replaceMethod(ms[j], mg.getMethod());
    }
    return cg.getJavaClass().getBytes();
}
 
Example #21
Source File: LoadOfKnownNullValue.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param classContext
 * @param nextHandle
 * @param next
 */
private boolean isNullTestedClose(ClassContext classContext, ALOAD load, InstructionHandle nextHandle, Instruction next) {
    if (!(next instanceof IFNULL)) {
        return false;
    }

    IFNULL ifNull = (IFNULL) next;
    InstructionHandle nextNextHandle = nextHandle.getNext(); // aload
    if (nextNextHandle == null) {
        return false;
    }
    Instruction nextInstruction = nextNextHandle.getInstruction();

    if (!(nextInstruction instanceof ALOAD)) {
        return false;
    }
    ALOAD nextLoad = (ALOAD) nextInstruction;
    if (load.getIndex() != nextLoad.getIndex()) {
        return false;
    }
    InstructionHandle nextNextNextHandle = nextNextHandle.getNext(); // invoke
    if (nextNextNextHandle == null) {
        return false;
    }
    Instruction nextNextNextInstruction = nextNextNextHandle.getInstruction();
    if (!(nextNextNextInstruction instanceof INVOKEVIRTUAL)) {
        return false;
    }
    INVOKEVIRTUAL invokeVirtual = (INVOKEVIRTUAL) nextNextNextInstruction;
    String methodName = invokeVirtual.getMethodName(classContext.getConstantPoolGen());
    String methodSig = invokeVirtual.getSignature(classContext.getConstantPoolGen());
    if (!"close".equals(methodName) || !"()V".equals(methodSig)) {
        return false;
    }
    InstructionHandle nextNextNextNextHandle = nextNextNextHandle.getNext(); // after
    return ifNull.getTarget() == nextNextNextNextHandle;
}
 
Example #22
Source File: ObligationAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Type acmpNullCheck(short opcode, Edge edge, InstructionHandle last, BasicBlock sourceBlock)
        throws DataflowAnalysisException {
    Type type = null;
    //
    // Make sure that IF a value has been compared to null,
    // this edge is the edge on which the
    // compared value is definitely null.
    //
    if ((opcode == Const.IF_ACMPEQ && edge.getType() == EdgeTypes.IFCMP_EDGE)
            || (opcode == Const.IF_ACMPNE && edge.getType() == EdgeTypes.FALL_THROUGH_EDGE)) {
        //
        // Check nullness and type of the top two stack values.
        //
        Location location = new Location(last, sourceBlock);
        IsNullValueFrame invFrame = invDataflow.getFactAtLocation(location);
        TypeFrame typeFrame = typeDataflow.getFactAtLocation(location);
        if (invFrame.isValid() && typeFrame.isValid()) {
            //
            // See if exactly one of the top two stack values is definitely
            // null
            //
            boolean leftIsNull = invFrame.getStackValue(1).isDefinitelyNull();
            boolean rightIsNull = invFrame.getStackValue(0).isDefinitelyNull();

            if ((leftIsNull || rightIsNull) && !(leftIsNull && rightIsNull)) {
                //
                // Now we can determine what type was compared to null.
                //
                type = typeFrame.getStackValue(leftIsNull ? 0 : 1);
                if (DEBUG_NULL_CHECK) {
                    System.out.println("acmp comparison of " + type + " to null at " + last);
                }
            }
        }
    }
    return type;
}
 
Example #23
Source File: TestReturn03Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void createMethod_0() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {  },
          "<init>", TEST_PACKAGE+".TestReturn03", il, _cp);

  final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
  final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID));
  Assert.assertNotNull(ih_4); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example #24
Source File: Subroutines.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public InstructionHandle getLeavingRET() {
    if (this == getTopLevel()) {
        throw new AssertionViolatedException("getLeavingRET() called on top level pseudo-subroutine.");
    }
    return theRET;
}
 
Example #25
Source File: HandleTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that instruction handles can be added an instruction list, without
 * corrupting the list.
 */
static void handles() {
    for (int i = 0; i < MAXI; i++) {
        final InstructionList list = new InstructionList();
        try {
            for (int j = 0; j < MAXJ; j++) {
                list.append(new ILOAD(j));
            }
            final InstructionHandle[] instructionHandles = list.getInstructionHandles();
            for (int j = 0; j < instructionHandles.length; j++) {
                final InstructionHandle handle = instructionHandles[j];
                checkLinkage(handle, j);
                if (j != ((ILOAD) handle.getInstruction()).getIndex()) {
                    final AssertionFailedError error = new AssertionFailedError("unexpected instruction at index " + j);
                    exception = error;
                    throw error;
                }
            }
            if (exception != null) {
                return;
            }
        } catch (final NullPointerException e) {
            System.out.println("NPE at i=" + i);
            exception = e;
            throw e;
        }
        list.dispose(); // this initializes caching of unused instruction handles
    }
}
 
Example #26
Source File: FindUnreleasedLock.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public boolean isResourceClose(BasicBlock basicBlock, InstructionHandle handle, ConstantPoolGen cpg, Lock resource,
        ResourceValueFrame frame) throws DataflowAnalysisException {

    if (!mightCloseResource(basicBlock, handle, cpg)) {
        return false;
    }
    ResourceValue topValue = frame.getTopValue();
    return topValue.isInstance();

}
 
Example #27
Source File: Location.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Location getFirstLocation(@Nonnull BasicBlock basicBlock) {
    InstructionHandle location = basicBlock.getFirstInstruction();
    if (location == null) {
        return null;
    }
    return new Location(location, basicBlock);
}
 
Example #28
Source File: VisitorSet.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Deep visit of an InstructionHandle
 * @param aInstruction the InstructionHandle
 */
private void visitInstructionHandle(InstructionHandle aInstruction)
{
    for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
        final IDeepVisitor visitor = (IDeepVisitor) iter.next();
        org.apache.bcel.generic.Visitor v =
            visitor.getGenericVisitor();
        aInstruction.accept(v);
    }
}
 
Example #29
Source File: FieldSetAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private @CheckForNull XField lookupField(InstructionHandle handle, FieldInstruction fins) {
    XField field = instructionToFieldMap.get(handle);
    if (field == null) {
        field = Hierarchy.findXField(fins, getCPG());
        instructionToFieldMap.put(handle, field);
    }
    return field;
}
 
Example #30
Source File: ASTLetExpr.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Fifth pass, produce Java byte code.
 */
@Override
public void byte_code(final InstructionList il, final MethodGen method, final ConstantPoolGen cp) {
  final int size = idents.length;
  final LocalVariableGen[] l = new LocalVariableGen[size];

  for(int i=0; i < size; i++) {
    final String           ident = idents[i].getName();
    final Variable         entry = (Variable)env.get(ident);
    final Type             t     = BasicType.getType((byte)idents[i].getType());
    final LocalVariableGen lg    = method.addLocalVariable(ident, t, null, null);
    final int              slot  = lg.getIndex();

    entry.setLocalVariable(lg);
    InstructionHandle start = il.getEnd();
    exprs[i].byte_code(il, method, cp);
    start = (start == null)? il.getStart() : start.getNext();
    lg.setStart(start);
    il.append(new ISTORE(slot));     ASTFunDecl.pop();
    l[i] = lg;
  }

  body.byte_code(il, method, cp);
  final InstructionHandle end = il.getEnd();
  for(int i=0; i < size; i++) {
      l[i].setEnd(end);
  }
}