Skip to content

Commit 4ec7535

Browse files
Java: Improved generics (#2812)
1 parent 5bc405e commit 4ec7535

File tree

3 files changed

+168
-36
lines changed

3 files changed

+168
-36
lines changed

components/prism-java.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
alias: 'punctuation'
6363
},
6464
'generics': {
65-
pattern: /<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<(?:[\w\s,.&?]|<[\w\s,.&?]*>)*>)*>)*>/,
65+
pattern: /<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&))*>)*>)*>)*>/,
6666
inside: {
6767
'class-name': className,
6868
'keyword': keywords,

components/prism-java.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/java/generics_feature.test

+166-34
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@ public class Solo<T extends com.foo.Foo.Bar> {}
22
Solo<Integer> val = new Solo<>();
33
Duo<Double, Character> dual = new Duo<Double, Character>(12.2585, 'C');
44

5+
List<?> list
6+
List<? extends Number> nums = ints;
7+
List<? super Integer> list
8+
Entry<String, String> pair = Entry.<String>twice("Hello");
9+
10+
public class Entry<KeyType, ValueType> {}
11+
class D <T extends A & B & C> {}
12+
13+
public <T extends Throwable> void throwMeConditional(boolean conditional, T exception) throws T {}
14+
15+
<T> T instantiateElementType(List<T> arg) {}
16+
17+
// not generics
18+
if (a<6&&b>6){}
19+
520
----------------------------------------------------
621

722
[
823
["keyword", "public"],
924
["keyword", "class"],
10-
["class-name", [
11-
"Solo"
12-
]],
25+
["class-name", ["Solo"]],
1326
["generics", [
1427
["punctuation", "<"],
15-
["class-name", [
16-
"T"
17-
]],
28+
["class-name", ["T"]],
1829
["keyword", "extends"],
1930
["class-name", [
2031
["namespace", [
@@ -32,22 +43,16 @@ Duo<Double, Character> dual = new Duo<Double, Character>(12.2585, 'C');
3243
["punctuation", "{"],
3344
["punctuation", "}"],
3445

35-
["class-name", [
36-
"Solo"
37-
]],
46+
["class-name", ["Solo"]],
3847
["generics", [
3948
["punctuation", "<"],
40-
["class-name", [
41-
"Integer"
42-
]],
49+
["class-name", ["Integer"]],
4350
["punctuation", ">"]
4451
]],
4552
" val ",
4653
["operator", "="],
4754
["keyword", "new"],
48-
["class-name", [
49-
"Solo"
50-
]],
55+
["class-name", ["Solo"]],
5156
["generics", [
5257
["punctuation", "<"],
5358
["punctuation", ">"]
@@ -56,43 +61,170 @@ Duo<Double, Character> dual = new Duo<Double, Character>(12.2585, 'C');
5661
["punctuation", ")"],
5762
["punctuation", ";"],
5863

59-
["class-name", [
60-
"Duo"
61-
]],
64+
["class-name", ["Duo"]],
6265
["generics", [
6366
["punctuation", "<"],
64-
["class-name", [
65-
"Double"
66-
]],
67+
["class-name", ["Double"]],
6768
["punctuation", ","],
68-
["class-name", [
69-
"Character"
70-
]],
69+
["class-name", ["Character"]],
7170
["punctuation", ">"]
7271
]],
7372
" dual ",
7473
["operator", "="],
7574
["keyword", "new"],
76-
["class-name", [
77-
"Duo"
78-
]],
75+
["class-name", ["Duo"]],
7976
["generics", [
8077
["punctuation", "<"],
81-
["class-name", [
82-
"Double"
83-
]],
78+
["class-name", ["Double"]],
8479
["punctuation", ","],
85-
["class-name", [
86-
"Character"
87-
]],
80+
["class-name", ["Character"]],
8881
["punctuation", ">"]
8982
]],
9083
["punctuation", "("],
9184
["number", "12.2585"],
9285
["punctuation", ","],
9386
["string", "'C'"],
9487
["punctuation", ")"],
95-
["punctuation", ";"]
88+
["punctuation", ";"],
89+
90+
["class-name", ["List"]],
91+
["generics", [
92+
["punctuation", "<"],
93+
["operator", "?"],
94+
["punctuation", ">"]
95+
]],
96+
" list\r\n",
97+
98+
["class-name", ["List"]],
99+
["generics", [
100+
["punctuation", "<"],
101+
["operator", "?"],
102+
["keyword", "extends"],
103+
["class-name", ["Number"]],
104+
["punctuation", ">"]
105+
]],
106+
" nums ",
107+
["operator", "="],
108+
" ints",
109+
["punctuation", ";"],
110+
111+
["class-name", ["List"]],
112+
["generics", [
113+
["punctuation", "<"],
114+
["operator", "?"],
115+
["keyword", "super"],
116+
["class-name", ["Integer"]],
117+
["punctuation", ">"]
118+
]],
119+
" list\r\n",
120+
121+
["class-name", ["Entry"]],
122+
["generics", [
123+
["punctuation", "<"],
124+
["class-name", ["String"]],
125+
["punctuation", ","],
126+
["class-name", ["String"]],
127+
["punctuation", ">"]
128+
]],
129+
" pair ",
130+
["operator", "="],
131+
["class-name", ["Entry"]],
132+
["punctuation", "."],
133+
["generics", [
134+
["punctuation", "<"],
135+
["class-name", ["String"]],
136+
["punctuation", ">"]
137+
]],
138+
["function", "twice"],
139+
["punctuation", "("],
140+
["string", "\"Hello\""],
141+
["punctuation", ")"],
142+
["punctuation", ";"],
143+
144+
["keyword", "public"],
145+
["keyword", "class"],
146+
["class-name", ["Entry"]],
147+
["generics", [
148+
["punctuation", "<"],
149+
["class-name", ["KeyType"]],
150+
["punctuation", ","],
151+
["class-name", ["ValueType"]],
152+
["punctuation", ">"]
153+
]],
154+
["punctuation", "{"],
155+
["punctuation", "}"],
156+
157+
["keyword", "class"],
158+
["class-name", ["D"]],
159+
["generics", [
160+
["punctuation", "<"],
161+
["class-name", ["T"]],
162+
["keyword", "extends"],
163+
["class-name", ["A"]],
164+
["operator", "&"],
165+
["class-name", ["B"]],
166+
["operator", "&"],
167+
["class-name", ["C"]],
168+
["punctuation", ">"]
169+
]],
170+
["punctuation", "{"],
171+
["punctuation", "}"],
172+
173+
["keyword", "public"],
174+
["generics", [
175+
["punctuation", "<"],
176+
["class-name", ["T"]],
177+
["keyword", "extends"],
178+
["class-name", ["Throwable"]],
179+
["punctuation", ">"]
180+
]],
181+
["keyword", "void"],
182+
["function", "throwMeConditional"],
183+
["punctuation", "("],
184+
["keyword", "boolean"],
185+
" conditional",
186+
["punctuation", ","],
187+
["class-name", ["T"]],
188+
" exception",
189+
["punctuation", ")"],
190+
["keyword", "throws"],
191+
["class-name", ["T"]],
192+
["punctuation", "{"],
193+
["punctuation", "}"],
194+
195+
["generics", [
196+
["punctuation", "<"],
197+
["class-name", ["T"]],
198+
["punctuation", ">"]
199+
]],
200+
["class-name", ["T"]],
201+
["function", "instantiateElementType"],
202+
["punctuation", "("],
203+
["class-name", ["List"]],
204+
["generics", [
205+
["punctuation", "<"],
206+
["class-name", ["T"]],
207+
["punctuation", ">"]
208+
]],
209+
" arg",
210+
["punctuation", ")"],
211+
["punctuation", "{"],
212+
["punctuation", "}"],
213+
214+
["comment", "// not generics"],
215+
216+
["keyword", "if"],
217+
["punctuation", "("],
218+
"a",
219+
["operator", "<"],
220+
["number", "6"],
221+
["operator", "&&"],
222+
"b",
223+
["operator", ">"],
224+
["number", "6"],
225+
["punctuation", ")"],
226+
["punctuation", "{"],
227+
["punctuation", "}"]
96228
]
97229

98230
----------------------------------------------------

0 commit comments

Comments
 (0)