apache/dubbo · error · IllegalArgumentException

No such class name in {}

Error message

No such class name in {}

What it means

Thrown by AbstractCompiler.compile when the source code string does not contain a class declaration matching the CLASS_PATTERN regex ('class <Name>'). The compiler needs a class name to compile against, so a source without one is rejected as invalid input.

Source

Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/AbstractCompiler.java:54

    private static final Map<String, Lock> CLASS_IN_CREATION_MAP = new ConcurrentHashMap<>();

    @Override
    public Class<?> compile(Class<?> neighbor, String code, ClassLoader classLoader) {
        code = code.trim();
        Matcher matcher = PACKAGE_PATTERN.matcher(code);
        String pkg;
        if (matcher.find()) {
            pkg = matcher.group(1);
        } else {
            pkg = "";
        }
        matcher = CLASS_PATTERN.matcher(code);
        String cls;
        if (matcher.find()) {
            cls = matcher.group(1);
        } else {
            throw new IllegalArgumentException("No such class name in " + code);
        }
        String className = pkg != null && pkg.length() > 0 ? pkg + "." + cls : cls;
        Lock lock = CLASS_IN_CREATION_MAP.get(className);
        if (lock == null) {
            CLASS_IN_CREATION_MAP.putIfAbsent(className, new ReentrantLock());
            lock = CLASS_IN_CREATION_MAP.get(className);
        }
        try {
            lock.lock();
            return Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException e) {
            if (!code.endsWith("}")) {
                throw new IllegalStateException("The java code not endsWith \"}\", code: \n" + code + "\n");
            }
            try {
                return doCompile(neighbor, classLoader, className, code);
            } catch (RuntimeException t) {
                throw t;

View on GitHub (pinned to 3a3043227f)

Solutions

  1. Ensure the source code contains a 'class <Name> {' declaration that the CLASS_PATTERN regex can match.
  2. If compiling an interface or enum, verify the regex expectations — the pattern specifically looks for 'class '.
  3. Log the full code string before calling compile to inspect what is actually being passed.
  4. Check the code generator (e.g. AdaptiveExtensionInjector codegen) that produced the source for bugs.

Example fix

// before
String code = "public void doSomething() {}";  // no class decl
// after
String code = "public class Foo { public void doSomething() {} }";
Defensive patterns

Strategy: validation

Validate before calling

String code = sourceCode.trim();
if (!code.matches("(?s).*\\bclass\\s+[$_a-zA-Z][$_a-zA-Z0-9]*\\b.*")) {
    throw new IllegalArgumentException("source has no class declaration");
}
compiler.compile(neighbor, code, classLoader);

Type guard

static boolean hasClassDeclaration(String code) {
    return code != null && java.util.regex.Pattern.compile(
        "class\\s+([$_a-zA-Z][$_a-zA-Z0-9]*)\\s+").matcher(code).find();
}

Try / catch

try {
    compiler.compile(neighbor, code, classLoader);
} catch (IllegalArgumentException e) {
    // no class name found; inspect and fix the source string
}

Prevention

When it happens

Trigger: Calling compiler.compile(neighbor, code, classLoader) with a code string that has no 'class Xxx ' token — e.g. a code fragment, an interface/enum without the 'class' keyword positioned as expected, or a malformed/truncated snippet.

Common situations: Dynamic compilation of a SPI implementation (e.g. Adaptive extensions generated by Dubbo) where the generated source is malformed. A custom Compiler user passing a snippet that is not a full class. Truncation of the source string during generation.

Related errors


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