0

I would like to understand of this issue

public class DogController extends FeedCommonController<DogModel extends CommonAnimalModel, FARMER_1_AUTH> {
  // something

 // This is working - but there is duplication
  @Security.Authenticated(FARMER_1_AUTH.class)
  public boolean feed_it() {
    // nothing special
    DogModel.getRandom().feed_it();
  }
}

public class CatController extends FeedCommonController<CatModel extends CommonAnimalModel, FARMER_2_AUTH> {
  // something

  // This is working - but there is duplication 
  @Security.Authenticated(FARMER_2_AUTH.class)
  public boolean feed_it() {
    // nothing special
     CatModel.getRandom().feed_it();
  }
}

And I want to simplify the code and remove the duplicate methods, but I cannot put Class type to annotation.

public abstract class CommonAnimalController< T extends CommonAnimalModel, XXXXXX> {

   @Security.Authenticated(XXXXXX.class) // <-- Here is a problem with declaration 
   public boolean feed_it() {
      T.getRandom().feed_it();
   }

} 
/**
Get Token From HTTP Request from Actual Thread
*/
public class Security {

  @With(AuthenticatedAction.class)
  @Target({ElementType.TYPE, ElementType.METHOD})
  @Retention(RetentionPolicy.RUNTIME)
  public @interface Authenticated {
    Class<? extends Authenticator> value() default Authenticator.class;
  }

}


Concept with Annotation is already created and implemented on hundred classes. So its not possible make huge changes. But Its some Elegant way how to solved this?

TyZet
  • 61
  • 5
  • since you are sharing the code, make it simple, share everything for a compilable code. Are missing the mdoels. DogModel, etc. Possibly all in a single code place so one can copy paste your code and check deeper. – Ermal Nov 23 '19 at 16:41
  • extended is not even a java syntax – Ermal Nov 23 '19 at 16:55
  • ITs Simple code, and its not question about compilation.. but about how to design the code and what patterns to use. – TyZet Nov 23 '19 at 17:47

1 Answers1

0

You have two problems in one question.
First problem you have is: How to get a class instance of generics type T
This is answered here: How to get a class instance of generics type T

The second problem you have, is how to avoid passing a constant to the annotation.
You will have a compliation error "Attribute value must be constant"
For this second problem seems there is no simple way to achieve it in Java. (pheraps I am wrong)
See this answer: How to supply value to an annotation from a Constant java

Solution for problem1

public abstract class CommonAnimalController<T extends CommonAnimalModel, XXXXXX> {
    final Class<XXXXXX> typeParameterClass;

    public CommonAnimalController(Class<XXXXXX> typeParameterClass) {
        this.typeParameterClass = typeParameterClass;
    }
    @Security.Authenticated(typeParameterClass)  // you will have "Attribute value must be constant"
    public boolean feed_it() {
        return T.getRandom().feed_it();
    }
}
Ermal
  • 441
  • 5
  • 19