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

mybatis / ibatis-2 / 678

27 Dec 2025 01:13AM UTC coverage: 65.584% (+0.05%) from 65.532%
678

push

github

hazendaz
Merge remote-tracking branch 'upstream/master' into support-javax

1606 of 2810 branches covered (57.15%)

167 of 305 new or added lines in 90 files covered. (54.75%)

6 existing lines in 4 files now uncovered.

5067 of 7726 relevant lines covered (65.58%)

0.66 hits per line

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

62.5
/src/main/java/com/ibatis/common/resources/Resources.java
1
/*
2
 * Copyright 2004-2025 the original author or authors.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.ibatis.common.resources;
17

18
import com.ibatis.common.beans.ClassInfo;
19

20
import java.io.File;
21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.io.InputStreamReader;
24
import java.io.Reader;
25
import java.lang.reflect.InvocationTargetException;
26
import java.net.URL;
27
import java.net.URLConnection;
28
import java.nio.charset.Charset;
29
import java.nio.file.Path;
30
import java.util.Properties;
31

32
/**
33
 * A class to simplify access to resources through the classloader.
34
 */
35
public class Resources extends Object {
36

37
  /** The default class loader. */
38
  private static ClassLoader defaultClassLoader;
39

40
  /**
41
   * Charset to use when calling getResourceAsReader. null means use the system default.
42
   */
43
  private static Charset charset;
44

45
  /**
46
   * Instantiates a new resources.
47
   */
48
  private Resources() {
49
  }
50

51
  /**
52
   * Returns the default classloader (may be null).
53
   *
54
   * @return The default classloader
55
   */
56
  public static ClassLoader getDefaultClassLoader() {
57
    return defaultClassLoader;
1✔
58
  }
59

60
  /**
61
   * Sets the default classloader.
62
   *
63
   * @param defaultClassLoader
64
   *          - the new default ClassLoader
65
   */
66
  public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
67
    Resources.defaultClassLoader = defaultClassLoader;
1✔
68
  }
1✔
69

70
  /**
71
   * Returns the URL of the resource on the classpath.
72
   *
73
   * @param resource
74
   *          The resource to find
75
   *
76
   * @return The resource
77
   *
78
   * @throws IOException
79
   *           If the resource cannot be found or read
80
   */
81
  public static URL getResourceURL(String resource) throws IOException {
82
    return getResourceURL(getClassLoader(), resource);
1✔
83
  }
84

85
  /**
86
   * Returns the URL of the resource on the classpath.
87
   *
88
   * @param loader
89
   *          The classloader used to load the resource
90
   * @param resource
91
   *          The resource to find
92
   *
93
   * @return The resource
94
   *
95
   * @throws IOException
96
   *           If the resource cannot be found or read
97
   */
98
  public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
99
    URL url = null;
1✔
100
    if (loader != null)
1!
101
      url = loader.getResource(resource);
1✔
102
    if (url == null)
1✔
103
      url = ClassLoader.getSystemResource(resource);
1✔
104
    if (url == null)
1✔
105
      throw new IOException("Could not find resource " + resource);
1✔
106
    return url;
1✔
107
  }
108

109
  /**
110
   * Returns a resource on the classpath as a Stream object.
111
   *
112
   * @param resource
113
   *          The resource to find
114
   *
115
   * @return The resource
116
   *
117
   * @throws IOException
118
   *           If the resource cannot be found or read
119
   */
120
  public static InputStream getResourceAsStream(String resource) throws IOException {
121
    return getResourceAsStream(getClassLoader(), resource);
1✔
122
  }
123

124
  /**
125
   * Returns a resource on the classpath as a Stream object.
126
   *
127
   * @param loader
128
   *          The classloader used to load the resource
129
   * @param resource
130
   *          The resource to find
131
   *
132
   * @return The resource
133
   *
134
   * @throws IOException
135
   *           If the resource cannot be found or read
136
   */
137
  public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
138
    InputStream in = null;
1✔
139
    if (loader != null)
1!
140
      in = loader.getResourceAsStream(resource);
1✔
141
    if (in == null)
1✔
142
      in = ClassLoader.getSystemResourceAsStream(resource);
