-
Notifications
You must be signed in to change notification settings - Fork 411
/
Lookup.kt
41 lines (35 loc) · 1.32 KB
/
Lookup.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the license, or (at your option) any later version.
*/
package org.gjt.jclasslib.structures
/**
* Base class for companion objects that facilitates fast bytecode to enum instance lookups.
* @property enumClass The class of the enum
* @property name A name for the enum for use in error messages
*/
abstract class Lookup<T>(val enumClass: Class<T>, val name: String) where T : Enum<T>, T : ClassFileEnum {
private val lookup = arrayOfNulls<Any?>(enumClass.enumConstants.maxBy { it.tag }!!.tag + 1)
init {
for (constant in enumClass.enumConstants) {
lookup[constant.tag] = constant
}
}
/**
* Get the enum instance for the specified bytecode tag.
* @throws InvalidByteCodeException if the bytecode tag is not found
*/
@Throws(InvalidByteCodeException::class)
fun getFromTag(tag: Int): T {
if (tag < lookup.size && tag >= 0) {
val constant = lookup[tag]
if (constant != null) {
@Suppress("UNCHECKED_CAST")
return constant as T
}
}
throw InvalidByteCodeException("Invalid $name: $tag")
}
}