vendredi 22 juillet 2016

java reflection :Avoid warning unchecked call to getConstructor(Class?> ...) as a member of the raw type Class

I have read this post

java: unchecked call to getConstructor(java.lang.Class<?>...)

for (Map.Entry<String, Class> entry : connectionRepository.entrySet()) {
            if (/*someconditionhere*/) {
                try {
                    cwsConnection = (CWSConnection)entry.getValue().getConstructor().newInstance();
                    break;
                } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
                    logger.error("Could not Create instance of CWSConnection for site version:\"{}\" Error:\"{}\"", entry.getKey(), e.getMessage(), e);
                } 
            }
        }

While compiling this piece of code I am getting a warning

warning: [unchecked] unchecked call to getConstructor(Class...) as a member of the raw type Class

I just wanted to get the default constructor of CWSConnection, therefore, I should not pass any parameters to getConstructor(Class...) method. Is there any better approach to get the default constructor(The one without arguments)

(I know @SupressWarning annotation will suppress this warning.)





Aucun commentaire:

Enregistrer un commentaire