title: Polymorphic construction from String identifier
body: <p>I'm trying to setup a polymorphic ExportRule system for my program, but I'm having trouble figuring out how to construct the desired ExportRule from a config file which has the ExportRule defined at the top of the file via a String.</p>
  
  <p>I was looking to do something like this:</p>
  
  <pre><code>private ExportRule loadExportRule(String fileName) throws IOException, InvalidConfigurationException, FileNotFoundException{
      if(fileName == null) {
          throw new NullPointerException("fileName cannot be null!");
      }
      YamlConfiguration exportRuleConfig = new YamlConfiguration();
      exportRuleConfig.load(getFilePath(fileName));
  
      return ExportRules.fromString(exportRuleConfig.getString("exportRule")).fromYamlConfiguration(exportRuleConfig);
  }
  </code></pre>
  
  <p>where ExportRules is an enum mapping a String to the actual .class (which extends ExportRule) and then would call a static fromFileConfiguration() factory method to generate the ExportRule from the file which looks like this:</p>
  
  <pre><code>exportRule: nameOfExportRule
  //all the configurationstuff for the rule
  </code></pre>
  
  <p>The problem I'm having is how to actually call the fromFileConfiguration() method as it is a static method and therefore java will not allow me to guarantee it's existence via the ExportRule interface</p>
  
  <p>Is there a clean way to achieve something like this or do I have to do some kind of switch statement in my enum for each ExportRule?</p>
  
  <p>edit:</p>
  
  <p>Thank you glee8e!</p>
  
  <p>here's my enum:</p>
  
  <pre><code>public enum ExportRules {
  COMMENT_CONCAT("CommentConcat") {
      @Override
      public ExportRule fromFileConfiguration(FileConfiguration fileConfiguration) throws InvalidConfigurationException {
          return CommentConcat.fromFileConfiguration(fileConfiguration);
      }
  };
  
  private String name;
  
  ExportRules(String name) {
      this.name = name;
  }
  
  public abstract ExportRule fromFileConfiguration(FileConfiguration fileConfiguration) throws InvalidConfigurationException;
  
  public String getName() {
      return name;
  }
  
  public static ExportRules fromString(String str) {
      for(ExportRules rule : ExportRules.values()) {
          if(str.equalsIgnoreCase(rule.getName())) return rule;
      }
      return null;
  }
  
  }
  </code></pre>