Skip to content

Commit

Permalink
🎨 字符串 拼接 使用 formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
TAKETODAY committed Apr 9, 2024
1 parent 4a213db commit 86b8b51
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ public final PropertyAccessor obtainAccessor() {
protected PropertyAccessor createAccessor() {
Field field = getField();
if (field == null) {
return PropertyAccessor.fromMethod(readMethod, writeMethod);
return PropertyAccessor.forMethod(readMethod, writeMethod);
}
return PropertyAccessor.fromField(field, readMethod, writeMethod);
return PropertyAccessor.forField(field, readMethod, writeMethod);
}

public PropertyAccessor getPropertyAccessor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class FieldBeanProperty extends BeanProperty {

@Override
protected PropertyAccessor createAccessor() {
return PropertyAccessor.fromField(field);
return PropertyAccessor.forField(field);
}

protected TypeDescriptor createDescriptor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static class PropertyTestBean {
@Test
void testProperty() throws IllegalAccessException {
Field field = ReflectionUtils.findField(PropertyTestBean.class, "value");
PropertyAccessor propertyAccessor = PropertyAccessor.fromField(field);
PropertyAccessor propertyAccessor = PropertyAccessor.forField(field);

field.trySetAccessible();

Expand Down
21 changes: 11 additions & 10 deletions today-core/src/main/java/cn/taketoday/core/AntPathMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,14 @@ public Map<String, String> extractUriTemplateVariables(String pattern, String pa
if (doMatch(pattern, path, true, variables)) {
return variables;
}
throw new IllegalStateException("Pattern \"" + pattern + "\" is not a match for \"" + path + "\"");
throw new IllegalStateException("Pattern \"%s\" is not a match for \"%s\"".formatted(pattern, path));
}

@Override
public String[] extractVariables(String pattern, String path) {
final String[] variables = getStringMatcher(pattern).extractVariables(path);
if (variables == null) {
throw new IllegalStateException("Pattern \"" + pattern + "\" is not a match for \"" + path + "\"");
throw new IllegalStateException("Pattern \"%s\" is not a match for \"%s\"".formatted(pattern, path));
}
return variables;
}
Expand Down Expand Up @@ -713,7 +713,7 @@ public String combine(final String pattern1, final String pattern2) {
final boolean ext1All = (ext1.equals(".*") || ext1.isEmpty());
final boolean ext2All = (ext2.equals(".*") || ext2.isEmpty());
if (!ext1All && !ext2All) {
throw new IllegalArgumentException("Cannot combine patterns: " + pattern1 + " vs " + pattern2);
throw new IllegalArgumentException("Cannot combine patterns: %s vs %s".formatted(pattern1, pattern2));
}
return file2.concat(ext1All ? ext2 : ext1);
}
Expand Down Expand Up @@ -863,8 +863,8 @@ public String[] extractVariables(String str) {

protected void throwIllegalArgumentException() {
throw new IllegalArgumentException(
"The number of capturing groups in the pattern segment " + this.pattern
+ " does not match the number of URI template variables it defines, which can occur if capturing groups are used in a URI template regex. Use non-capturing groups instead.");
"The number of capturing groups in the pattern segment %s does not match the number of URI template variables it defines, which can occur if capturing groups are used in a URI template regex. Use non-capturing groups instead."
.formatted(this.pattern));
}

/**
Expand All @@ -890,8 +890,8 @@ else if (pattern != null) {
for (int i = 1; i <= matcher.groupCount(); i++) {
String name = this.variableNames.get(i - 1);
if (name.startsWith("*")) {
throw new IllegalArgumentException("Capturing patterns (" + name + ") are not " +
"supported by the AntPathMatcher. Use the PathPatternParser instead.");
throw new IllegalArgumentException("Capturing patterns (%s) are not supported by the AntPathMatcher. Use the PathPatternParser instead."
.formatted(name));
}
String value = matcher.group(i);
uriTemplateVariables.put(name, value);
Expand Down Expand Up @@ -1070,7 +1070,7 @@ public int getLength() {
final Integer length = this.length;
if (length == null) {
return this.length = (this.pattern != null
? VARIABLE_PATTERN.matcher(this.pattern).replaceAll("#").length() : 0);
? VARIABLE_PATTERN.matcher(this.pattern).replaceAll("#").length() : 0);
}
return length;
}
Expand All @@ -1082,8 +1082,9 @@ public int getLength() {
*/
private static final class PathSeparatorPatternCache {

private final String endsOnWildCard;
private final String endsOnDoubleWildCard;
public final String endsOnWildCard;

public final String endsOnDoubleWildCard;

private PathSeparatorPatternCache(String pathSeparator) {
this.endsOnWildCard = pathSeparator + '*';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ public static <T> Class<T> resolveTypeArgument(Class<?> clazz, Class<?> genericI
@SuppressWarnings("unchecked")
@Nullable
private static <T> Class<T> getSingleGeneric(ResolvableType resolvableType) {
Assert.isTrue(resolvableType.getGenerics().length == 1,
() -> "Expected 1 type argument on generic interface [" + resolvableType +
"] but found " + resolvableType.getGenerics().length);
Assert.isTrue(resolvableType.getGenerics().length == 1, () -> "Expected 1 type argument on generic interface [%s] but found %d"
.formatted(resolvableType, resolvableType.getGenerics().length));
return (Class<T>) resolvableType.getGeneric().resolve();
}

Expand Down
4 changes: 2 additions & 2 deletions today-core/src/main/java/cn/taketoday/lang/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public static void doesNotContain(String textToSearch, String substring, Supplie
*/
public static void doesNotContain(String textToSearch, String substring) {
doesNotContain(textToSearch, substring,
() -> "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
() -> "[Assertion failed] - this String argument must not contain the substring [%s]".formatted(substring));
}

/**
Expand Down Expand Up @@ -683,7 +683,7 @@ private static void instanceCheckFailed(Class<?> type, Object obj, String msg) {
}
}
if (defaultMessage) {
result = result + ("Object of class [" + className + "] must be an instance of " + type);
result = result + ("Object of class [%s] must be an instance of %s".formatted(className, type));
}
throw new IllegalArgumentException(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public static int getInt(String key, int val) {
* method. The string value of this property is then interpreted as an integer
* value using the grammar supported by {@link Integer#decode decode} and
* an {@code Integer} object representing this value is returned.
*
* <p>
* in summary:
*
* <ul><li>If the property value begins with the two ASCII characters
Expand Down Expand Up @@ -399,7 +399,7 @@ public static long getLong(String key, long val) {
* method. The string value of this property is then interpreted as an integer
* value using the grammar supported by {@link Integer#decode decode} and
* an {@code Integer} object representing this value is returned.
*
* <p>
* in summary:
*
* <ul>
Expand Down
4 changes: 2 additions & 2 deletions today-core/src/main/java/cn/taketoday/lang/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
* @author TODAY 2021/10/11 23:28
* @since 4.0
*/
public record Version(
int major, int minor, int micro, String type, int step,
public record Version(int major, int minor, int micro, String type, int step,
@Nullable String extension, String implementationVersion) {

public static final String Draft = "Draft";
public static final String Alpha = "Alpha";
public static final String Beta = "Beta";
Expand Down
5 changes: 3 additions & 2 deletions today-core/src/main/java/cn/taketoday/logging/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
/**
* Logger From slf4j
*
* @author TODAY <br>
* 2019-11-03 13:15
* @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
* @since 2019-11-03 13:15
*/
public abstract class Logger implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

Expand Down
11 changes: 6 additions & 5 deletions today-core/src/main/java/cn/taketoday/logging/LoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cn.taketoday.logging;

/**
* Factory that creates {@link Logger} instances.
*
* @author TODAY <br>
* 2019-11-04 19:06
* @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
* @since 2019-11-04 19:06
*/
public abstract class LoggerFactory {

public static final String LOG_TYPE_SYSTEM_PROPERTY = "logger.factory";

private static final LoggerFactory factory = createFactory();

protected abstract Logger createLogger(String name);
Expand Down Expand Up @@ -54,9 +57,7 @@ private static synchronized LoggerFactory createFactory() {
}
catch (Throwable e) {
e.printStackTrace();
System.err.println(
"Could not find valid log-type from system property '" +
LOG_TYPE_SYSTEM_PROPERTY + "', value '" + type + "'");
System.err.printf("Could not find valid log-type from system property '%s', value '%s'%n", LOG_TYPE_SYSTEM_PROPERTY, type);
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private static void safeObjectAppend(StringBuilder sbuf, Object o) {
sbuf.append(o.toString());
}
catch (Throwable t) {
System.err.println("LOGGER: Failed toString() invocation on an object of type [" + o.getClass().getName() + "]");
System.err.printf("LOGGER: Failed toString() invocation on an object of type [%s]%n", o.getClass().getName());
System.err.println("Reported exception:");
t.printStackTrace();

Expand Down
19 changes: 10 additions & 9 deletions today-core/src/main/java/cn/taketoday/reflect/GetterMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/]
*/

package cn.taketoday.reflect;

import java.lang.reflect.Field;
Expand All @@ -28,8 +29,8 @@
/**
* Fast call bean's getter Method {@link java.lang.reflect.Method Method}
*
* @author TODAY <br>
* 2020-08-13 19:46
* @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
* @since 2020-08-13 19:46
*/
public interface GetterMethod {

Expand Down Expand Up @@ -58,12 +59,12 @@ default Method getReadMethod() {
* @param field given java reflect property
* @return GetterMethod
*/
static GetterMethod fromField(final Field field) {
static GetterMethod forField(final Field field) {
final Method readMethod = ReflectionUtils.getReadMethod(field);
if (readMethod == null) {
return fromReflective(field); // fallback to Reflective
return forReflective(field); // fallback to Reflective
}
return fromMethod(readMethod);
return forMethod(readMethod);
}

/**
Expand All @@ -73,9 +74,9 @@ static GetterMethod fromField(final Field field) {
* @return GetterMethod
* @see MethodInvoker#forMethod(Method)
*/
static GetterMethod fromMethod(final Method method) {
static GetterMethod forMethod(final Method method) {
final MethodInvoker accessor = MethodInvoker.forMethod(method);
return fromMethod(accessor);
return forMethod(accessor);
}

/**
Expand All @@ -85,7 +86,7 @@ static GetterMethod fromMethod(final Method method) {
* @return GetterMethod
* @see MethodInvoker#forMethod(Method)
*/
static GetterMethod fromMethod(final MethodInvoker invoker) {
static GetterMethod forMethod(final MethodInvoker invoker) {
return new MethodAccessorGetterMethod(invoker);
}

Expand All @@ -97,7 +98,7 @@ static GetterMethod fromMethod(final MethodInvoker invoker) {
* @see Field#get(Object)
* @see ReflectionUtils#getField(Field, Object)
*/
static GetterMethod fromReflective(final Field field) {
static GetterMethod forReflective(final Field field) {
ReflectionUtils.makeAccessible(field);
return new ReflectiveGetterMethod(field);
}
Expand Down
28 changes: 13 additions & 15 deletions today-core/src/main/java/cn/taketoday/reflect/PropertyAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public boolean isReadOnly() {
* @param field Field
* @return PropertyAccessor
*/
public static PropertyAccessor fromField(Field field) {
public static PropertyAccessor forField(Field field) {
Method readMethod = ReflectionUtils.getReadMethod(field);
boolean isReadOnly = Modifier.isFinal(field.getModifiers());
if (isReadOnly && readMethod != null) {
Expand All @@ -66,7 +66,7 @@ public static PropertyAccessor fromField(Field field) {
}
Method writeMethod = ReflectionUtils.getWriteMethod(field);
if (writeMethod != null && readMethod != null) {
return fromMethod(readMethod, writeMethod);
return forMethod(readMethod, writeMethod);
}
if (writeMethod != null) {
MethodInvoker accessor = MethodInvoker.forMethod(writeMethod);
Expand All @@ -81,7 +81,7 @@ public static PropertyAccessor fromField(Field field) {
}

// readMethod == null && setMethod == null
return fromReflective(field);
return forReflective(field);
}

/**
Expand All @@ -90,9 +90,9 @@ public static PropertyAccessor fromField(Field field) {
public static PropertyAccessor from(Class<?> targetClass, String name) {
Field field = ReflectionUtils.findField(targetClass, name);
if (field == null) {
throw new ReflectionException("No such property: '" + name + "' in class: " + targetClass);
throw new ReflectionException("No such property: '%s' in class: %s".formatted(name, targetClass));
}
return fromField(field);
return forField(field);
}

/**
Expand All @@ -102,7 +102,7 @@ public static PropertyAccessor from(Class<?> targetClass, String name) {
* @param readMethod getter method
* @return PropertyAccessor
*/
public static PropertyAccessor fromMethod(@Nullable Method readMethod, @Nullable Method writeMethod) {
public static PropertyAccessor forMethod(@Nullable Method readMethod, @Nullable Method writeMethod) {
if (readMethod != null) {
MethodInvoker readInvoker = MethodInvoker.forMethod(readMethod);
if (writeMethod == null) {
Expand Down Expand Up @@ -137,7 +137,7 @@ public void set(Object obj, Object value) {
* @param readMethod getter method
* @return PropertyAccessor
*/
public static PropertyAccessor fromMethod(GetterMethod readMethod, @Nullable SetterMethod writeMethod) {
public static PropertyAccessor forMethod(GetterMethod readMethod, @Nullable SetterMethod writeMethod) {
Assert.notNull(readMethod, "readMethod is required");
if (writeMethod != null) {
return new GetterSetterPropertyAccessor(readMethod, writeMethod);
Expand All @@ -150,15 +150,14 @@ public static PropertyAccessor fromMethod(GetterMethod readMethod, @Nullable Set
* @return PropertyAccessor
* @throws NullPointerException field is null
*/
public static PropertyAccessor fromField(
Field field, @Nullable Method readMethod, @Nullable Method writeMethod) {
public static PropertyAccessor forField(Field field, @Nullable Method readMethod, @Nullable Method writeMethod) {
boolean isReadOnly = Modifier.isFinal(field.getModifiers()) && writeMethod == null;
if (isReadOnly && readMethod != null) {
MethodInvoker invoker = MethodInvoker.forMethod(readMethod);
return new ReadOnlyMethodAccessorPropertyAccessor(invoker);
}
if (writeMethod != null && readMethod != null) {
return fromMethod(readMethod, writeMethod);
return forMethod(readMethod, writeMethod);
}
if (writeMethod != null) {
MethodInvoker accessor = MethodInvoker.forMethod(writeMethod);
Expand All @@ -173,7 +172,7 @@ public static PropertyAccessor fromField(
}

// readMethod == null && setMethod == null
return fromReflective(field);
return forReflective(field);
}

private static PropertyAccessor getPropertyAccessor(Field field, MethodInvoker accessor, @NonNull Method writeMethod) {
Expand Down Expand Up @@ -224,8 +223,8 @@ public Method getReadMethod() {
* @see ReflectionUtils#getField(Field, Object)
* @see ReflectionUtils#setField(Field, Object, Object)
*/
public static PropertyAccessor fromReflective(Field field) {
return fromReflective(field, null, null);
public static PropertyAccessor forReflective(Field field) {
return forReflective(field, null, null);
}

/**
Expand All @@ -238,8 +237,7 @@ public static PropertyAccessor fromReflective(Field field) {
* @see ReflectionUtils#getField(Field, Object)
* @see ReflectionUtils#setField(Field, Object, Object)
*/
public static PropertyAccessor fromReflective(
@Nullable Field field, @Nullable Method readMethod, @Nullable Method writeMethod) {
public static PropertyAccessor forReflective(@Nullable Field field, @Nullable Method readMethod, @Nullable Method writeMethod) {
boolean readOnly;
if (field != null) {
ReflectionUtils.makeAccessible(field);
Expand Down
Loading

0 comments on commit 86b8b51

Please sign in to comment.