All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.gradle.plugins.packaging.AliasHelper.groovy Maven / Gradle / Ivy

package com.netflix.gradle.plugins.packaging

import groovy.transform.CompileDynamic
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging

import java.lang.reflect.Field
import java.lang.reflect.Modifier

@CompileDynamic
class AliasHelper {
    private static Logger logger = Logging.getLogger(AliasHelper);

    static > void aliasEnumValues(T[] values, dynAware) {
        for (T value : values) {
            assert !dynAware.hasProperty(value.name())
            logger.debug("Setting ${value.name()} onto ${dynAware}")
            dynAware.metaClass."${value.name()}" = value
        }
    }

    static  void aliasStaticInstances(Class forClass, dynAware) {
        aliasStaticInstances(forClass, forClass, dynAware)
    }

    static boolean hasModifier(Field field, int modifier) {
        (field.modifiers & modifier) == modifier
    }

    static  void aliasStaticInstances(Class forClass, Class ofClass, dynAware) {
        for (Field field : forClass.fields) {
            if (field.type == ofClass && hasModifier(field, Modifier.STATIC)) {
                assert !dynAware.hasProperty(field.name)
                logger.debug("Setting ${field.name} to ${field.get(null)} onto ${dynAware}")
                dynAware.metaClass."${field.name}" = field.get(null)
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy