Add .tox/ to .gitignore
[odlparent.git] / karaf / karaf-maven-plugin / src / main / java / org / apache / karaf / tooling / commands / AbstractCommandHelpPrinter.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 package org.apache.karaf.tooling.commands;
20
21 import java.lang.annotation.Annotation;
22 import java.lang.reflect.Field;
23
24 import org.apache.karaf.shell.api.action.Action;
25 import org.apache.karaf.shell.api.action.Argument;
26
27 public abstract class AbstractCommandHelpPrinter implements CommandHelpPrinter {
28
29     protected Argument replaceDefaultArgument(Field field, Argument argument) {
30         if (Argument.DEFAULT.equals(argument.name())) {
31             final Argument delegate = argument;
32             final String name = field.getName();
33             argument = new Argument() {
34                 public String name() {
35                     return name;
36                 }
37
38                 public String description() {
39                     return delegate.description();
40                 }
41
42                 public boolean required() {
43                     return delegate.required();
44                 }
45
46                 public int index() {
47                     return delegate.index();
48                 }
49
50                 public boolean multiValued() {
51                     return delegate.multiValued();
52                 }
53
54                 public String valueToShowInHelp() {
55                     return delegate.valueToShowInHelp();
56                 }
57
58                 public Class<? extends Annotation> annotationType() {
59                     return delegate.annotationType();
60                 }
61             };
62         }
63         return argument;
64     }
65
66     protected Object getDefaultValue(Action action, Field field) {
67         try {
68             field.setAccessible(true);
69             return field.get(action);
70         } catch (Exception e) {
71             return null;
72         }
73     }
74
75     protected String getDefaultValueString(Object o) {
76         if (o != null
77                 && (!(o instanceof Boolean) || ((Boolean) o))
78                 && (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
79             return o.toString();
80         } else {
81             return null;
82         }
83     }
84
85 }