Skip to content

Commit 6849903

Browse files
committed
Removing one unchecked cast warning type.
1 parent d14a9aa commit 6849903

5 files changed

Lines changed: 44 additions & 27 deletions

File tree

core/src/main/kotlin/org/evomaster/core/parser/GenePostgresSimilarToVisitor.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.evomaster.core.parser
22

3-
import org.evomaster.core.search.gene.Gene
43
import org.evomaster.core.search.gene.regex.*
54
import org.evomaster.core.utils.CharacterRange
65

@@ -10,7 +9,7 @@ import org.evomaster.core.utils.CharacterRange
109
class GenePostgresSimilarToVisitor : PostgresSimilarToBaseVisitor<VisitResult>() {
1110

1211
/*
13-
WARNING: lot of code here is similar/adapted from ECMA262 visitor.
12+
WARNING: lots of code here is similar/adapted from the ECMA262 visitor.
1413
But, as the parser objects are different, it does not seem simple to reuse
1514
the code without avoiding copy&paste&adapt :(
1615
*/
@@ -36,7 +35,7 @@ class GenePostgresSimilarToVisitor : PostgresSimilarToBaseVisitor<VisitResult>()
3635

3736
val altRes = ctx.alternative().accept(this)
3837

39-
val disj = DisjunctionRxGene("disj", altRes.genes.map { it as Gene }, true, true)
38+
val disj = DisjunctionRxGene("disj", altRes.genes.map { it }, true, true)
4039

4140
val res = VisitResult(disj)
4241

@@ -182,7 +181,7 @@ class GenePostgresSimilarToVisitor : PostgresSimilarToBaseVisitor<VisitResult>()
182181

183182
val negated = ctx.CARET() != null
184183

185-
val ranges = ctx.classRanges().accept(this).data as List<CharacterRange>
184+
val ranges = ctx.classRanges().accept(this).requireDataList<CharacterRange>()
186185

187186
val gene = CharacterRangeRxGene(negated, ranges)
188187

@@ -195,7 +194,7 @@ class GenePostgresSimilarToVisitor : PostgresSimilarToBaseVisitor<VisitResult>()
195194
val list = mutableListOf<CharacterRange>()
196195

197196
if(ctx.nonemptyClassRanges() != null){
198-
val ranges = ctx.nonemptyClassRanges().accept(this).data as List<CharacterRange>
197+
val ranges = ctx.nonemptyClassRanges().accept(this).requireDataList<CharacterRange>()
199198
list.addAll(ranges)
200199
}
201200

@@ -222,12 +221,12 @@ class GenePostgresSimilarToVisitor : PostgresSimilarToBaseVisitor<VisitResult>()
222221
list.add(CharacterRange(start, end))
223222

224223
if(ctx.nonemptyClassRangesNoDash() != null){
225-
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).data as List<CharacterRange>
224+
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).requireDataList<CharacterRange>()
226225
list.addAll(ranges)
227226
}
228227

229228
if(ctx.classRanges() != null){
230-
val ranges = ctx.classRanges().accept(this).data as List<CharacterRange>
229+
val ranges = ctx.classRanges().accept(this).requireDataList<CharacterRange>()
231230
list.addAll(ranges)
232231
}
233232

@@ -255,12 +254,12 @@ class GenePostgresSimilarToVisitor : PostgresSimilarToBaseVisitor<VisitResult>()
255254
}
256255

257256
if(ctx.nonemptyClassRangesNoDash() != null){
258-
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).data as List<CharacterRange>
257+
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).requireDataList<CharacterRange>()
259258
list.addAll(ranges)
260259
}
261260

262261
if(ctx.classRanges() != null){
263-
val ranges = ctx.classRanges().accept(this).data as List<CharacterRange>
262+
val ranges = ctx.classRanges().accept(this).requireDataList<CharacterRange>()
264263
list.addAll(ranges)
265264
}
266265

