use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class PartiallyConstructedObjectAccess method visitClassContext.
/**
* implements the visitor to set up the stack and methodToCalledmethods map reports calls to public non final methods from methods called from constructors.
*
* @param classContext
* the context object of the currently parsed class
*/
@Override
public void visitClassContext(final ClassContext classContext) {
try {
JavaClass cls = classContext.getJavaClass();
if (!cls.isFinal()) {
stack = new OpcodeStack();
methodToCalledMethods = new HashMap<>();
super.visitClassContext(classContext);
if (!methodToCalledMethods.isEmpty()) {
reportChainedMethods();
}
}
} finally {
stack = null;
methodToCalledMethods = null;
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class PoorMansEnum method visitClassContext.
@Override
public void visitClassContext(ClassContext classContext) {
try {
JavaClass cls = classContext.getJavaClass();
if (cls.getMajor() >= Const.MAJOR_1_5) {
fieldValues = new HashMap<>();
nameToField = new HashMap<>();
for (Field f : cls.getFields()) {
if (f.isPrivate() && !f.isSynthetic()) {
String fieldName = f.getName();
// preallocating a set per field is just a waste, so just insert the empty set as a place holder
fieldValues.put(fieldName, Collections.emptySet());
nameToField.put(fieldName, f);
}
}
if (!fieldValues.isEmpty()) {
stack = new OpcodeStack();
firstFieldUse = new HashMap<>();
try {
super.visitClassContext(classContext);
for (Map.Entry<String, Set<Object>> fieldInfo : fieldValues.entrySet()) {
Set<Object> values = fieldInfo.getValue();
if (values.size() >= 3) {
String fieldName = fieldInfo.getKey();
bugReporter.reportBug(new BugInstance(this, BugType.PME_POOR_MANS_ENUM.name(), NORMAL_PRIORITY).addClass(this).addField(XFactory.createXField(cls, nameToField.get(fieldName))).addSourceLine(firstFieldUse.get(fieldName)));
}
}
} catch (StopOpcodeParsingException e) {
// no fields left
}
}
}
} finally {
fieldValues = null;
nameToField = null;
firstFieldUse = null;
stack = null;
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class LoggerOddities method hasExceptionOnStack.
/**
* returns whether an exception object is on the stack slf4j will find this, and not include it in the parm list so i we find one, just don't report
*
* @return whether or not an exception i present
*/
@SuppressWarnings("unchecked")
private boolean hasExceptionOnStack() {
try {
for (int i = 0; i < (stack.getStackDepth() - 1); i++) {
OpcodeStack.Item item = stack.getStackItem(i);
String sig = item.getSignature();
if (sig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
String name = SignatureUtils.stripSignature(sig);
JavaClass cls = Repository.lookupClass(name);
if (cls.instanceOf(throwableClass)) {
return true;
}
} else if (sig.startsWith(Values.SIG_ARRAY_PREFIX)) {
LOUserValue<Integer> uv = (LOUserValue<Integer>) item.getUserValue();
if ((uv != null) && (uv.getType() == LOUserValue.LOType.ARRAY_SIZE)) {
Integer sz = uv.getValue();
if ((sz != null) && (sz.intValue() < 0)) {
return true;
}
}
}
}
return false;
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
return true;
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class LoggerOddities method isNonPrivateLogField.
/**
* looks to see if this field is a logger, and declared non privately
*
* @param fieldClsName
* the owning class type of the field
* @param fieldName
* the name of the field
* @param fieldSig
* the signature of the field
* @return if the field is a logger and not private
*/
private boolean isNonPrivateLogField(String fieldClsName, String fieldName, String fieldSig) {
String fieldType = SignatureUtils.trimSignature(fieldSig);
if (!SLF4J_LOGGER.equals(fieldType) && !COMMONS_LOGGER.equals(fieldType) && !LOG4J_LOGGER.equals(fieldType) && !LOG4J2_LOGGER.equals(fieldType)) {
return false;
}
JavaClass cls = getClassContext().getJavaClass();
if (!cls.getClassName().equals(fieldClsName.replace('/', '.'))) {
return false;
}
for (Field f : getClassContext().getJavaClass().getFields()) {
if (f.getName().equals(fieldName)) {
return !f.isPrivate();
}
}
return true;
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class LostExceptionStackTrace method isPossibleExBuilder.
/**
* returns whether the method called might be a method that builds an exception using the original exception. It does so by looking to see if the method
* returns an exception, and if one of the parameters is the original exception
*
* @param excReg
* the register of the original exception caught
* @return whether this method call could be an exception builder method
*
* @throws ClassNotFoundException
* if the class of the return type can't be found
*/
public boolean isPossibleExBuilder(int excReg) throws ClassNotFoundException {
String sig = getSigConstantOperand();
String returnSig = SignatureUtils.getReturnSignature(sig);
if (returnSig.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
returnSig = SignatureUtils.trimSignature(returnSig);
JavaClass retCls = Repository.lookupClass(returnSig);
if (retCls.instanceOf(throwableClass)) {
int numParms = SignatureUtils.getNumParameters(sig);
if (stack.getStackDepth() >= numParms) {
for (int p = 0; p < numParms; p++) {
OpcodeStack.Item item = stack.getStackItem(p);
if (item.getRegisterNumber() == excReg) {
return true;
}
}
}
}
}
return false;
}
Aggregations