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

devonfw / IDEasy / 22303886886

23 Feb 2026 11:19AM UTC coverage: 70.647% (+0.2%) from 70.474%
22303886886

Pull #1714

github

web-flow
Merge f1f7e1e61 into 379acdc9d
Pull Request #1714: #404: #1713: advanced logging

4069 of 6360 branches covered (63.98%)

Branch coverage included in aggregate %.

10644 of 14466 relevant lines covered (73.58%)

3.1 hits per line

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

78.89
cli/src/main/java/com/devonfw/tools/ide/environment/AbstractEnvironmentVariables.java
1
package com.devonfw.tools.ide.environment;
2

3
import java.nio.file.Path;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.regex.Matcher;
9

10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
import org.slf4j.event.Level;
13

14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.variable.IdeVariables;
16
import com.devonfw.tools.ide.variable.VariableDefinition;
17
import com.devonfw.tools.ide.variable.VariableSyntax;
18
import com.devonfw.tools.ide.version.VersionIdentifier;
19

20
/**
21
 * Abstract base implementation of {@link EnvironmentVariables}.
22
 */
23
public abstract class AbstractEnvironmentVariables implements EnvironmentVariables {
24

25
  private static final Logger LOG = LoggerFactory.getLogger(AbstractEnvironmentVariables.class);
4✔
26

27
  /**
28
   * When we replace variable expressions with their value the resulting {@link String} can change in size (shrink or grow). By adding a bit of extra capacity
29
   * we reduce the chance that the capacity is too small and a new buffer array has to be allocated and array-copy has to be performed.
30
   */
31
  private static final int EXTRA_CAPACITY = 8;
32

33
  private static final String SELF_REFERENCING_NOT_FOUND = "";
34

35
  private static final int MAX_RECURSION = 9;
36

37
  /**
38
   * @see #getParent()
39
   */
40
  protected final AbstractEnvironmentVariables parent;
41

42
  /**
43
   * The {@link IdeContext} instance.
44
   */
45
  protected final IdeContext context;
46

47
  private VariableSource source;
48

49
  /**
50
   * The constructor.
51
   *
52
   * @param parent the parent {@link EnvironmentVariables} to inherit from.
53
   * @param context the {@link IdeContext}.
54
   */
55
  public AbstractEnvironmentVariables(AbstractEnvironmentVariables parent, IdeContext context) {
56

57
    super();
2✔
58
    this.parent = parent;
3✔
59
    if (context == null) {
2!
60
      if (parent == null) {
×
61
        throw new IllegalArgumentException("parent and logger must not both be null!");
×
62
      }
63
      this.context = parent.context;
×
64
    } else {
65
      this.context = context;
3✔
66
    }
67
  }
1✔
68

69
  @Override
70
  public EnvironmentVariables getParent() {
71

72
    return this.parent;
3✔
73
  }
74

75
  @Override
76
  public Path getPropertiesFilePath() {
77

78
    return null;
2✔
79
  }
80

81
  @Override
82
  public Path getLegacyPropertiesFilePath() {
83

84
    return null;
2✔
85
  }
86

87
  @Override
88
  public VariableSource getSource() {
89

90
    if (this.source == null) {
3✔
91
      this.source = new VariableSource(getType(), getPropertiesFilePath());
9✔
92
    }
93
    return this.source;
3✔
94
  }
95

96
  /**
97
   * @param name the name of the variable to check.
98
   * @return {@code true} if the variable shall be exported, {@code false} otherwise.
99
   */
100
  protected boolean isExported(String name) {
101

102
    if (this.parent != null) {
3✔
103
      return this.parent.isExported(name);
5✔
104
    }
105
    return false;
2✔
106
  }
107

108
  @Override
109
  public final List<VariableLine> collectVariables() {
110

111
    return collectVariables(false);
4✔
112
  }
113

114
  @Override
115
  public final List<VariableLine> collectExportedVariables() {
116

117
    return collectVariables(true);
4✔
118
  }
119

120
  private final List<VariableLine> collectVariables(boolean onlyExported) {
121

122
    Map<String, VariableLine> variables = new HashMap<>();
4✔
123
    collectVariables(variables, onlyExported, this);
5✔
124
    return new ArrayList<>(variables.values());
6✔
125
  }
126

127
  /**
128
   * @param variables the {@link Map} where to add the names of the variables defined here as keys, and their corresponding source as value.
129
   */
130
  protected void collectVariables(Map<String, VariableLine> variables, boolean onlyExported, AbstractEnvironmentVariables resolver) {
131

132
    if (this.parent != null) {
3✔
133
      this.parent.collectVariables(variables, onlyExported, resolver);
6✔
134
    }
135
  }
1✔
136

137
  protected VariableLine createVariableLine(String name, boolean onlyExported, AbstractEnvironmentVariables resolver) {
138

139
    boolean export = resolver.isExported(name);
4✔
140
    if (!onlyExported || export) {
4✔
141
      String value = resolver.get(name, false);
5✔
142
      if (value != null) {
2✔
143
        return VariableLine.of(export, name, value, getSource());
7✔
144
      }
145
    }
146
    return null;
2✔
147
  }
148

149
  /**
150
   * @param propertiesFolderPath the {@link Path} to the folder containing the {@link #getPropertiesFilePath() properties file} of the child
151
   *     {@link EnvironmentVariables}.
152
   * @param type the {@link #getType() type}.
153
   * @return the new {@link EnvironmentVariables}.
154
   */
155
  public AbstractEnvironmentVariables extend(Path propertiesFolderPath, EnvironmentVariablesType type) {
156

157
    return new EnvironmentVariablesPropertiesFile(this, type, propertiesFolderPath, null, this.context);
10✔
158
  }
159

160
  /**
161
   * @return a new child {@link EnvironmentVariables} that will resolve variables recursively or this instance itself if already satisfied.
162
   */
163
  public EnvironmentVariables resolved() {
164

165
    return new EnvironmentVariablesResolved(this);
5✔
166
  }
167

168
  @Override
169
  public String resolve(String string, Object source) {
170
    return resolveRecursive(string, source, 0, this, new ResolveContext(source, string, false, VariableSyntax.CURLY));
14✔
171
  }
172

173
  @Override
174
  public String resolve(String string, Object source, boolean legacySupport) {
175

176
    return resolveRecursive(string, source, 0, this, new ResolveContext(source, string, legacySupport, null));
14✔
177
  }
178

179
  /**
180
   * This method is called recursively. This allows you to resolve variables that are defined by other variables.
181
   *
182
   * @param value the {@link String} that potentially contains variables in the syntax "${«variable«}". Those will be resolved by this method and replaced
183
   *     with their {@link #get(String) value}.
184
   * @param source the source where the {@link String} to resolve originates from. Should have a reasonable {@link Object#toString() string representation}
185
   *     that will be used in error or log messages if a variable could not be resolved.
186
   * @param recursion the current recursion level. This is used to interrupt endless recursion.
187
   * @param resolvedVars this is a reference to an object of {@link EnvironmentVariablesResolved} being the lowest level in the
188
   *     {@link EnvironmentVariablesType hierarchy} of variables. In case of a self-referencing variable {@code x} the resolving has to continue one level
189
   *     higher in the {@link EnvironmentVariablesType hierarchy} to avoid endless recursion. The {@link EnvironmentVariablesResolved} is then used if another
190
   *     variable {@code y} must be resolved, since resolving this variable has to again start at the lowest level. For example: For levels {@code l1, l2} with
191
   *     {@code l1 < l2} and {@code x=${x} foo} and {@code y=bar} defined at level {@code l1} and {@code x=test ${y}} defined at level {@code l2}, {@code x} is
192
   *     first resolved at level {@code l1} and then up the {@link EnvironmentVariablesType hierarchy} at {@code l2} to avoid endless recursion. However,
193
   *     {@code y} must be resolved starting from the lowest level in the {@link EnvironmentVariablesType hierarchy} and therefore
194
   *     {@link EnvironmentVariablesResolved} is used.
195
   * @param context the {@link ResolveContext}.
196
   * @return the given {@link String} with the variables resolved.
197
   */
198
  private String resolveRecursive(String value, Object source, int recursion, AbstractEnvironmentVariables resolvedVars, ResolveContext context) {
199

200
    if (value == null) {
2!
201
      return null;
×
202
    }
203
    if (recursion > MAX_RECURSION) {
3!
204
      throw new IllegalStateException(
×
205
          "Reached maximum recursion resolving " + value + " for root variable " + context.rootSrc + " with value '" + context.rootValue + "'.");
206
    }
207
    recursion++;
1✔
208

209
    String resolved;
210
    if (context.syntax == null) {
3✔
211
      resolved = resolveWithSyntax(value, source, recursion, resolvedVars, context, VariableSyntax.SQUARE);
9✔
212
      if (context.legacySupport) {
3!
213
        resolved = resolveWithSyntax(resolved, source, recursion, resolvedVars, context, VariableSyntax.CURLY);
10✔
214
      }
215
    } else {
216
      resolved = resolveWithSyntax(value, source, recursion, resolvedVars, context, context.syntax);
10✔
217
    }
218
    return resolved;
2✔
219
  }
220

221
  private String resolveWithSyntax(final String value, final Object src, final int recursion, final AbstractEnvironmentVariables resolvedVars,
222
      final ResolveContext context, final VariableSyntax syntax) {
223

224
    Matcher matcher = syntax.getPattern().matcher(value);
5✔
225
    if (!matcher.find()) {
3✔
226
      return value;
2✔
227
    }
228
    StringBuilder sb = new StringBuilder(value.length() + EXTRA_CAPACITY);
8✔
229
    do {
230
      String variableName = syntax.getVariable(matcher);
4✔
231
      String variableValue = resolvedVars.getValue(variableName, false);
5✔
232
      if (variableValue == null) {
2✔
233
        Level logLevel = Level.WARN;
2✔
234
        if (context.legacySupport && (syntax == VariableSyntax.CURLY)) {
3!
235
          logLevel = Level.INFO;
×
236
        }
237
        String var = matcher.group();
3✔
238
        if (recursion > 1) {
3✔
239
          LOG.atLevel(logLevel).log("Undefined variable {} in '{}' at '{}={}'", var, context.rootSrc, src, value);
25✔
240
        } else {
241
          LOG.atLevel(logLevel).log("Undefined variable {} in '{}'", var, src);
7✔
242
        }
243
        continue;
1✔
244
      }
245
      EnvironmentVariables lowestFound = findVariable(variableName);
4✔
246
      if ((lowestFound == null) || !lowestFound.getFlat(variableName).equals(value)) {
8✔
247
        // looking for "variableName" starting from resolved upwards the hierarchy
248
        String replacement = resolvedVars.resolveRecursive(variableValue, variableName, recursion, resolvedVars, context);
8✔
249
        matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
6✔
250
      } else { // is self referencing
1✔
251
        // finding next occurrence of "variableName" up the hierarchy of EnvironmentVariablesType
252
        EnvironmentVariables next = lowestFound.getParent();
3✔
253
        while (next != null) {
2✔
254
          if (next.getFlat(variableName) != null) {
4✔
255
            break;
1✔
256
          }
257
          next = next.getParent();
4✔
258
        }
259
        if (next == null) {
2✔
260
          matcher.appendReplacement(sb, Matcher.quoteReplacement(SELF_REFERENCING_NOT_FOUND));
6✔
261
          continue;
1✔
262
        }
263
        // resolving a self referencing variable one level up the hierarchy of EnvironmentVariablesType, i.e. at "next",
264
        // to avoid endless recursion
265
        String replacement = ((AbstractEnvironmentVariables) next).resolveRecursive(next.getFlat(variableName), variableName, recursion, resolvedVars, context
11✔
266
        );
267
        matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
6✔
268

269
      }
270
    } while (matcher.find());
3✔
271
    matcher.appendTail(sb);
4✔
272

273
    return sb.toString();
3✔
274
  }
275

276
  /**
277
   * Like {@link #get(String)} but with higher-level features including to resolve {@link IdeVariables} with their default values.
278
   *
279
   * @param name the name of the variable to get.
280
   * @param ignoreDefaultValue - {@code true} if the {@link VariableDefinition#getDefaultValue(IdeContext) default value} of a potential
281
   *     {@link VariableDefinition} shall be ignored, {@code false} to return default instead of {@code null}.
282
   * @return the value of the variable.
283
   */
284
  protected String getValue(String name, boolean ignoreDefaultValue) {
285

286
    VariableDefinition<?> var = IdeVariables.get(name);
3✔
287
    String value;
288
    if ((var != null) && var.isForceDefaultValue()) {
5✔
289
      value = var.getDefaultValueAsString(this.context);
6✔
290
    } else {
291
      value = this.parent.get(name, false);
6✔
292
    }
293
    if ((value == null) && (var != null)) {
4✔
294
      String key = var.getName();
3✔
295
      if (!name.equals(key)) {
4✔
296
        // try new name (e.g. IDE_TOOLS or IDE_HOME) if no value could be found by given legacy name (e.g.
297
        // DEVON_IDE_TOOLS or DEVON_IDE_HOME)
298
        value = this.parent.get(key, false);
6✔
299
      }
300
      if ((value == null) && !ignoreDefaultValue) {
4!
301
        value = var.getDefaultValueAsString(this.context);
5✔
302
      }
303
    }
304
    if ((value != null) && (value.startsWith("~/"))) {
6✔
305
      value = this.context.getUserHome() + value.substring(1);
9✔
306
    }
307
    return value;
2✔
308
  }
309

310
  @Override
311
  public String inverseResolve(String string, Object src, VariableSyntax syntax) {
312

313
    String result = string;
×
314
    for (VariableDefinition<?> variable : IdeVariables.VARIABLES) {
×
315
      if (variable != IdeVariables.PATH) {
×
316
        String name = variable.getName();
×
317
        String value = get(name);
×
318
        if (value == null) {
×
319
          value = variable.getDefaultValueAsString(this.context);
×
320
        }
321
        if (value != null) {
×
322
          result = result.replace(value, syntax.create(name));
×
323
        }
324
      }
325
    }
×
326
    if (!result.equals(string)) {
×
327
      LOG.trace("Inverse resolved '{}' to '{}' from {}.", string, result, src);
×
328
    }
329
    return result;
×
330
  }
331

332
  @Override
333
  public VersionIdentifier getToolVersion(String tool) {
334

335
    String variable = EnvironmentVariables.getToolVersionVariable(tool);
3✔
336
    String value = get(variable);
4✔
337
    if (value == null) {
2✔
338
      return VersionIdentifier.LATEST;
2✔
339
    } else if (value.isEmpty()) {
3✔
340
      LOG.warn("Variable {} is configured with empty value, please fix your configuration.", variable);
4✔
341
      return VersionIdentifier.LATEST;
2✔
342
    }
343
    VersionIdentifier version = VersionIdentifier.of(value);
3✔
344
    if (version == null) {
2!
345
      // can actually never happen, but for robustness
346
      version = VersionIdentifier.LATEST;
×
347
    }
348
    return version;
2✔
349
  }
350

351
  @Override
352
  public String toString() {
353

354
    return getSource().toString();
×
355
  }
356

357
  /**
358
   * Simple record for the immutable arguments of recursive resolve methods.
359
   *
360
   * @param rootSrc the root source where the {@link String} to resolve originates from.
361
   * @param rootValue the root value to resolve.
362
   * @param legacySupport flag for legacy support (see {@link #resolve(String, Object, boolean)}). Only considered if {@link #syntax()} is {@code null}.
363
   * @param syntax the explicit {@link VariableSyntax} to use.
364
   */
365
  private static record ResolveContext(Object rootSrc, String rootValue, boolean legacySupport, VariableSyntax syntax) {
15✔
366

367
  }
368

369
}
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