{"record":{"id":"b41a67f07f941b7f","repo":"alibaba/spring-ai-alibaba","slug":"utility-class-b41a67","errorCode":null,"errorMessage":"Utility class","messagePattern":"Utility class","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"warning","filePath":"spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java","lineNumber":37,"sourceCode":"import java.io.IOException;\nimport java.net.URI;\nimport java.net.URL;\nimport java.nio.file.FileSystem;\nimport java.nio.file.FileSystems;\nimport java.nio.file.Files;\nimport java.nio.file.Path;\nimport java.nio.file.StandardCopyOption;\nimport java.util.Collections;\nimport java.util.Enumeration;\n\n/**\n * @author HeYQ\n * @since 2024-11-28 11:47\n */\npublic class FileUtils {\n\n\tprivate FileUtils() {\n\t\tthrow new IllegalStateException(\"Utility class\");\n\t}\n\n\tpublic static void writeCodeToFile(String workDir, String filename, String code) {\n\t\ttry {\n\t\t\tif (code == null) {\n\t\t\t\tthrow new IllegalArgumentException(\"Code must not be null\");\n\t\t\t}\n\t\t\tPath filepath = Path.of(workDir, filename);\n\t\t\t// ensure the parent directory exists\n\t\t\tPath fileDir = filepath.getParent();\n\t\t\tif (fileDir != null && !Files.exists(fileDir)) {\n\t\t\t\tFiles.createDirectories(fileDir);\n\t\t\t}\n\t\t\t// write the code to the file\n\t\t\tFiles.writeString(filepath, code);\n\t\t}\n\t\tcatch (IOException e) {\n\t\t\tthrow new RuntimeException(e);","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java#L19-L55","documentation":"FileUtils is a static utility class whose private constructor throws IllegalStateException(\"Utility class\") if instantiated. This is an intentional anti-instantiation guard, not a runtime failure path — hitting it means reflection, accidental subclassing, or deserialization tried to create a FileUtils instance.","triggerScenarios":"Calling new FileUtils() (compile error normally, but reachable via reflection), Class.newInstance, some serialization frameworks, or mocking frameworks instantiating the class.","commonSituations":"Mistakenly writing 'new FileUtils()' by habit; reflection-based frameworks (certain JSON serializers or DI tools) probing constructors; code coverage tooling instantiating private constructors.","solutions":["Remove the instantiation and call the static methods directly: FileUtils.writeCodeToFile(...).","If a framework requires an instance, wrap the static calls in your own injectable service class.","Suppress framework scanning of this class rather than bypassing the guard."],"exampleFix":"// before\nFileUtils fu = new FileUtils(); // IllegalStateException\nfu.writeCodeToFile(workDir, filename, code);\n// after\nFileUtils.writeCodeToFile(workDir, filename, code); // static call","handlingStrategy":"type-guard","validationCode":"// never instantiate utilities; call statically\nboolean ok = FileUtils.class != null; // use FileUtils.writeCodeToFile(...) directly","typeGuard":"static void writeCode(String workDir, String filename, String code) {\n    FileUtils.writeCodeToFile(workDir, filename, code); // static access, no instantiation\n}","tryCatchPattern":"try {\n    FileUtils.class.getDeclaredConstructor().setAccessible(true);\n    // avoid this entirely — FileUtils is non-instantiable by design\n} catch (Exception e) {\n    // expected: use static methods instead\n}","preventionTips":["Always call FileUtils methods statically","Do not register utility classes with frameworks that instantiate components","Wrap static utilities in an injectable service if DI is required"],"tags":["utility-class","reflection","instantiation"],"backgroundTag":"unsupported-operation","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}