Skip to content

Commit

Permalink
GraalVM / Quarkus native build support - Use Thread.currentThread().g…
Browse files Browse the repository at this point in the history
…etContextClassLoader() when calling Class.forName() vladmihalcea#177
  • Loading branch information
marceloverdijk authored and vladmihalcea committed Jan 26, 2020
1 parent aeb9db9 commit 0937afc
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ private ReflectionUtils() {
* @return new Java {@link Object} of the provided type
*/
public static <T> T newInstance(String className) {
try {
Class clazz = Class.forName(className);
return newInstance(clazz);
} catch (ClassNotFoundException e) {
throw handleException(e);
}
Class clazz = getClass(className);
return newInstance(clazz);
}

/**
Expand Down Expand Up @@ -430,7 +426,7 @@ public static <T> T invokeStaticMethod(Method method, Object... parameters) {
@SuppressWarnings("unchecked")
public static <T> Class<T> getClass(String className) {
try {
return (Class<T>) Class.forName(className);
return (Class<T>) Class.forName(className, false, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw handleException(e);
}
Expand All @@ -447,7 +443,7 @@ public static <T> Class<T> getClass(String className) {
@SuppressWarnings("unchecked")
public static <T> Class<T> getClassOrNull(String className) {
try {
return (Class<T>) Class.forName(className);
return (Class<T>) getClass(className);
} catch (Exception e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public DataSource dataSource() {

@Override
public Class<? extends DataSource> dataSourceClassName() {
try {
return (Class<? extends DataSource>) Class.forName("oracle.jdbc.pool.OracleDataSource");
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
return ReflectionUtils.getClass("oracle.jdbc.pool.OracleDataSource");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ private ReflectionUtils() {
* @return new Java {@link Object} of the provided type
*/
public static <T> T newInstance(String className) {
try {
Class clazz = Class.forName(className);
return newInstance(clazz);
} catch (ClassNotFoundException e) {
throw handleException(e);
}
Class clazz = getClass(className);
return newInstance(clazz);
}

/**
Expand Down Expand Up @@ -430,7 +426,7 @@ public static <T> T invokeStaticMethod(Method method, Object... parameters) {
@SuppressWarnings("unchecked")
public static <T> Class<T> getClass(String className) {
try {
return (Class<T>) Class.forName(className);
return (Class<T>) Class.forName(className, false, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw handleException(e);
}
Expand All @@ -447,7 +443,7 @@ public static <T> Class<T> getClass(String className) {
@SuppressWarnings("unchecked")
public static <T> Class<T> getClassOrNull(String className) {
try {
return (Class<T>) Class.forName(className);
return (Class<T>) getClass(className);
} catch (Exception e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public DataSource dataSource() {

@Override
public Class<? extends DataSource> dataSourceClassName() {
try {
return (Class<? extends DataSource>) Class.forName("oracle.jdbc.pool.OracleDataSource");
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
return ReflectionUtils.getClass("oracle.jdbc.pool.OracleDataSource");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ private ReflectionUtils() {
* @return new Java {@link Object} of the provided type
*/
public static <T> T newInstance(String className) {
try {
Class clazz = Class.forName(className);
return newInstance(clazz);
} catch (ClassNotFoundException e) {
throw handleException(e);
}
Class clazz = getClass(className);
return newInstance(clazz);
}

/**
Expand Down Expand Up @@ -430,7 +426,7 @@ public static <T> T invokeStaticMethod(Method method, Object... parameters) {
@SuppressWarnings("unchecked")
public static <T> Class<T> getClass(String className) {
try {
return (Class<T>) Class.forName(className);
return (Class<T>) Class.forName(className, false, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw handleException(e);
}
Expand All @@ -447,7 +443,7 @@ public static <T> Class<T> getClass(String className) {
@SuppressWarnings("unchecked")
public static <T> Class<T> getClassOrNull(String className) {
try {
return (Class<T>) Class.forName(className);
return (Class<T>) getClass(className);
} catch (Exception e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public DataSource dataSource() {

@Override
public Class<? extends DataSource> dataSourceClassName() {
try {
return (Class<? extends DataSource>) Class.forName("oracle.jdbc.pool.OracleDataSource");
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
return ReflectionUtils.getClass("oracle.jdbc.pool.OracleDataSource");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ private ReflectionUtils() {
* @return new Java {@link Object} of the provided type
*/
public static <T> T newInstance(String className) {
try {
Class clazz = Class.forName(className);
return newInstance(clazz);
} catch (ClassNotFoundException e) {
throw handleException(e);
}
Class clazz = getClass(className);
return newInstance(clazz);
}

/**
Expand Down Expand Up @@ -456,7 +452,7 @@ public static <T> T invokeStaticMethod(Method method, Object... parameters) {
@SuppressWarnings("unchecked")
public static <T> Class<T> getClass(String className) {
try {
return (Class<T>) Class.forName(className);
return (Class<T>) Class.forName(className, false, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw handleException(e);
}
Expand All @@ -473,7 +469,7 @@ public static <T> Class<T> getClass(String className) {
@SuppressWarnings("unchecked")
public static <T> Class<T> getClassOrNull(String className) {
try {
return (Class<T>) Class.forName(className);
return (Class<T>) getClass(className);
} catch (Exception e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public DataSource dataSource() {

@Override
public Class<? extends DataSource> dataSourceClassName() {
try {
return (Class<? extends DataSource>) Class.forName("oracle.jdbc.pool.OracleDataSource");
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
return ReflectionUtils.getClass("oracle.jdbc.pool.OracleDataSource");
}

@Override
Expand Down

0 comments on commit 0937afc

Please sign in to comment.