0

I have a class which spawn instances of itself, which adapts to its new extended class (if extended)

/// Class name is obviously for fun =)
public class HiveMind {

    /// It does have a public proper constructor
    public HiveMind() {
        doSomething();
    }

    /// With great ambition
    public void doSomething() {
        // Should we Take over the world?
    }
    ...

    /// Spawn and instance of the current class
    public final HiveMind spawnInstance() throws ServletException {
        try {
            Class<? extends HiveMind> instanceClass = this.getClass();
            HiveMind ret = instanceClass.newInstance();
            // Does some stuff here
            // And for those who says "newInstance" is evil
            // This is an evil HiveMind then D=
            return ret;
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

    ...
}

This works fine and all.... until someone tries to use it as an anonymous class.

HiveMind nextGeneration = new HiveMind() {
    // New generation new mind set.
    public void doSomething() {
        this.enslaveHumans(); // Yes we should
    }
};

// Time to spread the hive mind!
nextGeneration.spawnInstance();

Unfortunately (or fortunately?) this fails with the following.

java.lang.NoSuchMethodException: HiveMind_test$1.&lt;init&gt;()
    java.lang.Class.getConstructor0(Class.java:3082)
    java.lang.Class.newInstance(Class.java:412)
    HiveMind.spawnInstance(HiveMind.java:383)
    ....

So how does one spawn a new instance of an anonymous class in Java?

For clarification purposes, there is no reuse of variables outside of the Anonymous class, to avoid state capture.

PS: Dynamic construction of anonymous class confusion, does not provide the answer i need. It does explain lots of insight on why this does not work currently however.

Community
  • 1
  • 1
PicoCreator
  • 9,886
  • 7
  • 43
  • 64
  • An anonymous class is an *inner* class. You cannot instantiate it without associating the new instance with an instance of its outer class. – John Bollinger Jan 07 '17 at 13:34
  • Note that your approach will also fail if your class is extended by an ordinary, *named* subclass that does not have a nullary constructor. In fact, from the stack trace, it looks like that's effectively the same case as yours. – John Bollinger Jan 07 '17 at 13:37
  • @JohnBollinger : Oops ur right on the stack trace, i copied in the wrong one that happened after i was tweaking the code around. Amended. Is there no work around the inner class issue? Even if it means getting the new sub class, to add certain attributes / etc. – PicoCreator Jan 07 '17 at 13:39
  • Please don't use `newInstance` – torkleyy Jan 07 '17 at 13:39
  • @torkleyy : This is not about why "newInstance" is evil (i know) >_> its an evil hive mind ! – PicoCreator Jan 07 '17 at 13:50
  • Your example should even work. I think you simplified your code too much. You're probably using a local variable in your anonymous class. – torkleyy Jan 07 '17 at 13:56

0 Answers0