为什么我会收到此编译器错误(使用枚举作为单例)?
Why am I getting this compiler error (using enum as singleton)?
我有下面的class,封装了一些测试数据。我只需要它的一个实例,所以我创建了一个枚举。
public enum ErnstReuterPlatzBuildings {
INSTANCE; // Compiler error occurs here
private final Map<String, IBuilding> buildingsByIds;
ErnstReuterPlatzBuildings() throws ParserConfigurationException,
SAXException, XPathExpressionException, IOException {
this.buildingsByIds = composeBuildingsByIds(
getBuildings(ErnstReuterPlatzBuildings.class)
);
}
public Map<String, IBuilding> getBuildingsByIds() {
return buildingsByIds;
}
public static Document readErnstReuterPlatzData(final Class clazz)
throws ParserConfigurationException, SAXException, IOException {
final InputStream stream =
clazz.getClassLoader()
.getResourceAsStream("mc/ernstReuterPlatz/map.osm");
final DocumentBuilderFactory dbfac =
DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
return docBuilder.parse(stream);
}
private Map<String, IBuilding> composeBuildingsByIds(
final Set<IBuilding> buildings) {
final Map<String,IBuilding> buildingsByIds = new HashMap<>();
for (final IBuilding building : buildings) {
buildingsByIds.put(building.getWayId(), building);
}
return buildingsByIds;
}
private Set<IBuilding> getBuildings(final Class clazz)
throws ParserConfigurationException, SAXException, IOException,
XPathExpressionException {
final Document doc = readErnstReuterPlatzData(clazz);
final PointsReader pointsReader = new PointsReader(doc);
pointsReader.init();
final BuildingExtractor testObject =
new BuildingExtractor(pointsReader);
return testObject.extractBuildings(doc);
}
}
在其唯一元素的声明中,INSTANCE;
我在 IntelliJ Idea 中得到以下编译器错误:
Error:(22, 5) java: unreported exception javax.xml.parsers.ParserConfigurationException; must be caught or declared to be thrown
鉴于它出现在定义了 元素 而不是方法的那一行,我该如何修复它?
How can I fix it, given that it occurs at the line, where the element is defined, not a method?
在幕后 Java 通过调用构造函数创建一个实例 INSTANCE
。声明行中的代码类似于:
public static final INSTANCE = new ErnstReuterPlatzBuildings();
这就是错误来自该行的原因。
据我所知,无法通过允许用户捕获已检查的异常来解决此问题,因为 INSTANCE
是在任何方法调用之外的上下文中初始化的。
您可以通过自己捕获异常并将其包装在适当类型的未检查 RuntimeException
甚至 ExceptionInInitializerError
:
中来解决此问题
ErnstReuterPlatzBuildings() {
try {
this.buildingsByIds = composeBuildingsByIds(
getBuildings(ErnstReuterPlatzBuildings.class)
);
} catch (ParserConfigurationException pce) {
throw new ExceptionInInitializerError(pce);
} catch (SAXException sxe) {
throw new ExceptionInInitializerError(sxe);
} catch (XPathExpressionException xpe) {
throw new ExceptionInInitializerError(xpe);
} catch (IOException ioe) {
throw new ExceptionInInitializerError(ioe);
}
}
我有下面的class,封装了一些测试数据。我只需要它的一个实例,所以我创建了一个枚举。
public enum ErnstReuterPlatzBuildings {
INSTANCE; // Compiler error occurs here
private final Map<String, IBuilding> buildingsByIds;
ErnstReuterPlatzBuildings() throws ParserConfigurationException,
SAXException, XPathExpressionException, IOException {
this.buildingsByIds = composeBuildingsByIds(
getBuildings(ErnstReuterPlatzBuildings.class)
);
}
public Map<String, IBuilding> getBuildingsByIds() {
return buildingsByIds;
}
public static Document readErnstReuterPlatzData(final Class clazz)
throws ParserConfigurationException, SAXException, IOException {
final InputStream stream =
clazz.getClassLoader()
.getResourceAsStream("mc/ernstReuterPlatz/map.osm");
final DocumentBuilderFactory dbfac =
DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
return docBuilder.parse(stream);
}
private Map<String, IBuilding> composeBuildingsByIds(
final Set<IBuilding> buildings) {
final Map<String,IBuilding> buildingsByIds = new HashMap<>();
for (final IBuilding building : buildings) {
buildingsByIds.put(building.getWayId(), building);
}
return buildingsByIds;
}
private Set<IBuilding> getBuildings(final Class clazz)
throws ParserConfigurationException, SAXException, IOException,
XPathExpressionException {
final Document doc = readErnstReuterPlatzData(clazz);
final PointsReader pointsReader = new PointsReader(doc);
pointsReader.init();
final BuildingExtractor testObject =
new BuildingExtractor(pointsReader);
return testObject.extractBuildings(doc);
}
}
在其唯一元素的声明中,INSTANCE;
我在 IntelliJ Idea 中得到以下编译器错误:
Error:(22, 5) java: unreported exception javax.xml.parsers.ParserConfigurationException; must be caught or declared to be thrown
鉴于它出现在定义了 元素 而不是方法的那一行,我该如何修复它?
How can I fix it, given that it occurs at the line, where the element is defined, not a method?
在幕后 Java 通过调用构造函数创建一个实例 INSTANCE
。声明行中的代码类似于:
public static final INSTANCE = new ErnstReuterPlatzBuildings();
这就是错误来自该行的原因。
据我所知,无法通过允许用户捕获已检查的异常来解决此问题,因为 INSTANCE
是在任何方法调用之外的上下文中初始化的。
您可以通过自己捕获异常并将其包装在适当类型的未检查 RuntimeException
甚至 ExceptionInInitializerError
:
ErnstReuterPlatzBuildings() {
try {
this.buildingsByIds = composeBuildingsByIds(
getBuildings(ErnstReuterPlatzBuildings.class)
);
} catch (ParserConfigurationException pce) {
throw new ExceptionInInitializerError(pce);
} catch (SAXException sxe) {
throw new ExceptionInInitializerError(sxe);
} catch (XPathExpressionException xpe) {
throw new ExceptionInInitializerError(xpe);
} catch (IOException ioe) {
throw new ExceptionInInitializerError(ioe);
}
}