/*
* Copyright 2002-2005 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
namespace Spring.Util
{
///
/// General utility methods for working with annotations
///
public class AttributeUtils
{
///
/// Find a single Attribute of the type 'attributeType' from the supplied class,
/// traversing it interfaces and super classes if no attribute can be found on the
/// class iteslf.
///
///
/// This method explicitly handles class-level attributes which are not declared as
/// inherited as well as attributes on interfaces.
///
/// The class to look for attributes on .
/// Type of the attribibute to look for.
/// the attribute of the given type found, or null
public static Attribute FindAttribute(Type type, Type attributeType)
{
Attribute[] attributes = Attribute.GetCustomAttributes(type, attributeType, false); // we will traverse hierarchy ourselves.
if (attributes.Length > 0)
{
return attributes[0];
}
foreach (Type interfaceType in type.GetInterfaces())
{
Attribute attrib = FindAttribute(interfaceType, attributeType);
if (attrib != null)
{
return attrib;
}
}
if (type.BaseType == null)
{
return null;
}
return FindAttribute(type.BaseType, attributeType);
}
}
}