如何获得超类的this.getClass()。getConstructor?

时间:2015-01-10 16:40:45

标签: java reflection constructor

我有没有办法直接通过super获得getContructor实施?我想在“这个类或任何超类”上调用构造函数。

方案详细信息是我有一个基类,它使用反射构建其数据,但数据来自外部文件。外部加载器有一个查找,用于检查特定类是否存在数据,并且所有数据都包含在下面的ImplementedCard中。

这很好(足够)并且除了我需要能够从ImplementedCard实例开始创建所有这些实例之外与问题没有直接关系:

public class Card implements DeepCopyable<Card> {
   protected ImplementedCardList.ImplementedCard implementedCard;

   public Card() {
        this.implementedCard = ImplementedCardList.getInstance().getCardForClass(this.getClass());
        this.initFromImplementedCard(this.implementedCard);
    }

    public Card(ImplementedCardList.ImplementedCard implementedCard) {
        this.implementedCard = implementedCard;
        this.initFromImplementedCard(this.implementedCard);
    }

    public void initFromImplementedCard(ImplementedCardList.ImplementedCard implementedCard) {
        if (implementedCard != null) {
            this.name_ = implementedCard.name_;
            /* ... and so on */
       }
    }

    // This deepCopy pattern is required because we use the class of each card to recreate it under certain circumstances
    @Override
    public Card deepCopy() {
        Card copy = null;
        try {
            try {
                copy = this.getClass().getConstructor(ImplementedCardList.ImplementedCard.class).newInstance(this.implementedCard);
            } catch(NoSuchMethodException e) {
                if(!this.getClass().equals(TestHero.class)) {
                    log.warn(this.getClass().toString() + " is missing ImplementedCard constructor");
                }
                copy = getClass().newInstance();
            } catch(InvocationTargetException e) {
                log.error("InvocationTargetException error", e);
                copy = getClass().newInstance();
            }
        } catch(InstantiationException e) {
            log.error("instantiation error", e);
        } catch(IllegalAccessException e) {
            log.error("illegal access error", e);
        }
        if (copy == null) {
            throw new RuntimeException("unable to instantiate card.");
        }

        copy.name_ = this.name_;
        /* ... and so on */

        return copy;
    }
}

然后像这样扩展这个基类:

public class Minion extends Card implements CardEndTurnInterface, CardStartTurnInterface {
    public Minion() {
        super();
    }

    public Minion(ImplementedCardList.ImplementedCard implementedCard) {
        super(implementedCard);
    }

    @Override
    public void initFromImplementedCard(ImplementedCardList.ImplementedCard implementedCard) {
        if (implementedCard != null) {
            super.initFromImplementedCard(implementedCard);

            /* custom init goes here */
        }
    }

    /* other class details follow */
}

public abstract class Hero extends Minion implements MinionSummonedInterface {
    public Hero() {
        super();
    }

    public Hero(ImplementedCardList.ImplementedCard implementedCard) {
        super(implementedCard);
    }

    /* no custom init; other class details follow */
}

public class Hunter extends Hero {
    public Hunter() {
        super();
    }

    public Hunter(ImplementedCardList.ImplementedCard implementedCard) {
        super(implementedCard);
    }

    /* no custom init; other class details follow */
}

这种情况持续了数百个课程。我想要做的是拉出除了使用相同参数调用super之外什么都不做的构造函数,但是当我这样做时,它会中断getConstructor中的deepCopy调用。

2 个答案:

答案 0 :(得分:1)

对于每个班级,你可以这样做:

Hero h = new Hero();
Class hc = h.getClass();

// Get super class and its constructor.
Class<?> sc = hc.getSuperclass();
Constructor scConst = sc.getConstructor(ImplementedCard.class);

// Get super class's parent and its constructor.
Class<?> ssc = sc.getSuperclass();
Constructor sscConst = ssc.getConstructor(ImplementedCard.class);

你也可以将它放在一个循环中,直到你到达Object.class或你想要破解的类层次结构中的其他一些点。

答案 1 :(得分:1)

正如@nhylated建议的那样,试试

this.getClass().getSuperClass()

Here是一个很好的解释为什么

super.getClass()

表现得像它一样。