Skip to content

Commit cd2b283

Browse files
committed
fixed constructor problem of ValueWrapper in Term::wrapConstant
1 parent 9708adf commit cd2b283

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

src/main/java/org/dynapi/squirtle/core/Utils.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,21 @@ public static void validate(Object[] args, Throwable exc, Class<?> type) throws
7878
* @param initArgs arguments to pass to the class
7979
* @return instance of {@code type}
8080
*/
81-
public static<T> T newInstance(Class<T> type, Object... initArgs) {
81+
public static<T> T newInstance(Class<T> type, Object[] initArgs) {
82+
Class<?>[] argTypes = (Class<?>[]) Arrays.stream(initArgs).map(Object::getClass).toList().toArray(new Class[0]);
83+
return newInstance(type, initArgs, argTypes);
84+
}
85+
86+
/**
87+
*
88+
* @param type class to instantiate
89+
* @param initArgs arguments to pass to the class
90+
* @param signature signature of the constructor if initArgs classes don't match
91+
* @return instance of {@code type}
92+
*/
93+
public static<T> T newInstance(Class<T> type, Object[] initArgs, Class<?>[] signature) {
8294
try {
83-
Class<?>[] argTypes = (Class<?>[]) Arrays.stream(initArgs).map(Object::getClass).toList().toArray(new Class[0]);
84-
return type.getConstructor(argTypes).newInstance(initArgs);
95+
return type.getConstructor(signature).newInstance(initArgs);
8596
} catch (Exception e) {
8697
String argsString = String.join(", ", Arrays.stream(initArgs).map(Object::toString).toList());
8798
String errorMessage = String.format("Can't instantiate %s(%s) because %s (%s)", type.getCanonicalName(), argsString, e.getClass().getSimpleName(), e.getMessage());

src/main/java/org/dynapi/squirtle/core/queries/QueryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public QueryBuilder set(String fieldName, Objects value) {
400400
return set(new Field(null, fieldName, null), value);
401401
}
402402
public QueryBuilder set(Field field, Objects value) {
403-
updates.add(new UpdateEntry(field, Utils.newInstance(wrapperClass, null, value)));
403+
updates.add(new UpdateEntry(field, Utils.newInstance(wrapperClass, new Object[]{ null, value})));
404404
return this;
405405
}
406406

src/main/java/org/dynapi/squirtle/core/queries/Table.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ public int hashCode() {
111111
}
112112

113113
public QueryBuilder select(Object... terms) {
114-
return Utils.newInstance(queryClass, this).select(List.of(terms));
114+
return Utils.newInstance(queryClass, new Object[]{ this }).select(List.of(terms));
115115
}
116116

117117
public QueryBuilder update() {
118-
return Utils.newInstance(queryClass, this).update(this);
118+
return Utils.newInstance(queryClass, new Object[]{ this }).update(this);
119119
}
120120

121121
public QueryBuilder insert(Object... terms) {
122-
return Utils.newInstance(queryClass, this).into(this).insert(terms);
122+
return Utils.newInstance(queryClass, new Object[]{ this }).into(this).insert(terms);
123123
}
124124
}

src/main/java/org/dynapi/squirtle/core/terms/Term.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static Term wrapConstant(Object value, Class<? extends ValueWrapper> wrap
6969

7070
if (wrapperClass == null)
7171
wrapperClass = ValueWrapper.class;
72-
return Utils.newInstance(wrapperClass, value);
72+
return Utils.newInstance(wrapperClass, new Object[]{ value }, new Class<?>[] { Object.class });
7373
}
7474

7575
public static Node wrapJson(Object value) {
@@ -86,7 +86,7 @@ public static Node wrapJson(Object value, Class<? extends ValueWrapper> wrapperC
8686
if (value instanceof Interval interval)
8787
return interval;
8888
if (value instanceof String || value instanceof Integer || value instanceof Boolean)
89-
return Utils.newInstance(wrapperClass, value);
89+
return Utils.newInstance(wrapperClass, new Object[]{ value });
9090

9191
return new JSON(null, value);
9292
}

0 commit comments

Comments
 (0)