use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.
the class SillynessPotPourri method checkNotEqualsStringBuilderLength.
private void checkNotEqualsStringBuilderLength() {
byte[] bytes = getCode().getCode();
if ((lastPCs[2] != -1) && (CodeByteUtils.getbyte(bytes, lastPCs[3]) == Const.INVOKEVIRTUAL) && (CodeByteUtils.getbyte(bytes, lastPCs[2]) == Const.INVOKEVIRTUAL)) {
ConstantPool pool = getConstantPool();
int toStringIndex = CodeByteUtils.getshort(bytes, lastPCs[2] + 1);
ConstantMethodref toStringMR = (ConstantMethodref) pool.getConstant(toStringIndex);
String toStringCls = toStringMR.getClass(pool);
if (toStringCls.startsWith("java.lang.StringBu")) {
int nandtIndex = toStringMR.getNameAndTypeIndex();
ConstantNameAndType cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
if (Values.TOSTRING.equals(cnt.getName(pool))) {
int lengthIndex = CodeByteUtils.getshort(bytes, lastPCs[3] + 1);
ConstantMethodref lengthMR = (ConstantMethodref) pool.getConstant(lengthIndex);
nandtIndex = lengthMR.getNameAndTypeIndex();
cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
if ("length".equals(cnt.getName(pool))) {
bugReporter.reportBug(new BugInstance(this, BugType.SPP_USE_STRINGBUILDER_LENGTH.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
}
}
}
use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.
the class SillynessPotPourri method checkForEmptyStringAndNullChecks.
private void checkForEmptyStringAndNullChecks(int seen) {
if (lastLoadWasString && (lastPCs[0] != -1)) {
byte[] bytes = getCode().getCode();
int loadIns = CodeByteUtils.getbyte(bytes, lastPCs[2]);
if ((((loadIns >= Const.ALOAD_0) && (loadIns <= Const.ALOAD_3)) || (loadIns == Const.ALOAD)) && (CodeByteUtils.getbyte(bytes, lastPCs[3]) == Const.INVOKEVIRTUAL) && (CodeByteUtils.getbyte(bytes, lastPCs[2]) == loadIns) && (CodeByteUtils.getbyte(bytes, lastPCs[1]) == Const.IFNULL) && (CodeByteUtils.getbyte(bytes, lastPCs[0]) == loadIns) && ((loadIns != Const.ALOAD) || (CodeByteUtils.getbyte(bytes, lastPCs[2] + 1) == CodeByteUtils.getbyte(bytes, lastPCs[0] + 1)))) {
int brOffset = (loadIns == Const.ALOAD) ? 11 : 10;
if ((seen == Const.IFNE) ? CodeByteUtils.getshort(bytes, lastPCs[1] + 1) > brOffset : CodeByteUtils.getshort(bytes, lastPCs[1] + 1) == brOffset) {
int nextOp = CodeByteUtils.getbyte(bytes, getNextPC());
if ((nextOp != Const.GOTO) && (nextOp != Const.GOTO_W)) {
ConstantPool pool = getConstantPool();
int mpoolIndex = CodeByteUtils.getshort(bytes, lastPCs[3] + 1);
ConstantMethodref cmr = (ConstantMethodref) pool.getConstant(mpoolIndex);
int nandtIndex = cmr.getNameAndTypeIndex();
ConstantNameAndType cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
if ("length".equals(cnt.getName(pool))) {
bugReporter.reportBug(new BugInstance(this, BugType.SPP_SUSPECT_STRING_TEST.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
}
}
}
}
use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.
the class RegisterUtilsTest method shouldGetParameterRegisters.
@Test(dataProvider = "parameterRegisters")
public void shouldGetParameterRegisters(int accessFlags, String signature, int[] expected) {
Method method = new Method();
method.setAccessFlags(accessFlags);
Constant[] cnst = new Constant[] { new ConstantUtf8(signature) };
ConstantPool cp = new ConstantPool(cnst) {
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
};
method.setConstantPool(cp);
method.setSignatureIndex(0);
int[] regs = RegisterUtils.getParameterRegisters(method);
assertEquals(regs, expected);
}
use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.
the class OptionalIssues method sawOpcode.
/**
* implements the visitor to look for reference compares of Optional, Optional use when more specific Optionals should be used, and use of orElse when
* orElseGet would be more appropriate
*
* @param seen
* the opcode of the currently parsed instruction
*/
@Override
public void sawOpcode(int seen) {
FQMethod curCalledMethod = null;
Boolean sawPlainOptional = null;
try {
switch(seen) {
case Const.IFNULL:
case Const.IFNONNULL:
if (stack.getStackDepth() > 0) {
OpcodeStack.Item itm = stack.getStackItem(0);
if ("Ljava/util/Optional;".equals(itm.getSignature())) {
bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_CHECKING_REFERENCE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
break;
case Const.INVOKEDYNAMIC:
// smells like a hack. Not sure how to do this better
ConstantInvokeDynamic id = (ConstantInvokeDynamic) getConstantRefOperand();
ConstantPool cp = getConstantPool();
ConstantNameAndType nameAndType = (ConstantNameAndType) cp.getConstant(id.getNameAndTypeIndex());
ConstantUtf8 typeConstant = (ConstantUtf8) cp.getConstant(nameAndType.getSignatureIndex());
curCalledMethod = new FQMethod(getClassName(), "lambda$" + id.getBootstrapMethodAttrIndex(), typeConstant.getBytes());
break;
case Const.INVOKESTATIC:
case Const.INVOKEINTERFACE:
case Const.INVOKESPECIAL:
String clsName = getClassConstantOperand();
String methodName = getNameConstantOperand();
curCalledMethod = new FQMethod(clsName, methodName, getSigConstantOperand());
if ("java/util/Optional".equals(clsName) && "of".equals(methodName)) {
if (stack.getStackDepth() > 0) {
OpcodeStack.Item itm = stack.getStackItem(0);
String itmSig = itm.getSignature();
if (BOXED_OPTIONAL_TYPES.contains(itmSig)) {
bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
}
break;
case Const.INVOKEVIRTUAL:
curCalledMethod = new FQMethod(getClassConstantOperand(), getNameConstantOperand(), getSigConstantOperand());
if (OR_ELSE_METHODS.contains(curCalledMethod)) {
if (stack.getStackDepth() > 0) {
OpcodeStack.Item itm = stack.getStackItem(0);
if ((itm.getRegisterNumber() < 0) && (itm.getReturnValueOf() != null) && !isTrivialStackOps()) {
bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
if (OPTIONAL_OR_ELSE_METHOD.equals(curCalledMethod)) {
sawPlainOptional = Boolean.TRUE;
}
} else if (OR_ELSE_GET_METHODS.contains(curCalledMethod)) {
if (!activeStackOps.isEmpty()) {
ActiveStackOp op = activeStackOps.getLast();
FQMethod method = op.getMethod();
if (method == null) {
bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_USES_ORELSEGET_WITH_NULL.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
} else {
Method getMethod = getLambdaMethod(method.getMethodName());
if (getMethod != null) {
byte[] byteCode = getMethod.getCode().getCode();
if (byteCode.length <= 4) {
// we are looking for ALOAD, GETFIELD, or LDC followed by ARETURN, that should fit in 4 bytes
if (!hasInvoke(byteCode)) {
bugReporter.reportBug(new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_USES_DELAYED_EXECUTION.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
}
}
}
if (OPTIONAL_OR_ELSE_GET_METHOD.equals(curCalledMethod)) {
sawPlainOptional = Boolean.TRUE;
}
} else if (OPTIONAL_GET_METHOD.equals(curCalledMethod)) {
sawPlainOptional = Boolean.TRUE;
}
break;
}
} finally {
stack.sawOpcode(this, seen);
int stackDepth = stack.getStackDepth();
if (stackDepth == 0) {
activeStackOps.clear();
} else {
activeStackOps.addLast(new ActiveStackOp(seen, curCalledMethod));
while (activeStackOps.size() > stackDepth) {
activeStackOps.removeFirst();
}
if (sawPlainOptional != null) {
OpcodeStack.Item itm = stack.getStackItem(0);
itm.setUserValue(sawPlainOptional);
}
}
}
}
use of org.apache.bcel.classfile.ConstantPool in project fb-contrib by mebigfatguy.
the class IncorrectInternalClassUse method visitClassContext.
/**
* implements the visitor to look for classes that reference com.sun.xxx, or org.apache.xerces.xxx classes by looking for class Const in the constant pool
*
* @param context
* the context object of the currently parsed class
*/
@Override
public void visitClassContext(ClassContext context) {
JavaClass cls = context.getJavaClass();
if (!isInternal(cls.getClassName())) {
ConstantPool pool = cls.getConstantPool();
int numItems = pool.getLength();
for (int i = 0; i < numItems; i++) {
Constant c = pool.getConstant(i);
if (c instanceof ConstantClass) {
String clsName = ((ConstantClass) c).getBytes(pool);
if (isInternal(clsName)) {
bugReporter.reportBug(new BugInstance(this, BugType.IICU_INCORRECT_INTERNAL_CLASS_USE.name(), NORMAL_PRIORITY).addClass(cls).addString(clsName));
}
}
}
}
}
Aggregations