A Swing-based interactive console for evaluating Groovy expressions and scripts with syntax highlighting, code completion, and an integrated AST browser.
—
The AST Browser provides powerful Abstract Syntax Tree visualization and analysis tools for understanding Groovy code structure, compilation phases, and bytecode generation.
class AstBrowser {
// Constructor
AstBrowser(inputArea, rootElement, classLoader, config = null)
// Core Operations
void refresh()
static void main(String[] args)
}Constructor Parameters:
inputArea: Input text area containing the source code to analyzerootElement: Root element for the AST tree displayclassLoader: ClassLoader for compilation and class loadingconfig (CompilerConfiguration, optional): Compiler configuration, defaults to nullmain() Parameters:
args (String[]): Command line arguments for standalone AST browserenum CompilePhaseAdapter {
INITIALIZATION,
PARSING,
CONVERSION,
SEMANTIC_ANALYSIS,
CANONICALIZATION,
INSTRUCTION_SELECTION,
CLASS_GENERATION,
OUTPUT,
FINALIZATION
}The CompilePhaseAdapter enum represents the different phases of Groovy compilation that can be analyzed in the AST browser.
class TreeNodeWithProperties extends DefaultMutableTreeNode {
List<List<String>> properties
// Constructor
TreeNodeWithProperties(Object userObject)
TreeNodeWithProperties(Object userObject, List<List<String>> properties)
// Property Management
List<List<String>> getProperties()
void setProperties(List<List<String>> properties)
}
interface AstBrowserNodeMaker<T> {
// Interface for creating different types of tree nodes
}
class SwingTreeNodeMaker implements AstBrowserNodeMaker<DefaultMutableTreeNode> {
// Creates Swing tree nodes for AST display
}class AstNodeToScriptAdapter {
static void main(String[] args)
// Converts AST nodes back to script text
}
class AstNodeToScriptVisitor implements GroovyCodeVisitor, GroovyClassVisitor {
// Visitor pattern implementation for AST traversal and conversion
}class ScriptToTreeNodeAdapter {
// Converts Groovy scripts to tree node representations
}
class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation {
// Operation for building tree nodes during compilation
}
class TreeNodeBuildingVisitor extends CodeVisitorSupport {
// Visitor for building tree structures from AST
}class BytecodeCollector extends ClassCollector {
// Collects bytecode information during compilation
}
class GeneratedBytecodeAwareGroovyClassLoader extends GroovyClassLoader {
// ClassLoader that tracks generated bytecode for analysis
}class AstBrowserUiPreferences {
// UI configuration and preferences for AST browser
}// Launch standalone AST browser
groovy.console.ui.AstBrowser.main([] as String[])
// Or from legacy package
groovy.inspect.swingui.AstBrowser.main([] as String[])def sourceCode = '''
class Example {
def method() {
println "Hello World"
}
}
'''
def inputArea = new JTextArea(sourceCode)
def rootElement = new DefaultMutableTreeNode("Root")
def classLoader = new GroovyClassLoader()
def astBrowser = new groovy.console.ui.AstBrowser(inputArea, rootElement, classLoader)
astBrowser.refresh()// Convert AST nodes back to script text
groovy.console.ui.AstNodeToScriptAdapter.main([
"--source", "MyScript.groovy"
] as String[])The AST browser integrates with the main Console application:
From the Console, access AST browser via:
The AST browser provides:
For non-GUI environments, text-based AST analysis is available:
class TextNode {
String text
List<TextNode> children
// Constructors
TextNode(String text)
TextNode(String text, List<TextNode> children)
// Tree Navigation
String getText()
void setText(String text)
List<TextNode> getChildren()
void setChildren(List<TextNode> children)
void addChild(TextNode child)
// String Representation
String toString()
}
class TextTreeNodeMaker implements AstBrowserNodeMaker<TextNode> {
// Creates text-based tree nodes for console output
TextNode makeNode(Object userObject)
TextNode makeNodeWithProperties(Object userObject, List<List<String>> properties)
}The TextNode and TextTreeNodeMaker classes provide text-based AST representation for command-line environments or when GUI components are not available. This enables AST analysis in headless environments or for programmatic processing of syntax trees.
The same AST browser functionality is available in the legacy packages:
// Legacy imports (same API)
import groovy.inspect.swingui.AstBrowser
import groovy.inspect.swingui.AstNodeToScriptAdapter
import groovy.inspect.swingui.ScriptToTreeNodeAdapterView AST at different compilation phases:
The AST browser can display:
Navigate the AST with:
Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-console