use of org.apache.bcel.classfile.Method in project contribution by checkstyle.
the class HiddenStaticMethodCheck method visitObject.
/**
* @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
*/
public void visitObject(Object aJavaClass) {
final JavaClass javaClass = (JavaClass) aJavaClass;
final String className = javaClass.getClassName();
final JavaClass[] superClasses = javaClass.getSuperClasses();
final Method[] methods = javaClass.getMethods();
// Check all methods
for (int i = 0; i < methods.length; i++) {
final Method method = methods[i];
// Check that the method is a possible match
if (!method.isPrivate() && method.isStatic()) {
// Go through all their superclasses
for (int j = 0; j < superClasses.length; j++) {
final JavaClass superClass = superClasses[j];
final String superClassName = superClass.getClassName();
final Method[] superClassMethods = superClass.getMethods();
// Go through the methods of the superclasses
for (int k = 0; k < superClassMethods.length; k++) {
final Method superClassMethod = superClassMethods[k];
if (superClassMethod.getName().equals(method.getName()) && !ignore(className, method)) {
Type[] methodTypes = method.getArgumentTypes();
Type[] superTypes = superClassMethod.getArgumentTypes();
if (methodTypes.length == superTypes.length) {
boolean match = true;
for (int arg = 0; arg < methodTypes.length; arg++) {
if (!methodTypes[arg].equals(superTypes[arg])) {
match = false;
}
}
// Same method parameters
if (match) {
log(javaClass, 0, "hidden.static.method", new Object[] { method, superClassName });
}
}
}
}
}
}
}
}
use of org.apache.bcel.classfile.Method in project fb-contrib by mebigfatguy.
the class DeprecatedTypesafeEnumPattern method visitClassContext.
/**
* implements the visitor to look for classes compiled with 1.5 or better that have all constructors that are private
*
* @param context
* the currently parsed class context object
*/
@Override
public void visitClassContext(ClassContext context) {
try {
JavaClass cls = context.getJavaClass();
if (!cls.isEnum() && (cls.getMajor() >= Const.MAJOR_1_5)) {
Method[] methods = cls.getMethods();
for (Method m : methods) {
if (Values.CONSTRUCTOR.equals(m.getName()) && !m.isPrivate()) {
return;
}
}
firstEnumPC = 0;
enumCount = 0;
enumConstNames = new HashSet<String>(10);
super.visitClassContext(context);
}
} finally {
enumConstNames = null;
}
}
use of org.apache.bcel.classfile.Method in project fb-contrib by mebigfatguy.
the class ExceptionSoftening method getConstrainingInfo.
/**
* finds the super class or interface that constrains the types of exceptions that can be thrown from the given method
*
* @param cls
* the currently parsed class
* @param m
* the method to check
* @return a map containing the class name to a set of exceptions that constrain this method
*
* @throws ClassNotFoundException
* if a super class or super interface can't be loaded from the repository
*/
@Nullable
private Map<String, Set<String>> getConstrainingInfo(JavaClass cls, Method m) throws ClassNotFoundException {
String methodName = m.getName();
String methodSig = m.getSignature();
{
// First look for the method in interfaces of the class
JavaClass[] infClasses = cls.getInterfaces();
for (JavaClass infCls : infClasses) {
Method infMethod = findMethod(infCls, methodName, methodSig);
if (infMethod != null) {
return buildConstrainingInfo(infCls, infMethod);
}
Map<String, Set<String>> constrainingExs = getConstrainingInfo(infCls, m);
if (constrainingExs != null) {
return constrainingExs;
}
}
}
{
// Next look at the superclass
JavaClass superCls = cls.getSuperClass();
if (superCls == null) {
return null;
}
Method superMethod = findMethod(superCls, methodName, methodSig);
if (superMethod != null) {
return buildConstrainingInfo(superCls, superMethod);
}
// Otherwise recursively call this on the super class
return getConstrainingInfo(superCls, m);
}
}
use of org.apache.bcel.classfile.Method in project fb-contrib by mebigfatguy.
the class ExceptionSoftening method visitCode.
/**
* overrides the visitor to look for methods that catch checked exceptions and rethrow runtime exceptions
*
* @param obj
* the context object of the currently parsed code block
*/
@Override
public void visitCode(Code obj) {
try {
Method method = getMethod();
if (method.isSynthetic()) {
return;
}
isBooleanMethod = Type.BOOLEAN.equals(method.getReturnType());
if (isBooleanMethod || prescreen(method)) {
catchHandlerPCs = collectExceptions(obj.getExceptionTable());
if (!catchHandlerPCs.isEmpty()) {
stack.resetForMethodEntry(this);
catchInfos = new ArrayList<>();
lvt = method.getLocalVariableTable();
constrainingInfo = null;
hasValidFalseReturn = false;
catchFalseReturnPC = -1;
super.visitCode(obj);
if (!hasValidFalseReturn && (catchFalseReturnPC >= 0) && !method.getName().startsWith("is")) {
bugReporter.reportBug(new BugInstance(this, BugType.EXS_EXCEPTION_SOFTENING_RETURN_FALSE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, catchFalseReturnPC));
}
}
}
} finally {
catchInfos = null;
catchHandlerPCs = null;
lvt = null;
constrainingInfo = null;
}
}
use of org.apache.bcel.classfile.Method in project fb-contrib by mebigfatguy.
the class PossibleIncompleteSerialization method hasSerializingMethods.
/**
* looks to see if this class implements method described by Serializable or Externalizable
*
* @param cls
* the class to examine for serializing methods
* @return whether the class handles it's own serializing/externalizing
*/
private static boolean hasSerializingMethods(JavaClass cls) {
Method[] methods = cls.getMethods();
for (Method m : methods) {
if (!m.isStatic()) {
String methodName = m.getName();
String methodSig = m.getSignature();
if ("writeObject".equals(methodName) && SIG_OBJECT_OUTPUT_STREAM_TO_VOID.equals(methodSig)) {
return true;
}
if ("writeExternal".equals(methodName) && SIG_OBJECT_OUTPUT_TO_VOID.equals(methodSig)) {
return true;
}
}
}
return false;
}
Aggregations