Just iterate over all the static fields that are looking for the one that has the desired value. As in the following groovy script example
import static java.lang.reflect.Modifier.isStatic
class Autoresponder {
static String TYPE_NONE = "none"
static String TYPE_GETRESPONSE = "GetResponse"
static String TYPE_MAILCHIMP = "MailChimp"
static String TYPE_AWEBER = "AWeber"
static String TYPE_INFUSIONSOFT = "InfusionSoft"
static String TYPE_ICONTACT = "iContact"
static String TYPE_SENDY = "Sendy"
static String TYPE_ACTIVECAMPAIGN = "ActiveCampaign"
static String TYPE_API_ACTIVATE = "activate"
static String TYPE_ONTRAPORT = "ontraport"
}
def getStaticAttributeWithValue(Class clazz, Object searchedValue) {
clazz.declaredFields
.findAll{ isStatic(it.modifiers) }
.find { clazz[it.name] == searchedValue }
}
assert getStaticAttributeWithValue(Autoresponder, "AWeber") != null
assert getStaticAttributeWithValue(Autoresponder, "NonExist") == null
null, , . ( java.lang.reflect.Field)
, groovy MetaClass,
def getStaticAttributeWithValue(Class clazz, Object searchedValue) {
clazz.metaClass.properties
.findAll{ it.getter.static }
.find { clazz[it.name] == searchedValue }
}
groovy.lang.MetaBeanProperty