Java Code Examples for org.springframework.core.annotation.AnnotationAttributes#put()

The following examples show how to use org.springframework.core.annotation.AnnotationAttributes#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MergedSqlConfig.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Construct a {@code MergedSqlConfig} instance by merging the configuration
 * from the supplied local (potentially method-level) {@code @SqlConfig} annotation
 * with class-level configuration discovered on the supplied {@code testClass}.
 * <p>Local configuration overrides class-level configuration.
 * <p>If the test class is not annotated with {@code @SqlConfig}, no merging
 * takes place and the local configuration is used "as is".
 */
MergedSqlConfig(SqlConfig localSqlConfig, Class<?> testClass) {
	Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null");
	Assert.notNull(testClass, "testClass must not be null");

	// Get global attributes, if any.
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
			testClass, SqlConfig.class.getName(), false, false);

	// Override global attributes with local attributes.
	if (attributes != null) {
		for (String key : attributes.keySet()) {
			Object value = AnnotationUtils.getValue(localSqlConfig, key);
			if (value != null) {
				// Is the value explicit (i.e., not a 'default')?
				if (!value.equals("") && value != TransactionMode.DEFAULT && value != ErrorMode.DEFAULT) {
					attributes.put(key, value);
				}
			}
		}
	}
	else {
		// Otherwise, use local attributes only.
		attributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
	}

	this.dataSource = attributes.getString("dataSource");
	this.transactionManager = attributes.getString("transactionManager");
	this.transactionMode = getEnum(attributes, "transactionMode", TransactionMode.DEFAULT, TransactionMode.INFERRED);
	this.encoding = attributes.getString("encoding");
	this.separator = getString(attributes, "separator", ScriptUtils.DEFAULT_STATEMENT_SEPARATOR);
	this.commentPrefix = getString(attributes, "commentPrefix", ScriptUtils.DEFAULT_COMMENT_PREFIX);
	this.blockCommentStartDelimiter = getString(attributes, "blockCommentStartDelimiter",
			ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER);
	this.blockCommentEndDelimiter = getString(attributes, "blockCommentEndDelimiter",
			ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER);
	this.errorMode = getEnum(attributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR);
}
 
Example 2
Source File: MergedSqlConfig.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Construct a {@code MergedSqlConfig} instance by merging the configuration
 * from the supplied local (potentially method-level) {@code @SqlConfig} annotation
 * with class-level configuration discovered on the supplied {@code testClass}.
 * <p>Local configuration overrides class-level configuration.
 * <p>If the test class is not annotated with {@code @SqlConfig}, no merging
 * takes place and the local configuration is used "as is".
 */
MergedSqlConfig(SqlConfig localSqlConfig, Class<?> testClass) {
	Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null");
	Assert.notNull(testClass, "testClass must not be null");

	// Get global attributes, if any.
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
			testClass, SqlConfig.class.getName(), false, false);

	// Override global attributes with local attributes.
	if (attributes != null) {
		for (String key : attributes.keySet()) {
			Object value = AnnotationUtils.getValue(localSqlConfig, key);
			if (value != null) {
				// Is the value explicit (i.e., not a 'default')?
				if (!value.equals("") && value != TransactionMode.DEFAULT && value != ErrorMode.DEFAULT) {
					attributes.put(key, value);
				}
			}
		}
	}
	else {
		// Otherwise, use local attributes only.
		attributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
	}

	this.dataSource = attributes.getString("dataSource");
	this.transactionManager = attributes.getString("transactionManager");
	this.transactionMode = getEnum(attributes, "transactionMode", TransactionMode.DEFAULT, TransactionMode.INFERRED);
	this.encoding = attributes.getString("encoding");
	this.separator = getString(attributes, "separator", ScriptUtils.DEFAULT_STATEMENT_SEPARATOR);
	this.commentPrefix = getString(attributes, "commentPrefix", ScriptUtils.DEFAULT_COMMENT_PREFIX);
	this.blockCommentStartDelimiter = getString(attributes, "blockCommentStartDelimiter",
			ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER);
	this.blockCommentEndDelimiter = getString(attributes, "blockCommentEndDelimiter",
			ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER);
	this.errorMode = getEnum(attributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR);
}
 
