{"record":{"id":"b02f556da80d2ce7","repo":"spring-projects/spring-ai","slug":"this-is-a-utility-class-and-cannot-be-instantiated-b02f55","errorCode":null,"errorMessage":"This is a utility class and cannot be instantiated","messagePattern":"This is a utility class and cannot be instantiated","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java","lineNumber":267,"sourceCode":"\t}\n\n\tprivate float[] getUserQueryEmbedding(String query) {\n\t\treturn this.embeddingModel.embed(query);\n\t}\n\n\t@Override\n\tpublic VectorStoreObservationContext.Builder createObservationContextBuilder(String operationName) {\n\n\t\treturn VectorStoreObservationContext.builder(VectorStoreProvider.SIMPLE.value(), operationName)\n\t\t\t.dimensions(this.embeddingModel.dimensions())\n\t\t\t.collectionName(\"in-memory-map\")\n\t\t\t.similarityMetric(VectorStoreSimilarityMetric.COSINE.value());\n\t}\n\n\tpublic static final class EmbeddingMath {\n\n\t\tprivate EmbeddingMath() {\n\t\t\tthrow new UnsupportedOperationException(\"This is a utility class and cannot be instantiated\");\n\t\t}\n\n\t\tpublic static double cosineSimilarity(float[] vectorX, float[] vectorY) {\n\t\t\tif (vectorX == null || vectorY == null) {\n\t\t\t\tthrow new RuntimeException(\"Vectors must not be null\");\n\t\t\t}\n\t\t\tif (vectorX.length != vectorY.length) {\n\t\t\t\tthrow new IllegalArgumentException(\"Vectors lengths must be equal\");\n\t\t\t}\n\n\t\t\tfloat dotProduct = dotProduct(vectorX, vectorY);\n\t\t\tfloat normX = norm(vectorX);\n\t\t\tfloat normY = norm(vectorY);\n\n\t\t\tif (normX == 0 || normY == 0) {\n\t\t\t\tthrow new IllegalArgumentException(\"Vectors cannot have zero norm\");\n\t\t\t}\n","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java#L249-L285","documentation":"SimpleVectorStore.EmbeddingMath is a pure static utility class whose constructor deliberately throws UnsupportedOperationException. It exists only to host static math helpers (cosineSimilarity, dotProduct, norm) and is never meant to be instantiated.","triggerScenarios":"Calling new SimpleVectorStore.EmbeddingMath() or using reflection/instantiation utilities (e.g. Jackson, Spring bean creation, tests) against the nested class.","commonSituations":"IDE auto-complete accidents; framework scanning that instantiates nested classes; writing tests that accidentally construct the utility.","solutions":["Do not instantiate it; call static methods directly: SimpleVectorStore.EmbeddingMath.cosineSimilarity(x, y).","Remove any code, config, or test that tries to construct the class."],"exampleFix":"// before\nSimpleVectorStore.EmbeddingMath math = new SimpleVectorStore.EmbeddingMath();\ndouble s = math.cosineSimilarity(x, y);\n\n// after\ndouble s = SimpleVectorStore.EmbeddingMath.cosineSimilarity(x, y);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isEmbeddingMathInstance(Object o) {\n    return o instanceof SimpleVectorStore.EmbeddingMath; // never construct it\n}","tryCatchPattern":"double s = SimpleVectorStore.EmbeddingMath.cosineSimilarity(x, y); // static call only","preventionTips":["Always use static access for utility classes.","Exclude utility classes from reflective instantiation/framework scanning."],"tags":["utility-class","unsupported-operation","instantiation"],"backgroundTag":"unsupported-operation","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}