-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowcase.bsh
More file actions
69 lines (59 loc) · 2.6 KB
/
Copy pathshowcase.bsh
File metadata and controls
69 lines (59 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* BeanShell plugin showcase — open this file in the sandbox IDE and try:
*
* • Syntax highlighting, code folding (fold the imports / this comment / blocks)
* • Structure view (Alt+7) and breadcrumbs (enclosing method/class)
* • Ctrl+Click:
* - a local method/variable -> jumps within this file
* - a Java type (ArrayList) -> jumps into java.util.ArrayList
* - a chained member -> sb.append(..).append(..), list.iterator().next()
* • Rename (Shift+F6) a variable/method/parameter
* • Completion (Ctrl+Space) for keywords and in-scope names
* • Parameter info (Ctrl+P) inside a call's parentheses
* • Debug (right-click -> Debug 'showcase.bsh'):
* - breakpoints on .bsh lines, Step Over/Into/Out, Variables panel
* - breakpoints in Java code you call also work (Java debug session)
*/
// Fold this import group (click the gutter arrow) and the doc comment above.
import java.util.ArrayList;
import java.util.List;
// --- A recursive method: good for breakpoints + Step Into ------------------
int factorial(int n) {
print("factorial(" + n + ")"); // trace output — shows up in the debug Console
if (n <= 1) {
return 1;
}
int sub = factorial(n - 1); // recurse first...
int result = n * sub; // ...then combine
return result; // breakpoint here: inspect n, sub, result
}
// --- A loosely typed BeanShell class ---------------------------------------
class Greeter {
String name;
Greeter(String name) {
this.name = name;
}
String greet() {
return "Hello, " + name + "!";
}
}
// --- Java type + fluent chain: Ctrl+Click ArrayList / add / iterator / next -
List numbers = new ArrayList(); // untyped var; type inferred from `new ArrayList()`
for (int i = 1; i <= 6; i++) {
numbers.add(factorial(i)); // Ctrl+Click `add` -> java.util.List#add
}
first = numbers.get(0); // Ctrl+Click `get` -> java.util.List#get
// Typed variable + StringBuilder chain — Ctrl+Click each `append` walks the
// chain into Java (append() returns StringBuilder, so the type propagates):
StringBuilder report = new StringBuilder();
report.append("factorials: ").append(numbers).append(" first=").append(first);
// --- Control flow, operators, ternary --------------------------------------
total = 0;
for (int i = 0; i < numbers.size(); i++) {
total += numbers.get(i);
}
parity = (total % 2 == 0) ? "even" : "odd";
Greeter greeter = new Greeter("BeanShell");
print(greeter.greet());
print(report.toString());
print("sum = " + total + " (" + parity + ")");