Skip to content

Commit

Permalink
Add generics to the AdhocObjectFactory.getJavaClass() method
Browse files Browse the repository at this point in the history
  • Loading branch information
stariy95 committed Feb 28, 2024
1 parent b0ab8e4 commit 69baef2
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Except
throw new IllegalArgumentException("'nodeDescriptor' contains no datasource descriptor");
}

Driver driver = (Driver)objectFactory.getJavaClass(dataSourceDescriptor.getJdbcDriver()).getDeclaredConstructor().newInstance();
Driver driver = objectFactory.<Driver>getJavaClass(dataSourceDescriptor.getJdbcDriver()).getDeclaredConstructor().newInstance();
return new DriverDataSource(driver, dataSourceDescriptor.getDataSourceUrl(), dataSourceDescriptor.getUserName(),
dataSourceDescriptor.getPassword());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ public interface AdhocObjectFactory {
*
* @since 4.0
*/
Class<?> getJavaClass(String className);
<T> Class<? extends T> getJavaClass(String className);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public <T> T newInstance(Class<? super T> superType, String className) {

T instance;
try {
Provider<T> provider0 = new ConstructorInjectingProvider<T>(type, (DefaultInjector) injector);
Provider<T> provider1 = new FieldInjectingProvider<T>(provider0, (DefaultInjector) injector);
Provider<T> provider0 = new ConstructorInjectingProvider<>(type, (DefaultInjector) injector);
Provider<T> provider1 = new FieldInjectingProvider<>(provider0, (DefaultInjector) injector);
instance = provider1.get();
} catch (Exception e) {
throw new DIRuntimeException("Error creating instance of class %s of type %s", e, className,
Expand All @@ -76,53 +76,53 @@ public <T> T newInstance(Class<? super T> superType, String className) {
return instance;
}

@SuppressWarnings("unchecked")
@Override
public Class<?> getJavaClass(String className) {

// is there a better way to get array class from string name?

if (className == null) {
throw new NullPointerException("Null class name");
}

ClassLoader classLoader = classLoaderManager.getClassLoader(className.replace('.', '/'));

// use custom logic on failure only, assuming primitives and arrays are
// not that common
// use custom logic on failure only, assuming primitives and arrays are not that common
try {
return Class.forName(className, true, classLoader);
} catch (ClassNotFoundException e) {
if (!className.endsWith("[]")) {
if ("byte".equals(className)) {
return Byte.TYPE;
} else if ("int".equals(className)) {
return Integer.TYPE;
} else if ("short".equals(className)) {
return Short.TYPE;
} else if ("char".equals(className)) {
return Character.TYPE;
} else if ("double".equals(className)) {
return Double.TYPE;
} else if ("long".equals(className)) {
return Long.TYPE;
} else if ("float".equals(className)) {
return Float.TYPE;
} else if ("boolean".equals(className)) {
return Boolean.TYPE;
} else if ("void".equals(className)) {
return Void.TYPE;
}
// try inner class often specified with "." instead of $
else {
int dot = className.lastIndexOf('.');
if (dot > 0 && dot + 1 < className.length()) {
className = className.substring(0, dot) + "$" + className.substring(dot + 1);
try {
return Class.forName(className, true, classLoader);
} catch (ClassNotFoundException nestedE) {
// ignore, throw the original exception...
switch (className) {
case "byte":
return Byte.TYPE;
case "int":
return Integer.TYPE;
case "short":
return Short.TYPE;
case "char":
return Character.TYPE;
case "double":
return Double.TYPE;
case "long":
return Long.TYPE;
case "float":
return Float.TYPE;
case "boolean":
return Boolean.TYPE;
case "void":
return Void.TYPE;

// try inner class often specified with "." instead of $
default:
int dot = className.lastIndexOf('.');
if (dot > 0 && dot + 1 < className.length()) {
className = className.substring(0, dot) + "$" + className.substring(dot + 1);
try {
return Class.forName(className, true, classLoader);
} catch (ClassNotFoundException nestedE) {
// ignore, throw the original exception...
}
}
}
break;
}

throw new DIRuntimeException("Invalid class: '%s'", e, className);
Expand All @@ -135,22 +135,23 @@ public Class<?> getJavaClass(String className) {
// TODO: support for multi-dim arrays
className = className.substring(0, className.length() - 2);

if ("byte".equals(className)) {
return byte[].class;
} else if ("int".equals(className)) {
return int[].class;
} else if ("long".equals(className)) {
return long[].class;
} else if ("short".equals(className)) {
return short[].class;
} else if ("char".equals(className)) {
return char[].class;
} else if ("double".equals(className)) {
return double[].class;
} else if ("float".equals(className)) {
return float[].class;
} else if ("boolean".equals(className)) {
return boolean[].class;
switch (className) {
case "byte":
return byte[].class;
case "int":
return int[].class;
case "long":
return long[].class;
case "short":
return short[].class;
case "char":
return char[].class;
case "double":
return double[].class;
case "float":
return float[].class;
case "boolean":
return boolean[].class;
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,14 +808,15 @@ class SingleEmbeddableConversionStrategy extends ObjectConversionStrategy<DataRo
List<EmbeddableObject> convert(List<DataRow> mainRows) {
EmbeddableResultSegment resultSegment = (EmbeddableResultSegment) metadata.getResultSetMapping().get(0);
Embeddable embeddable = resultSegment.getEmbeddable();
Class<?> embeddableClass = objectFactory.getJavaClass(embeddable.getClassName());
Class<? extends EmbeddableObject> embeddableClass = objectFactory.getJavaClass(embeddable.getClassName());
List<EmbeddableObject> result = new ArrayList<>(mainRows.size());
mainRows.forEach(dataRow -> {
EmbeddableObject eo;
try {
eo = (EmbeddableObject) embeddableClass.getDeclaredConstructor().newInstance();
eo = embeddableClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new CayenneRuntimeException("Unable to materialize embeddable '%s'", e, embeddable.getClassName());
throw new CayenneRuntimeException("Unable to materialize embeddable '%s'", e,
embeddable.getClassName());
}
dataRow.forEach(eo::writePropertyDirectly);
result.add(eo);
Expand Down Expand Up @@ -948,8 +949,7 @@ private List<PrefetchProcessorNode> doInPlaceConversion(List<Object[]> result) {
} else if (mapping instanceof EmbeddableResultSegment) {
EmbeddableResultSegment resultSegment = (EmbeddableResultSegment) mapping;
Embeddable embeddable = resultSegment.getEmbeddable();
@SuppressWarnings("unchecked")
Class<? extends EmbeddableObject> embeddableClass = (Class<? extends EmbeddableObject>) objectFactory
Class<? extends EmbeddableObject> embeddableClass = objectFactory
.getJavaClass(embeddable.getClassName());
try {
Constructor<? extends EmbeddableObject> declaredConstructor = embeddableClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Except
UnmanagedPoolingDataSource.MAX_QUEUE_WAIT_DEFAULT);
String validationQuery = properties.get(Constants.JDBC_VALIDATION_QUERY_PROPERTY);

Driver driver = (Driver)objectFactory.getJavaClass(driverClass).getDeclaredConstructor().newInstance();
Driver driver = objectFactory.<Driver>getJavaClass(driverClass).getDeclaredConstructor().newInstance();
return DataSourceBuilder.url(url).driver(driver).userName(username).password(password)
.pool(minConnections, maxConnections).maxQueueWaitTime(maxQueueWaitTime)
.validationQuery(validationQuery).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Except
long maxQueueWaitTime = properties
.getLong(Constants.JDBC_MAX_QUEUE_WAIT_TIME, UnmanagedPoolingDataSource.MAX_QUEUE_WAIT_DEFAULT);

Driver driver = (Driver)objectFactory.getJavaClass(descriptor.getJdbcDriver())
Driver driver = objectFactory.<Driver>getJavaClass(descriptor.getJdbcDriver())
.getDeclaredConstructor().newInstance();

return DataSourceBuilder.url(descriptor.getDataSourceUrl())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ public void nodeCreated(Object nodeId) {
}

Persistent persistent;
Class<?> javaClass = context.getEntityResolver().getObjectFactory().getJavaClass(entity.getJavaClassName());
Class<? extends Persistent> javaClass = context.getEntityResolver().getObjectFactory()
.getJavaClass(entity.getJavaClassName());
try {
persistent = (Persistent) javaClass.getDeclaredConstructor().newInstance();
persistent = javaClass.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new CayenneRuntimeException("Error instantiating object.", ex);
}
Expand Down

0 comments on commit 69baef2

Please sign in to comment.