Hi, my name is Paulo, I am portuguese, and I am currently working as a freelance Software Engineer in Luxembourg. This is my personal blog, where I express my views in various subjects. You can contact me using barracadopaulo@gmail.com. Welcome!

3/23/10

FindBugs- Detecting EJB instantiation

In projects using EJB3, every now and then we bump into manual instantiation of EJBs, causing unexpected results. This is the typical issue which is very easy to automatically detect. The following FindBugs detector does the job, assuming that you annotate your EJBs:

public class EJBInstantiation extends BytecodeScanningDetector {

private static final Collection EJB_ANNOT_NAMES = Arrays.asList(
"Stateless", "Statefull", "MessageDriven");

private BugReporter bugReporter;

public EJBInstantiation(BugReporter bugReporter) {
this.bugReporter = bugReporter;
}

@Override
public void sawOpcode(int seen) {
try {
if ((seen == INVOKESPECIAL)
&& getNameConstantOperand().equals("")
&& getSigConstantOperand().equals("()V")) {
XClass clazz = getXClassOperand();
if (clazz != null && isEJB(clazz)) {
bugReporter.reportBug(new BugInstance(this,
"EJB_INST", HIGH_PRIORITY).addClassAndMethod(
this).addSourceLine(this));
}
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
}

private boolean isEJB(XClass clazz) throws ClassNotFoundException {
boolean result = false;
Collection annotations = clazz
.getAnnotationDescriptors();
if (annotations != null && !annotations.isEmpty()) {
for (ClassDescriptor annotation : annotations) {
if (EJB_ANNOT_NAMES.contains(annotation.getSimpleName())) {
result = true;
break;
}
}
}
return result;
}
}

0 comments:

Archive

My Profile