use of org.apache.bcel.classfile.AnnotationEntry in project fb-contrib by mebigfatguy.
the class FieldCouldBeLocal method visitClassContext.
/**
* overrides the visitor to collect localizable fields, and then report those that survive all method checks.
*
* @param classContext
* the context object that holds the JavaClass parsed
*/
@Override
public void visitClassContext(ClassContext classContext) {
try {
localizableFields = new HashMap<>();
visitedBlocks = new BitSet();
clsContext = classContext;
clsName = clsContext.getJavaClass().getClassName();
clsSig = SignatureUtils.classToSignature(clsName);
JavaClass cls = classContext.getJavaClass();
Field[] fields = cls.getFields();
ConstantPool cp = classContext.getConstantPoolGen().getConstantPool();
for (Field f : fields) {
if (!f.isStatic() && !f.isVolatile() && (f.getName().indexOf(Values.SYNTHETIC_MEMBER_CHAR) < 0) && f.isPrivate()) {
FieldAnnotation fa = new FieldAnnotation(cls.getClassName(), f.getName(), f.getSignature(), false);
boolean hasExternalAnnotation = false;
for (AnnotationEntry entry : f.getAnnotationEntries()) {
ConstantUtf8 cutf = (ConstantUtf8) cp.getConstant(entry.getTypeIndex());
if (!cutf.getBytes().startsWith(Values.JAVA)) {
hasExternalAnnotation = true;
break;
}
}
localizableFields.put(f.getName(), new FieldInfo(fa, hasExternalAnnotation));
}
}
if (!localizableFields.isEmpty()) {
buildMethodFieldModifiers(classContext);
super.visitClassContext(classContext);
for (FieldInfo fi : localizableFields.values()) {
FieldAnnotation fa = fi.getFieldAnnotation();
SourceLineAnnotation sla = fi.getSrcLineAnnotation();
BugInstance bug = new BugInstance(this, BugType.FCBL_FIELD_COULD_BE_LOCAL.name(), NORMAL_PRIORITY).addClass(this).addField(fa);
if (sla != null) {
bug.addSourceLine(sla);
}
bugReporter.reportBug(bug);
}
}
} finally {
localizableFields = null;
visitedBlocks = null;
clsContext = null;
methodFieldModifiers = null;
}
}
use of org.apache.bcel.classfile.AnnotationEntry in project fb-contrib by mebigfatguy.
the class UnsynchronizedSingletonFieldWrites method isSingleton.
private boolean isSingleton(JavaClass cls) {
if (cls.isEnum()) {
return true;
}
AnnotationEntry[] annotations = cls.getAnnotationEntries();
if (CollectionUtils.isEmpty(annotations)) {
return false;
}
boolean isSpringBean = false;
for (AnnotationEntry annotation : annotations) {
String type = annotation.getAnnotationType();
if (SPRING_CLASS_ANNOTATIONS.contains(type)) {
isSpringBean = true;
} else if (SPRING_SCOPE_ANNOTATION.equals(type)) {
ElementValuePair[] pairs = annotation.getElementValuePairs();
if (!CollectionUtils.isEmpty(pairs)) {
for (ElementValuePair pair : pairs) {
String propName = pair.getNameString();
if ("value".equals(propName) || "scopeName".equals(propName)) {
ElementValue value = pair.getValue();
return "singleton".equals(value.stringifyValue());
}
}
}
}
}
return isSpringBean;
}
use of org.apache.bcel.classfile.AnnotationEntry in project fb-contrib by mebigfatguy.
the class UnsynchronizedSingletonFieldWrites method isIgnorableMethod.
/**
* looks for methods that should not be scanned for fields writes for a variety of reasons
* <ul>
* <li>Constructor</li>
* <li>Static Initializer</li>
* <li>static method</li>
* <li>Has a synchronized attribute</li>
* <li>Has a @PostConstruct annotation</li>
* <li>Has an @Autowired annotation</li>
* </ul>
*
* @param m
* the method to check
* @return if the method should not be scanned
*/
private boolean isIgnorableMethod(Method m) {
if (m.isStatic() || m.isSynchronized()) {
return true;
}
String name = m.getName();
if (Values.CONSTRUCTOR.equals(name) || Values.STATIC_INITIALIZER.equals(name)) {
return true;
}
AnnotationEntry[] annotations = m.getAnnotationEntries();
if (CollectionUtils.isEmpty(annotations)) {
return false;
}
for (AnnotationEntry annotation : annotations) {
String type = annotation.getAnnotationType();
if (IGNORABLE_METHOD_ANNOTATIONS.contains(type)) {
return true;
}
}
return false;
}
use of org.apache.bcel.classfile.AnnotationEntry in project fb-contrib by mebigfatguy.
the class UnitTestAssertionOddities method detectFrameworkType.
/**
* Attempt to identify whether we are dealing with JUnit or TestNG.
*/
private void detectFrameworkType() {
hasAnnotation = false;
Method m = getMethod();
if (isTestCaseDerived && m.getName().startsWith("test")) {
frameworkType = TestFrameworkType.JUNIT;
return;
}
frameworkType = TestFrameworkType.UNKNOWN;
if (!isAnnotationCapable) {
return;
}
AnnotationEntry[] annotations = m.getAnnotationEntries();
if (annotations == null) {
return;
}
for (AnnotationEntry annotation : annotations) {
String annotationType = annotation.getAnnotationType();
if (annotation.isRuntimeVisible()) {
if (TEST_ANNOTATION_SIGNATURE.equals(annotationType)) {
frameworkType = TestFrameworkType.JUNIT;
hasAnnotation = true;
return;
} else if (TESTNG_ANNOTATION_SIGNATURE.equals(annotationType)) {
frameworkType = TestFrameworkType.TESTNG;
hasAnnotation = true;
return;
}
}
}
}
use of org.apache.bcel.classfile.AnnotationEntry in project fb-contrib by mebigfatguy.
the class WiringIssues method visitCode.
@Override
public void visitCode(Code obj) {
Method m = getMethod();
for (AnnotationEntry annotation : m.getAnnotationEntries()) {
String type = annotation.getAnnotationType();
if (type.startsWith("Lorg/junit/") || type.startsWith("Lorg/testng/")) {
return;
}
}
stack.resetForMethodEntry(this);
super.visitCode(obj);
}
Aggregations