{"id":"bb79997430f65d37","repo":"junit-team/junit5","slug":"invoke-convert-string-class-classloader-inst","errorCode":null,"errorMessage":"Invoke convert(String, Class<?>, ClassLoader) instead","messagePattern":"Invoke convert\\(String, Class<\\?>, ClassLoader\\) instead","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToClassConverter.java","lineNumber":24,"sourceCode":" * accompanies this distribution and is available at\n *\n * https://www.eclipse.org/legal/epl-v20.html\n */\n\npackage org.junit.platform.commons.support.conversion;\n\nimport org.junit.platform.commons.support.ReflectionSupport;\n\nclass StringToClassConverter implements StringToObjectConverter {\n\n\t@Override\n\tpublic boolean canConvertTo(Class<?> targetType) {\n\t\treturn targetType == Class.class;\n\t}\n\n\t@Override\n\tpublic Object convert(String source, Class<?> targetType) throws Exception {\n\t\tthrow new UnsupportedOperationException(\"Invoke convert(String, Class<?>, ClassLoader) instead\");\n\t}\n\n\t@Override\n\tpublic Class<?> convert(String className, Class<?> targetType, ClassLoader classLoader) throws Exception {\n\t\t// @formatter:off\n\t\treturn ReflectionSupport.tryToLoadClass(className, classLoader)\n\t\t\t\t.getNonNullOrThrow(cause -> new ConversionException(\n\t\t\t\t\t\t\"Failed to convert String \\\"\" + className + \"\\\" to type java.lang.Class\", cause));\n\t\t// @formatter:on\n\t}\n\n}\n","sourceCodeStart":6,"sourceCodeEnd":37,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/StringToClassConverter.java#L6-L37","documentation":"StringToClassConverter's 2-arg convert(String, Class<?>) throws UnsupportedOperationException because resolving a Class requires a ClassLoader; callers must use the 3-arg convert(String, Class<?>, ClassLoader). ConversionSupport always invokes the 3-arg overload, so this only surfaces when the converter is used directly/programmatically.","triggerScenarios":"Calling `new StringToClassConverter().convert(name, Class.class)` directly, or invoking the StringToObjectConverter 2-arg interface method on this converter via reflection or a generic pipeline that does not pass a ClassLoader.","commonSituations":"Custom conversion code that reuses JUnit's internal converters directly; reflection dispatch that picks the 2-arg method.","solutions":["Route through ConversionSupport.convert(source, Class.class, classLoader), which calls the 3-arg overload.","If calling the converter directly, use the 3-arg convert(String, Class<?>, ClassLoader) overload and pass an explicit ClassLoader (or null for the default).","Avoid relying on internal converter classes; prefer the public ConversionSupport facade."],"exampleFix":"// before\nconverter.convert(\"java.lang.String\", Class.class);\n\n// after\nconverter.convert(\"java.lang.String\", Class.class, classLoader);\n// or simply:\nConversionSupport.convert(\"java.lang.String\", Class.class, classLoader);","handlingStrategy":"validation","validationCode":"// Always go through the 3-arg (ClassLoader-aware) entry point.\nString name = \"java.lang.String\";\nClassLoader cl = Thread.currentThread().getContextClassLoader();\nClass<?> loaded = ConversionSupport.convert(name, Class.class, cl);","typeGuard":null,"tryCatchPattern":"try {\n    converter.convert(name, Class.class);\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage().contains(\"Invoke convert(String, Class<?>, ClassLoader)\")) {\n        // call the 3-arg overload with an explicit ClassLoader instead\n    } else throw e;\n}","preventionTips":["Prefer the public ConversionSupport facade over internal converter classes.","When invoking a converter directly, always pass a ClassLoader to the 3-arg overload.","Treat the 2-arg StringToObjectConverter method as unsupported for class-loading converters."],"tags":["junit","platform-commons","conversion","internal-api","misuse"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}