try { JavaClass classStored = first.getJavaClass(); if (classStored == null) { return;
type = item.getJavaClass(); } catch (ClassNotFoundException e) { AnalysisContext.reportMissingClass(e);
uselessVoidCandidate = false; try { JavaClass javaClass = exceptionItem.getJavaClass(); if (javaClass != null && ALLOWED_EXCEPTIONS.contains(javaClass.getClassName())) { break;
/** * increment the count of class access of the class on the stack * * @param classAtStackIndex * the position on the stack of the class in question * * @return true if the class is counted */ private boolean countClassAccess(final int classAtStackIndex) { String calledClass; try { if (stack.getStackDepth() > classAtStackIndex) { OpcodeStack.Item itm = stack.getStackItem(classAtStackIndex); JavaClass cls = itm.getJavaClass(); if (cls != null) { calledClass = cls.getClassName(); countClassAccess(calledClass); return true; } } } catch (ClassNotFoundException cfne) { bugReporter.reportMissingClass(cfne); } return false; }
/** * increment the count of class access of the class on the stack * * @param classAtStackIndex * the position on the stack of the class in question * * @return true if the class is counted */ private boolean countClassAccess(final int classAtStackIndex) { String calledClass; try { if (stack.getStackDepth() > classAtStackIndex) { OpcodeStack.Item itm = stack.getStackItem(classAtStackIndex); JavaClass cls = itm.getJavaClass(); if (cls != null) { calledClass = cls.getClassName(); countClassAccess(calledClass); return true; } } } catch (ClassNotFoundException cfne) { bugReporter.reportMissingClass(cfne); } return false; }
/** * returns whether the item at the stackPos location on the stack is an enum, and doesn't implement any interfaces * * @param stackPos * the position on the opstack to check * * @return whether the class is an enum * @throws ClassNotFoundException * if the class can not be loaded */ private boolean isEnum(int stackPos) throws ClassNotFoundException { if (stack.getStackDepth() <= stackPos) { return false; } OpcodeStack.Item item = stack.getStackItem(stackPos); if (!item.getSignature().startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) { return false; } JavaClass cls = item.getJavaClass(); if ((cls == null) || !cls.isEnum()) { return false; } // If the cls implements any interface, it's possible the collection // is based on that interface, so ignore return cls.getInterfaces().length == 0; }
/** * looks for calls to setSize on components, rather than letting the layout manager set them * * @param methodName * the method that was called on a component * * @throws ClassNotFoundException * if the gui class wasn't found */ private void processSetSizeOps(String methodName) throws ClassNotFoundException { if ("setSize".equals(methodName)) { int argCount = SignatureUtils.getNumParameters(getSigConstantOperand()); if ((windowClass != null) && (stack.getStackDepth() > argCount)) { OpcodeStack.Item item = stack.getStackItem(argCount); JavaClass cls = item.getJavaClass(); if ((cls != null) && cls.instanceOf(windowClass)) { bugReporter.reportBug( new BugInstance(this, BugType.S508C_NO_SETSIZE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this)); } } } } }
private void defaultToStringSilliness() throws ClassNotFoundException { if (stack.getStackDepth() >= 1) { OpcodeStack.Item item = stack.getStackItem(0); JavaClass toStringClass = item.getJavaClass(); if (toStringClass != null) { String toStringClassName = toStringClass.getClassName(); if (!toStringClass.isInterface() && !toStringClass.isAbstract() && !Values.DOTTED_JAVA_LANG_OBJECT.equals(toStringClassName) && !Values.DOTTED_JAVA_LANG_STRING.equals(toStringClassName) && toStringClasses.add(toStringClassName)) { try { JavaClass cls = Repository.lookupClass(toStringClassName); if (!hasToString(cls)) { bugReporter.reportBug( new BugInstance(this, BugType.SPP_NON_USEFUL_TOSTRING.name(), toStringClass.isFinal() ? NORMAL_PRIORITY : LOW_PRIORITY) .addClass(this).addMethod(this).addSourceLine(this)); } } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } } } } }
/** * implements the visitor to find return values where the types of objects returned from the method are related only by object. * * @param seen * the opcode of the currently parsed instruction */ @Override public void sawOpcode(int seen) { try { stack.precomputation(this); if ((seen == Const.ARETURN) && (stack.getStackDepth() > 0)) { OpcodeStack.Item itm = stack.getStackItem(0); if (!itm.isNull()) { returnTypes.put(itm.getJavaClass(), Integer.valueOf(getPC())); } } } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } finally { stack.sawOpcode(this, seen); } }
/** * looks for calls to setSize on components, rather than letting the layout manager set them * * @param methodName * the method that was called on a component * * @throws ClassNotFoundException * if the gui class wasn't found */ private void processSetSizeOps(String methodName) throws ClassNotFoundException { if ("setSize".equals(methodName)) { int argCount = SignatureUtils.getNumParameters(getSigConstantOperand()); if ((windowClass != null) && (stack.getStackDepth() > argCount)) { OpcodeStack.Item item = stack.getStackItem(argCount); JavaClass cls = item.getJavaClass(); if ((cls != null) && cls.instanceOf(windowClass)) { bugReporter.reportBug( new BugInstance(this, BugType.S508C_NO_SETSIZE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this)); } } } } }
private void equalsSilliness(String className) { try { JavaClass cls = Repository.lookupClass(className); if (cls.isEnum()) { bugReporter.reportBug( new BugInstance(this, BugType.SPP_EQUALS_ON_ENUM.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this)); } else { if (stack.getStackDepth() >= 2) { OpcodeStack.Item item = stack.getStackItem(1); cls = item.getJavaClass(); if (cls != null) { String clsName = cls.getClassName(); if (oddMissingEqualsClasses.contains(clsName)) { bugReporter.reportBug(new BugInstance(this, BugType.SPP_EQUALS_ON_STRING_BUILDER.name(), NORMAL_PRIORITY).addClass(this) .addMethod(this).addSourceLine(this)); } } } } } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } }
/** * implements the visitor to find return values where the types of objects returned from the method are related only by object. * * @param seen * the opcode of the currently parsed instruction */ @Override public void sawOpcode(int seen) { try { stack.precomputation(this); if ((seen == ARETURN) && (stack.getStackDepth() > 0)) { OpcodeStack.Item itm = stack.getStackItem(0); if (!itm.isNull()) { returnTypes.put(itm.getJavaClass(), Integer.valueOf(getPC())); } } } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } finally { stack.sawOpcode(this, seen); } }
private void defaultToStringSilliness() throws ClassNotFoundException { if (stack.getStackDepth() >= 1) { OpcodeStack.Item item = stack.getStackItem(0); JavaClass toStringClass = item.getJavaClass(); if (toStringClass != null) { String toStringClassName = toStringClass.getClassName(); if (!toStringClass.isInterface() && !toStringClass.isAbstract() && !Values.DOTTED_JAVA_LANG_OBJECT.equals(toStringClassName) && !Values.DOTTED_JAVA_LANG_STRING.equals(toStringClassName) && toStringClasses.add(toStringClassName)) { try { JavaClass cls = Repository.lookupClass(toStringClassName); if (!hasToString(cls)) { bugReporter.reportBug( new BugInstance(this, BugType.SPP_NON_USEFUL_TOSTRING.name(), toStringClass.isFinal() ? NORMAL_PRIORITY : LOW_PRIORITY) .addClass(this).addMethod(this).addSourceLine(this)); } } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } } } } }
private void equalsSilliness(String className) { try { JavaClass cls = Repository.lookupClass(className); if (cls.isEnum()) { bugReporter.reportBug( new BugInstance(this, BugType.SPP_EQUALS_ON_ENUM.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this)); } else { if (stack.getStackDepth() >= 2) { OpcodeStack.Item item = stack.getStackItem(1); cls = item.getJavaClass(); if (cls != null) { String clsName = cls.getClassName(); if (oddMissingEqualsClasses.contains(clsName)) { bugReporter.reportBug(new BugInstance(this, BugType.SPP_EQUALS_ON_STRING_BUILDER.name(), NORMAL_PRIORITY).addClass(this) .addMethod(this).addSourceLine(this)); } } } } } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } }
/** @return true if given Item is throwable */ public boolean checkThrowable(Item stackItem) { try { JavaClass typeOfStack = stackItem.getJavaClass(); if (typeOfStack == null) { // means primitive or array return false; } return typeOfStack.instanceOf(THROWABLE); } catch (ClassNotFoundException e) { AnalysisContext.reportMissingClass(e); return false; } } }
private void checkForWEM() throws ClassNotFoundException { if (stack.getStackDepth() == 0) { return; } OpcodeStack.Item item = stack.getStackItem(0); if (item.getUserValue() == null) { return; } JavaClass exClass = item.getJavaClass(); if ((exClass == null) || !ignorableExceptionTypes.contains(exClass.getClassName())) { bugReporter.reportBug( new BugInstance(this, BugType.WEM_WEAK_EXCEPTION_MESSAGING.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this)); } } }
private void checkForWEM() throws ClassNotFoundException { if (stack.getStackDepth() == 0) { return; } OpcodeStack.Item item = stack.getStackItem(0); if (item.getUserValue() == null) { return; } JavaClass exClass = item.getJavaClass(); if ((exClass == null) || !ignorableExceptionTypes.contains(exClass.getClassName())) { bugReporter.reportBug( new BugInstance(this, BugType.WEM_WEAK_EXCEPTION_MESSAGING.name(), LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this)); } } }
private void memorizeResultOfGetClassMethod(int code) { Item caller = getStack().getStackItem(0); JavaType classOfCaller; try { classOfCaller = JavaType.from(caller.getJavaClass()); } catch (ClassNotFoundException e) { throw new AssertionError(e); } finally { super.afterOpcode(code); } Item returnedClass = getStack().getStackItem(0); returnedClass.setUserValue(classOfCaller); }
private void checkForProblemsWithLoggerSingleArgumentMethod() throws ClassNotFoundException { final JavaClass clazz = stack.getStackItem(0).getJavaClass(); if ((clazz != null) && clazz.instanceOf(throwableClass)) { bugReporter.reportBug(new BugInstance(this, BugType.LO_LOGGER_LOST_EXCEPTION_STACK_TRACE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this) .addSourceLine(this)); } }
private void checkForProblemsWithLoggerSingleArgumentMethod() throws ClassNotFoundException { final JavaClass clazz = stack.getStackItem(0).getJavaClass(); if ((clazz != null) && clazz.instanceOf(throwableClass)) { bugReporter.reportBug(new BugInstance(this, BugType.LO_LOGGER_LOST_EXCEPTION_STACK_TRACE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this) .addSourceLine(this)); } }