core/src/main/kotlin/org/evomaster/core/parser/GeneRegexEcma262Visitor.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class GeneRegexEcma262Visitor : RegexEcma262BaseVisitor<VisitResult>(){
211211

212212
val negated = ctx.CARET() != null
213213

214-
val ranges = ctx.classRanges().accept(this).data as List<CharacterRange>
214+
val ranges = ctx.classRanges().accept(this).requireDataList<CharacterRange>()
215215

216216
val gene = CharacterRangeRxGene(negated, ranges)
217217

@@ -224,7 +224,7 @@ class GeneRegexEcma262Visitor : RegexEcma262BaseVisitor<VisitResult>(){
224224
val list = mutableListOf<CharacterRange>()
225225

226226
if(ctx.nonemptyClassRanges() != null){
227-
val ranges = ctx.nonemptyClassRanges().accept(this).data as List<CharacterRange>
227+
val ranges = ctx.nonemptyClassRanges().accept(this).requireDataList<CharacterRange>()
228228
list.addAll(ranges)
229229
}
230230

@@ -239,7 +239,7 @@ class GeneRegexEcma262Visitor : RegexEcma262BaseVisitor<VisitResult>(){
239239

240240
if (ctx.classAtom()[0]?.classAtomNoDash()?.classEscape() != null){
241241
if (ctx.classAtom().size == 2) throw IllegalArgumentException("Not implemented yet")
242-
val rec = ctx.classAtom()[0].accept(this).data as List<CharacterRange>
242+
val rec = ctx.classAtom()[0].accept(this).requireDataList<CharacterRange>()
243243
list.addAll(rec)
244244
} else {
245245
val startText = ctx.classAtom()[0].text
@@ -269,12 +269,12 @@ class GeneRegexEcma262Visitor : RegexEcma262BaseVisitor<VisitResult>(){
269269
}
270270

271271
if(ctx.nonemptyClassRangesNoDash() != null){
272-
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).data as List<CharacterRange>
272+
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).requireDataList<CharacterRange>()
273273
list.addAll(ranges)
274274
}
275275

276276
if(ctx.classRanges() != null){
277-
val ranges = ctx.classRanges().accept(this).data as List<CharacterRange>
277+
val ranges = ctx.classRanges().accept(this).requireDataList<CharacterRange>()
278278
list.addAll(ranges)
279279
}
280280

@@ -298,7 +298,7 @@ class GeneRegexEcma262Visitor : RegexEcma262BaseVisitor<VisitResult>(){
298298
} else {
299299

300300
if (ctx.classAtom()?.classAtomNoDash()?.classEscape() != null || ctx.classAtomNoDash()?.classEscape() != null){
301-
val rec = (ctx.classAtom() ?: ctx.classAtomNoDash()).accept(this).data as List<CharacterRange>
301+
val rec = (ctx.classAtom() ?: ctx.classAtomNoDash()).accept(this).requireDataList<CharacterRange>()
302302
list.addAll(rec)
303303
} else {
304304
val char = (ctx.classAtom() ?: ctx.classAtomNoDash()).text[0]
@@ -307,12 +307,12 @@ class GeneRegexEcma262Visitor : RegexEcma262BaseVisitor<VisitResult>(){
307307
}
308308

309309
if(ctx.nonemptyClassRangesNoDash() != null){
310-
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).data as List<CharacterRange>
310+
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).requireDataList<CharacterRange>()
311311
list.addAll(ranges)
312312
}
313313

314314
if(ctx.classRanges() != null){
315-
val ranges = ctx.classRanges().accept(this).data as List<CharacterRange>
315+
val ranges = ctx.classRanges().accept(this).requireDataList<CharacterRange>()
316316
list.addAll(ranges)
317317
}
318318

core/src/main/kotlin/org/evomaster/core/parser/GeneRegexJavaVisitor.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
235235

236236
val negated = ctx.CARET() != null
237237

238-
val ranges = ctx.classRanges().accept(this).data as List<CharacterRange>
238+
val ranges = ctx.classRanges().accept(this).requireDataList<CharacterRange>();
239239

240240
val gene = CharacterRangeRxGene(negated, ranges)
241241

@@ -248,7 +248,7 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
248248
val list = mutableListOf<CharacterRange>()
249249

250250
if(ctx.nonemptyClassRanges() != null){
251-
val ranges = ctx.nonemptyClassRanges().accept(this).data as List<CharacterRange>
251+
val ranges = ctx.nonemptyClassRanges().accept(this).requireDataList<CharacterRange>()
252252
list.addAll(ranges)
253253
}
254254

@@ -263,7 +263,7 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
263263

264264
if (ctx.classAtom()[0]?.classAtomNoDash()?.classEscape() != null){
265265
if (ctx.classAtom().size == 2) throw IllegalArgumentException("Not implemented yet")
266-
val rec = ctx.classAtom()[0].accept(this).data as List<CharacterRange>
266+
val rec = ctx.classAtom()[0].accept(this).requireDataList<CharacterRange>()
267267
list.addAll(rec)
268268
} else {
269269
val startText = ctx.classAtom()[0].text
@@ -293,12 +293,12 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
293293
}
294294

295295
if(ctx.nonemptyClassRangesNoDash() != null){
296-
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).data as List<CharacterRange>
296+
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).requireDataList<CharacterRange>()
297297
list.addAll(ranges)
298298
}
299299

