Java 实例化抽象超类的子类

Java 实例化抽象超类的子类,java,reflection,subclass,instantiation,invoke,Java,Reflection,Subclass,Instantiation,Invoke,我有一个这样的超类,它有一个工厂方法: @DiscriminatorColumn( name = "etype", discriminatorType = DiscriminatorType.STRING ) public abstract class ChallengeReward { public static ChallengeReward createFromFactory(String rewardType){ ChallengeRewa

我有一个这样的超类,它有一个工厂方法:

@DiscriminatorColumn(
      name = "etype",
      discriminatorType = DiscriminatorType.STRING
)
public abstract class ChallengeReward {
      public static ChallengeReward createFromFactory(String rewardType){
      ChallengeRewardType type = ChallengeReward.fromString(rewardType);

      ChallengeReward challengeReward = null;
      switch(type){
      case point:
         challengeReward = new PointChallengeReward();
         break;
      case notification:
         challengeReward = new NotificationChallengeReward();
         break;
      case item:
         challengeReward = new ItemChallengeReward();
         break;
      }

      return challengeReward;
   }

   public String getClientId(){
      return "ABCDEF";
   }
}
子类本身没有构造函数。因此,所有挑战奖励都位于同一个表中,有一个名为“etype”的鉴别器列

现在的问题是我想反射性地调用方法getClientId(),但我不能实例化ChallengerWard,因为它是抽象的。所以我需要实例化它的一个子类,但我不能做subclass.newInstance()

我的选择是什么

编辑1: 对不起,我的问题不是很清楚。问题是我正在编写一个通用servlet,它将遍历包中的所有类,因此需要进行反射。虽然该方法实际上是静态的,但我不知道如何静态地调用它,因为我只知道运行时的当前类

编辑2:
事实证明,您可以调用method.invoke(null)来调用静态方法,谢谢您3

我认为您可以通过使用类名本身获得
方法,然后按如下方式调用该方法:

     String clientId = null;
     Class challengeRewardClass =Class.forName(ChallengeReward.class.getName());
     Method[] methods = challengeRewardClass.getMethods();
     for(Method method: methods){
        if(method.getName().equals("getClientId")){
            clientId = method.invoke(objectoToBeUsedForMethodCall, null);
        }
     }

要调用工厂方法,您不需要一个实例:一般来说,如果您使用的是反射,那么您已经将自己过度设计到了一个角落,需要考虑更改您的体系结构。您不能将
getClientId
设置为静态吗?