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

mybatis / ibatis-2 / 575

26 May 2025 06:27PM UTC coverage: 65.515% (-0.02%) from 65.532%
575

push

github

web-flow
Merge pull request #285 from hazendaz/master

Use declared constructor to call new instance

1611 of 2820 branches covered (57.13%)

0 of 4 new or added lines in 2 files covered. (0.0%)

5082 of 7757 relevant lines covered (65.52%)

0.66 hits per line

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

62.67
/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.*;
21
import java.lang.reflect.InvocationTargetException;
22
import java.net.URL;
23
import java.net.URLConnection;
24
import java.nio.charset.Charset;
25
import java.nio.file.Path;
26
import java.util.Properties;
27

28
/**
29
 * A class to simplify access to resources through the classloader.
30
 */
31
public class Resources extends Object {
32

33
  /** The default class loader. */
34
  private static ClassLoader defaultClassLoader;
35

36
  /**
37
   * Charset to use when calling getResourceAsReader. null means use the system default.
38
   */
39
  private static Charset charset;
40

41
  /**
42
   * Instantiates a new resources.
43
   */
44
  private Resources() {
45
  }
46

47
  /**
48
   * Returns the default classloader (may be null).
49
   *
50
   * @return The default classloader
51
   */
52
  public static ClassLoader getDefaultClassLoader() {
53
    return defaultClassLoader;
1✔
54
  }
55

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

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

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

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

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

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

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

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

207
    return reader;
1✔
208
  }
209

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

231
    return reader;
×
232
  }
233

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

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

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

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

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

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

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

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

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

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

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

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