300300
if(ctx.classRanges() != null){
301-
val ranges = ctx.classRanges().accept(this).data as List<CharacterRange>
301+
val ranges = ctx.classRanges().accept(this).requireDataList<CharacterRange>()
302302
list.addAll(ranges)
303303
}
304304

@@ -322,7 +322,7 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
322322
} else {
323323

324324
if (ctx.classAtom()?.classAtomNoDash()?.classEscape() != null || ctx.classAtomNoDash()?.classEscape() != null){
325-
val rec = (ctx.classAtom() ?: ctx.classAtomNoDash()).accept(this).data as List<CharacterRange>
325+
val rec = (ctx.classAtom() ?: ctx.classAtomNoDash()).accept(this).requireDataList<CharacterRange>()
326326
list.addAll(rec)
327327
} else {
328328
val char = (ctx.classAtom() ?: ctx.classAtomNoDash()).text[0]
@@ -331,12 +331,12 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
331331
}
332332

333333
if(ctx.nonemptyClassRangesNoDash() != null){
334-
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).data as List<CharacterRange>
334+
val ranges = ctx.nonemptyClassRangesNoDash().accept(this).requireDataList<CharacterRange>()
335335
list.addAll(ranges)
336336
}
337337

338338
if(ctx.classRanges() != null){
339-
val ranges = ctx.classRanges().accept(this).data as List<CharacterRange>
339+
val ranges = ctx.classRanges().accept(this).requireDataList<CharacterRange>()
340340
list.addAll(ranges)
341341
}
342342

core/src/main/kotlin/org/evomaster/core/parser/VisitResult.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,26 @@ class VisitResult(
99
val genes: MutableList<Gene> = mutableListOf(),
1010
var data: Any? = null
1111
){
12-
1312
constructor(gene: Gene) : this() {
1413
genes.add(gene)
1514
}
15+
16+
inline fun <reified T> requireDataList(): List<T> {
17+
val values = data
18+
?: throw IllegalStateException("Expected List<${T::class.simpleName}> in VisitResult, but it was null")
19+
20+
if (values !is List<*>) {
21+
throw IllegalStateException(
22+
"Expected List<${T::class.simpleName}> in VisitResult, but got ${values::class.qualifiedName}"
23+
)
24+
}
25+
26+
@Suppress("UNCHECKED_CAST")
27+
return values.mapIndexed { index, value ->
28+
value as? T
29+
?: throw IllegalStateException(
30+
"Expected ${T::class.simpleName} entry at VisitResult[$index], but got " +
31+
(value?.let { it::class.qualifiedName } ?: "null"))
32+
} as List<T>
33+
}
1634
}

core/src/main/kotlin/org/evomaster/core/utils/CharacterRange.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.evomaster.core.utils
22

3-
class CharacterRange constructor(val start: Char, val end: Char){
3+
class CharacterRange(val start: Char, val end: Char){
44
constructor(a: Int, b: Int) : this(a.toChar(), b.toChar())
55

66
init {

0 commit comments

Comments
 (0)