Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Change Null Check to Optional Class #103

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -55,7 +55,7 @@ public ConstructorExpression<?> getConstructorExpression(Class<?> type, Relation
columnNameToExpression,
embeddedConstructorParameterNameToPath,
parameter))
.collect(Collectors.toList());
.toList();

Class<?>[] paramTypes = pairs.stream().map(ParameterAndExpressionPair::parameterType).toArray(Class[]::new);
Expression<?>[] expressions = pairs.stream().map(ParameterAndExpressionPair::expression).toArray(Expression[]::new);
Expand Down Expand Up @@ -133,24 +133,20 @@ private Optional<Class<?>> getEmbeddedType(Class<?> type, Parameter parameter) {
}

private Constructor<?> getConstructor(Class<?> type) {
PreferredConstructor<?, ?> preferredConstructor = PreferredConstructorDiscoverer.discover(type);

if (preferredConstructor == null) {
return null;
}

return preferredConstructor.getConstructor();
return Optional.ofNullable(PreferredConstructorDiscoverer.discover(type))
.map(PreferredConstructor::getConstructor)
.orElseGet(null);
}

public RelationalPathBase<?> getRelationalPathBaseFromQueryRepositoryClass(Class<?> repositoryInterface) {

var entityType = ResolvableType.forClass(repositoryInterface)
.as(repositoryTargetType)
.getGeneric(0)
.resolve();
if (entityType == null) {
throw new IllegalArgumentException("Could not resolve query class for " + repositoryInterface);
}
var entityType = Optional.ofNullable(
ResolvableType.forClass(repositoryInterface)
.as(repositoryTargetType)
.getGeneric(0)
.resolve()
)
.orElseThrow(() -> new IllegalArgumentException("Could not resolve query class for " + repositoryInterface));

return getRelationalPathBaseFromQueryClass(getQueryClass(entityType));
}
Expand All @@ -166,11 +162,8 @@ private Class<?> getQueryClass(Class<?> entityType) {

private RelationalPathBase<?> getRelationalPathBaseFromQueryClass(Class<?> queryClass) {
var fieldName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, queryClass.getSimpleName().substring(1));
var field = ReflectionUtils.findField(queryClass, fieldName);

if (field == null) {
throw new IllegalArgumentException("Did not find a static field of the same type in " + queryClass);
}
var field = Optional.ofNullable(ReflectionUtils.findField(queryClass, fieldName))
.orElseThrow(() -> new IllegalArgumentException("Did not find a static field of the same type in " + queryClass));

return (RelationalPathBase<?>) ReflectionUtils.getField(field, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.common.base.CaseFormat;
Expand Down Expand Up @@ -65,7 +64,7 @@ public EntityType getEntityType(TypeMirror typeMirror, boolean deep) {
var element = types.asElement(typeMirror);
var entityType = super.getEntityType(typeMirror, deep);
var embeddedlessProperties =
entityType.getProperties()
Objects.requireNonNull(entityType).getProperties()
xeounxzxu marked this conversation as resolved.
Show resolved Hide resolved
.stream()
.flatMap(property -> {
if (Embeddeds.isEmbedded(configuration, element, property)) {
Expand All @@ -74,12 +73,12 @@ public EntityType getEntityType(TypeMirror typeMirror, boolean deep) {

return Stream.of(property);
})
.collect(Collectors.toList());
.toList();
entityType.getProperties().clear();
entityType.getProperties().addAll(embeddedlessProperties);
entityType.getPropertyNames().clear();
entityType.getPropertyNames()
.addAll(embeddedlessProperties.stream().map(Property::getName).collect(Collectors.toList()));
.addAll(embeddedlessProperties.stream().map(Property::getName).toList());
updateModel(element, entityType);
return entityType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Objects;
import java.util.Optional;

import com.querydsl.sql.types.Type;
Expand All @@ -18,7 +19,7 @@ public int[] getSQLTypes() {

@Override
public String getLiteral(AggregateReference value) {
return Optional.ofNullable(value).map(AggregateReference::getId).map(Object::toString).orElse(null);
return Optional.ofNullable(value).map(AggregateReference::getId).map(Object::toString).orElseGet(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ class QuerydslParameterBinder {
}

private boolean resolve(SQLTemplates sqlTemplates) {

if(sqlTemplates instanceof MySQLTemplates) {
return true;
}

return false;
return sqlTemplates instanceof MySQLTemplates;
xeounxzxu marked this conversation as resolved.
Show resolved Hide resolved
}

DatabaseClient.GenericExecuteSpec bind(DatabaseClient databaseClient, List<Object> bindings, String sql) {
Expand Down
Loading