Example 3
Source File: MergedSqlConfig.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a {@code MergedSqlConfig} instance by merging the configuration
 * from the supplied local (potentially method-level) {@code @SqlConfig} annotation
 * with class-level configuration discovered on the supplied {@code testClass}.
 * <p>Local configuration overrides class-level configuration.
 * <p>If the test class is not annotated with {@code @SqlConfig}, no merging
 * takes place and the local configuration is used "as is".
 */
MergedSqlConfig(SqlConfig localSqlConfig, Class<?> testClass) {
	Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null");
	Assert.notNull(testClass, "testClass must not be null");

	// Get global attributes, if any.
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(testClass,
		SqlConfig.class.getName(), false, false);

	// Override global attributes with local attributes.
	if (attributes != null) {
		for (String key : attributes.keySet()) {
			Object value = AnnotationUtils.getValue(localSqlConfig, key);
			if (value != null) {
				// Is the value explicit (i.e., not a 'default')?
				if (!value.equals("") && (value != TransactionMode.DEFAULT) && (value != ErrorMode.DEFAULT)) {
					attributes.put(key, value);
				}
			}
		}
	}
	else {
		// Otherwise, use local attributes only.
		attributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
	}

	this.dataSource = attributes.getString("dataSource");
	this.transactionManager = attributes.getString("transactionManager");
	this.transactionMode = getEnum(attributes, "transactionMode", TransactionMode.DEFAULT, TransactionMode.INFERRED);
	this.encoding = attributes.getString("encoding");
	this.separator = getString(attributes, "separator", ScriptUtils.DEFAULT_STATEMENT_SEPARATOR);
	this.commentPrefix = getString(attributes, "commentPrefix", ScriptUtils.DEFAULT_COMMENT_PREFIX);
	this.blockCommentStartDelimiter = getString(attributes, "blockCommentStartDelimiter",
		ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER);
	this.blockCommentEndDelimiter = getString(attributes, "blockCommentEndDelimiter",
		ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER);
	this.errorMode = getEnum(attributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR);
}
 
Example 4
Source File: AnnotationReadingVisitorUtils.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public static AnnotationAttributes convertClassValues(Object annotatedElement,
		@Nullable ClassLoader classLoader, AnnotationAttributes original, boolean classValuesAsString) {

	AnnotationAttributes result = new AnnotationAttributes(original);
	AnnotationUtils.postProcessAnnotationAttributes(annotatedElement, result, classValuesAsString);

	for (Map.Entry<String, Object> entry : result.entrySet()) {
		try {
			Object value = entry.getValue();
			if (value instanceof AnnotationAttributes) {
				value = convertClassValues(
						annotatedElement, classLoader, (AnnotationAttributes) value, classValuesAsString);
			}
			else if (value instanceof AnnotationAttributes[]) {
				AnnotationAttributes[] values = (AnnotationAttributes[]) value;
				for (int i = 0; i < values.length; i++) {
					values[i] = convertClassValues(annotatedElement, classLoader, values[i], classValuesAsString);
				}
				value = values;
			}
			else if (value instanceof Type) {
				value = (classValuesAsString ? ((Type) value).getClassName() :
						ClassUtils.forName(((Type) value).getClassName(), classLoader));
			}
			else if (value instanceof Type[]) {
				Type[] array = (Type[]) value;
				Object[] convArray =
						(classValuesAsString ? new String[array.length] : new Class<?>[array.length]);
				for (int i = 0; i < array.length; i++) {
					convArray[i] = (classValuesAsString ? array[i].getClassName() :
							ClassUtils.forName(array[i].getClassName(), classLoader));
				}
				value = convArray;
			}
			else if (classValuesAsString) {
				if (value instanceof Class) {
					value = ((Class<?>) value).getName();
				}
				else if (value instanceof Class[]) {
					Class<?>[] clazzArray = (Class<?>[]) value;
					String[] newValue = new String[clazzArray.length];
					for (int i = 0; i < clazzArray.length; i++) {
						newValue[i] = clazzArray[i].getName();
					}
					value = newValue;
				}
			}
			entry.setValue(value);
		}
		catch (Throwable ex) {
			// Class not found - can't resolve class reference in annotation attribute.
			result.put(entry.getKey(), ex);
		}
	}

	return result;
}
 
Example 5
Source File: AnnotationReadingVisitorUtils.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Retrieve the merged attributes of the annotation of the given type,
 * if any, from the supplied {@code attributesMap}.
 * <p>Annotation attribute values appearing <em>lower</em> in the annotation
 * hierarchy (i.e., closer to the declaring class) will override those
 * defined <em>higher</em> in the annotation hierarchy.
 * @param attributesMap the map of annotation attribute lists, keyed by
 * annotation type name
 * @param metaAnnotationMap the map of meta annotation relationships,
 * keyed by annotation type name
 * @param annotationName the fully qualified class name of the annotation
 * type to look for
 * @return the merged annotation attributes, or {@code null} if no
 * matching annotation is present in the {@code attributesMap}
 * @since 4.0.3
 */
@Nullable
public static AnnotationAttributes getMergedAnnotationAttributes(
		LinkedMultiValueMap<String, AnnotationAttributes> attributesMap,
		Map<String, Set<String>> metaAnnotationMap, String annotationName) {

	// Get the unmerged list of attributes for the target annotation.
	List<AnnotationAttributes> attributesList = attributesMap.get(annotationName);
	if (CollectionUtils.isEmpty(attributesList)) {
		return null;
	}

	// To start with, we populate the result with a copy of all attribute values
	// from the target annotation. A copy is necessary so that we do not
	// inadvertently mutate the state of the metadata passed to this method.
	AnnotationAttributes result = new AnnotationAttributes(attributesList.get(0));

	Set<String> overridableAttributeNames = new HashSet<>(result.keySet());
	overridableAttributeNames.remove(AnnotationUtils.VALUE);

	// Since the map is a LinkedMultiValueMap, we depend on the ordering of
	// elements in the map and reverse the order of the keys in order to traverse
	// "down" the annotation hierarchy.
	List<String> annotationTypes = new ArrayList<>(attributesMap.keySet());
	Collections.reverse(annotationTypes);

	// No need to revisit the target annotation type:
	annotationTypes.remove(annotationName);

	for (String currentAnnotationType : annotationTypes) {
		List<AnnotationAttributes> currentAttributesList = attributesMap.get(currentAnnotationType);
		if (!ObjectUtils.isEmpty(currentAttributesList)) {
			Set<String> metaAnns = metaAnnotationMap.get(currentAnnotationType);
			if (metaAnns != null && metaAnns.contains(annotationName)) {
				AnnotationAttributes currentAttributes = currentAttributesList.get(0);
				for (String overridableAttributeName : overridableAttributeNames) {
					Object value = currentAttributes.get(overridableAttributeName);
					if (value != null) {
						// Store the value, potentially overriding a value from an attribute
						// of the same name found higher in the annotation hierarchy.
						result.put(overridableAttributeName, value);
					}
				}
			}
		}
	}

	return result;
}
 
Example 6
Source File: AnnotationReadingVisitorUtils.java    From java-technology-stack with MIT License 4 votes vote down vote up
public static AnnotationAttributes convertClassValues(Object annotatedElement,
		@Nullable ClassLoader classLoader, AnnotationAttributes original, boolean classValuesAsString) {

	AnnotationAttributes result = new AnnotationAttributes(original);
	AnnotationUtils.postProcessAnnotationAttributes(annotatedElement, result, classValuesAsString);

	for (Map.Entry<String, Object> entry : result.entrySet()) {
		try {
			Object value = entry.getValue();
			if (value instanceof AnnotationAttributes) {
				value = convertClassValues(
						annotatedElement, classLoader, (AnnotationAttributes) value, classValuesAsString);
			}
			else if (value instanceof AnnotationAttributes[]) {
				AnnotationAttributes[] values = (AnnotationAttributes[]) value;
				for (int i = 0; i < values.length; i++) {
					values[i] = convertClassValues(annotatedElement, classLoader, values[i], classValuesAsString);
				}
				value = values;
			}
			else if (value instanceof Type) {
				value = (classValuesAsString ? ((Type) value).getClassName() :
						ClassUtils.forName(((Type) value).getClassName(), classLoader));
			}
			else if (value instanceof Type[]) {
				Type[] array = (Type[]) value;
				Object[] convArray =
						(classValuesAsString ? new String[array.length] : new Class<?>[array.length]);
				for (int i = 0; i < array.length; i++) {
					convArray[i] = (classValuesAsString ? array[i].getClassName() :
							ClassUtils.forName(array[i].getClassName(), classLoader));
				}
				value = convArray;
			}
			else if (classValuesAsString) {
				if (value instanceof Class) {
					value = ((Class<?>) value).getName();
				}
				else if (value instanceof Class[]) {
					Class<?>[] clazzArray = (Class<?>[]) value;
					String[] newValue = new String[clazzArray.length];
					for (int i = 0; i < clazzArray.length; i++) {
						newValue[i] = clazzArray[i].getName();
					}
					value = newValue;
				}
			}
			entry.setValue(value);
		}
		catch (Throwable ex) {
			// Class not found - can't resolve class reference in annotation attribute.
			result.put(entry.getKey(), ex);
		}
	}

	return result;
}
 
Example 7
Source File: AnnotationReadingVisitorUtils.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Retrieve the merged attributes of the annotation of the given type,
 * if any, from the supplied {@code attributesMap}.
 * <p>Annotation attribute values appearing <em>lower</em> in the annotation
 * hierarchy (i.e., closer to the declaring class) will override those
 * defined <em>higher</em> in the annotation hierarchy.
 * @param attributesMap the map of annotation attribute lists, keyed by
 * annotation type name
 * @param metaAnnotationMap the map of meta annotation relationships,
 * keyed by annotation type name
 * @param annotationName the fully qualified class name of the annotation
 * type to look for
 * @return the merged annotation attributes, or {@code null} if no
 * matching annotation is present in the {@code attributesMap}
 * @since 4.0.3
 */
@Nullable
public static AnnotationAttributes getMergedAnnotationAttributes(
		LinkedMultiValueMap<String, AnnotationAttributes> attributesMap,
		Map<String, Set<String>> metaAnnotationMap, String annotationName) {

	// Get the unmerged list of attributes for the target annotation.
	List<AnnotationAttributes> attributesList = attributesMap.get(annotationName);
	if (CollectionUtils.isEmpty(attributesList)) {
		return null;
	}

	// To start with, we populate the result with a copy of all attribute values
	// from the target annotation. A copy is necessary so that we do not
	// inadvertently mutate the state of the metadata passed to this method.
	AnnotationAttributes result = new AnnotationAttributes(attributesList.get(0));

	Set<String> overridableAttributeNames = new HashSet<>(result.keySet());
	overridableAttributeNames.remove(AnnotationUtils.VALUE);

	// Since the map is a LinkedMultiValueMap, we depend on the ordering of
	// elements in the map and reverse the order of the keys in order to traverse
	// "down" the annotation hierarchy.
	List<String> annotationTypes = new ArrayList<>(attributesMap.keySet());
	Collections.reverse(annotationTypes);

	// No need to revisit the target annotation type:
	annotationTypes.remove(annotationName);

	for (String currentAnnotationType : annotationTypes) {
		List<AnnotationAttributes> currentAttributesList = attributesMap.get(currentAnnotationType);
		if (!ObjectUtils.isEmpty(currentAttributesList)) {
			Set<String> metaAnns = metaAnnotationMap.get(currentAnnotationType);
			if (metaAnns != null && metaAnns.contains(annotationName)) {
				AnnotationAttributes currentAttributes = currentAttributesList.get(0);
				for (String overridableAttributeName : overridableAttributeNames) {
					Object value = currentAttributes.get(overridableAttributeName);
					if (value != null) {
						// Store the value, potentially overriding a value from an attribute
						// of the same name found higher in the annotation hierarchy.
						result.put(overridableAttributeName, value);
					}
				}
			}
		}
	}

	return result;
}
 
Example 8
Source File: AnnotationReadingVisitorUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static AnnotationAttributes convertClassValues(Object annotatedElement,
		ClassLoader classLoader, AnnotationAttributes original, boolean classValuesAsString) {

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

	AnnotationAttributes result = new AnnotationAttributes(original);
	AnnotationUtils.postProcessAnnotationAttributes(annotatedElement, result, classValuesAsString);

	for (Map.Entry<String, Object> entry : result.entrySet()) {
		try {
			Object value = entry.getValue();
			if (value instanceof AnnotationAttributes) {
				value = convertClassValues(
						annotatedElement, classLoader, (AnnotationAttributes) value, classValuesAsString);
			}
			else if (value instanceof AnnotationAttributes[]) {
				AnnotationAttributes[] values = (AnnotationAttributes[]) value;
				for (int i = 0; i < values.length; i++) {
					values[i] = convertClassValues(annotatedElement, classLoader, values[i], classValuesAsString);
				}
				value = values;
			}
			else if (value instanceof Type) {
				value = (classValuesAsString ? ((Type) value).getClassName() :
						classLoader.loadClass(((Type) value).getClassName()));
			}
			else if (value instanceof Type[]) {
				Type[] array = (Type[]) value;
				Object[] convArray =
						(classValuesAsString ? new String[array.length] : new Class<?>[array.length]);
				for (int i = 0; i < array.length; i++) {
					convArray[i] = (classValuesAsString ? array[i].getClassName() :
							classLoader.loadClass(array[i].getClassName()));
				}
				value = convArray;
			}
			else if (classValuesAsString) {
				if (value instanceof Class) {
					value = ((Class<?>) value).getName();
				}
				else if (value instanceof Class[]) {
					Class<?>[] clazzArray = (Class<?>[]) value;
					String[] newValue = new String[clazzArray.length];
					for (int i = 0; i < clazzArray.length; i++) {
						newValue[i] = clazzArray[i].getName();
					}
					value = newValue;
				}
			}
			entry.setValue(value);
		}
		catch (Throwable ex) {
			// Class not found - can't resolve class reference in annotation attribute.
			result.put(entry.getKey(), ex);
		}
	}

	return result;
}
 
Example 9
Source File: AnnotationReadingVisitorUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Retrieve the merged attributes of the annotation of the given type,
 * if any, from the supplied {@code attributesMap}.
 * <p>Annotation attribute values appearing <em>lower</em> in the annotation
 * hierarchy (i.e., closer to the declaring class) will override those
 * defined <em>higher</em> in the annotation hierarchy.
 * @param attributesMap the map of annotation attribute lists, keyed by
 * annotation type name
 * @param metaAnnotationMap the map of meta annotation relationships,
 * keyed by annotation type name
 * @param annotationName the fully qualified class name of the annotation
 * type to look for
 * @return the merged annotation attributes, or {@code null} if no
 * matching annotation is present in the {@code attributesMap}
 * @since 4.0.3
 */
public static AnnotationAttributes getMergedAnnotationAttributes(
		LinkedMultiValueMap<String, AnnotationAttributes> attributesMap,
		Map<String, Set<String>> metaAnnotationMap, String annotationName) {

	// Get the unmerged list of attributes for the target annotation.
	List<AnnotationAttributes> attributesList = attributesMap.get(annotationName);
	if (attributesList == null || attributesList.isEmpty()) {
		return null;
	}

	// To start with, we populate the result with a copy of all attribute values
	// from the target annotation. A copy is necessary so that we do not
	// inadvertently mutate the state of the metadata passed to this method.
	AnnotationAttributes result = new AnnotationAttributes(attributesList.get(0));

	Set<String> overridableAttributeNames = new HashSet<String>(result.keySet());
	overridableAttributeNames.remove(AnnotationUtils.VALUE);

	// Since the map is a LinkedMultiValueMap, we depend on the ordering of
	// elements in the map and reverse the order of the keys in order to traverse
	// "down" the annotation hierarchy.
	List<String> annotationTypes = new ArrayList<String>(attributesMap.keySet());
	Collections.reverse(annotationTypes);

	// No need to revisit the target annotation type:
	annotationTypes.remove(annotationName);

	for (String currentAnnotationType : annotationTypes) {
		List<AnnotationAttributes> currentAttributesList = attributesMap.get(currentAnnotationType);
		if (!ObjectUtils.isEmpty(currentAttributesList)) {
			Set<String> metaAnns = metaAnnotationMap.get(currentAnnotationType);
			if (metaAnns != null && metaAnns.contains(annotationName)) {
				AnnotationAttributes currentAttributes = currentAttributesList.get(0);
				for (String overridableAttributeName : overridableAttributeNames) {
					Object value = currentAttributes.get(overridableAttributeName);
					if (value != null) {
						// Store the value, potentially overriding a value from an attribute
						// of the same name found higher in the annotation hierarchy.
						result.put(overridableAttributeName, value);
					}
				}
			}
		}
	}

	return result;
}
 
Example 10
Source File: AnnotationReadingVisitorUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public static AnnotationAttributes convertClassValues(ClassLoader classLoader, AnnotationAttributes original,
		boolean classValuesAsString) {

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

	AnnotationAttributes result = new AnnotationAttributes(original.size());
	for (Map.Entry<String, Object> entry : original.entrySet()) {
		try {
			Object value = entry.getValue();
			if (value instanceof AnnotationAttributes) {
				value = convertClassValues(classLoader, (AnnotationAttributes) value, classValuesAsString);
			}
			else if (value instanceof AnnotationAttributes[]) {
				AnnotationAttributes[] values = (AnnotationAttributes[]) value;
				for (int i = 0; i < values.length; i++) {
					values[i] = convertClassValues(classLoader, values[i], classValuesAsString);
				}
			}
			else if (value instanceof Type) {
				value = (classValuesAsString ? ((Type) value).getClassName() :
						classLoader.loadClass(((Type) value).getClassName()));
			}
			else if (value instanceof Type[]) {
				Type[] array = (Type[]) value;
				Object[] convArray = (classValuesAsString ? new String[array.length] : new Class<?>[array.length]);
				for (int i = 0; i < array.length; i++) {
					convArray[i] = (classValuesAsString ? array[i].getClassName() :
							classLoader.loadClass(array[i].getClassName()));
				}
				value = convArray;
			}
			else if (classValuesAsString) {
				if (value instanceof Class) {
					value = ((Class<?>) value).getName();
				}
				else if (value instanceof Class[]) {
					Class<?>[] clazzArray = (Class[]) value;
					String[] newValue = new String[clazzArray.length];
					for (int i = 0; i < clazzArray.length; i++) {
						newValue[i] = clazzArray[i].getName();
					}
					value = newValue;
				}
			}
			result.put(entry.getKey(), value);
		}
		catch (Exception ex) {
			// Class not found - can't resolve class reference in annotation attribute.
			result.put(entry.getKey(), ex);
		}
	}
	return result;
}
 
Example 11
Source File: AnnotationReadingVisitorUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieve the merged attributes of the annotation of the given type,
 * if any, from the supplied {@code attributesMap}.
 * <p>Annotation attribute values appearing <em>lower</em> in the annotation
 * hierarchy (i.e., closer to the declaring class) will override those
 * defined <em>higher</em> in the annotation hierarchy.
 * @param attributesMap the map of annotation attribute lists, keyed by
 * annotation type name
 * @param metaAnnotationMap the map of meta annotation relationships,
 * keyed by annotation type name
 * @param annotationName the fully qualified class name of the annotation
 * type to look for
 * @return the merged annotation attributes, or {@code null} if no
 * matching annotation is present in the {@code attributesMap}
 * @since 4.0.3
 */
public static AnnotationAttributes getMergedAnnotationAttributes(
		LinkedMultiValueMap<String, AnnotationAttributes> attributesMap,
		Map<String, Set<String>> metaAnnotationMap, String annotationName) {

	// Get the unmerged list of attributes for the target annotation.
	List<AnnotationAttributes> attributesList = attributesMap.get(annotationName);
	if (attributesList == null || attributesList.isEmpty()) {
		return null;
	}

	// To start with, we populate the results with a copy of all attribute
	// values from the target annotation. A copy is necessary so that we do
	// not inadvertently mutate the state of the metadata passed to this
	// method.
	AnnotationAttributes results = new AnnotationAttributes(attributesList.get(0));

	Set<String> overridableAttributeNames = new HashSet<String>(results.keySet());
	overridableAttributeNames.remove(AnnotationUtils.VALUE);

	// Since the map is a LinkedMultiValueMap, we depend on the ordering of
	// elements in the map and reverse the order of the keys in order to traverse
	// "down" the annotation hierarchy.
	List<String> annotationTypes = new ArrayList<String>(attributesMap.keySet());
	Collections.reverse(annotationTypes);

	// No need to revisit the target annotation type:
	annotationTypes.remove(annotationName);

	for (String currentAnnotationType : annotationTypes) {
		List<AnnotationAttributes> currentAttributesList = attributesMap.get(currentAnnotationType);
		if (!ObjectUtils.isEmpty(currentAttributesList)) {
			Set<String> metaAnns = metaAnnotationMap.get(currentAnnotationType);
			if (metaAnns != null && metaAnns.contains(annotationName)) {
				AnnotationAttributes currentAttributes = currentAttributesList.get(0);
				for (String overridableAttributeName : overridableAttributeNames) {
					Object value = currentAttributes.get(overridableAttributeName);
					if (value != null) {
						// Store the value, potentially overriding a value from an
						// attribute of the same name found higher in the annotation
						// hierarchy.
						results.put(overridableAttributeName, value);
					}
				}
			}
		}
	}

	return results;
}