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

devonfw / IDEasy / 12779414521

15 Jan 2025 01:07AM UTC coverage: 67.807% (-0.02%) from 67.826%
12779414521

Pull #938

github

web-flow
Merge 326e3f493 into 6f167c101
Pull Request #938: #853: fix the nullpointer exception

2691 of 4337 branches covered (62.05%)

Branch coverage included in aggregate %.

6962 of 9899 relevant lines covered (70.33%)

3.09 hits per line

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

11.94
cli/src/main/java/com/devonfw/tools/ide/property/PathProperty.java
1
package com.devonfw.tools.ide.property;
2

3
import java.io.IOException;
4
import java.nio.file.Files;
5
import java.nio.file.Path;
6
import java.util.stream.Stream;
7

8
import com.devonfw.tools.ide.commandlet.Commandlet;
9
import com.devonfw.tools.ide.completion.CompletionCandidateCollector;
10
import com.devonfw.tools.ide.context.IdeContext;
11
import com.devonfw.tools.ide.validation.PropertyValidator;
12
import com.devonfw.tools.ide.validation.ValidationResult;
13
import com.devonfw.tools.ide.validation.ValidationState;
14

15
/**
16
 * {@link Property} with {@link Path} as {@link #getValueType() value type}.
17
 */
18
public class PathProperty extends Property<Path> {
19

20
  private final boolean mustExist;
21

22
  /**
23
   * The constructor.
24
   *
25
   * @param name the {@link #getName() property name}.
26
   * @param required the {@link #isRequired() required flag}.
27
   * @param mustExist the {@link #isPathRequiredToExist() required to exist flag}.
28
   * @param alias the {@link #getAlias() property alias}.
29
   */
30
  public PathProperty(String name, boolean required, String alias, boolean mustExist) {
31

32
    this(name, required, alias, mustExist, null);
×
33
  }
×
34

35
  /**
36
   * The constructor.
37
   *
38
   * @param name the {@link #getName() property name}.
39
   * @param required the {@link #isRequired() required flag}.
40
   * @param alias the {@link #getAlias() property alias}.
41
   * @param mustExist the {@link #isPathRequiredToExist() required to exist flag}.
42
   * @param validator the {@link PropertyValidator} used to {@link #validate() validate} the {@link #getValue() value}.
43
   */
44
  public PathProperty(String name, boolean required, String alias, boolean mustExist, PropertyValidator<Path> validator) {
45

46
    super(name, required, alias, false, validator);
7✔
47
    this.mustExist = mustExist;
3✔
48
  }
1✔
49

50
  @Override
51
  public Class<Path> getValueType() {
52

53
    return Path.class;
×
54
  }
55

56
  @Override
57
  public Path parse(String valueAsString, IdeContext context) {
58

59
    return Path.of(valueAsString);
×
60
  }
61

62
  @Override
63
  public ValidationResult validate() {
64
    ValidationState state = new ValidationState(this.getNameOrAlias());
6✔
65
    for (Path path : this.value) {
7!
66
      if (path != null && Files.exists(path)) {
×
67
        if (isPathRequiredToBeFile() && !Files.isRegularFile(path)) {
×
68
          state.addErrorMessage("Path " + path + " is not a file.");
×
69
        } else if (isPathRequiredToBeFolder() && !Files.isDirectory(path)) {
×
70
          state.addErrorMessage("Path " + path + " is not a folder.");
×
71
        }
72
      } else if (isPathRequiredToExist()) {
×
73
        state.addErrorMessage("Path " + path + " does not exist.");
×
74
      }
75
    }
×
76
    state.add(super.validate());
4✔
77
    return state;
2✔
78
  }
79

80
  /**
81
   * @return {@code true} if the {@link Path} {@link #getValue() value} must {@link Files#exists(Path, java.nio.file.LinkOption...) exist} if set, {@code false}
82
   *     otherwise.
83
   */
84
  protected boolean isPathRequiredToExist() {
85

86
    return this.mustExist;
×
87
  }
88

89
  /**
90
   * @return {@code true} if the {@link Path} {@link #getValue() value} must be a {@link Files#isDirectory(Path, java.nio.file.LinkOption...) folder} if it
91
   *     exists, {@code false} otherwise.
92
   */
93
  protected boolean isPathRequiredToBeFolder() {
94

95
    return false;
×
96
  }
97

98
  /**
99
   * @return {@code true} if the {@link Path} {@link #getValue() value} must be a {@link Files#isRegularFile(Path, java.nio.file.LinkOption...) file} if it
100
   *     exists, {@code false} otherwise.
101
   */
102
  protected boolean isPathRequiredToBeFile() {
103

104
    return false;
×
105
  }
106

107
  @Override
108
  protected void completeValue(String arg, IdeContext context, Commandlet commandlet, CompletionCandidateCollector collector) {
109

110
    Path path = Path.of(arg);
×
111
    Path parent = path.getParent();
×
112
    //if parent directory can not be obtained, use current directory to avoid the nullpointer exception
113
    if (parent == null) {
×
114
      parent = Path.of(".");
×
115
    }
116
    String filename = path.getFileName().toString();
×
117
    if (Files.isDirectory(parent)) {
×
118
      try (Stream<Path> children = Files.list(parent)) {
×
119
        children.filter(child -> isValidPath(path, filename)).forEach(child -> collector.add(child.toString(), null, this, commandlet));
×
120
      } catch (IOException e) {
×
121
        throw new IllegalStateException(e);
×
122
      }
×
123
    }
124
  }
×
125

126
  private boolean isValidPath(Path path, String filename) {
127

128
    if (isPathRequiredToBeFile() && !Files.isRegularFile(getValue())) {
×
129
      return false;
×
130
    } else if (isPathRequiredToBeFolder() && !Files.isDirectory(getValue())) {
×
131
      return false;
×
132
    }
133
    return path.getFileName().toString().startsWith(filename);
×
134
  }
135

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