001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.sampullara.cli;
018    
019    import com.intellij.openapi.util.text.StringUtil;
020    import com.intellij.util.Function;
021    import com.intellij.util.containers.ComparatorUtil;
022    import org.jetbrains.annotations.NotNull;
023    
024    import java.lang.reflect.Field;
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    public class ArgumentUtils {
029    
030        private ArgumentUtils() {}
031    
032        @NotNull
033        public static <T> List<String> convertArgumentsToStringList(@NotNull T arguments, @NotNull T defaultArguments) {
034            List<String> result = new ArrayList<String>();
035            convertArgumentsToStringList(arguments, defaultArguments, result);
036            return result;
037        }
038    
039        public static <T> void convertArgumentsToStringList(@NotNull T arguments, @NotNull T defaultArguments, @NotNull List<String> result) {
040            convertArgumentsToStringList(arguments, defaultArguments, arguments.getClass(), result);
041        }
042    
043        private static <T> void convertArgumentsToStringList(T arguments, T defaultArguments, Class clazz, List<String> result) {
044            Class superClazz = clazz.getSuperclass();
045            if (superClazz != null) {
046                convertArgumentsToStringList(arguments, defaultArguments, superClazz, result);
047            }
048    
049            for (Field field : clazz.getDeclaredFields()) {
050                Argument argument = field.getAnnotation(Argument.class);
051                if (argument == null) continue;
052    
053                Object value;
054                Object defaultValue;
055                try {
056                    value = field.get(arguments);
057                    defaultValue = field.get(defaultArguments);
058                }
059                catch (IllegalAccessException ignored) {
060                    // skip this field
061                    continue;
062                }
063    
064                if (ComparatorUtil.equalsNullable(value, defaultValue)) continue;
065    
066                String name = Args.getAlias(argument);
067                if (name == null) {
068                    name = Args.getName(argument, field);
069                }
070    
071                Class<?> fieldType = field.getType();
072    
073                if (fieldType.isArray()) {
074                    Object[] values = (Object[]) value;
075                    if (values.length == 0) continue;
076                    value = StringUtil.join(values, Function.TO_STRING, argument.delimiter());
077                }
078    
079                result.add(argument.prefix() + name);
080    
081                if (fieldType == boolean.class || fieldType == Boolean.class) continue;
082    
083                result.add(value.toString());
084            }
085        }
086    
087    }