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

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

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

87.65
today-context/src/main/java/infra/origin/TextResourceOrigin.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.origin;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.io.IOException;
23
import java.util.Objects;
24

25
import infra.core.io.ClassPathResource;
26
import infra.core.io.Resource;
27
import infra.util.ObjectUtils;
28

29
/**
30
 * {@link Origin} for an item loaded from a text resource. Provides access to the original
31
 * {@link Resource} that loaded the text and a {@link Location} within it. If the provided
32
 * resource provides an {@link Origin} (e.g. it is an {@link OriginTrackedResource}), then
33
 * it will be used as the {@link Origin#getParent() origin parent}.
34
 *
35
 * @author Madhura Bhave
36
 * @author Phillip Webb
37
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
38
 * @see OriginTrackedResource
39
 * @since 4.0
40
 */
41
public class TextResourceOrigin implements Origin {
42

43
  @Nullable
44
  private final Resource resource;
45

46
  @Nullable
47
  private final Location location;
48

49
  public TextResourceOrigin(@Nullable Resource resource, @Nullable Location location) {
2✔
50
    this.resource = resource;
3✔
51
    this.location = location;
3✔
52
  }
1✔
53

54
  /**
55
   * Return the resource where the property originated.
56
   *
57
   * @return the text resource or {@code null}
58
   */
59
  @Nullable
60
  public Resource getResource() {
61
    return this.resource;
3✔
62
  }
63

64
  /**
65
   * Return the location of the property within the source (if known).
66
   *
67
   * @return the location or {@code null}
68
   */
69
  @Nullable
70
  public Location getLocation() {
71
    return this.location;
3✔
72
  }
73

74
  @Nullable
75
  @Override
76
  public Origin getParent() {
77
    return Origin.from(this.resource);
4✔
78
  }
79

80
  @Override
81
  public boolean equals(Object obj) {
82
    if (this == obj) {
3✔
83
      return true;
2✔
84
    }
85
    if (obj == null) {
2!
86
      return false;
×
87
    }
88
    if (obj instanceof TextResourceOrigin other) {
6!
89
      return Objects.equals(this.resource, other.resource)
11✔
90
              && Objects.equals(this.location, other.location);
5✔
91
    }
92
    return super.equals(obj);
×
93
  }
94

95
  @Override
96
  public int hashCode() {
97
    int result = 1;
2✔
98
    result = 31 * result + ObjectUtils.nullSafeHashCode(this.resource);
8✔
99
    result = 31 * result + ObjectUtils.nullSafeHashCode(this.location);
8✔
100
    return result;
2✔
101
  }
102

103
  @Override
104
  public String toString() {
105
    StringBuilder result = new StringBuilder();
4✔
106
    result.append(getResourceDescription(this.resource));
7✔
107
    if (this.location != null) {
3✔
108
      result.append(" - ").append(this.location);
7✔
109
    }
110
    return result.toString();
3✔
111
  }
112

113
  private String getResourceDescription(@Nullable Resource resource) {
114
    if (resource instanceof OriginTrackedResource) {
3✔
115
      return getResourceDescription(((OriginTrackedResource) resource).getDelegate());
6✔
116
    }
117
    if (resource == null) {
2✔
118
      return "unknown resource [?]";
2✔
119
    }
120
    if (resource instanceof ClassPathResource) {
3!
121
      return getResourceDescription((ClassPathResource) resource);
5✔
122
    }
123
    return resource.toString();
×
124
  }
125

126
  private String getResourceDescription(ClassPathResource resource) {
127
    try {
128
      JarUri jarUri = JarUri.from(resource.getURI());
4✔
129
      if (jarUri != null) {
2✔
130
        return jarUri.getDescription(resource.toString());
5✔
131
      }
132
    }
133
    catch (IOException ignored) {
1✔
134
    }
1✔
135
    return resource.toString();
3✔
136
  }
137

138
  /**
139
   * A location (line and column number) within the resource.
140
   */
141
  public static final class Location {
142

143
    private final int line;
144

145
    private final int column;
146

147
    /**
148
     * Create a new {@link Location} instance.
149
     *
150
     * @param line the line number (zero indexed)
151
     * @param column the column number (zero indexed)
152
     */
153
    public Location(int line, int column) {
2✔
154
      this.line = line;
3✔
155
      this.column = column;
3✔
156
    }
1✔
157

158
    /**
159
     * Return the line of the text resource where the property originated.
160
     *
161
     * @return the line number (zero indexed)
162
     */
163
    public int getLine() {
164
      return this.line;
3✔
165
    }
166

167
    /**
168
     * Return the column of the text resource where the property originated.
169
     *
170
     * @return the column number (zero indexed)
171
     */
172
    public int getColumn() {
173
      return this.column;
3✔
174
    }
175

176
    @Override
177
    public boolean equals(Object obj) {
178
      if (this == obj) {
3✔
179
        return true;
2✔
180
      }
181
      if (obj == null || getClass() != obj.getClass()) {
7!
182
        return false;
×
183
      }
184
      Location other = (Location) obj;
3✔
185
      return this.line == other.line
14!
186
              && this.column == other.column;
187
    }
188

189
    @Override
190
    public int hashCode() {
191
      return (31 * this.line) + this.column;
8✔
192
    }
193

194
    @Override
195
    public String toString() {
196
      return (this.line + 1) + ":" + (this.column + 1);
10✔
197
    }
198

199
  }
200

201
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc