{"record":{"id":"13c16f97aa466928","repo":"bazelbuild/bazel","slug":"class-s-has-multiple-overloaded-methods-named-s","errorCode":null,"errorMessage":"Class %s has multiple overloaded methods named '%s' annotated with @StarlarkMethod","messagePattern":"Class (.+?) has multiple overloaded methods named '(.+?)' annotated with @StarlarkMethod","errorType":"validation","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/net/starlark/java/annot/StarlarkAnnotations.java","lineNumber":235,"sourceCode":"        Class<?>[] paramTypes = method.getParameterTypes();\n\n        if (paramTypes.length == paramsToMatch.length) {\n          for (int i = 0; i < paramTypes.length; i++) {\n            // This verifies assignability of the method signature to ensure this is not a\n            // coincidental overload. We verify assignability instead of matching exact parameter\n            // classes in order to match generic methods.\n            if (!paramTypes[i].isAssignableFrom(paramsToMatch[i])) {\n              throw new IllegalStateException(\n                  String.format(\n                      \"Class %s has an incompatible overload of annotated method %s declared by %s\",\n                      classObj, signatureToMatch.getName(), signatureToMatch.getDeclaringClass()));\n            }\n          }\n        }\n        if (callable == null) {\n          callable = method.getAnnotation(StarlarkMethod.class);\n        } else {\n          throw new IllegalStateException(\n              String.format(\n                  \"Class %s has multiple overloaded methods named '%s' annotated \"\n                      + \"with @StarlarkMethod\",\n                  classObj, signatureToMatch.getName()));\n        }\n      }\n    }\n    return callable;\n  }\n\n  private StarlarkAnnotations() {}\n}\n","sourceCodeStart":217,"sourceCodeEnd":248,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/net/starlark/java/annot/StarlarkAnnotations.java#L217-L248","documentation":"Starlark methods are dispatched by name only, so a Java class may expose at most one @StarlarkMethod-annotated method per name. If reflection finds a second annotated method with the same name (across the class and its supertypes), this IllegalStateException is thrown while building the class descriptor. Note Java overloading itself is legal; the restriction is on annotating both overloads.","triggerScenarios":"Two @StarlarkMethod-annotated methods with the same name (same Starlark name attribute counts too) found on a class or inherited from interfaces/superclasses; one declared locally, one inherited annotated.","commonSituations":"Copy-pasting an annotated method into a subclass; two interfaces implemented by a class both annotating a method of the same name; renaming the Starlark-side name= of two methods to collide.","solutions":["Give each annotated method a distinct Starlark name (@StarlarkMethod(name = ...)).","Remove the annotation from the method that should not be Starlark-visible.","Rename or delete the redundant Java overload.","If overriding, keep @StarlarkMethod only on the base declaration."],"exampleFix":"// before\nclass M {\n  @StarlarkMethod(name=\"items\") public Sequence<?> items() {...}\n  @StarlarkMethod(name=\"items\") public Sequence<?> items(int n) {...}\n}\n\n// after\nclass M {\n  @StarlarkMethod(name=\"items\") public Sequence<?> items() {...}\n  @StarlarkMethod(name=\"take\") public Sequence<?> items(int n) {...}\n}","handlingStrategy":"validation","validationCode":"// Assert at most one annotated method per Starlark name on a class\nstatic void checkUniqueNames(Class<?> c) {\n  Set<String> seen = new HashSet<>();\n  for (Class<?> k = c; k != null; k = k.getSuperclass()) {\n    for (Method m : k.getDeclaredMethods()) {\n      StarlarkMethod a = m.getAnnotation(StarlarkMethod.class);\n      if (a == null) continue;\n      if (!seen.add(a.name())) throw new AssertionError(\"duplicate Starlark name \" + a.name() + \" on \" + c);\n    }\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use distinct @StarlarkMethod(name=...) values for every exposed method in a hierarchy.","Avoid copy-pasting annotations when adding convenience methods.","Test that a class's full descriptor builds without exceptions."],"tags":["starlark","annotations","method-resolution","java"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}