gpt4 book ai didi

java - 有没有办法在 Java 中推断泛型类型?

转载 作者:行者123 更新时间:2023-11-29 08:27:36 25 4
gpt4 key购买 nike

我想创建一个通用的方法来发送带附件的电子邮件,我正在考虑使用这样的方法:

public class MailAttachment<T> {

private String attachmentName;
private T attachment;

public MailAttachment(String attachmentName, T attachment) {
super();
this.attachmentName = attachmentName;
this.attachment = attachment;
}
//Getters and setters

}

稍后,发送泛型类型并让 Java 在编译时决定发送的实例类型。但是编译器添加了一个错误,指出需要将其转换为正确的类型(File、DataSource、InputStreamSource)。

   @Autowired
private JavaMailSender mailSender;

public void sendHtmlMail(Mail mail, MailAttachment<?> attachment) {
new Thread(() -> {
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();
}

所以我不得不使用变通方法来验证并转换为正确的类型,如下所示:

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());
}

所以,我想知道是否有更好的方法来仅使用此方法检查此类内容,同时在参数中保留泛型类型?

最佳答案

创建一个可附加的接口(interface):

interface Attachable {
void attach(Helper helper);
}

然后为各种类型实现它:

class DataSourceAttachable implements Attachable {
String name;
DataSource dataSource; // Initialize in ctor.

void attach(Helper helper) {
helper.addAttachment(name, dataSource);
}
}

// etc, for other types

那么就不需要泛型了:

public void sendHtmlMail(Mail mail, Attachable attachable) {
// ...
attachable.attach(helper);
// ...
}

关于java - 有没有办法在 Java 中推断泛型类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51294942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com