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

The following examples show how to use org.springframework.core.annotation.AnnotationAttributes#keySet() . 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
/**
 * 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 5
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 6
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 7
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;
}