@Override public void sawOpcode(int seen) { if (seen == Const.LDC || seen == Const.LDC_W || seen == Const.LDC2_W) { Constant c = getConstantRefOperand(); if (c instanceof ConstantFloat) { checkConst(((ConstantFloat) c).getBytes()); } else if (c instanceof ConstantDouble) { checkConst(((ConstantDouble) c).getBytes()); } return; } // Lower priority if the constant is put into array immediately or after the boxing: // this is likely to be just similar number in some predefined dataset (like lookup table) if(seen == Const.INVOKESTATIC && lastBug != null) { if (getNextOpcode() == Const.AASTORE && getNameConstantOperand().equals("valueOf") && (getClassConstantOperand().equals("java/lang/Double") || getClassConstantOperand().equals( "java/lang/Float"))) { lastBug = ((BugInstance)lastBug.clone()); lastBug.setPriority(lastPriority+1); bugAccumulator.forgetLastBug(); bugAccumulator.accumulateBug(lastBug, this); } } lastBug = null; }
private void checkConst(Number constValue) { double candidate = constValue.doubleValue(); if (Double.isNaN(candidate) || Double.isInfinite(candidate)) { return; } for (BadConstant badConstant : badConstants) { int priority = getPriority(badConstant, constValue, candidate); if(getNextOpcode() == Const.FASTORE || getNextOpcode() == Const.DASTORE) { priority++; } if(priority < IGNORE_PRIORITY) { lastPriority = priority; lastBug = new BugInstance(this, "CNT_ROUGH_CONSTANT_VALUE", priority).addClassAndMethod(this) .addString(constValue.toString()).addString(badConstant.replacement); bugAccumulator.accumulateBug(lastBug, this); return; } } } }
@Override public void visitClassContext(ClassContext classContext) { if(hasInterestingConstant(classContext.getJavaClass().getConstantPool())) { super.visitClassContext(classContext); } }
private boolean isInteresting(Number constValue, double candidate) { for (BadConstant badConstant : badConstants) { if(getPriority(badConstant, constValue, candidate) < IGNORE_PRIORITY) { return true; } } return false; }
private boolean hasInterestingConstant(ConstantPool cp) { for(Constant constant : cp.getConstantPool()) { if(constant instanceof ConstantFloat) { float val = ((ConstantFloat)constant).getBytes(); if(isInteresting(val, val)) { return true; } } if(constant instanceof ConstantDouble) { double val = ((ConstantDouble)constant).getBytes(); if(isInteresting(val, val)) { return true; } } } return false; }
private boolean isInteresting(Number constValue, double candidate) { for (BadConstant badConstant : badConstants) { if(getPriority(badConstant, constValue, candidate) < IGNORE_PRIORITY) { return true; } } return false; }
private boolean hasInterestingConstant(ConstantPool cp) { for(Constant constant : cp.getConstantPool()) { if(constant instanceof ConstantFloat) { float val = ((ConstantFloat)constant).getBytes(); if(isInteresting(val, val)) { return true; } } if(constant instanceof ConstantDouble) { double val = ((ConstantDouble)constant).getBytes(); if(isInteresting(val, val)) { return true; } } } return false; }
@Override public void sawOpcode(int seen) { if (seen == LDC || seen == LDC_W || seen == LDC2_W) { Constant c = getConstantRefOperand(); if (c instanceof ConstantFloat) { checkConst(((ConstantFloat) c).getBytes()); } else if (c instanceof ConstantDouble) { checkConst(((ConstantDouble) c).getBytes()); } return; } // Lower priority if the constant is put into array immediately or after the boxing: // this is likely to be just similar number in some predefined dataset (like lookup table) if(seen == INVOKESTATIC && lastBug != null) { if (getNextOpcode() == AASTORE && getNameConstantOperand().equals("valueOf") && (getClassConstantOperand().equals("java/lang/Double") || getClassConstantOperand().equals( "java/lang/Float"))) { lastBug = ((BugInstance)lastBug.clone()); lastBug.setPriority(lastPriority+1); bugAccumulator.forgetLastBug(); bugAccumulator.accumulateBug(lastBug, this); } } lastBug = null; }
private void checkConst(Number constValue) { double candidate = constValue.doubleValue(); if (Double.isNaN(candidate) || Double.isInfinite(candidate)) { return; } for (BadConstant badConstant : badConstants) { int priority = getPriority(badConstant, constValue, candidate); if(getNextOpcode() == FASTORE || getNextOpcode() == DASTORE) { priority++; } if(priority < IGNORE_PRIORITY) { lastPriority = priority; lastBug = new BugInstance(this, "CNT_ROUGH_CONSTANT_VALUE", priority).addClassAndMethod(this) .addString(constValue.toString()).addString(badConstant.replacement); bugAccumulator.accumulateBug(lastBug, this); return; } } } }
@Override public void visitClassContext(ClassContext classContext) { if(hasInterestingConstant(classContext.getJavaClass().getConstantPool())) { super.visitClassContext(classContext); } }