title: Is there a way to infer generic types in Java?
body: <p>I want to create a generic method for sending emails with attachments, I was thinking about using something like this:</p>
  
  <pre><code>public class MailAttachment&lt;T&gt; {
  
      private String attachmentName;
      private T attachment;
  
      public MailAttachment(String attachmentName, T attachment) {
          super();
          this.attachmentName = attachmentName;
          this.attachment = attachment;
      }
      //Getters and setters
  
  }
  </code></pre>
  
  <p>Later, send the generic type and let Java decide in compilation time the instance type sent. But the compiler adds an error saying that it needs to be cast to the correct type  (File, DataSource, InputStreamSource).</p>
  
  <pre><code>   @Autowired
      private JavaMailSender mailSender;
  
   public void sendHtmlMail(Mail mail, MailAttachment&lt;?&gt; attachment) {
              new Thread(() -&gt; {
                  try {
                      MimeMessage message = mailSender.createMimeMessage();
                      MimeMessageHelper helper = new MimeMessageHelper(message, true);
  
                      helper.setTo(mail.getTo());
                      helper.setSubject(mail.getSubject());
                      helper.setFrom(host);
                      message.setContent(mail.getContent(), "text/html");
                      if (attachment != null) {    
                          //Casting error 
                     helper.addAttachment(attachment.getAttachmentName(), attachment.getAttachment());
                      }
                      mailSender.send(message);
                  } catch (MessagingException | MailException e) {
                      logger.error("Error sending email: ", e);
                  }
              }).start();
          }
  </code></pre>
  
  <p>So I had to use a workaround to validate and cast to the correct type, something like this:</p>
  
  <pre><code>if (attachment.getAttachment() instanceof DataSource) {
      helper.addAttachment(attachment.getAttachmentName(),
                          (DataSource) attachment.getAttachment());
  } else if (attachment.getAttachment() instanceof File) {
      helper.addAttachment(attachment.getAttachmentName(),
                          (File) attachment.getAttachment());
  } else if (attachment.getAttachment() instanceof InputStreamSource) {
      helper.addAttachment(attachment.getAttachmentName(),
                          (InputStreamSource) attachment.getAttachment());
  }
  </code></pre>
  
  <p><strong>So, I was wondering if there is a better way to check this kind of stuff using only this method while keeping the generic type in the parameter?</strong></p>