hpopiolkiewicz
hpopiolkiewicz

Reputation: 3538

JShell - How to add method to existing class without replacing this class?

tl;dr : How to add method to existing class without replacing this class?

Description:

When I run following command in JShell:

public class TestClass {}

following output is printed:

created class TestClass

Running this command once more gives following output:

modified class TestClass

Let's create class with one method, as below:

public class TestClass {
    public static void testMethod1() {
        System.out.println("In testMethod1");
    }      
}

It's worth to mention that the output is slightly different from previous class overwriting:

replaced class TestClass

Running testMethod1 ends successfully and In testMethod1 is printed in console.

Now I want to add new method to existing TestClass without loosing testMethod1. So I run following snippet:

public class TestClass {
    public static void testMethod2() {
        System.out.println("In testMethod2");
    }      
}

...and testMethod1 is lost, because whole TestClass has been replaced.

How can I add new method to existing class without overwriting it? What if I have written like 10 methods? Am I supposed to write down existing methods next to new method that I want to add to class? Shouldn't JShell prompt user about class to be replaced in form of warning?

Any hints or help is appreciated.

Upvotes: 3

Views: 1272

Answers (2)

Naman
Naman

Reputation: 31888

How can I add a new method to existing class without overwriting it?

You can perform this using the

/edit

which opens up a Jshell edit pad, where you can add your new method and Accept to update the class definition in your case. The jshell commands also illustrates several other attributes to edit multiple snippets using their name or id. Adding a screenshot for how to do this:

Jshell Edit Pad

What if I have written like 10 methods? Am I supposed to write down existing methods next to new method that I want to add to class?

Such a use case seems more looking out for an IDE rather than the REPL based jshell and is clearly mentioned as a Non Goal on the JEP#222 jshell: The Java Shell (Read-Eval-Print Loop)

Out of scope are graphical interfaces and debugger support. The JShell API is intended to allow JShell functionality in IDEs and other tools, but the jshell tool is not intended to be an IDE.


Shouldn't JShell prompt user about class to be replaced in form of warning?

Would second @Elliot as mentioned in the comments, Apparently it doesn't for now. Though this sounds an interesting ask IMHO but would have to be equally weighed against the thoughts of implementing it, along with not bring down the performance of the tool. The answer by @Rafael provides a hint about maintaining such overhead.

Upvotes: 2

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

JShell cannot do this and I do not think it would be what most people want. If you want to regularly merge into existing code, you probably want to use an editor that keeps the source code intact and compile your source from scratch. JShell would need to keep track of all kinds of source code information in addition to the compiled byte code to allow for what you want. For example, what would happen if you added a method that already was implemented as a bridge method as result of defining a generic method?

The reason the message differs with replaced or modified is because of JShells use of HotSwap which can only be applied when a class does not change its shape, i.e. declares the same methods and fields with the same signatures.

Upvotes: 3

Related Questions