Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import org.jetbrains.annotations.NotNull;

/**
* {@code AbstractModule} provides a base class for annotation based dependency injection
Expand Down Expand Up @@ -117,45 +119,44 @@ public final <T> T get(Class<T> iface, String name) {
}
}

@SuppressWarnings("unchecked")
private <T> T createInstance(Class<? extends T> implementation) {
Constructor<?> constructor = null;
for (Constructor<?> c : implementation.getConstructors()) {
if (c.isAnnotationPresent(Inject.class)) {
constructor = c;
break;
}
var constructor =
Arrays.stream(implementation.getConstructors())
.filter(c -> c.isAnnotationPresent(Inject.class))
.findFirst()
.orElseGet(() -> fallbackToDefaultConstructor(implementation));

var args = getConstructorParameters(constructor);
try {
//noinspection unchecked
return (T) constructor.newInstance(args);
// TODO : populate fields as well?!?
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

// fallback to default constructor
if (constructor == null) {
try {
constructor = implementation.getConstructor();
} catch (SecurityException | NoSuchMethodException e) {
throw new RuntimeException(e);
private Object @NotNull [] getConstructorParameters(Constructor<?> constructor) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
var args = new Object[parameterTypes.length];
for (var i = 0; i < parameterTypes.length; i++) {
var named = getNamedAnnotation(parameterAnnotations[i]);
if (named != null) {
args[i] = get(parameterTypes[i], named.value());
} else {
args[i] = get(parameterTypes[i]);
}
}
return args;
}

if (constructor != null) {
var args = new Object[constructor.getParameterTypes().length];
for (var i = 0; i < constructor.getParameterTypes().length; i++) {
var named = getNamedAnnotation(constructor.getParameterAnnotations()[i]);
if (named != null) {
args[i] = get(constructor.getParameterTypes()[i], named.value());
} else {
args[i] = get(constructor.getParameterTypes()[i]);
}
}
try {
return (T) constructor.newInstance(args);
// TODO : populate fields as well?!?
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}

} else {
throw new IllegalArgumentException(
"Got no annotated constructor for " + implementation.getName());
private static <T> @NotNull Constructor<? extends T> fallbackToDefaultConstructor(
Class<? extends T> implementation) {
try {
return implementation.getConstructor();
} catch (SecurityException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.lang.annotation.Annotation;

/**
* {@code AnnotationHelper} defines a interface to provide custom annotation processing for {@link
* {@code AnnotationHelper} defines an interface to provide custom annotation processing for {@link
* TypeFactory}.
*
* @author dyorgio
Expand All @@ -36,7 +36,7 @@ public interface AnnotationHelper {
* Get specific object that will be used as part of type cache key.
*
* @param annotation Annotation instance.
* @return Any object, normally a annotation param. Can be {@code null}.
* @return Any object, normally an annotation param. Can be {@code null}.
*/
Object getCustomKey(Annotation annotation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ public class BeanSerializer implements Serializer {
public static final boolean DEFAULT_PROPERTY_ANNOTATIONS = true;

private static final Function<Property, Parameter> propertyToParameter =
new Function<>() {
@Override
public Parameter apply(Property input) {
return new Parameter(input.getName(), input.getType());
}
};
input -> new Parameter(input.getName(), input.getType());
private final Class<? extends Annotation> generatedAnnotationClass;

private final boolean propertyAnnotations;
Expand Down Expand Up @@ -242,7 +237,7 @@ protected void addFullConstructor(EntityType model, CodeWriter writer) throws IO
writer.end();

// full constructor
writer.beginConstructor(model.getProperties(), propertyToParameter::apply);
writer.beginConstructor(model.getProperties(), propertyToParameter);
for (Property property : model.getProperties()) {
writer.line("this.", property.getEscapedName(), " = ", property.getEscapedName(), ";");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -169,7 +168,7 @@ private String normalizePackage(String packageName, SchemaAndTable schemaAndTabl
protected Property createProperty(
EntityType classModel, String normalizedColumnName, String propertyName, Type typeModel) {
return new Property(
classModel, propertyName, propertyName, typeModel, Collections.<String>emptyList(), false);
classModel, propertyName, propertyName, typeModel, Collections.emptyList(), false);
}

/**
Expand All @@ -184,8 +183,8 @@ public void export(DatabaseMetaData md) throws SQLException {

if (config.getNamingStrategyClass() != null) {
try {
namingStrategy = config.getNamingStrategyClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
namingStrategy = config.getNamingStrategyClass().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
} else {
Expand Down Expand Up @@ -308,8 +307,8 @@ private void configureModule() {
serializer = new BeanSerializer();
} else {
try {
serializer = config.getBeanSerializerClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
serializer = config.getBeanSerializerClass().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -338,8 +337,11 @@ private void configureModule() {
for (CustomType cl : config.getCustomTypes()) {
try {
configuration.register(
(com.querydsl.sql.types.Type) Class.forName(cl.getClassName()).newInstance());
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
Class.forName(cl.getClassName())
.asSubclass(com.querydsl.sql.types.Type.class)
.getDeclaredConstructor()
.newInstance());
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -361,9 +363,7 @@ private void configureModule() {
}

if (config.getColumnComparatorClass() != null) {
module.bind(
SQLCodegenModule.COLUMN_COMPARATOR,
config.getColumnComparatorClass().asSubclass(Comparator.class));
module.bind(SQLCodegenModule.COLUMN_COMPARATOR, config.getColumnComparatorClass());
}

if (config.getSerializerClass() != null) {
Expand Down Expand Up @@ -596,7 +596,7 @@ private void write(Serializer serializer, File targetFile, EntityType type) thro
var generate = true;
var bytes = w.toString().getBytes(config.getSourceEncoding());
if (targetFile.exists() && targetFile.length() == bytes.length) {
var str = new String(Files.readAllBytes(targetFile.toPath()), config.getSourceEncoding());
var str = Files.readString(targetFile.toPath(), config.getSourceEncoding());
if (str.equals(w.toString())) {
generate = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import com.querydsl.codegen.Property;
import com.querydsl.sql.ColumnMetadata;
import java.util.Arrays;
import java.util.Comparator;

/** Compares {@link Property} instances based on their ordinal position in the table */
Expand All @@ -27,13 +26,12 @@ public OrdinalPositionComparator() {

@Override
public int compare(Property property1, Property property2) {
Integer comparison = null;
for (Property property : Arrays.asList(property1, property2)) {
var data = property.getData();
var columnMetadata = (ColumnMetadata) data.get("COLUMN");
var index = columnMetadata.getIndex();
comparison = comparison == null ? index : comparison - index;
}
return comparison;
return getOrdinalPosition(property1) - getOrdinalPosition(property2);
}

private static int getOrdinalPosition(Property property) {
var data = property.getData();
var columnMetadata = (ColumnMetadata) data.get("COLUMN");
return columnMetadata.getIndex();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,36 +191,22 @@ public class AntMetaDataExporter extends Task implements MetadataExporterConfig
private List<RenameMapping> renameMappings = new ArrayList<>();

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void execute() {
if (targetFolder == null) {
throw new BuildException("targetFolder is a mandatory property");
}

Connection dbConn = null;
try {
Class.forName(jdbcDriver).newInstance();

dbConn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
Class.forName(jdbcDriver).getDeclaredConstructor().newInstance();
} catch (RuntimeException | ReflectiveOperationException e) {
throw new BuildException(e);
}

try (Connection dbConn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword)) {
var exporter = new MetaDataExporter(this);

exporter.export(dbConn.getMetaData());

} catch (RuntimeException
| SQLException
| ClassNotFoundException
| IllegalAccessException
| InstantiationException e) {
} catch (RuntimeException | SQLException e) {
throw new BuildException(e);
} finally {
if (dbConn != null) {
try {
dbConn.close();
} catch (SQLException e2) {
throw new BuildException(e2);
}
}
}
}

Expand Down Expand Up @@ -343,7 +329,7 @@ public Class<? extends NamingStrategy> getNamingStrategyClass() {
return null;
}
try {
return (Class<? extends NamingStrategy>) Class.forName(namingStrategyClass);
return Class.forName(namingStrategyClass).asSubclass(NamingStrategy.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Expand All @@ -357,7 +343,7 @@ public void setNamingStrategyClass(String namingStrategyClass) {
public Class<? extends BeanSerializer> getBeanSerializerClass() {
if (exportBeans && beanSerializerClass != null) {
try {
return (Class<? extends BeanSerializer>) Class.forName(beanSerializerClass);
return Class.forName(beanSerializerClass).asSubclass(BeanSerializer.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Expand All @@ -375,7 +361,7 @@ public Class<? extends Serializer> getSerializerClass() {
return null;
}
try {
return (Class<? extends Serializer>) Class.forName(serializerClass);
return Class.forName(serializerClass).asSubclass(Serializer.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -581,7 +567,8 @@ public Class<? extends Comparator<Property>> getColumnComparatorClass() {
return null;
}
try {
return (Class<? extends Comparator<Property>>) Class.forName(columnComparatorClass);
return (Class<? extends Comparator<Property>>)
Class.forName(columnComparatorClass).asSubclass(Comparator.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ public void apply(Configuration configuration) {
try {
Class<?> typeClass = Class.forName(type);
if (Type.class.isAssignableFrom(typeClass)) {
configuration.register(table, column, (Type<?>) typeClass.newInstance());
configuration.register(
table, column, (Type<?>) typeClass.getDeclaredConstructor().newInstance());
} else {
configuration.register(table, column, typeClass);
}
} catch (ClassNotFoundException e) {
configuration.register(table, column, new com.querydsl.sql.types.SimpleType(type));
} catch (IllegalAccessException | InstantiationException e) {
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
Expand Down
Loading