[英]Refactor Java enum into a core project which does not contain classes that the enum points at
I have 2 Java projects, core-project
and component-project
.我有 2 个 Java 项目,
core-project
和component-project
。
core-project
is a dependency to component-project
. core-project
是对component-project
的依赖。
component-project
originally contained an Enum
eg: component-project
最初包含一个Enum
,例如:
public enum ResourceType {
APP("app", "App", App.class),
WORKSPACE("ws", "Workspace", Workspace.class);
private String extension;
private String description;
private Class<?> klass;
ResourceType(final String extension, final String description, final Class<?> klass) {
this.extension = extension;
this.description = description;
this.klass = klass;
}
}
The problem is that I now need to move ResourceType
down into core-project
but I cannot move Workspace.class
down into core-project
, it must remain in component-project
.问题是我现在需要将
ResourceType
向下移动到core-project
中,但我不能将Workspace.class
向下移动到core-project
中,它必须保留在component-project
中。
What would be an elegant approach to refactor this Enum
into core-project
?将此
Enum
重构为core-project
的优雅方法是什么?
One (not too elegant) way could be something like一种(不太优雅)的方式可能是这样的
public enum ResourceType {
APP("app", "App", "com.example.App"),
WORKSPACE("ws", "Workspace", "com.example.Workspace");
private String extension;
private String description;
private Class<?> klass;
ResourceType(final String extension, final String description, final String klass) {
this.extension = extension;
this.description = description;
try {
this.klass = Class.forName(klass);
} catch (Exception e) {
this.klass = null;
}
}
}
With the expectation of some NPEs when the classes aren't present...当课程不存在时,一些 NPE 的期望......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.