{"record":{"id":"37677e19ca875c8e","repo":"stanfordnlp/CoreNLP","slug":"illegal-construction-of-distsimfeaturefactory-it","errorCode":null,"errorMessage":"Illegal construction of DistsimFeatureFactory.  It must be created with a path to a cluster file","messagePattern":"Illegal construction of DistsimFeatureFactory\\.  It must be created with a path to a cluster file","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/shiftreduce/DistsimFeatureFactory.java","lineNumber":18,"sourceCode":"package edu.stanford.nlp.parser.shiftreduce;\n\nimport java.util.List;\n\nimport edu.stanford.nlp.ling.CoreLabel;\nimport edu.stanford.nlp.tagger.maxent.Distsim;\n\n/**\n * Featurizes words based only on their distributional similarity classes.\n * Borrows the Distsim class from the tagger.\n *\n * @author John Bauer\n */\npublic class DistsimFeatureFactory extends FeatureFactory {\n  private final Distsim distsim;\n\n  DistsimFeatureFactory() {\n    throw new UnsupportedOperationException(\"Illegal construction of DistsimFeatureFactory.  It must be created with a path to a cluster file\");\n  }\n\n  DistsimFeatureFactory(String path) {\n    distsim = Distsim.initLexicon(path);\n  }\n\n  public void addDistsimFeatures(List<String> features, CoreLabel label, String featureName) {\n    if (label == null) {\n      return;\n    }\n\n    String word = getFeatureFromCoreLabel(label, FeatureComponent.HEADWORD);\n    String tag = getFeatureFromCoreLabel(label, FeatureComponent.HEADTAG);\n\n    String cluster = distsim.getMapping(word);\n\n    features.add(featureName + \"dis-\" + cluster);\n    features.add(featureName + \"disT-\" + cluster + \"-\" + tag);","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/shiftreduce/DistsimFeatureFactory.java#L1-L36","documentation":"DistsimFeatureFactory has a no-argument constructor that unconditionally throws UnsupportedOperationException. It is deliberately made illegal: the factory requires a path to a word-cluster (distributional similarity) file to build its Distsim lexicon, so the default constructor exists only to block misconfiguration.","triggerScenarios":"Calling the package-private no-arg DistsimFeatureFactory() constructor, typically via reflection, serialization frameworks, or factory code that instantiates FeatureFactory subclasses by default constructor.","commonSituations":"Reflective instantiation of feature factories from config; a deserializer or DI container choosing the no-arg constructor; copy-pasted factory creation code without the cluster file path.","solutions":["Call DistsimFeatureFactory(String path) with the path to your cluster file instead","Ensure the cluster file exists and points to a valid Distsim-format lexicon","If using reflection, select the String-argument constructor via getConstructor(String.class)"],"exampleFix":"// before\nDistsimFeatureFactory factory = DistsimFeatureFactory.class.getDeclaredConstructor().newInstance();\n\n// after\nDistsimFeatureFactory factory = new DistsimFeatureFactory(\"/path/to/word_clusters.txt\");","handlingStrategy":"type-guard","validationCode":"if (path == null || path.isEmpty()) {\n  throw new IllegalArgumentException(\"DistsimFeatureFactory requires a cluster file path\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  factory = ctor.newInstance();\n} catch (InvocationTargetException e) {\n  throw new IllegalStateException(\"Use DistsimFeatureFactory(String path), not the no-arg constructor\", e.getCause());\n}","preventionTips":["Never instantiate FeatureFactory subclasses reflectively with the default constructor","Keep cluster file paths in configuration, not code","Check Distsim.initLexicon file existence before constructing"],"tags":["parser","feature-factory","constructor","configuration"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}