Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 69f9cff

Browse files
authored
Merge pull request #210 from github/update-java-interfaces
Update Java tssg for enum switches, interfaces, class extension
2 parents 879b026 + 7261ec7 commit 69f9cff

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

languages/tree-sitter-stack-graphs-java/src/stack-graphs.tsg

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n
127127

128128
(class_declaration
129129
superclass: (superclass
130-
(type_identifier) @superclass_name)
130+
(_) @superclass_name)
131131
body: (class_body) @class_body) @class {
132132

133133
node ref
@@ -152,7 +152,6 @@ attribute node_symbol = node => symbol = (source-text node), source_n
152152
edge @class.type -> ref
153153
}
154154

155-
156155
(class_declaration (type_parameters)) @class {
157156
node @class.type_parameters
158157
edge @class.lexical_scope -> @class.type_parameters
@@ -359,6 +358,16 @@ attribute node_symbol = node => symbol = (source-text node), source_n
359358
edge @type.lexical_scope -> @this.lexical_scope
360359
}
361360

361+
(interface_declaration
362+
type_parameters: (type_parameters
363+
(type_parameter
364+
(type_identifier) @type_identifier))
365+
body: (_) @body) @this {
366+
node type_ident
367+
attr (type_ident) node_definition = @type_identifier
368+
edge @body.lexical_scope -> type_ident
369+
}
370+
362371
(interface_body) @this {
363372
node @this.defs
364373
node @this.lexical_scope
@@ -605,6 +614,41 @@ attribute node_symbol = node => symbol = (source-text node), source_n
605614
edge @stmt.after_scope -> @stmt.before_scope
606615
}
607616

617+
(method_declaration
618+
parameters:
619+
(formal_parameters
620+
(formal_parameter
621+
type: (generic_type
622+
(type_arguments
623+
(type_identifier) @type))))
624+
body:
625+
(block
626+
(switch_expression
627+
condition: (_)
628+
body: (switch_block
629+
(switch_block_statement_group
630+
(switch_label
631+
(identifier))
632+
@label))
633+
)) @stmt) {
634+
635+
node ident
636+
attr (ident) node_reference = @type
637+
638+
node ref__typeof
639+
attr(ref__typeof) push_symbol = "."
640+
node implicit_this__typeof
641+
attr(implicit_this__typeof) push_symbol = ":"
642+
643+
edge implicit_this__typeof -> @label.lexical_scope
644+
edge @label.lexical_scope -> ref__typeof
645+
edge ref__typeof -> implicit_this__typeof
646+
edge implicit_this__typeof -> ident
647+
648+
edge ident -> @stmt.after_scope
649+
}
650+
651+
608652
(switch_block) @this {
609653
node @this.lexical_scope
610654
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
class TestClass extends SecondTestClass {
2-
// ^ defined: 5
1+
class String {
2+
public int length() {}
3+
};
4+
5+
class HashMap<K, V> {
6+
public Set<MapEntry<K, V>> entrySet() {}
7+
// ^ defined: 5
38
}
49

5-
class SecondTestClass {
10+
class LRUCache<K, V>
11+
extends HashMap<K, V> {
12+
// ^ defined: 5
13+
// ^ defined: 10
14+
// ^ defined: 10
615
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
enum Level {
2+
LOW,
3+
MEDIUM,
4+
HIGH
5+
}
6+
7+
class Supplier<T> {
8+
T get() {}
9+
}
10+
11+
class App {
12+
boolean isSufficient(Supplier<Level> level_supplier) {
13+
switch (level_supplier.get()) {
14+
case LOW: return false;
15+
// ^ defined: 2
16+
case MEDIUM: return false;
17+
// ^ defined: 3
18+
case Level.HIGH: return true;
19+
// ^ defined: 1
20+
// ^ defined: 4
21+
}
22+
}
23+
}

languages/tree-sitter-stack-graphs-java/test/decl/interface.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@ class A implements I {}
55
// ^ defined: 1
66
interface J extends I {}
77
// ^ defined: 1
8+
9+
interface Iterator<E> {
10+
public E next() {}
11+
// ^ defined: 9
12+
}
13+
14+
interface Set<E> {
15+
public Iterator<E> iterator() {}
16+
}

0 commit comments

Comments
 (0)