JShell은 Java 9 버전에서 새롭게 도입된 도구로, REPL(Read-Evaluate-Print-Loop) 방식을 기반으로 합니다. 이를 통해 Java 코드를 작성하고 즉시 결과를 확인할 수 있습니다. JShell 세션에서 선언된 타입(class, interface, enum 등)의 목록을 확인하려면 "/types" 명령어를 사용하면 됩니다.
JShell의 /types 명령어 종류
JShell에서 사용할 수 있는 "/types" 명령어는 다음과 같은 형태로 제공됩니다.
/types /types [ID] /types [Type_Name] /types -start /types -all
- /types: JShell에서 생성된 모든 활성(active) 타입(class, interface, enum)의 목록을 표시합니다.
- /types [ID]: 지정한 식별자(ID)에 해당하는 타입 정보를 화면에 출력합니다.
- /types [Type_Name]: 지정한 이름(Type_Name)과 일치하는 타입 정보를 출력합니다.
- /types -start: JShell 시작 스크립트(startup script)에 추가된 타입들의 목록을 확인할 수 있습니다.
- /types -all: 현재 세션의 모든 타입(활성 상태, 비활성 상태, 그리고 JShell 시작 시 로드된 타입까지)을 한 번에 표시합니다.
/types 명령어 실습 예제
아래 코드 예제에서는 enum, class, interface 타입을 각각 생성한 뒤, 다양한 "/types" 명령어를 적용하여 그 결과를 확인할 수 있습니다.
jshell> enum Operation {
...> ADDITION,
...> DIVISION;
...> }
| created enum Operation
jshell> class Employee {
...> String empName;
...> int age;
...> public void empData() {
...> System.out.println("Employee Name is: " + empName);
...> System.out.println("Employee Age is: " + age);
...> }
...> }
| created class Employee
jshell> interface TestInterface {
...> public void sum();
...> }
| created interface TestInterface
jshell> /types
| enum Operation
| class Employee
| interface TestInterface
jshell> /types 1
| enum Operation
jshell> /types -start
jshell> /drop Operation
| dropped enum Operation
jshell> /types -all
| enum Operation
| class Employee
| interface TestInterface
실행 결과 분석
/types를 입력하면 현재 세션에서 생성된 세 가지 타입(enum Operation, class Employee, interface TestInterface)이 모두 출력됩니다./types 1처럼 ID를 지정하면 해당 ID에 매핑된 타입만 조회됩니다./types -start는 시작 스크립트에 별도로 정의된 타입이 없기 때문에 아무 결과도 출력하지 않습니다./drop명령으로 Operation enum을 삭제한 후에도/types -all을 실행하면 삭제된(비활성) 타입까지 포함되어 전체 목록이 표시되는 것을 확인할 수 있습니다.
이처럼 "/types" 명령어와 다양한 옵션을 활용하면 JShell 세션에서 선언된 타입들을 손쉽게 관리하고 추적할 수 있습니다.