{"record":{"id":"dd21c4158e54469f","repo":"apache/maven","slug":"is-not-an-instance-of","errorCode":null,"errorMessage":"{} is not an instance of {}","messagePattern":"(.+?) is not an instance of (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"impl/maven-impl/src/main/java/org/apache/maven/impl/ImplUtils.java","lineNumber":34,"sourceCode":" * specific language governing permissions and limitations\n * under the License.\n */\npackage org.apache.maven.impl;\n\nimport java.util.Collection;\nimport java.util.List;\nimport java.util.Objects;\nimport java.util.function.Function;\nimport java.util.stream.Collectors;\n\nclass ImplUtils {\n\n    public static <T> T cast(Class<T> clazz, Object o, String name) {\n        if (!clazz.isInstance(o)) {\n            if (o == null) {\n                throw new IllegalArgumentException(name + \" is null\");\n            }\n            throw new IllegalArgumentException(name + \" is not an instance of \" + clazz.getName());\n        }\n        return clazz.cast(o);\n    }\n\n    public static <U, V> List<V> map(Collection<U> list, Function<U, V> mapper) {\n        return list.stream().map(mapper).filter(Objects::nonNull).collect(Collectors.toList());\n    }\n}\n","sourceCodeStart":16,"sourceCodeEnd":43,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-impl/src/main/java/org/apache/maven/impl/ImplUtils.java#L16-L43","documentation":"ImplUtils.cast(clazz, object, name) throws IllegalArgumentException(name + \" is not an instance of \" + clazz.getName()) when the object is non-null but not of the required internal type. The Maven impl layer uses this to reject substitutes that do not carry its internal implementation class — e.g. a user-written Session implementation passed where the impl's InternalSession is required.","triggerScenarios":"Implementing org.apache.maven.api.Session (or another API interface) yourself and passing it to code that calls InternalSession.from / ImplUtils.cast; wrapping or delegating Maven objects through proxies that strip their concrete type; mixing API versions so the impl class no longer matches.","commonSituations":"Third-party frameworks wrapping the Maven Session in dynamic proxies; plugins compiled against a different Maven core version than the one running; partial mocks in tests replacing real Session objects.","solutions":["Do not hand-implement or proxy Maven SPI interfaces; always obtain instances (Session, Model, services) from Maven's own lookup","Align the Maven core / API versions between your plugin or extension and the running Maven","In tests, use the real objects from a bootstrapped session instead of mocks for anything crossing into the impl layer"],"exampleFix":"// before: hand-rolled Session implementation\nSession mySession = new MyCustomSession();\nInternalSession.from(mySession); // \"is not an instance of\" InternalSession\n\n// after: use the session Maven provides\nSession session = mavenExecutionRequestSession; // obtained from Maven\nInternalSession.from(session);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// guard before passing API objects into impl-layer code\nstatic boolean isUsableSession(Object s) {\n    return s != null && s.getClass().getName().startsWith(\"org.apache.maven.impl.\");\n    // stronger: s instanceof org.apache.maven.impl.InternalSession (impl classpath only)\n}","tryCatchPattern":"try {\n    InternalSession.from(session);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"is not an instance of\")) {\n        // a wrapped/custom implementation was passed; re-fetch the real session from Maven\n        throw new IllegalStateException(\"Use the Session provided by Maven, not a custom implementation\", e);\n    }\n    throw e;\n}","preventionTips":["Never implement or proxy Maven SPI interfaces yourself; get instances from lookup","Keep plugin/extension Maven core versions aligned with the running Maven","Avoid deep proxies around Session/Model objects crossing into impl code"],"tags":["maven","internal-api","type-check","session","di"],"backgroundTag":"type-mismatch","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}