General Information about how to check for non unique IDs in Java code
<p>In a very huge project, there are alot of ExceptionClasses where exception details are implemented as <code>enum</code>.</p>
<pre><code>NAME_OF_EXCEPTION_DETAIL(ID, PRIORITY)
</code></pre>
<p>e.g:</p>
<pre><code>NO_LICENSE_FOUND(100, 0)
UNSUPPORTED_LICENSE(101, 1)
WRONG_LICENSE_FOR_VERSION(101, 0)
</code></pre>
<p>In some cases the exception details ID is the same, wich should never have happend. It is pretty damn possible that there are more duplicates but this project is to huge to check this by hand ...</p>
<p>Is there a smart way to check the code for duplicates, e.g. UnitTesting - never used it befor.</p>
<p>Thank's for your help!</p>
<p><strong>Edit</strong> (Bens answer is a good way to solve this problem when using just one enum):
To be more clear in my specific task this are the circumstances.
There are alot of Classes that are written to handle ExceptionDetails e.g.</p>
<pre><code>TopicOneExceptionDetails.java
TopicTwoExceptionDetails.java  (and so on)
</code></pre>
<p>Each of these classes defindes a enum for this topic like so:</p>
<pre><code>public enum TopicOneExceptionDetails implements ApplicationException.ExceptionDetails { .... }
</code></pre>
<p>followed by the declaration of errors related to TopicOne errors e.g.</p>
<pre><code>SETTING_TIMEZONE_FAILED(55, ...)
Setting_DATETIME_FAILED(56, ...)
</code></pre>
<p>in this enum for TopicOne every error ID has to be unique. But for example ID 55 (here used for SETTING_TIMEZONE_FAILED) can be used in the declaration of errors related to TopicTwo without a problem.</p>
<p><strong>Edit 2</strong> (Use reflections ?)
I found a <a href="https://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful">pretty informative answer</a> about java <code>Reflections</code> written by <a href="https://stackoverflow.com/users/797/matt-sheppard">Matt Sheppard</a>. I think this could solve my problem. I need to take a break but i will report back. My current thinkings are:</p>
<pre><code>Reflections reflections = new Reflections(&quot;project.de&quot;)
for(Class&lt;? extends ApplicationException.ExceptionDetails&gt; exceptionDetailsClasses : reflections.getSubTypesOf(ApplicationException.ExceptionDetails.class)){
for ( ... ) .. }
</code></pre>
<p>I should be able to check for the IDs e.g. like Ben advised. I'll report back later.</p>
<p><strong>EDIT 3</strong> (Question Solved!)
Like in edit 2 described i was able to easily solve this issue using <code>reflections</code>.</p>
<pre><code>public class ApplicationExceptionDetailsErrorCodeValidationTest {


@Test
  public void validateErrorCodeUniqueness() throws ErrorCodeUniquenessBroken{
    Reflections reflections = new Reflections(&quot;de.xyz&quot;);
for (Class&lt;? extends ApplicationException.ExceptionDetails&gt; exceptionDetailsClass : reflections.getSubTypesOf(ApplicationException.ExceptionDetails.class)) {
  if (exceptionDetailsClass.getSimpleName().equals(&quot;TestExceptionDetails&quot;)) {
    continue;
  }

  List&lt;Integer&gt; errorCodes = new ArrayList&lt;Integer&gt;();

  for (ApplicationException.ExceptionDetails exceptionDetails : exceptionDetailsClass.getEnumConstants()) {
    if (errorCodes.contains(exceptionDetails.getErrorCode().getId()))  throw new ErrorCodeUniquenessBroken(&quot;ErrorCode uniqueness broken! ErrorCode: &quot; + exceptionDetails.getMessageKey() + &quot; has repeated ErrorCode! Change it!&quot;);
    else errorCodes.add(exceptionDetails.getErrorCode().getId());
  }
}
</code></pre>
<p>}
}</p>
<p>Special thanks to Ben and Spotted for their effort. The both answered with some pretty good examples in how to resolve the topics question in general. In my case it just got a bit more complicated.</p>