title: How to convert two methods with identical body into one method using generics?
body: <p>I have two following methods:</p>
  <pre><code>public class LoaderService {
      public static Config loadConfigFile() {
          try (var jsonReader = new JsonReader(new FileReader(Path.PROJECT_RESOURCES + &quot;config&quot; + Extension.JSON))) {
              return new Gson().fromJson(jsonReader, Config.class);
          } catch (IOException e) {
              return Config.builder().build();
          }
      }
  
      public static UriContainer loadUriContainerFile() {
          try (var jsonReader = new JsonReader(new FileReader(Path.PROJECT_RESOURCES + &quot;URI container&quot; + Extension.JSON))) {
              return new Gson().fromJson(jsonReader, UriContainer.class);
          } catch (IOException e) {
              return UriContainer.builder().build();
          }
      }
  }
  </code></pre>
  <p>As you can see, body are identical - I can pass fileName through method parameter. The methods load the content from a JSON file. The only significant difference in their bodies is the type of class that has been applied.<br />
  I thought about something like that:</p>
  <pre><code>public class LoaderService&lt;T&gt; {
      public T loadFile(String fileName) {
          try (var jsonReader = new JsonReader(new FileReader(fileName))) {
              return new Gson().fromJson(jsonReader, T.class);
          } catch (IOException e) {
              return T.builder().build();
          }
      }
  </code></pre>
  <p>but I got error there:</p>
  <pre><code>return T.builder().build();
  // ---&gt; Cannot resolve method 'builder' in 'T'
  </code></pre>
  <p>my dto - for example Config:</p>
  <pre><code>package dto;
  
  import lombok.Builder;
  import lombok.Value;
  
  @Value
  @Builder
  public class Config {
      String url;
      String username;
      String password;
      String jdbcDriverPath;
      String driverClassName;
      String hostname;
      String port;
      String serverPath;
  }
  </code></pre>
  <hr />
  <p>Is it possible to apply generics in above situation?<br />
  <strong>Sorry if this question doesn't make sense because I'm just learning generics</strong></p>