use of org.apache.bcel.classfile.JavaClass 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.JavaClass in project fb-contrib by mebigfatguy.
the class PossibleMemoryBloat method parseFields.
private void parseFields(ClassContext classContext) {
JavaClass cls = classContext.getJavaClass();
Field[] fields = cls.getFields();
for (Field f : fields) {
String sig = f.getSignature();
if (f.isStatic()) {
if (bloatableSigs.contains(sig)) {
bloatableCandidates.put(XFactory.createXField(cls.getClassName(), f.getName(), f.getSignature(), f.isStatic()), FieldAnnotation.fromBCELField(cls, f));
}
} else if ("Ljava/lang/ThreadLocal;".equals(sig)) {
threadLocalNonStaticFields.add(FieldAnnotation.fromBCELField(cls, f));
}
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class PresizeCollections method isSizedSource.
@Nullable
private PSCUserValue isSizedSource(OpcodeStack.Item itm) {
try {
String sig = itm.getSignature();
JavaClass cls = Repository.lookupClass(sig.substring(1, sig.length() - 1));
if (cls.instanceOf(collectionClass)) {
return new PSCUserValue(true);
}
} catch (ClassNotFoundException e) {
bugReporter.reportMissingClass(e);
}
return null;
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class ReflectionOnObjectMethods method visitClassContext.
/**
* implements the visitor to create the stack and local and field maps for Class arrays to be used for getting the reflection method
*
* @param classContext
* the context object of the currently parse class
*/
@Override
public void visitClassContext(ClassContext classContext) {
try {
stack = new OpcodeStack();
localClassTypes = new HashMap<>();
fieldClassTypes = new HashMap<>();
JavaClass cls = classContext.getJavaClass();
Method staticInit = findStaticInitializer(cls);
if (staticInit != null) {
setupVisitorForClass(cls);
doVisitMethod(staticInit);
}
super.visitClassContext(classContext);
} finally {
stack = null;
localClassTypes = null;
fieldClassTypes = null;
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class RuntimeExceptionDeclared method visitMethod.
/**
* overrides the visitor to find declared runtime exceptions
*
* @param obj
* the method object of the currently parsed method
*/
@Override
public void visitMethod(final Method obj) {
if (obj.isSynthetic()) {
return;
}
ExceptionTable et = obj.getExceptionTable();
if (et != null) {
String[] exNames = et.getExceptionNames();
Set<String> methodRTExceptions = new HashSet<>(6);
int priority = LOW_PRIORITY;
boolean foundRuntime = false;
for (String ex : exNames) {
boolean isRuntime = false;
if (runtimeExceptions.contains(ex)) {
isRuntime = true;
} else {
try {
JavaClass exClass = Repository.lookupClass(ex);
if (exClass.instanceOf(runtimeExceptionClass)) {
runtimeExceptions.add(ex);
if (ex.startsWith("java.lang.")) {
priority = NORMAL_PRIORITY;
}
isRuntime = true;
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
}
if (isRuntime) {
foundRuntime = true;
methodRTExceptions.add(ex);
}
}
if (foundRuntime) {
BugInstance bug = new BugInstance(this, BugType.DRE_DECLARED_RUNTIME_EXCEPTION.name(), priority).addClass(this).addMethod(this);
for (String ex : methodRTExceptions) {
bug.add(new StringAnnotation(ex));
}
bugReporter.reportBug(bug);
}
}
}
Aggregations