Hej,
Mam adnotacje:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface ConfigurationObject {
String fileName();
}
Interfejs:
public interface IConfiguration {
void setConfiguration(String fileContent) throws JAXBException;
}
Klasa implementująca interfejs i oznaczona adnotacją:
@Singleton
@ConfigurationObject(fileName = "Configuration.xml")
public class Configuration implements IConfiguration{
@Inject
private Logger logger;
private ConfigurationFile configurationFile;
@Override
public void setConfiguration(String fileContent) throws JAXBException {
logger.info("PLIK ZALADOWANY!: " + fileContent);
}
}
Oraz pobieranie obiektów danego interfejsu:
@Singleton
@Startup
public class ConfigurationsReader {
private final String CONFIG_DIRECTORY = "/opt/game/DATA/GameFiles/";
@Inject
private Logger logger;
@Inject @Any
Instance<IConfiguration> configurationBeans;
@PostConstruct
public void loadConfigurations() {
for(IConfiguration confObject : configurationBeans) {
String fileName = confObject.getClass().getAnnotation(ConfigurationObject.class).fileName();
if(fileName != null) {
String fileContent = "";
try {
fileContent = new String(Files.readAllBytes(Paths.get(CONFIG_DIRECTORY + fileName)));
confObject.setConfiguration(fileContent);
} catch (IOException | JAXBException ex) {
if(ex instanceof IOException) {
logger.error("File read exception, file name: " + fileName + ", EXCEPTION: ", ex.getMessage());
} else if(ex instanceof JAXBException) {
logger.error("Error while xml marshall in class: " + confObject.getClass().getName()
+ ", EXCEPTION: ", ex.getMessage());
}
}
}
}
}
Przy wykonaniu pobrania adnotacji wywala NullPointerException. Ogólnie nie widzi żadnej adnotacji. Jak wyciągnąć adnotacje z klasy implementującej dany interface?