/** * TODO Refactor, instanceof code smell. TODO Add new annotation in here for the moment * * @param ai * @param instance */ private void initClassFields(final Class<? extends AI> ai, final AI instance) throws IllegalAccessException { for (Field field : ai.getDeclaredFields()) { for (Annotation annotation : field.getDeclaredAnnotations()) { if (annotation instanceof AIDigitParameters) { initClassFieldDigitParameter(ai, instance, field, (AIDigitParameters) annotation); } else if (annotation instanceof AIBooleanParameter) { initClassFieldBooleanParameter(ai, instance, field, (AIBooleanParameter) annotation); } // else if ( annotation instanceof WhatEverFieldAnnotation ) { } -> put it here! } } }
public GameAILoader() { _finder = new ClassAIFinder(); _classes = _finder.getAIClasses(AI.class, "ch.hesso"); ImmutableMap.Builder<String, AI> builder = ImmutableMap.builder(); for (Class<? extends AI> ai : _classes) { try { AI instance = ai.newInstance(); instance.initialize(GameController.this); if (instance.kind().equals(PLAYER)) { _default = instance; _defaultAIName = instance.name(); } builder.put(instance.name(), instance); initClassFields(ai, instance); } catch (InstantiationException | IllegalAccessException e) { LOG.log(Level.SEVERE, "[GameController.GameAILoader.GameAILoader]: " + e.getMessage()); } } _ais = builder.build(); if (_default == null && _ais.size() > 0) { _default = _ais.values().asList().get(0); _defaultAIName = _ais.keySet().asList().get(0); } }