Get generic class type and name in abstract class without constructor

huangapple go评论60阅读模式
英文:

Get generic class type and name in abstract class without constructor

问题

我的类:

public class MyClass extends AbstracyEntity {
   
   ...

}

我的服务:

@Service
@RequiredArgsConstructor
public class MyService extends MyAbstractService<MyClass> {
   
   ...

}

我的抽象类:

public abstract class MyAbstractService<E extends AbstracyEntity> {
   
   public void myAbstractMethod() {
      Class<?> c = E.getClass();
      System.out.println(c.getSimpleName());
   }

}

最终期望的抽象服务输出 -> myAbstractMethod():

MyClass

从我的抽象服务抽象出的任何实体,我需要相关实体的类型和类名。因为我想用实体名创建具有共同条件的动态查询。例如:

String sql = "Select t From " + E.getClass.getSimpleName() + " t where ...";

我通过在抽象类中定义一个构造函数来解决了这个问题,但我认为这不是最好的解决办法。什么是最好的解决办法?

英文:

My class:

public class MyClass extends AbstracyEntity {
   
   ...

}

My service:

@Service
@RequiredArgsConstructor
public class MyService extends MyAbstractService<MyClass> {
   
   ...

}

My abstract class:

public abstract class MyAbstractService<E extends AbstracyEntity> {
   
   public void myAbstractMethod() {
      Class<?> c = E.getClass();
      System.out.println(c.getSimpleName());
   }

}

Finally expected output from Abstract Service -> myAbstractMethod() :

MyClass

From whatever entity my abstracted service is abstracted from, I need the related entity's type and class name. Because I aim to create dynamic queries with common conditions with entity name. Example:

String sql = "Select t From " + E.getClass.getSimpleName() + " t where ...";

I solved the problem by defining a constructor in an abstract class, but I don't think it's the best solution. What is the best solution?

答案1

得分: 1

You may infer generic type parameter via something like:

ResolvableType type = 
 ResolvableType.forClass(getClass()).as(MyAbstractService.class);
Class<?>[] typeArguments = type.resolveGenerics(Object.class);
return (Class<E>) typeArguments[0];

However, nothing prevents you from writing that in a clearer way:

public abstract class MyAbstractService<E extends AbstractEntity> {

   private Class<E> type;

   public MyAbstractService(Class<E> type) {
       this.type = type;
   }

}

public class MyService extends MyAbstractService<MyClass> {

    public MyService() {
        super(MyClass.class);
    }
}

英文:

You may infer generic type parameter via something like:

ResolvableType type = 
 ResolvableType.forClass(getClass()).as(MyAbstractService.class);
Class&lt;?&gt;[] typeArguments = type.resolveGenerics(Object.class);
return (Class&lt;E&gt;) typeArguments[0];

However nothing prevents you from writing that in more clear way:

public abstract class MyAbstractService&lt;E extends AbstracyEntity&gt; {

   private Class&lt;E&gt; type;

   pubic MyAbstractService(Class&lt;E&gt; type) {
       this.type = type;
   }

}

public class MyService extends MyAbstractService&lt;MyClass&gt; {

    pubic MyService() {
        super(MyClass.class);
    }
}

huangapple
  • 本文由 发表于 2023年5月7日 19:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定