|
| 1 | +/* |
| 2 | + * Copyright or © or Copr. ZLib contributors (2015 - 2016) |
| 3 | + * |
| 4 | + * This software is governed by the CeCILL-B license under French law and |
| 5 | + * abiding by the rules of distribution of free software. You can use, |
| 6 | + * modify and/ or redistribute the software under the terms of the CeCILL-B |
| 7 | + * license as circulated by CEA, CNRS and INRIA at the following URL |
| 8 | + * "http://www.cecill.info". |
| 9 | + * |
| 10 | + * As a counterpart to the access to the source code and rights to copy, |
| 11 | + * modify and redistribute granted by the license, users are provided only |
| 12 | + * with a limited warranty and the software's author, the holder of the |
| 13 | + * economic rights, and the successive licensors have only limited |
| 14 | + * liability. |
| 15 | + * |
| 16 | + * In this respect, the user's attention is drawn to the risks associated |
| 17 | + * with loading, using, modifying and/or developing or reproducing the |
| 18 | + * software by the user in light of its specific status of free software, |
| 19 | + * that may mean that it is complicated to manipulate, and that also |
| 20 | + * therefore means that it is reserved for developers and experienced |
| 21 | + * professionals having in-depth computer knowledge. Users are therefore |
| 22 | + * encouraged to load and test the software's suitability as regards their |
| 23 | + * requirements in conditions enabling the security of their systems and/or |
| 24 | + * data to be ensured and, more generally, to use and operate it in the |
| 25 | + * same conditions as regards security. |
| 26 | + * |
| 27 | + * The fact that you are presently reading this means that you have had |
| 28 | + * knowledge of the CeCILL-B license and that you accept its terms. |
| 29 | + */ |
| 30 | + |
| 31 | +package fr.zcraft.zlib.components.commands2; |
| 32 | + |
| 33 | +import fr.zcraft.zlib.components.commands2.annotations.Subcommand; |
| 34 | +import sun.reflect.generics.reflectiveObjects.NotImplementedException; |
| 35 | + |
| 36 | +import java.lang.reflect.Field; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.List; |
| 40 | +import java.util.stream.Collectors; |
| 41 | + |
| 42 | +/** |
| 43 | + * This class contains various utilities to generate Command objects from their bound runnable class. |
| 44 | + */ |
| 45 | +class CommandGenerator { |
| 46 | + static public <T extends CommandRunnable> Command<T> fromClass(Class<T> runnableClass, String name) { |
| 47 | + if(isCommandGroup(runnableClass)) { |
| 48 | + return fromEnumClass(runnableClass, name); |
| 49 | + } else { |
| 50 | + return fromPlainClass(runnableClass, name); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + static private boolean isCommandGroup(Class<? extends CommandRunnable> runnableClass) { |
| 55 | + return Enum.class.isAssignableFrom(runnableClass); |
| 56 | + } |
| 57 | + |
| 58 | + static private <T extends CommandRunnable> Command<T> fromEnumClass(Class<T> runnableClass, String name) { |
| 59 | + List<SubCommand<?, ?>> subcommands = Arrays.stream(runnableClass.getDeclaredFields()) |
| 60 | + .filter(Field::isEnumConstant) |
| 61 | + .map(CommandGenerator::fromField) |
| 62 | + .collect(Collectors.toList()); |
| 63 | + |
| 64 | + return new Command(runnableClass, name, true, subcommands); |
| 65 | + } |
| 66 | + |
| 67 | + static private <T extends CommandRunnable> Command<T> fromPlainClass(Class<T> runnableClass, String name) { |
| 68 | + return new Command(runnableClass, name, false, new ArrayList<>()); |
| 69 | + } |
| 70 | + |
| 71 | + static private <T extends CommandRunnable> SubCommand<?, ?> fromField(Field field) { |
| 72 | + Subcommand subcommand = field.getAnnotation(Subcommand.class); |
| 73 | + if(subcommand == null) throw new RuntimeException("No subcommand annotation"); //TODO: Better exception |
| 74 | + |
| 75 | + String commandName = subcommand.name(); |
| 76 | + if(commandName.isEmpty()) commandName = field.getName().toLowerCase(); |
| 77 | + Command<?> innerCommand = fromClass(subcommand.value(), commandName); |
| 78 | + T parentValue; |
| 79 | + try { |
| 80 | + parentValue = (T) field.get(null); |
| 81 | + } catch (IllegalAccessException e) { |
| 82 | + throw new RuntimeException(e); //TODO: Better exception |
| 83 | + } |
| 84 | + |
| 85 | + return new SubCommand(innerCommand, parentValue, field); |
| 86 | + } |
| 87 | +} |
0 commit comments