Chrizzldi Chrizzldi - 1 year ago 70
Java Question

General Information about how to check for non unique IDs in Java code

In a very huge project, there are alot of ExceptionClasses where exception details are implemented as





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 ...

Is there a smart way to check the code for duplicates, e.g. UnitTesting - never used it befor.

Thank's for your help!

Edit (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. (and so on)

Each of these classes defindes a enum for this topic like so:

public enum TopicOneExceptionDetails implements ApplicationException.ExceptionDetails { .... }

followed by the declaration of errors related to TopicOne errors e.g.

Setting_DATETIME_FAILED(56, ...)

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.

Edit 2 (Use reflections ?)
I found a pretty informative answer about java
written by Matt Sheppard. I think this could solve my problem. I need to take a break but i will report back. My current thinkings are:

Reflections reflections = new Reflections("")
for(Class<? extends ApplicationException.ExceptionDetails> exceptionDetailsClasses : reflections.getSubTypesOf(ApplicationException.ExceptionDetails.class)){
for ( ... ) .. }

I should be able to check for the IDs e.g. like Ben advised. I'll report back later.

EDIT 3 (Question Solved!)
Like in edit 2 described i was able to easily solve this issue using

public class ApplicationExceptionDetailsErrorCodeValidationTest {

public void validateErrorCodeUniqueness() throws ErrorCodeUniquenessBroken{
Reflections reflections = new Reflections("");
for (Class<? extends ApplicationException.ExceptionDetails> exceptionDetailsClass : reflections.getSubTypesOf(ApplicationException.ExceptionDetails.class)) {
if (exceptionDetailsClass.getSimpleName().equals("TestExceptionDetails")) {

List<Integer> errorCodes = new ArrayList<Integer>();

for (ApplicationException.ExceptionDetails exceptionDetails : exceptionDetailsClass.getEnumConstants()) {
if (errorCodes.contains(exceptionDetails.getErrorCode().getId())) throw new ErrorCodeUniquenessBroken("ErrorCode uniqueness broken! ErrorCode: " + exceptionDetails.getMessageKey() + " has repeated ErrorCode! Change it!");
else errorCodes.add(exceptionDetails.getErrorCode().getId());


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.

Ben Ben
Answer Source

One way to check your Enum for duplicate IDs (It's not tested, but should work).
You can use parts of this code for your unit-test (or use it standalone):

import java.util.HashSet;
import java.util.Set;

public class NewClass 
   public static enum Errors
      ERROR_D(2), //duplicate is here!

      int m_id;
      Errors(int id)
          m_id = id;

      public int getId()
          return m_id;

   public static void main(String[] args) 
      Set<Integer> usedIds = new HashSet<>();
      for (Errors e : Errors.values()) //iterate over all Error-Enum-Items
          int id = e.getId(); //fetch id from enum-item
          if (usedIds.contains(id)) //check if id was used before
              System.err.println("ID ALREADY USED: " + + ":" + id);
              usedIds.add(id); //remember new used id here


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download