Skip to content

Commit

Permalink
Avoid String allocations with Assert.isTrue()
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Nov 5, 2022
1 parent 8282351 commit ac5eb9b
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void addFile(Kind kind, String path, InputStreamSource content) {
Assert.notNull(content, "'content' must not be null");
Path root = this.roots.apply(kind).toAbsolutePath().normalize();
Path relativePath = root.resolve(path).toAbsolutePath().normalize();
Assert.isTrue(relativePath.startsWith(root), () -> "'path' must be relative");
Assert.isTrue(relativePath.startsWith(root), "'path' must be relative");
try {
try (InputStream inputStream = content.getInputStream()) {
Files.createDirectories(relativePath.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ default void addFile(Kind kind, String path, ThrowingConsumer<Appendable> conten

private static String getClassNamePath(String className) {
Assert.hasLength(className, "'className' must not be empty");
Assert.isTrue(isJavaIdentifier(className),
"'className' must be a valid identifier");
Assert.isTrue(isJavaIdentifier(className), "'className' must be a valid identifier");
return ClassUtils.convertClassNameToResourcePath(className) + ".java";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ private static class MetadataHelper {
private final List<MimeType> mimeTypes = new ArrayList<>();

public void addMetadata(Object metadata) {
Assert.isTrue(this.metadata.size() == this.mimeTypes.size(), "Invalid state: " + this);
Assert.isTrue(this.metadata.size() == this.mimeTypes.size(), () -> "Invalid state: " + this);
this.metadata.add(metadata);
}

public void addMimeType(MimeType mimeType) {
Assert.isTrue(this.metadata.size() == (this.mimeTypes.size() + 1), "Invalid state: " + this);
Assert.isTrue(this.metadata.size() == (this.mimeTypes.size() + 1), () -> "Invalid state: " + this);
this.mimeTypes.add(mimeType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class InjectionCodeGenerator {


InjectionCodeGenerator(ClassName targetClassName, RuntimeHints hints) {
Assert.notNull(hints, "TargetClassName must not be null");
Assert.notNull(hints, "Hints must not be null");
Assert.notNull(hints, "ClassName must not be null");
Assert.notNull(hints, "RuntimeHints must not be null");
this.targetClassName = targetClassName;
this.hints = hints;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ private CodeBlock generateMethodInjectionCode(Method method, String instanceVari
CodeBlock resourceToInject) {

Assert.isTrue(method.getParameterCount() == 1,
"Method '" + method.getName() + "' must declare a single parameter");
() -> "Method '" + method.getName() + "' must declare a single parameter");
CodeBlock.Builder code = CodeBlock.builder();
AccessControl accessControl = AccessControl.forMember(method);
if (!accessControl.isAccessibleFrom(this.targetClassName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ private static boolean isRFC5987AttrChar(byte c) {
* @see <a href="https://tools.ietf.org/html/rfc2047">RFC 2047</a>
*/
private static String decodeQuotedPrintableFilename(String filename, Charset charset) {
Assert.notNull(filename, "'input' String` should not be null");
Assert.notNull(filename, "'input' String should not be null");
Assert.notNull(charset, "'charset' should not be null");

byte[] value = filename.getBytes(US_ASCII);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void addSingleValue(
}

if (value == null) {
Assert.isTrue(!required, "Missing " + valueLabel + " value '" + name + "'");
Assert.isTrue(!required, () -> "Missing " + valueLabel + " value '" + name + "'");
return;
}

Expand Down

0 comments on commit ac5eb9b

Please sign in to comment.