简体   繁体   English

有关Java反射的几个问题

[英]A few questions about Java Reflection

I have parent class Entity : 我有父类Entity

package incubator;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class Entity {

    private String getTableName() {
        String result = null;
        Class<?> cl = getClass();
        System.out.println(cl.getName());
        for (Annotation a : cl.getAnnotations()) {
            if (a instanceof EntityTable) {
                EntityTable ent = (EntityTable) a;
                result = ent.name();
                break;
            }
        }
        return result;
    }

    private String getKeyName() {
        String result = null;
        Class<?> cl = getClass();
        System.out.println(cl.getName());
        for (Field f : cl.getDeclaredFields()) {
            for (Annotation a : f.getAnnotations()) {
                if (a instanceof PrimaryKey) {
                    PrimaryKey ann = (PrimaryKey) a;
                    result = ann.name();
                }
            }
        }
        return result;
    }

    public Entity get(int id) throws IllegalAccessException, InstantiationException {
            System.out.println("SELECT * FROM "
                    + getTableName() + " WHERE (" + getKeyName() + "=?);");
            return getClass().newInstance();
    }

    public void delete() {
            System.out.println("DELETE FROM "
                    + getTableName() + " WHERE (" + getKeyName() + "=?);");
    }

}

And child class Child : 和子类Child

package incubator;

@EntityTable(name="table")
public class Child extends Entity {
  @PrimaryKey(name="tbl_pcode")
  private int id;
  @DataField(name="tbl_text")
  public String text;
  @DataField(name = "tbl_data")
  public String data;

  public Child() {
      id = 0;
  }
}

All annotations is like 所有注释都像

package incubator;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EntityTable {
    String name();
}

So, I have a question: Is there any way to make static method get(final int id) of class Entity that will return instance of Child ? 所以,我有一个问题:有什么方法可以使Entity类的静态方法get(final int id)返回Child实例? How can I specify resulting type of child class[es] in parent class? 如何在父类中指定子类的结果类型?

Thanks for wasting your time for me. 感谢您浪费我的时间。 Best regards. 最好的祝福。

Because of type erasure there is no way to get the actual type in a static context at runtime. 由于类型擦除,无法在运行时在静态上下文中获取实际类型。 You'd have to explicitly declare the class when calling the method. 调用该方法时,您必须显式声明该类。 Using a generic method it would look like this: 使用通用方法将如下所示:

public static <T extends Entity> T get(int id, Class<T> clazz) {
    return clazz.newInstance();
}

I'm not sure if this is useful in your case but it's the only way if you want to go static. 我不确定这是否对您有用,但这是您保持静态的唯一方法。

Use generics for this: 为此使用泛型:

// Subclasses will have to pass their type as a generic type argument
// Which you will use to declare the return type of get()

class Entity<T extends Entity<T>> {
    T get(int id) {
        T value = ...; // Load the value from db or whatever
        return value;
    }
}

// Child tells its parent that the dynamic type is Child
class Child extends Entity<Child> {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM