use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class BogusExceptionDeclaration method visitCode.
/**
* implements the visitor to see if the method declares that it throws any checked exceptions.
*
* @param obj
* the context object of the currently parsed code block
*/
@Override
public void visitCode(Code obj) {
Method method = getMethod();
if (method.isSynthetic()) {
return;
}
declaredCheckedExceptions.clear();
stack.resetForMethodEntry(this);
ExceptionTable et = method.getExceptionTable();
if (et != null) {
if (classIsFinal || classIsAnonymous || method.isStatic() || method.isPrivate() || method.isFinal() || ((Values.CONSTRUCTOR.equals(method.getName()) && !isAnonymousInnerCtor(method, getThisClass())))) {
String[] exNames = et.getExceptionNames();
for (String exName : exNames) {
try {
JavaClass exCls = Repository.lookupClass(exName);
if (!exCls.instanceOf(runtimeExceptionClass)) {
declaredCheckedExceptions.add(exName);
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
}
if (!declaredCheckedExceptions.isEmpty()) {
try {
super.visitCode(obj);
if (!declaredCheckedExceptions.isEmpty()) {
BugInstance bi = new BugInstance(this, BugType.BED_BOGUS_EXCEPTION_DECLARATION.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, 0);
for (String ex : declaredCheckedExceptions) {
bi.addString(ex.replaceAll("/", "."));
}
bugReporter.reportBug(bi);
}
} catch (StopOpcodeParsingException e) {
// no exceptions left
}
}
}
String[] exNames = et.getExceptionNames();
for (int i = 0; i < (exNames.length - 1); i++) {
try {
JavaClass exCls1 = Repository.lookupClass(exNames[i]);
for (int j = i + 1; j < exNames.length; j++) {
JavaClass exCls2 = Repository.lookupClass(exNames[j]);
JavaClass childEx;
JavaClass parentEx;
if (exCls1.instanceOf(exCls2)) {
childEx = exCls1;
parentEx = exCls2;
} else if (exCls2.instanceOf(exCls1)) {
childEx = exCls2;
parentEx = exCls1;
} else {
continue;
}
if (!parentEx.equals(exceptionClass)) {
bugReporter.reportBug(new BugInstance(this, BugType.BED_HIERARCHICAL_EXCEPTION_DECLARATION.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addString(childEx.getClassName() + " derives from " + parentEx.getClassName()));
return;
}
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
}
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class BogusExceptionDeclaration method removeThrownExceptionHierarchy.
/**
* removes this thrown exception the list of declared thrown exceptions, including all exceptions in this exception's hierarchy. If an exception class is
* found that can't be loaded, then just clear the list of declared checked exceptions and get out.
*
* @param thrownException
* the exception and it's hierarchy to remove
*/
private void removeThrownExceptionHierarchy(String thrownException) {
try {
if (Values.DOTTED_JAVA_LANG_EXCEPTION.equals(thrownException) || Values.DOTTED_JAVA_LANG_THROWABLE.equals(thrownException)) {
// Exception/Throwable can be thrown even tho the method isn't declared to throw
// Exception/Throwable in the case of templated Exceptions
clearExceptions();
} else {
removeException(thrownException);
JavaClass exCls = Repository.lookupClass(thrownException);
String clsName;
do {
exCls = exCls.getSuperClass();
if (exCls == null) {
break;
}
clsName = exCls.getClassName();
removeException(clsName);
} while (!declaredCheckedExceptions.isEmpty() && !Values.DOTTED_JAVA_LANG_EXCEPTION.equals(clsName) && !Values.DOTTED_JAVA_LANG_ERROR.equals(clsName));
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
clearExceptions();
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class BogusExceptionDeclaration method visitClassContext.
/**
* overrides the visitor to create the opcode stack
*
* @param classContext
* the context object of the currently parsed class
*/
@Override
public void visitClassContext(ClassContext classContext) {
try {
if ((runtimeExceptionClass != null) && (exceptionClass != null)) {
stack = new OpcodeStack();
declaredCheckedExceptions = new HashSet<>(6);
JavaClass cls = classContext.getJavaClass();
classIsFinal = cls.isFinal();
classIsAnonymous = cls.isAnonymous();
super.visitClassContext(classContext);
}
} finally {
declaredCheckedExceptions = null;
stack = null;
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class ClassEnvy method countClassAccess.
/**
* increment the count of class access of the class on the stack
*
* @param classAtStackIndex
* the position on the stack of the class in question
*
* @return true if the class is counted
*/
private boolean countClassAccess(final int classAtStackIndex) {
String calledClass;
try {
if (stack.getStackDepth() > classAtStackIndex) {
OpcodeStack.Item itm = stack.getStackItem(classAtStackIndex);
JavaClass cls = itm.getJavaClass();
if (cls != null) {
calledClass = cls.getClassName();
countClassAccess(calledClass);
return true;
}
}
} catch (ClassNotFoundException cfne) {
bugReporter.reportMissingClass(cfne);
}
return false;
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class CloneUsability method visitCode.
/**
* overrides the visitor to grab the method name and reset the state.
*
* @param obj
* the method being parsed
*/
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "FCBL_FIELD_COULD_BE_LOCAL", justification = "False positives occur when state is maintained across callbacks")
@Override
public void visitCode(Code obj) {
try {
Method m = getMethod();
if (m.isPublic() && !m.isSynthetic() && "clone".equals(m.getName()) && (m.getArgumentTypes().length == 0)) {
String returnClsName = m.getReturnType().getSignature();
returnClsName = SignatureUtils.stripSignature(returnClsName);
if (!clsName.equals(returnClsName)) {
if (Values.DOTTED_JAVA_LANG_OBJECT.equals(returnClsName)) {
bugReporter.reportBug(new BugInstance(this, BugType.CU_CLONE_USABILITY_OBJECT_RETURN.name(), NORMAL_PRIORITY).addClass(this).addMethod(this));
} else {
JavaClass clonedClass = Repository.lookupClass(returnClsName);
if (!cls.instanceOf(clonedClass)) {
bugReporter.reportBug(new BugInstance(this, BugType.CU_CLONE_USABILITY_MISMATCHED_RETURN.name(), HIGH_PRIORITY).addClass(this).addMethod(this));
}
}
}
ExceptionTable et = m.getExceptionTable();
if ((et != null) && (et.getLength() > 0)) {
throwsCNFE = false;
if (prescreen(m)) {
stack.resetForMethodEntry(this);
super.visitCode(obj);
}
if (!throwsCNFE) {
bugReporter.reportBug(new BugInstance(this, BugType.CU_CLONE_USABILITY_THROWS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this));
}
}
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
}
Aggregations