JShell API questions / feedback

Roy van Rijn roy.van.rijn at gmail.com
Mon Feb 1 16:01:04 UTC 2016


Some more information about what I've been trying to do with the JShell API.

> I've been working on a project to incorporate the JShell API into an
> environment to help teach the Java language to absolute beginners
> (mainly kids). For this I'm using the JShell API and making my own editor. The eval()
> results have to be parsed and evaluated by my code. But I've ran into
> a few issues (or maybe just questions).

The reason for me to have more information in the resulting Snippets
is the following. First I'd love to teach people more about the code
they've just written. My goal is to make an application (in JavaFX)
with small programming exercises, much like the language-teaching
website Duolingo, but to teach Java programming instead.

For example I ask the user to type a number between 1 and 100 and
press 'Evaluate!'
The user now types: 42
The program receives a SnippetEvent with Snippet and this gets
evaluated by my application. In this case I'm looking for a VarSnippet
of type int etc.

Another question would be: Create a method that returns your name (as a String)
The result should be a Snippet that declares a method, with return
type String (etc).

Also I want to have a more explanatory description of what happens if
you evaluate some code, for absolute beginners. For example:
Evaluate: "int age = 33;" the program responds with:
"You've created a variable of type 'int' and named it 'age' and the
current value is: 33"

For this I'd like to have access to more meta info, for example the
return type (so I don't have to parse this). Also hence the question
about having access to the created objects. Currently the
implementation is that SnippetEvent.value() returns the toString, so
for example if you evaluate:

class MyClass { }
new MyClass();

The result is:
"You've created a nameless variable of type 'MyClass', I've named it
'odin' and the current value is: MyClass at 445b84c0"

In my application, I'd like to give challenges like "write a method
that returns your current age". When I eval() the created code I don't
have many options, I'm unable to access the created code using the
JShell API.

Currently I'm stuck with:
Input: "int myAge() { return 12; }"
Output: "You've created the method 'myAge' without any arguments and
returns 'int'"

The only way I can currently 'check' if the inputted code works is to
execute it myself in an instance of the shell behind the scenes:
shell.eval(methodName+"("+optionalArguments+");");

Now I get the result as VarSnippet, I can parse it to an int safely
because I know the method returns int... and check the value. But that
is rather ugly and error-prone of course. Another way would be to
parse the given code itself (kind of beating the reason I'm using the
JShell API).

Having a proxy object surrounding the created objects and methods
would certainly be useful for me in this case. Not sure about other
use cases but I'm pretty sure they exist as well.

Regarding the API's get-less method names: understood, it just seemed
strange to me initially. The resulting Snippet instance 'feels' like a
data object that has fields with information. For those types of
objects I'm used to just type 'get' and let my IDE give me suggestions
on the fields. But this obviously doesn't work when all the fields
have get-less method names. But indeed, String has length() etc. There
are also counter examples of course String.getBytes(), it has a getter
with no corresponding setter. It would be nice if there was no longer
"two schools of thought in the JDK world", but I have the sneaking
suspicion this is neither the place nor the time for that discussion.

Roy


More information about the kulla-dev mailing list