bridge99

Published: (March 23, 2026 at 04:58 PM EDT)
1 min read
Source: Dev.to

Source: Dev.to

private String extractValue(Object value) {
    if (value == null) return "null";

    // 1. Unmask Spring's TypedStringValue immediately
    if (value instanceof TypedStringValue) {
        String raw = ((TypedStringValue) value).getValue();
        return xmlEscape(raw != null ? raw.trim() : "null");
    }

    // 2. Handle Bean References (@name)
    if (value instanceof BeanReference) {
        return "@" + ((BeanReference) value).getBeanName();
    }

    // 3. Handle Inner Bean Definitions (Short Class Name only)
    if (value instanceof BeanDefinition) {
        String className = ((BeanDefinition) value).getBeanClassName();
        if (className != null) {
            return "*" + className.substring(className.lastIndexOf('.') + 1).trim() + "*";
        }
        return "*InnerBean*";
    }

    // 4. Handle Collections (Lists/Sets) - Recursive clean
    if (value instanceof Iterable) {
        List cleaned = new ArrayList<>();
        int count = 0;
        for (Object item : (Iterable) value) {
            if (count++ >= 15) { 
                cleaned.add("..."); 
                break; 
            }
            cleaned.add(extractValue(item)); // Recursion handles the TypedStrings inside the list
        }
        return String.join("", cleaned);
    }

    // 5. Handle Maps
    if (value instanceof Map) {
        StringBuilder mapStr = new StringBuilder();
        for (Map.Entry entry : ((Map) value).entrySet()) {
            mapStr.append(extractValue(entry.getKey()))
                  .append("=")
                  .append(extractValue(entry.getValue()))
                  .append("");
        }
        return mapStr.toString();
    }

    // Default fallback with aggressive trimming
    String result = value.toString().trim();
    if (result.length() > 60) {
        result = result.substring(0, 57).trim() + "...";
    }
    return xmlEscape(result);
}
0 views
Back to Blog

Related posts

Read more »

SJF4J: A Structured JSON Facade for Java

Introduction Working with JSON in Java usually means choosing between two approaches: - Data binding POJO – strong typing, but rigid - Tree model JsonNode / Ma...

two sum- java

Solving Two Sum Using Hashing My Thought Process When I first saw the Two Sum problem, my initial idea was simple: - Target = 9 - Current number = 2 I don’t ne...