Effective Java, Second Edition recommends (Item 24) to suppress warnings when necessary, but at the smallest scope possible. Sometimes this means the need to declare a local variable that would normally be flagged by this rule. Would be nice to enhance this rule such that variables with such a SuppressWarnings do not trigger the rule. Here is an example function.
private <T extends UserVisibleEventTypeValue<?>> T findEventTypeValueByName(Class<T> entityClass, String eventTypeName, String eventTypeValueName) {
Criteria criteria = this.session.createCriteria(entityClass)
.add(Restrictions.eq(UserVisibleEventTypeValue_.internalName, eventTypeValueName))
.createCriteria(UserVisibleEventTypeValue_.type)
.add(Restrictions.eq(UserVisibleEventType_.name, eventTypeName))
.setCacheable(true)
;
@SuppressWarnings("unchecked")
T result = (T) criteria.uniqueResult();
return result;
}
The result variable is triggering this. It is prefered to do this however rather than put the annotation on the method level.
See https://github.com/pmd/pmd/pull/262