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

name.remal.gradle_plugins.merge_resources.mergers.BytecodeAnnotation Maven / Gradle / Ivy

The newest version!
package name.remal.gradle_plugins.merge_resources.mergers;

import static java.util.stream.Collectors.joining;
import static name.remal.gradle_plugins.toolkit.StringUtils.escapeJava;
import static org.objectweb.asm.Type.getType;

import java.util.List;
import java.util.Map;
import lombok.NonNull;
import lombok.Value;
import org.objectweb.asm.Type;

@Value
class BytecodeAnnotation {

    @NonNull
    String desc;

    @NonNull
    Map values;

    @Override
    public String toString() {
        return '@'
            + getType(desc).getClassName()
            + values.entrySet().stream()
            .map(entry -> entry.getKey() + " = " + valueToString(entry.getValue()))
            .collect(joining(", ", "(", ")"));
    }

    private static String valueToString(Object value) {
        if (value instanceof List) {
            return ((List) value).stream()
                .map(BytecodeAnnotation::valueToString)
                .collect(joining(", ", "{", "}"));

        } else if (value instanceof String) {
            return '"' + escapeJava("" + value) + '"';

        } else if (value instanceof Character) {
            return '\'' + escapeJava("" + value) + '\'';

        } else if (value instanceof Type) {
            return ((Type) value).getClassName();

        } else {
            return value.toString();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy