Skip to content

Commit e8cf2cd

Browse files
authored
Merge pull request #25 from fugerit-org/hotfix/issue_23_class_helper_new_instance
Hotfix/issue 23 class helper new instance
2 parents acc22dd + 198c393 commit e8cf2cd

File tree

7 files changed

+241
-23
lines changed

7 files changed

+241
-23
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- ClassHelper.newInstance(String) no longer throws exception (#23).
13+
- fixed-config-extract, it previously updated the source xlsx.
14+
15+
### Changed
16+
17+
- Updated module fj-core-jvfs dependancy fj-daogen-maven-plugin to 1.1.6
18+
19+
## [8.2.1 and previous]
20+
21+
### Changed
22+
23+
- only the [release notes](docgen/release-notes.txt) are available.

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
Provides helper libraries for other java projects (io, configuration, etc.)
44

5-
[![Maven Central](https://img.shields.io/maven-central/v/org.fugerit.java/fj-core.svg)](https://mvnrepository.com/artifact/org.fugerit.java/fj-core)
6-
[![license](https://img.shields.io/badge/License-Apache%20License%202.0-teal.svg)](https://opensource.org/licenses/Apache-2.0)
5+
6+
[![Keep a Changelog v1.1.0 badge](https://img.shields.io/badge/changelog-Keep%20a%20Changelog%20v1.1.0-%23E05735)](CHANGELOG.md)
7+
[![Maven Central](https://img.shields.io/maven-central/v/org.fugerit.java/fj-core.svg)](https://mvnrepository.com/artifact/org.fugerit.java/fj-core)
8+
[![license](https://img.shields.io/badge/License-Apache%20License%202.0-teal.svg)](https://opensource.org/licenses/Apache-2.0)
79
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=fugerit-org_fj-lib&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=fugerit-org_fj-lib)
810

911
**Note : Starting from version 0.7+, minimum required java version changed to 1.8**

docgen/release-notes.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
(next)
2-
------------------
3-
+ [fix][fj-tool] fixed-config-extract used to update the source xlsx
4-
+ [enhancement][fj-core-jvfs] fj-daogen-maven-plugin 1.1.5 linked
5-
6-
8.2.1 (2023-08-29)
1+
8.2.1 (2023-08-29)
72
------------------
83
+ [fix][fj-core] fix TransformerXML.newSafeTransformerFactory() bug [8.2.1-rc.1]
94
+ [fix][fj-tool] fixed fixed-config-extract parsing [8.2.1-rc.1]

fj-core-jvfs/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</organization>
3131

3232
<properties>
33-
<fj-daogen-version>1.1.5</fj-daogen-version>
33+
<fj-daogen-version>1.1.6</fj-daogen-version>
3434
<build-helper-maven-plugin-version>3.4.0</build-helper-maven-plugin-version>
3535
<gen.base.dir>${project.basedir}</gen.base.dir>
3636
<generated.source.daogen>target/generated-sources/daogen</generated.source.daogen>

fj-core/src/main/java/org/fugerit/java/core/lang/helpers/ClassHelper.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
package org.fugerit.java.core.lang.helpers;
2222

2323
import java.io.InputStream;
24+
import java.lang.reflect.InvocationTargetException;
2425

26+
import org.fugerit.java.core.cfg.ConfigException;
2527
import org.fugerit.java.core.cfg.ConfigRuntimeException;
28+
import org.fugerit.java.core.util.ObjectUtils;
2629

2730
import lombok.extern.slf4j.Slf4j;
2831

@@ -50,11 +53,7 @@ private ClassHelper() {}
5053
*
5154
*/
5255
public static ClassLoader getDefaultClassLoader() {
53-
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
54-
if ( classLoader == null ) {
55-
classLoader = ClassHelper.class.getClassLoader();
56-
}
57-
return classLoader;
56+
return ObjectUtils.objectWithDefault( Thread.currentThread().getContextClassLoader() , ClassHelper.class.getClassLoader() );
5857
}
5958

6059
/**
@@ -64,30 +63,54 @@ public static ClassLoader getDefaultClassLoader() {
6463
*
6564
* @param type fully qualified name fo the class for which the new instance will be created
6665
* @return the new istance
66+
* @throws ClassNotFoundException
67+
* @throws SecurityException
68+
* @throws NoSuchMethodException
69+
* @throws InvocationTargetException
70+
* @throws IllegalArgumentException
71+
* @throws IllegalAccessException
72+
* @throws InstantiationException
6773
*/
68-
public static Object newInstance( String type ) {
74+
public static Object newInstance( String type ) throws ClassNotFoundException, NoSuchMethodException, ConfigException {
6975
Object result = null;
7076
try {
7177
ClassLoader classLoader = getDefaultClassLoader();
7278
Class<?> c = classLoader.loadClass( type );
73-
result = c.getDeclaredConstructor().newInstance();
79+
result = c.getDeclaredConstructor().newInstance();
7480
} catch (Exception e) {
75-
throw ConfigRuntimeException.convertExMethod( "newInstance" , e );
81+
if ( e instanceof ClassNotFoundException ) {
82+
throw (ClassNotFoundException)e;
83+
} else if ( e instanceof NoSuchMethodException ) {
84+
throw (NoSuchMethodException)e;
85+
} else {
86+
throw ConfigException.convertExMethod( "newInstance" , e );
87+
}
7688
}
7789
return result;
7890
}
7991

80-
public static InputStream loadFromClassLoader( Object caller, String path ) {
92+
public static InputStream loadFromClassLoader( String path, ClassLoader... cl ) {
8193
InputStream is = null;
82-
try {
83-
is = getDefaultClassLoader().getResourceAsStream( path );
84-
} catch (Exception e) {
85-
log.warn( "Failed to load from default class loader, trying caller loader ("+e+")" );
86-
is = caller.getClass().getResourceAsStream( path );
94+
if ( cl != null ) {
95+
for ( int k=0; k<cl.length && is == null; k++ ) {
96+
if ( cl[k] != null ) {
97+
is = cl[k].getResourceAsStream( path );
98+
if ( is == null ) {
99+
log.trace( "Not found on class loader {}, path {}", k, path );
100+
}
101+
}
102+
}
103+
}
104+
if ( is == null ) {
105+
is = loadFromDefaultClassLoader(path);
87106
}
88107
return is;
89108
}
90109

110+
public static InputStream loadFromClassLoader( Object caller, String path ) {
111+
return loadFromClassLoader( path, getDefaultClassLoader(), (caller != null) ? caller.getClass().getClassLoader() : null );
112+
}
113+
91114
public static InputStream loadFromDefaultClassLoader( String path ) {
92115
return getDefaultClassLoader().getResourceAsStream( path );
93116
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package test.org.fugerit.java.core.lang.helpers;
2+
3+
import java.io.InputStream;
4+
5+
import org.fugerit.java.core.cfg.ConfigException;
6+
import org.fugerit.java.core.io.StreamIO;
7+
import org.fugerit.java.core.lang.helpers.ClassHelper;
8+
import org.fugerit.java.core.lang.helpers.StringUtils;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
import lombok.Getter;
13+
import lombok.extern.slf4j.Slf4j;
14+
import test.org.fugerit.java.BasicTest;
15+
16+
@Slf4j
17+
public class TestClassHelper extends BasicTest {
18+
19+
private static final String PATH_TXT_OK = "core/test_test.txt";
20+
21+
private static final String PATH_TXT_KO = "ko_"+PATH_TXT_OK;
22+
23+
private boolean testNewInstanceWorker( String type, boolean expectedException ) {
24+
boolean ok = false;
25+
try {
26+
log.info( "try to create : {}", type );
27+
Object res = ClassHelper.newInstance( type );
28+
log.info( "object created : {}", res );
29+
ok = !expectedException && (res != null);
30+
} catch (ConfigException | ClassNotFoundException | NoSuchMethodException e) {
31+
if ( expectedException ) {
32+
log.info( "expected exception ok : {}", e.toString() );
33+
ok = true;
34+
} else {
35+
this.failEx(e);
36+
}
37+
} catch (Exception e) {
38+
this.failEx(e);
39+
}
40+
return ok;
41+
}
42+
43+
private boolean testLoadFromDefaultClassLoaderWorker( String path, boolean expectedException ) {
44+
boolean ok = false;
45+
log.info( "try to load path : {}", path );
46+
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( path ) ) {
47+
String text = StreamIO.readString( is );
48+
log.info( "found : {}", text );
49+
ok = !expectedException && ( StringUtils.isNotEmpty( text ) );
50+
} catch (Exception e) {
51+
if ( expectedException ) {
52+
log.info( "expected exception ok : {}", e.toString() );
53+
ok = true;
54+
} else {
55+
this.failEx(e);
56+
}
57+
}
58+
return ok;
59+
}
60+
61+
private boolean testLoadFromClassLoaderWorker( String path, boolean expectedException, Class<?> c ) {
62+
boolean ok = false;
63+
log.info( "try to load path : {}", path );
64+
try ( InputStream is = ClassHelper.loadFromClassLoader( c, path ) ) {
65+
String text = StreamIO.readString( is );
66+
log.info( "found : {}", text );
67+
ok = !expectedException && ( StringUtils.isNotEmpty( text ) );
68+
} catch (Exception e) {
69+
if ( expectedException ) {
70+
log.info( "expected exception ok : {}", e.toString() );
71+
ok = true;
72+
} else {
73+
this.failEx(e);
74+
}
75+
}
76+
return ok;
77+
}
78+
79+
@Test
80+
public void testNewInstanceOk() {
81+
boolean ok = this.testNewInstanceWorker( TestClassHelper.class.getName(), false );
82+
Assert.assertTrue( ok );
83+
}
84+
85+
@Test
86+
public void testNewInstanceClassNotFoundException() {
87+
boolean ok = this.testNewInstanceWorker( TestClassHelper.class.getName()+"NotExist", true );
88+
Assert.assertTrue( ok );
89+
}
90+
91+
@Test
92+
public void testNewInstanceConfigException() {
93+
boolean ok = this.testNewInstanceWorker( TestPrivateConstructor.class.getName(), true );
94+
Assert.assertTrue( ok );
95+
}
96+
97+
@Test
98+
public void testNewInstanceNoSuchMethodException() {
99+
boolean ok = this.testNewInstanceWorker( TestNoDefaultConstructor.class.getName(), true );
100+
Assert.assertTrue( ok );
101+
}
102+
103+
104+
@Test
105+
public void testLoadFromDefaultClassLoaderOk() {
106+
boolean ok = this.testLoadFromDefaultClassLoaderWorker( PATH_TXT_OK, false );
107+
Assert.assertTrue( ok );
108+
}
109+
110+
@Test
111+
public void testLoadFromClassLoaderOk1() {
112+
boolean ok = this.testLoadFromClassLoaderWorker( PATH_TXT_OK, false, TestClassHelper.class );
113+
Assert.assertTrue( ok );
114+
}
115+
116+
@Test
117+
public void testLoadFromClassLoaderOk2() {
118+
boolean ok = this.testLoadFromClassLoaderWorker( PATH_TXT_OK, false, ClassHelper.class );
119+
Assert.assertTrue( ok );
120+
}
121+
122+
@Test
123+
public void testLoadFromClassLoaderOk3() {
124+
boolean ok = this.testLoadFromClassLoaderWorker( PATH_TXT_OK, false, null );
125+
Assert.assertTrue( ok );
126+
}
127+
128+
@Test
129+
public void testLoadFromDefaultClassLoaderko() {
130+
boolean ok = this.testLoadFromDefaultClassLoaderWorker( PATH_TXT_KO, true );
131+
Assert.assertTrue( ok );
132+
}
133+
134+
@Test
135+
public void testLoadFromClassLoaderko1() {
136+
boolean ok = this.testLoadFromClassLoaderWorker( PATH_TXT_KO, true, TestClassHelper.class );
137+
Assert.assertTrue( ok );
138+
}
139+
140+
@Test
141+
public void testLoadFromClassLoaderKo2() {
142+
boolean ok = this.testLoadFromClassLoaderWorker( PATH_TXT_KO, true, ClassHelper.class );
143+
Assert.assertTrue( ok );
144+
}
145+
146+
@Test
147+
public void testToFullClassName() {
148+
String name = ClassHelper.toFullClassName( new TestClassHelper() );
149+
log.info( "tesToFullClassName {}", name );
150+
Assert.assertEquals( TestClassHelper.class.getName(), name );
151+
}
152+
153+
@Test
154+
public void testToSimpleClassName() {
155+
String name = ClassHelper.toSimpleClassName( new TestClassHelper() );
156+
log.info( "testToSimpleClassName {}", name );
157+
Assert.assertEquals( TestClassHelper.class.getSimpleName() , name );
158+
}
159+
160+
}
161+
162+
class TestPrivateConstructor {
163+
164+
private TestPrivateConstructor() {}
165+
166+
}
167+
168+
class TestNoDefaultConstructor {
169+
170+
@Getter private boolean test;
171+
172+
public TestNoDefaultConstructor( boolean test ) { this.test = test; }
173+
174+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test TXT

0 commit comments

Comments
 (0)