• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Generic method

Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on an application that invokes several different downstream 3rd party web services. Each web service has its own ServiceError class (tool generated javax.xml.bind.annotation class).

Processing errors from each different service invocation is the same so I wrote this generic method:

Is there a better way to write this code?

ServiceError1 and ServiceError2 come from different package and do not implement the same/any interface. These classes are tool generated and I cannot manually change them.

Thank you!

Posts: 22700
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code isn't great, because each time a new generated class is added, you need to add a new else-if clause to that method. If you forget to then nothing will happen. Perhaps adding an else clause that throws an exception if none of the clauses matches can at least help you find such occurrences instead of failing very silently.

However, this is tedious and error prone. Can you modify the code generation code to add an interface? If not, can you check if you can generate this method somehow?
Something must be done about this. Let's start by reading this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic