Merge pull request #797 from marschall/SPR-13007

* SPR-13007:
  Avoid eager formatting in pre-condition checks
This commit is contained in:
Sam Brannen
2015-05-10 12:43:27 +02:00
3 changed files with 18 additions and 12 deletions

View File

@@ -21,7 +21,6 @@ import java.lang.annotation.Annotation;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
/**
* Convenient base class for {@link ImportSelector} implementations that select imports
@@ -62,13 +61,17 @@ public abstract class AdviceModeImportSelector<A extends Annotation> implements
public final String[] selectImports(AnnotationMetadata importingClassMetadata) {
Class<?> annoType = GenericTypeResolver.resolveTypeArgument(getClass(), AdviceModeImportSelector.class);
AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(importingClassMetadata, annoType);
Assert.notNull(attributes, String.format(
if (attributes == null) {
throw new IllegalArgumentException(String.format(
"@%s is not present on importing class '%s' as expected",
annoType.getSimpleName(), importingClassMetadata.getClassName()));
}
AdviceMode adviceMode = attributes.getEnum(this.getAdviceModeAttributeName());
String[] imports = selectImports(adviceMode);
Assert.notNull(imports, String.format("Unknown AdviceMode: '%s'", adviceMode));
if (imports == null) {
throw new IllegalArgumentException(String.format("Unknown AdviceMode: '%s'", adviceMode));
}
return imports;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -105,7 +105,9 @@ public class AnnotationAttributes extends LinkedHashMap<String, Object> {
private <T> T doGet(String attributeName, Class<T> expectedType) {
Assert.hasText(attributeName, "attributeName must not be null or empty");
Object value = get(attributeName);
Assert.notNull(value, String.format("Attribute '%s' not found", attributeName));
if (value == null) {
throw new IllegalArgumentException(String.format("Attribute '%s' not found", attributeName));
}
if (!expectedType.isInstance(value)) {
if (expectedType.isArray() && expectedType.getComponentType().isInstance(value)) {
Object arrayValue = Array.newInstance(expectedType.getComponentType(), 1);
@@ -114,8 +116,8 @@ public class AnnotationAttributes extends LinkedHashMap<String, Object> {
}
else {
throw new IllegalArgumentException(
String.format("Attribute '%s' is of type [%s], but [%s] was expected. Cause: ",
attributeName, value.getClass().getSimpleName(), expectedType.getSimpleName()));
String.format("Attribute '%s' is of type [%s], but [%s] was expected.",
attributeName, value.getClass().getName(), expectedType.getName()));
}
}
return (T) value;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,8 +21,6 @@ import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.springframework.util.Assert;
/**
* Read-only {@code Map<String, String>} implementation that is backed by system
* properties or environment variables.
@@ -50,8 +48,11 @@ abstract class ReadOnlySystemAttributesMap implements Map<String, String> {
*/
@Override
public String get(Object key) {
Assert.isInstanceOf(String.class, key,
String.format("Expected key [%s] to be of type String, got %s", key, key.getClass().getName()));
if (!(key instanceof String)) {
throw new IllegalArgumentException(
"Key of type [" + (key != null ? key.getClass().getName() : "null") +
"] must be an instance of java.lang.String.");
}
return this.getSystemAttribute((String) key);
}