If I understand your issue correctly, you want to parametrize the query. You can use regular parameters with named queries like this:
@NamedQuery(name = "allFailures", query = "select failureInfo from FailureInfo
failureInfo where failureInfo.counter <= :counter
AND failureInfo.counter <> 0
order by failureInfo.counter asc LIMIT 1")
Once you have constructed the query, set the parameter, using a static method a regular method or a variable:
TypedQuery<FailureInfo> query =
em.createNamedQuery("FailureInfo.allFailures", FailureInfo.class);
query.setParameter("counter", MyClass.myMethod());
query.getResultList();