• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 22241505980

20 Feb 2026 09:16PM UTC coverage: 70.656% (+0.2%) from 70.474%
22241505980

Pull #1710

github

web-flow
Merge 04e4bdacd into 379acdc9d
Pull Request #1710: #404: allow logging via SLF4J

4121 of 6440 branches covered (63.99%)

Branch coverage included in aggregate %.

10704 of 14542 relevant lines covered (73.61%)

3.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.63
cli/src/main/java/com/devonfw/tools/ide/commandlet/Commandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Objects;
9

10
import com.devonfw.tools.ide.context.AbstractIdeContext;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.nls.NlsBundle;
13
import com.devonfw.tools.ide.property.KeywordProperty;
14
import com.devonfw.tools.ide.property.Property;
15
import com.devonfw.tools.ide.tool.ToolCommandlet;
16
import com.devonfw.tools.ide.validation.ValidationResult;
17
import com.devonfw.tools.ide.validation.ValidationState;
18
import com.devonfw.tools.ide.version.VersionIdentifier;
19

20
/**
21
 * A {@link Commandlet} is a sub-command of the IDE CLI.
22
 */
23
public abstract class Commandlet {
1✔
24

25
  /** The {@link IdeContext} instance. */
26
  protected final IdeContext context;
27

28
  private final List<Property<?>> propertiesList;
29

30
  private final List<Property<?>> properties;
31

32
  private final List<Property<?>> valuesList;
33

34
  private final List<Property<?>> values;
35

36
  private final Map<String, Property<?>> optionMap;
37

38
  private Property<?> multiValued;
39

40
  private KeywordProperty firstKeyword;
41

42
  /**
43
   * The constructor.
44
   *
45
   * @param context the {@link IdeContext}.
46
   */
47
  public Commandlet(IdeContext context) {
48

49
    super();
2✔
50
    this.context = context;
3✔
51
    this.propertiesList = new ArrayList<>();
5✔
52
    this.properties = Collections.unmodifiableList(this.propertiesList);
5✔
53
    this.valuesList = new ArrayList<>();
5✔
54
    this.values = Collections.unmodifiableList(this.valuesList);
5✔
55
    this.optionMap = new HashMap<>();
5✔
56
  }
1✔
57

58
  /**
59
   * @return the {@link List} with all {@link Property properties} of this {@link Commandlet}.
60
   */
61
  public List<Property<?>> getProperties() {
62

63
    return this.properties;
3✔
64
  }
65

66
  /**
67
   * @return the {@link List} of {@link Property properties} that are {@link Property#isValue() values}.
68
   */
69
  public List<Property<?>> getValues() {
70

71
    return this.values;
3✔
72
  }
73

74
  /**
75
   * Clear the set values on all properties of the {@link Commandlet#propertiesList}
76
   */
77
  public void reset() {
78

79
    for (Property<?> property : this.propertiesList) {
11✔
80
      property.clearValue();
2✔
81
    }
1✔
82
  }
1✔
83

84
  /**
85
   * @param nameOrAlias the potential {@link Property#getName() name} or {@link Property#getAlias() alias} of the requested {@link Property}.
86
   * @return the requested {@link Property property} or {@code null} if not found.
87
   */
88
  public Property<?> getOption(String nameOrAlias) {
89

90
    return this.optionMap.get(nameOrAlias);
6✔
91
  }
92

93
  /**
94
   * @param keyword the {@link KeywordProperty keyword} to {@link #add(Property) add}.
95
   */
96
  protected void addKeyword(String keyword) {
97

98
    addKeyword(keyword, null);
4✔
99
  }
1✔
100

101
  /**
102
   * @param keyword the {@link KeywordProperty keyword} to {@link #add(Property) add}.
103
   * @param alias the optional {@link KeywordProperty#getAlias() alias}.
104
   */
105
  protected void addKeyword(String keyword, String alias) {
106

107
    KeywordProperty property = new KeywordProperty(keyword, true, alias);
7✔
108
    if (this.firstKeyword == null) {
3✔
109
      if (!this.properties.isEmpty()) {
4!
110
        throw new IllegalStateException(property + " must be first property in " + getClass().getSimpleName());
×
111
      }
112
      this.firstKeyword = property;
3✔
113
    }
114
    add(property);
4✔
115
  }
1✔
116

117
  /**
118
   * @param <P> type of the {@link Property}.
119
   * @param property the {@link Property} to register.
120
   * @return the given {@link Property}.
121
   */
122
  protected <P extends Property<?>> P add(P property) {
123

124
    if (this.multiValued != null) {
3!
125
      throw new IllegalStateException("The multi-valued property " + this.multiValued + " can not be followed by " + property);
×
126
    }
127
    this.propertiesList.add(property);
5✔
128
    if (property.isOption()) {
3✔
129
      add(property.getName(), property, false);
6✔
130
      add(property.getAlias(), property, true);
6✔
131
    }
132
    if (property.isValue()) {
3✔
133
      this.valuesList.add(property);
5✔
134
    }
135
    if (property.isMultiValued()) {
3✔
136
      this.multiValued = property;
3✔
137
    }
138
    return property;
2✔
139
  }
140

141
  private void add(String name, Property<?> property, boolean alias) {
142

143
    if (alias && (name == null)) {
4✔
144
      return;
1✔
145
    }
146
    Objects.requireNonNull(name);
3✔
147
    assert (name.equals(name.trim()));
6!
148
    if (name.isEmpty() && !alias) {
3!
149
      return;
×
150
    }
151
    Property<?> duplicate = this.optionMap.put(name, property);
7✔
152
    if (duplicate != null) {
2!
153
      throw new IllegalStateException("Duplicate name or alias " + name + " for " + property + " and " + duplicate);
×
154
    }
155
  }
1✔
156

157
  /**
158
   * @return the name of this {@link Commandlet} (e.g. "help").
159
   */
160
  public abstract String getName();
161

162
  /**
163
   * @return the first keyword of this {@link Commandlet}. Typically the same as {@link #getName() name} but may also differ (e.g. "set" vs. "set-version").
164
   */
165
  public KeywordProperty getFirstKeyword() {
166

167
    return this.firstKeyword;
3✔
168
  }
169

170
  /**
171
   * @param <C> type of the {@link Commandlet}.
172
   * @param commandletType the {@link Class} reflecting the requested {@link Commandlet}.
173
   * @return the requested {@link Commandlet}.
174
   * @see CommandletManager#getCommandlet(Class)
175
   */
176
  protected <C extends Commandlet> C getCommandlet(Class<C> commandletType) {
177

178
    return this.context.getCommandletManager().getCommandlet(commandletType);
×
179
  }
180

181
  /**
182
   * @return {@code true} if {@link IdeContext#getIdeHome() IDE_HOME} is required for this commandlet, {@code false} otherwise.
183
   */
184
  public boolean isIdeHomeRequired() {
185

186
    return isIdeRootRequired();
3✔
187
  }
188

189
  /**
190
   * @return {@code true} if {@link IdeContext#getIdeRoot() IDE_ROOT} is required for this commandlet, {@code false} otherwise.
191
   */
192
  public boolean isIdeRootRequired() {
193

194
    return true;
2✔
195
  }
196

197
  /**
198
   * @return {@code true} to suppress the {@link com.devonfw.tools.ide.step.StepImpl#logSummary(boolean) step summary success message}.
199
   */
200
  public boolean isSuppressStepSuccess() {
201

202
    return false;
2✔
203
  }
204

205
  /**
206
   * @return {@code true} if the output of this commandlet is (potentially) processed automatically from outside, {@code false} otherwise. For example
207
   *     {@link CompleteCommandlet} logs the suggestions for auto-completion to a bash script. Also the {@link EnvironmentCommandlet} logs the environment
208
   *     variables for the {@code ide} wrapper script. In such scenarios these logs shall not be spammed with warnings like "IDE_ROOT is not set" that would
209
   *     break the processing of the output.
210
   */
211
  public boolean isProcessableOutput() {
212

213
    return false;
2✔
214
  }
215

216
  protected boolean isActivateJaveUtilLogging() {
217
    return !isProcessableOutput();
7✔
218
  }
219

220
  /**
221
   * Runs this {@link Commandlet}.
222
   */
223
  public final void run() {
224

225
    if (isActivateJaveUtilLogging()) {
3✔
226
      ((AbstractIdeContext) this.context).configureJavaUtilLogging();
4✔
227
    }
228
    doRun();
2✔
229
  }
1✔
230

231
  protected abstract void doRun();
232

233
  /**
234
   * @return {@code true} if this {@link Commandlet} is the valid candidate to be {@link #run()}, {@code false} otherwise.
235
   * @see Property#validate()
236
   */
237
  public ValidationResult validate() {
238
    ValidationState state = new ValidationState(null);
5✔
239
    // avoid validation exception if not a candidate to be run.
240
    for (Property<?> property : this.propertiesList) {
11✔
241
      state.add(property.validate());
4✔
242
    }
1✔
243
    return state;
2✔
244
  }
245

246
  /**
247
   * Provide additional usage help of this {@link Commandlet} to the user.
248
   *
249
   * @param bundle the {@link NlsBundle} to get I18N messages from.
250
   */
251
  public void printHelp(NlsBundle bundle) {
252

253
  }
1✔
254

255
  @Override
256
  public String toString() {
257

258
    return getClass().getSimpleName() + "[" + getName() + "]";
7✔
259
  }
260

261
  /**
262
   * @return the {@link ToolCommandlet} set in a {@link Property} of this commandlet used for auto-completion of a {@link VersionIdentifier} or
263
   *     {@link com.devonfw.tools.ide.tool.plugin.ToolPluginDescriptor}, otherwise {@code null} if not exists or not configured.
264
   */
265
  public ToolCommandlet getToolForCompletion() {
266
    return null;
2✔
267
  }
268
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc