{"record":{"id":"c99194409a2273de","repo":"redis/jedis","slug":"must-not-instantiate-this-class-c99194","errorCode":null,"errorMessage":"Must not instantiate this class","messagePattern":"Must not instantiate this class","errorType":"exception","errorClass":"InstantiationError","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/search/querybuilder/QueryBuilders.java","lineNumber":15,"sourceCode":"package redis.clients.jedis.search.querybuilder;\n\nimport java.util.Arrays;\n\nimport static redis.clients.jedis.search.querybuilder.Values.value;\n\n/**\n * Created by mnunberg on 2/23/18.\n *\n * This class contains methods to construct query nodes. These query nodes can be added to parent\n * query nodes (building a chain) or used as the root query node.\n */\npublic class QueryBuilders {\n  private QueryBuilders() {\n    throw new InstantiationError(\"Must not instantiate this class\");\n  }\n\n  /**\n   * Create a new intersection node with child nodes. An intersection node is true if all its\n   * children are also true\n   *\n   * @param n sub-condition to add\n   * @return The node\n   */\n  public static QueryNode intersect(Node... n) {\n    return new IntersectNode().add(n);\n  }\n\n  /**\n   * Create a new intersection node with a field-value pair.\n   *\n   * @param field The field that should contain this value. If this value is empty, then any field\n   * will be checked.","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/search/querybuilder/QueryBuilders.java#L1-L33","documentation":"QueryBuilders is a static utility class whose only constructor is private and deliberately throws java.lang.InstantiationError. It exists purely as a namespace for static query-node factory methods (interse ction, union, disjunction, tag, etc.), so instantiation is forbidden by design. Reaching this error means the class itself was constructed reflectively or via an unsupported path, since the compiler normally prevents it.","triggerScenarios":"Calling new QueryBuilders() will not compile; this error is thrown only when the private constructor is invoked via reflection (Class.newInstance / Constructor.newInstance), serialization frameworks, bytecode tools, or wrappers that bypass access checks.","commonSituations":"Reflection-based test utilities or coverage/instrumentation tooling (e.g. JaCoCo, Mockito inline mocking) instantiating all classes in a package; generic factory/IOC code that reflects over constructors of utility classes; code generators producing `new QueryBuilders()`.","solutions":["Use the static factory methods directly, e.g. QueryBuilders.interse ction(...), instead of instantiating the class.","If reflective instantiation is intentional, exclude utility classes like QueryBuilders from the reflection-based tooling/instantiation list.","If a wrapper is needed, write your own delegating class rather than subclassing or constructing QueryBuilders."],"exampleFix":"// before\nQueryBuilders qb = new QueryBuilders(); // InstantiationError (e.g. via reflection)\n// after\nQuery.Node node = QueryBuilders.tag(\"foo\", \"bar\"); // use static API","handlingStrategy":"try-catch","validationCode":"if (clazz == QueryBuilders.class) {\n  throw new IllegalStateException(\"QueryBuilders is a utility class; use its static methods\");\n}","typeGuard":"boolean isUtilityClass(Class<?> c) {\n  return c == QueryBuilders.class || c.getDeclaredConstructors().length == 1 && isPrivate(c.getDeclaredConstructors()[0]);\n}","tryCatchPattern":"try {\n  ctor.setAccessible(true);\n  ctor.newInstance();\n} catch (java.lang.InstantiationError e) {\n  log.warn(\"{} is a static utility class; use static factory methods\", e.getMessage());\n}","preventionTips":["Never instantiate utility classes; always call their static methods.","Exclude *s / utility classes from reflective instantiation in test-coverage and mock tooling.","Recognize java.lang.InstantiationError with this message as an intentional design guard, not a bug."],"tags":["java","reflection","utility-class","search"],"backgroundTag":"internal-invariant-violation","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}