apache/dubbo · error · IllegalArgumentException

Can not create wrapper for primitive type

Error message

Can not create wrapper for primitive type: {}

What it means

Thrown by makeWrapper when the class passed in is a Java primitive type (boolean.class, int.class, etc.). Wrapper generates bytecode to access properties and invoke methods, which is meaningless for primitives, so it rejects them up front.

Solutions

  1. Box the primitive before wrapping: use the corresponding wrapper class (Integer.class for int.class) via ClassUtils or manually.
  2. Check c.isPrimitive() before calling getWrapper and route primitives through a different path.
  3. Inspect the calling code that resolves the Class to ensure it uses getReturnType()/getField().getType() correctly rather than a primitive literal.

Example fix

// before
Wrapper w = Wrapper.getWrapper(int.class);  // throws
// after
Class<?> c = int.class;
if (c.isPrimitive()) c = getBoxedType(c);
Wrapper w = Wrapper.getWrapper(c);
Defensive patterns

Strategy: validation

Validate before calling

Class<?> c = someClass;
if (c.isPrimitive()) {
    // route primitive types away from Wrapper
    c = boxPrimitive(c); // or skip wrapping
}
Wrapper w = Wrapper.getWrapper(c);

Type guard

static boolean isWrapperSafe(Class<?> c) {
    return !c.isPrimitive();
}

Try / catch

try {
    Wrapper w = Wrapper.getWrapper(c);
} catch (IllegalArgumentException e) {
    // c was primitive; use boxed type or alternate path
}

Prevention

When it happens

Trigger: Calling Wrapper.getWrapper(int.class), Wrapper.getWrapper(boolean.class), or any c.isPrimitive() class directly, or a code path that resolves a field/return type to a primitive .class object and forwards it to getWrapper.

Common situations: A reflection utility or serialization layer obtains the raw primitive Class object (e.g. from a field of primitive type) and passes it to getWrapper instead of its wrapper type. Also happens when method return types are inspected incorrectly.

Related errors


AI-assisted analysis of apache/dubbo@3a3043227f (2026-08-14). Data as JSON: /api/errors/919a8a6b79552e21. Report an issue: GitHub.

Appendix: source

Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java:130

     * @return Wrapper instance(not null).
     */
    public static Wrapper getWrapper(Class<?> c) {
        return ConcurrentHashMapUtils.computeIfAbsent(WRAPPER_MAP, c, (clazz) -> {
            while (ClassGenerator.isDynamicClass(clazz)) // can not wrapper on dynamic class.
            {
                clazz = clazz.getSuperclass();
            }

            if (clazz == Object.class) {
                return OBJECT_WRAPPER;
            }
            return makeWrapper(clazz);
        });
    }

    private static Wrapper makeWrapper(Class<?> c) {
        if (c.isPrimitive()) {
            throw new IllegalArgumentException("Can not create wrapper for primitive type: " + c);
        }

        String name = c.getName();
        ClassLoader cl = ClassUtils.getClassLoader(c);

        StringBuilder c1 = new StringBuilder("public void setPropertyValue(Object o, String n, Object v){ ");
        StringBuilder c2 = new StringBuilder("public Object getPropertyValue(Object o, String n){ ");
        StringBuilder c3 =
                new StringBuilder("public Object invokeMethod(Object o, String n, Class[] p, Object[] v) throws "
                        + InvocationTargetException.class.getName() + "{ ");

        c1.append(name)
                .append(" w; try{ w = ((")
                .append(name)
                .append(")$1); }catch(Throwable e){ throw new IllegalArgumentException(e); }");
        c2.append(name)
                .append(" w; try{ w = ((")
                .append(name)

View on GitHub (pinned to 3a3043227f)