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

devonfw / IDEasy / 13976220243

20 Mar 2025 06:02PM UTC coverage: 67.674% (+0.007%) from 67.667%
13976220243

push

github

web-flow
#1039: fix how jasypt commandlet invokes java (#1156)

3037 of 4914 branches covered (61.8%)

Branch coverage included in aggregate %.

7824 of 11135 relevant lines covered (70.26%)

3.07 hits per line

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

86.36
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.IdeContext;
11
import com.devonfw.tools.ide.nls.NlsBundle;
12
import com.devonfw.tools.ide.property.KeywordProperty;
13
import com.devonfw.tools.ide.property.Property;
14
import com.devonfw.tools.ide.tool.ToolCommandlet;
15
import com.devonfw.tools.ide.validation.ValidationResult;
16
import com.devonfw.tools.ide.validation.ValidationState;
17
import com.devonfw.tools.ide.version.VersionIdentifier;
18

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

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

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

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

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

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

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

37
  private Property<?> multiValued;
38

39
  private KeywordProperty firstKeyword;
40

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

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

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

62
    return this.properties;
3✔
63
  }
64

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

70
    return this.values;
3✔
71
  }
72

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

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

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

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

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

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

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

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

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

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

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

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

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

161
  /**
162
   * @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").
163
   */
164
  public KeywordProperty getFirstKeyword() {
165

166
    return this.firstKeyword;
3✔
167
  }
168

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

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

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

185
    return isIdeRootRequired();
3✔
186
  }
187

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

193
    return true;
2✔
194
  }
195

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

201
    return false;
2✔
202
  }
203

204
  /**
205
   * @return {@code true} if the output of this commandlet is (potentially) processed automatically from outside, {@code false} otherwise. For example
206
   *     {@link CompleteCommandlet} logs the suggestions for auto-completion to a bash script. Also the {@link EnvironmentCommandlet} logs the environment
207
   *     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
208
   *     break the processing of the output.
209
   */
210
  public boolean isProcessableOutput() {
211

212
    return false;
2✔
213
  }
214

215
  /**
216
   * Runs this {@link Commandlet}.
217
   */
218
  public abstract void run();
219

220
  /**
221
   * @return {@code true} if this {@link Commandlet} is the valid candidate to be {@link #run()}, {@code false} otherwise.
222
   * @see Property#validate()
223
   */
224
  public ValidationResult validate() {
225
    ValidationState state = new ValidationState(null);
5✔
226
    // avoid validation exception if not a candidate to be run.
227
    for (Property<?> property : this.propertiesList) {
11✔
228
      state.add(property.validate());
4✔
229
    }
1✔
230
    return state;
2✔
231
  }
232

233
  /**
234
   * Provide additional usage help of this {@link Commandlet} to the user.
235
   *
236
   * @param bundle the {@link NlsBundle} to get I18N messages from.
237
   */
238
  public void printHelp(NlsBundle bundle) {
239

240
  }
1✔
241

242
  @Override
243
  public String toString() {
244

245
    return getClass().getSimpleName() + "[" + getName() + "]";
7✔
246
  }
247

248
  /**
249
   * @return the {@link ToolCommandlet} set in a {@link Property} of this commandlet used for auto-completion of a {@link VersionIdentifier} or
250
   *     {@link com.devonfw.tools.ide.tool.plugin.ToolPluginDescriptor}, otherwise {@code null} if not exists or not configured.
251
   */
252
  public ToolCommandlet getToolForCompletion() {
253
    return null;
2✔
254
  }
255
}
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