1✔
143
    if (in == null)
1✔
144
      throw new IOException("Could not find resource " + resource);
1✔
145
    return in;
1✔
146
  }
147

148
  /**
149
   * Returns a resource on the classpath as a Properties object.
150
   *
151
   * @param resource
152
   *          The resource to find
153
   *
154
   * @return The resource
155
   *
156
   * @throws IOException
157
   *           If the resource cannot be found or read
158
   */
159
  public static Properties getResourceAsProperties(String resource) throws IOException {
160
    Properties props = new Properties();
1✔
161
    String propfile = resource;
1✔
162
    InputStream in = getResourceAsStream(propfile);
1✔
163
    props.load(in);
1✔
164
    in.close();
1✔
165
    return props;
1✔
166
  }
167

168
  /**
169
   * Returns a resource on the classpath as a Properties object.
170
   *
171
   * @param loader
172
   *          The classloader used to load the resource
173
   * @param resource
174
   *          The resource to find
175
   *
176
   * @return The resource
177
   *
178
   * @throws IOException
179
   *           If the resource cannot be found or read
180
   */
181
  public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
182
    Properties props = new Properties();
1✔
183
    String propfile = resource;
1✔
184
    InputStream in = getResourceAsStream(loader, propfile);
1✔
185
    props.load(in);
1✔
186
    in.close();
1✔
187
    return props;
1✔
188
  }
189

190
  /**
191
   * Returns a resource on the classpath as a Reader object.
192
   *
193
   * @param resource
194
   *          The resource to find
195
   *
196
   * @return The resource
197
   *
198
   * @throws IOException
199
   *           If the resource cannot be found or read
200
   */
201
  public static Reader getResourceAsReader(String resource) throws IOException {
202
    Reader reader;
203
    if (charset == null) {
1!
204
      reader = new InputStreamReader(getResourceAsStream(resource));
1✔
205
    } else {
206
      reader = new InputStreamReader(getResourceAsStream(resource), charset);
×
207
    }
208

209
    return reader;
1✔
210
  }
211

212
  /**
213
   * Returns a resource on the classpath as a Reader object.
214
   *
215
   * @param loader
216
   *          The classloader used to load the resource
217
   * @param resource
218
   *          The resource to find
219
   *
220
   * @return The resource
221
   *
222
   * @throws IOException
223
   *           If the resource cannot be found or read
224
   */
225
  public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
226
    Reader reader;
227
    if (charset == null) {
×
228
      reader = new InputStreamReader(getResourceAsStream(loader, resource));
×
229
    } else {
230
      reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
×
231
    }
232

233
    return reader;
×
234
  }
235

236
  /**
237
   * Returns a resource on the classpath as a File object.
238
   *
239
   * @param resource
240
   *          The resource to find
241
   *
242
   * @return The resource
243
   *
244
   * @throws IOException
245
   *           If the resource cannot be found or read
246
   */
247
  public static File getResourceAsFile(String resource) throws IOException {
248
    return Path.of(getResourceURL(resource).toString()).toFile();
×
249
  }
250

251
  /**
252
   * Returns a resource on the classpath as a File object.
253
   *
254
   * @param loader
255
   *          - the classloader used to load the resource
256
   * @param resource
257
   *          - the resource to find
258
   *
259
   * @return The resource
260
   *
261
   * @throws IOException
262
   *           If the resource cannot be found or read
263
   */
264
  public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
265
    return Path.of(getResourceURL(loader, resource).toString()).toFile();
×
266
  }
267

268
  /**
269
   * Gets a URL as an input stream.
270
   *
271
   * @param urlString
272
   *          - the URL to get
273
   *
274
   * @return An input stream with the data from the URL
275
   *
276
   * @throws IOException
277
   *           If the resource cannot be found or read
278
   */
279
  public static InputStream getUrlAsStream(String urlString) throws IOException {
280
    URL url = new URL(urlString);
×
281
    URLConnection conn = url.openConnection();
×
282
    return conn.getInputStream();
×
283
  }
284

285
  /**
286
   * Gets a URL as a Reader.
287
   *
288
   * @param urlString
289
   *          - the URL to get
290
   *
291
   * @return A Reader with the data from the URL
292
   *
293
   * @throws IOException
294
   *           If the resource cannot be found or read
295
   */
