jeudi 13 mai 2021

How to use a default Constructor genericly

I wanna activate a default constructor in a generic way.

As in, getting an array of constructors and activating one of them (lets assume the first one is default).

The bit of code i wanna use it in is as so:

  List<Constructor> constructors=
  Arrays.stream(clazz.getDeclaredConstructors()).filter(X->X.isAnnotationPresent(Inject.class)).collect(Collectors.toList());
       if(constructors.size()>1)
                throw new MultipleInjectConstructorsException();
        else if(constructors.size()==0){
                List<Constructor> defaultctor=
                Arrays.stream(clazz.getDeclaredConstructors()).filter(X->X.getParameterCount()==0).collect(Collectors.toList());
                if(defaultctor.size()==0)
                    throw new NoConstructorFoundException();
                else {
                    /**activate the default constructor i got*/
                    //obj=defaultctor.get(0); 
    
                }
            }

How can i use the default constructor which is in defaultctor.get(0)?





Aucun commentaire:

Enregistrer un commentaire