Search code examples
javasun-codemodel

adding method for existing with CodeModel


I am trying to add abstract methods to my interfaces with CodeModel. Is this possible?

JCodeModel cm = new JCodeModel();
JClass ref = cm.ref(Sample.class);

After having a reference to actual class, I do not know to how I should add a method? Actually I want to use as a JDefinedClass.


Solution

  • CodeModel is not used to modify exiting classes. However you can define a new class as abstract:

    To use abstract methods you have to make a class abstract:

    JCodeModel cm = new JCodeModel();
    JDefinedClass ref = cm._class(JMod.ABSTRACT | JMod.PUBLIC, "AbstractSampe", ClassType.CLASS);
    

    Then to make methods abstract its just a matter of declaring them as such:

    ref.method(JMod.ABSTRACT | JMod.PUBLIC, codeModel.VOID, "abstractMethod");