296
  public static Reader getUrlAsReader(String urlString) throws IOException {
297
    return new InputStreamReader(getUrlAsStream(urlString));
×
298
  }
299

300
  /**
301
   * Gets a URL as a Properties object.
302
   *
303
   * @param urlString
304
   *          - the URL to get
305
   *
306
   * @return A Properties object with the data from the URL
307
   *
308
   * @throws IOException
309
   *           If the resource cannot be found or read
310
   */
311
  public static Properties getUrlAsProperties(String urlString) throws IOException {
312
    Properties props = new Properties();
×
313
    String propfile = urlString;
×
NEW
314
    InputStream in = getUrlAsStream(propfile);
×
315
    props.load(in);
×
316
    in.close();
×
317
    return props;
×
318
  }
319

320
  /**
321
   * Loads a class.
322
   *
323
   * @param className
324
   *          - the class to load
325
   *
326
   * @return The loaded class
327
   *
328
   * @throws ClassNotFoundException
329
   *           If the class cannot be found (duh!)
330
   */
331
  public static Class classForName(String className) throws ClassNotFoundException {
332
    Class clazz = null;
1✔
333
    try {
334
      clazz = getClassLoader().loadClass(className);
1✔
335
    } catch (Exception e) {
×
336
      // Ignore. Failsafe below.
337
    }
1✔
338
    if (clazz == null) {
1!
339
      clazz = Class.forName(className);
×
340
    }
341
    return clazz;
1✔
342
  }
343

344
  /**
345
   * Creates an instance of a class.
346
   *
347
   * @param className
348
   *          - the class to create
349
   *
350
   * @return An instance of the class
351
   *
352
   * @throws ClassNotFoundException
353
   *           If the class cannot be found (duh!)
354
   * @throws InstantiationException
355
   *           If the class cannot be instantiated
356
   * @throws IllegalAccessException
357
   *           If the class is not public, or other access problems arise
358
   */
359
  public static Object instantiate(String className)
360
      throws ClassNotFoundException, InstantiationException, IllegalAccessException {
361
    return instantiate(classForName(className));
1✔
362
  }
363

364
  /**
365
   * Creates an instance of a class.
366
   *
367
   * @param clazz
368
   *          - the class to create
369
   *
370
   * @return An instance of the class
371
   *
372
   * @throws InstantiationException
373
   *           If the class cannot be instantiated
374
   * @throws IllegalAccessException
375
   *           If the class is not public, or other access problems arise
376
   */
377
  public static Object instantiate(Class clazz) throws InstantiationException, IllegalAccessException {
378
    try {
379
      return ClassInfo.getInstance(clazz).instantiateClass();
1✔
380
    } catch (Exception e) {
×
381
      // Try alternative...theoretically should fail for the exact same
382
      // reason, but in case of a weird security manager, this will help
383
      // some cases.
384
      try {
NEW
385
        return clazz.getDeclaredConstructor().newInstance();
×
NEW
386
      } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e1) {
×
387
        // Should never happen, but just in case...
NEW
388
        return null;
×
389
      }
390
    }
391
  }
392

393
  /**
394
   * Gets the class loader.
395
   *
396
   * @return the class loader
397
   */
398
  private static ClassLoader getClassLoader() {
399
    if (defaultClassLoader != null) {
1!
400
      return defaultClassLoader;
×
401
    } else {
402
      return Thread.currentThread().getContextClassLoader();
1✔
403
    }
404
  }
405

406
  /**
407
   * Gets the charset.
408
   *
409
   * @return the charset
410
   */
411
  public static Charset getCharset() {
412
    return charset;
×
413
  }
414

415
  /**
416
   * Use this method to set the Charset to be used when calling the getResourceAsReader methods. This will allow iBATIS
417
   * to function properly when the system default encoding doesn't deal well with unicode (IBATIS-340, IBATIS-349)
418
   *
419
   * @param charset
420
   *          the new charset
421
   */
422
  public static void setCharset(Charset charset) {
423
    Resources.charset = charset;
×
424
  }
×
425

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