Home / Class/ ChannelHandlerMetadataUtil Class — netty Architecture

ChannelHandlerMetadataUtil Class — netty Architecture

Architecture documentation for the ChannelHandlerMetadataUtil class in ChannelHandlerMetadataUtil.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  746b9c8e_354b_ecaf_85dc_a1e83631fad5["ChannelHandlerMetadataUtil"]
  d030fca3_bb76_03ba_ccb8_1f5000eb521f["ChannelHandlerMetadataUtil.java"]
  746b9c8e_354b_ecaf_85dc_a1e83631fad5 -->|defined in| d030fca3_bb76_03ba_ccb8_1f5000eb521f
  575908e1_9402_5003_ace6_9fef902499ea["ChannelHandlerMetadataUtil()"]
  746b9c8e_354b_ecaf_85dc_a1e83631fad5 -->|method| 575908e1_9402_5003_ace6_9fef902499ea
  551c0cf8_9074_2186_24dc_6d297424d67b["generateMetadata()"]
  746b9c8e_354b_ecaf_85dc_a1e83631fad5 -->|method| 551c0cf8_9074_2186_24dc_6d297424d67b
  4af8bb15_8b41_7e81_dfb7_5f30251ffaf2["findChannelHandlerSubclasses()"]
  746b9c8e_354b_ecaf_85dc_a1e83631fad5 -->|method| 4af8bb15_8b41_7e81_dfb7_5f30251ffaf2
  855d0dfc_00f4_1814_ebb2_01d0cf409efa["isTestClass()"]
  746b9c8e_354b_ecaf_85dc_a1e83631fad5 -->|method| 855d0dfc_00f4_1814_ebb2_01d0cf409efa
  42b4869f_e828_c40a_620e_104bc498444c["String()"]
  746b9c8e_354b_ecaf_85dc_a1e83631fad5 -->|method| 42b4869f_e828_c40a_620e_104bc498444c

Relationship Graph

Source Code

transport/src/test/java/io/netty/nativeimage/ChannelHandlerMetadataUtil.java lines 49–245

public final class ChannelHandlerMetadataUtil {

    private static final Type HANDLER_METADATA_LIST_TYPE = new TypeToken<List<HandlerMetadata>>() {
    }.getType();
    private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();

    private ChannelHandlerMetadataUtil() {
    }

    public static void generateMetadata(String... packageNames) {
        String projectGroupId = System.getProperty("nativeImage.handlerMetadataGroupId");
        String projectArtifactId = System.getProperty("nativeimage.handlerMetadataArtifactId");

        Set<Class<? extends ChannelHandler>> subtypes = findChannelHandlerSubclasses(packageNames);

        if (Arrays.asList(packageNames).contains("io.netty.channel")) {
            // We want the metadata for the ChannelHandler itself too
            subtypes.add(ChannelHandler.class);
        }

        Set<HandlerMetadata> handlerMetadata = new HashSet<HandlerMetadata>();
        for (Class<?> subtype : subtypes) {
            handlerMetadata.add(new HandlerMetadata(subtype.getName(), new Condition(subtype.getName()), true));
        }

        String projectRelativeResourcePath = "src/main/resources/META-INF/native-image/" + projectGroupId + "/" +
                projectArtifactId + "/generated/handlers/reflect-config.json";
        File existingMetadataFile = new File(projectRelativeResourcePath);
        String existingMetadataPath = existingMetadataFile.getAbsolutePath();
        if (!existingMetadataFile.exists()) {
            if (handlerMetadata.size() == 0) {
                return;
            }

            String message = "Native Image reflection metadata is required for handlers in this project. " +
                    "This metadata was not found under " +
                    existingMetadataPath +
                    "\nPlease create this file with the following content: \n" +
                    getMetadataJsonString(handlerMetadata) +
                    "\n";
            Assertions.fail(message);
        }

        List<HandlerMetadata> existingMetadata = null;
        try {
            FileReader reader = new FileReader(existingMetadataFile);
            existingMetadata = gson.fromJson(reader, HANDLER_METADATA_LIST_TYPE);
        } catch (IOException e) {
            Assertions.fail("Failed to open the native-image metadata file at: " + existingMetadataPath, e);
        }

        Set<HandlerMetadata> newMetadata = new HashSet<HandlerMetadata>(handlerMetadata);
        newMetadata.removeAll(existingMetadata);

        Set<HandlerMetadata> removedMetadata = new HashSet<HandlerMetadata>(existingMetadata);
        removedMetadata.removeAll(handlerMetadata);

        if (!newMetadata.isEmpty() || !removedMetadata.isEmpty()) {
            StringBuilder builder = new StringBuilder();
            builder.append("In the native-image handler metadata file at ")
                    .append(existingMetadataPath)
                    .append("\n");

            if (!newMetadata.isEmpty()) {
                builder.append("The following new metadata must be added:\n\n")
                        .append(getMetadataJsonString(newMetadata))
                        .append("\n\n");
            }
            if (!removedMetadata.isEmpty()) {
                builder.append("The following metadata must be removed:\n\n")
                        .append(getMetadataJsonString(removedMetadata))
                        .append("\n\n");
            }

            builder.append("Expected metadata file contents:\n\n")
                    .append(getMetadataJsonString(handlerMetadata))
                    .append("\n");
            Assertions.fail(builder.toString());
        }
    }

Frequently Asked Questions

What is the ChannelHandlerMetadataUtil class?
ChannelHandlerMetadataUtil is a class in the netty codebase, defined in transport/src/test/java/io/netty/nativeimage/ChannelHandlerMetadataUtil.java.
Where is ChannelHandlerMetadataUtil defined?
ChannelHandlerMetadataUtil is defined in transport/src/test/java/io/netty/nativeimage/ChannelHandlerMetadataUtil.java at line 49.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free