use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class CollectionNamingConfusion method checkConfusedName.
/**
* looks for a name that mentions a collection type but the wrong type for the variable
*
* @param methodOrVariableName
* the method or variable name
* @param signature
* the variable signature
* @return whether the name doesn't match the type
*/
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "EXS_EXCEPTION_SOFTENING_RETURN_FALSE", justification = "No other simple way to determine whether class exists")
private boolean checkConfusedName(String methodOrVariableName, String signature) {
try {
String name = methodOrVariableName.toLowerCase(Locale.ENGLISH);
if ((name.endsWith("map") || (name.endsWith("set") && !name.endsWith("toset")) || name.endsWith("list") || name.endsWith("queue")) && signature.startsWith("Ljava/util/")) {
String clsName = SignatureUtils.stripSignature(signature);
JavaClass cls = Repository.lookupClass(clsName);
if ((cls.implementationOf(mapInterface) && !name.endsWith("map")) || (cls.implementationOf(setInterface) && !name.endsWith("set")) || ((cls.implementationOf(listInterface) || cls.implementationOf(queueInterface)) && !name.endsWith("list") && !name.endsWith("queue"))) {
return true;
}
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
return false;
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class IncorrectInternalClassUse method visitClassContext.
/**
* implements the visitor to look for classes that reference com.sun.xxx, or org.apache.xerces.xxx classes by looking for class Const in the constant pool
*
* @param context
* the context object of the currently parsed class
*/
@Override
public void visitClassContext(ClassContext context) {
JavaClass cls = context.getJavaClass();
if (!isInternal(cls.getClassName())) {
ConstantPool pool = cls.getConstantPool();
int numItems = pool.getLength();
for (int i = 0; i < numItems; i++) {
Constant c = pool.getConstant(i);
if (c instanceof ConstantClass) {
String clsName = ((ConstantClass) c).getBytes(pool);
if (isInternal(clsName)) {
bugReporter.reportBug(new BugInstance(this, BugType.IICU_INCORRECT_INTERNAL_CLASS_USE.name(), NORMAL_PRIORITY).addClass(cls).addString(clsName));
}
}
}
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class JPAIssues method getAnnotatedRollbackExceptions.
/**
* parses an spring-tx @Transactional annotations for rollbackFor/noRollbackfor attributes of a @Transactional annotation.
*
* @param method
* the currently parsed method
*
* @return the exception classes declared in the @Transactional annotation
*
* @throws ClassNotFoundException
* if exception classes are not found
*/
private Set<JavaClass> getAnnotatedRollbackExceptions(Method method) throws ClassNotFoundException {
for (AnnotationEntry annotation : method.getAnnotationEntries()) {
if ("Lorg/springframework/transaction/annotation/Transactional;".equals(annotation.getAnnotationType())) {
if (annotation.getNumElementValuePairs() == 0) {
return Collections.<JavaClass>emptySet();
}
Set<JavaClass> rollbackExceptions = new HashSet<>();
for (ElementValuePair pair : annotation.getElementValuePairs()) {
if ("rollbackFor".equals(pair.getNameString()) || "noRollbackFor".equals(pair.getNameString())) {
String exNames = pair.getValue().stringifyValue();
Matcher m = annotationClassPattern.matcher(exNames);
while (m.find()) {
String exName = m.group(1);
JavaClass exCls = Repository.lookupClass(SignatureUtils.trimSignature(exName));
if (!exCls.instanceOf(runtimeExceptionClass)) {
rollbackExceptions.add(exCls);
}
}
}
}
return rollbackExceptions;
}
}
return Collections.<JavaClass>emptySet();
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class ConfusingFunctionSemantics method visitCode.
/**
* implements the visitor to look for any non-immutable typed parameters are assignable to the return type. If found, the method is parsed.
*
* @param obj
* the context object of the currently parsed code block
*/
@Override
public void visitCode(Code obj) {
try {
possibleParmRegs.clear();
Method m = getMethod();
String methodSignature = m.getSignature();
String retSignature = SignatureUtils.getReturnSignature(methodSignature);
JavaClass returnClass = null;
int[] parmRegs = null;
if (retSignature.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX) && !knownImmutables.contains(retSignature)) {
List<String> parmTypes = SignatureUtils.getParameterSignatures(methodSignature);
for (int p = 0; p < parmTypes.size(); p++) {
String parmSignature = parmTypes.get(p);
if (parmSignature.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX) && !knownImmutables.contains(parmSignature)) {
if (returnClass == null) {
returnClass = Repository.lookupClass(SignatureUtils.trimSignature(retSignature));
parmRegs = RegisterUtils.getParameterRegisters(m);
}
if (parmRegs != null) {
JavaClass parmClass = Repository.lookupClass(SignatureUtils.stripSignature(parmSignature));
if (parmClass.instanceOf(returnClass)) {
possibleParmRegs.put(Integer.valueOf(parmRegs[p]), new ParmUsage());
}
}
}
}
if (!possibleParmRegs.isEmpty()) {
try {
stack.resetForMethodEntry(this);
super.visitCode(obj);
for (ParmUsage pu : possibleParmRegs.values()) {
if ((pu.returnPC >= 0) && (pu.alteredPC >= 0) && (pu.returnPC > pu.alteredPC)) {
bugReporter.reportBug(new BugInstance(this, BugType.CFS_CONFUSING_FUNCTION_SEMANTICS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, pu.returnPC).addSourceLine(this, pu.alteredPC));
}
}
} catch (StopOpcodeParsingException e) {
// no parm regs left
}
}
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
}
use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.
the class CopiedOverriddenMethod method visitClassContext.
/**
* overrides the visitor to accept classes derived from non java.lang.Object classes.
*
* @param clsContext
* the context object of the currently parsed class
*/
@Override
public void visitClassContext(ClassContext clsContext) {
try {
JavaClass cls = clsContext.getJavaClass();
String superName = cls.getSuperclassName();
if (!Values.DOTTED_JAVA_LANG_OBJECT.equals(superName)) {
this.classContext = clsContext;
superclassCode = new HashMap<>();
JavaClass superCls = cls.getSuperClass();
childPoolGen = new ConstantPoolGen(cls.getConstantPool());
parentPoolGen = new ConstantPoolGen(superCls.getConstantPool());
Method[] methods = superCls.getMethods();
for (Method m : methods) {
String methodName = m.getName();
if (m.isPublic() && !m.isAbstract() && !m.isSynthetic() && !Values.CONSTRUCTOR.equals(methodName) && !Values.STATIC_INITIALIZER.equals(methodName)) {
String methodInfo = methodName + ':' + m.getSignature();
superclassCode.put(methodInfo, new CodeInfo(m.getCode(), m.getAccessFlags()));
}
}
cls.accept(this);
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
} finally {
superclassCode = null;
this.classContext = null;
childPoolGen = null;
parentPoolGen = null;
}
}
Aggregations