{"record":{"id":"9e557b7b9644a7f1","repo":"alibaba/spring-ai-alibaba","slug":"utility-class","errorCode":null,"errorMessage":"Utility class","messagePattern":"Utility class","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"warning","filePath":"spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java","lineNumber":31,"sourceCode":" * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.alibaba.cloud.ai.graph.utils;\n\nimport java.io.IOException;\nimport java.net.URL;\nimport java.nio.file.Files;\nimport java.nio.file.Path;\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":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java#L13-L49","documentation":"FileUtils is a static utility class whose private constructor deliberately throws this IllegalStateException to prevent instantiation via reflection or accidental new FileUtils(). It is an intentional guard, not a bug in the library.","triggerScenarios":"Any attempt to instantiate the class: new FileUtils(), or reflective instantiation (Class.newInstance / ReflectionUtils) — e.g. frameworks or serializers that try to construct every class on the classpath.","commonSituations":"Java reflection-based tools, code-coverage/serialization frameworks instantiating utility classes, or a developer newing up the class by habit.","solutions":["Use the static methods directly: FileUtils.writeCodeToFile(...), FileUtils.copyResourceJarToWorkDir(...).","Never instantiate utility classes; call them statically.","If a framework insists on instantiation, register the class as excluded or make members static-accessed only."],"exampleFix":"// before\nFileUtils fu = new FileUtils(); // throws\nfu.writeCodeToFile(dir, name, code);\n// after\nFileUtils.writeCodeToFile(dir, name, code);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isUtilityClass(Class<?> c) {\n    return java.lang.reflect.Modifier.isPublic(c.getModifiers())\n        && c.getDeclaredConstructors().length == 1\n        && java.lang.reflect.Modifier.isPrivate(c.getDeclaredConstructors()[0].getModifiers());\n}","tryCatchPattern":"try {\n    Constructor<FileUtils> c = FileUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (InvocationTargetException e) {\n    // expected: IllegalStateException \"Utility class\" — use static methods instead\n}","preventionTips":["Never instantiate utility classes; call static methods directly.","Exclude utility classes from reflective instantiation/serialization frameworks.","Recognize this exception as an intentional guard, not a library bug."],"tags":["utility-class","instantiation","reflection"],"backgroundTag":"internal-invariant-violation","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}