本文整理匯總了Java中java.lang.reflect.Field.getAnnotations方法的典型用法代碼示例。如果您正苦於以下問題:Java Field.getAnnotations方法的具體用法?Java Field.getAnnotations怎麽用?Java Field.getAnnotations使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.Field
的用法示例。
在下文中一共展示了Field.getAnnotations方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: inject
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Injects all fields that are marked with the {@link InjectView} annotation.
* <p>
* For each field marked with the InjectView annotation, a call to
* {@link Activity#findViewById(int)} will be made, passing in the resource id stored in the
* value() method of the InjectView annotation as the int parameter, and the result of this call
* will be assigned to the field.
*
* @throws IllegalStateException if injection fails, common causes being that you have used an
* invalid id value, or you haven't called setContentView() on your Activity.
*/
public void inject() {
for (Field field : mActivity.getClass().getDeclaredFields()) {
for (Annotation annotation : field.getAnnotations()) {
if (annotation.annotationType().equals(InjectView.class)) {
try {
Class<?> fieldType = field.getType();
int idValue = InjectView.class.cast(annotation).value();
field.setAccessible(true);
Object injectedValue = fieldType.cast(mActivity.findViewById(idValue));
if (injectedValue == null) {
throw new IllegalStateException("findViewById(" + idValue
+ ") gave null for " +
field + ", can't inject");
}
field.set(mActivity, injectedValue);
field.setAccessible(false);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
}
}
}
示例2: InjectionPoint
import java.lang.reflect.Field; //導入方法依賴的package包/類
InjectionPoint(TypeLiteral<?> type, Field field) {
this.member = field;
Inject inject = field.getAnnotation(Inject.class);
this.optional = inject.optional();
Annotation[] annotations = field.getAnnotations();
Errors errors = new Errors(field);
Key<?> key = null;
try {
key = Annotations.getKey(type.getFieldType(field), field, annotations, errors);
} catch (ErrorsException e) {
errors.merge(e.getErrors());
}
errors.throwConfigurationExceptionIfErrorsExist();
this.dependencies = Collections.<Dependency<?>>singletonList(
newDependency(key, Nullability.allowsNull(annotations), -1));
}
示例3: getFields
import java.lang.reflect.Field; //導入方法依賴的package包/類
private static List<Resource> getFields(Map<String, Type> lookup, Class clazz, boolean includingPrivate) {
ArrayList<Resource> resources = new ArrayList<Resource>();
for (Field field : ReflectKit.getAllFields(clazz, includingPrivate)) {
Annotation[] annotations = field.getAnnotations();
if (annotations ==null||annotations.length==0) {//排除未注解變量
continue;
}
if (Modifier.isStatic(field.getModifiers())) { //去掉 static 變量
continue;
}
if (Modifier.isTransient(field.getModifiers())) {
continue;
}
if (!includingPrivate && !Modifier.isPublic(field.getType().getModifiers())) {
continue;
}
if (includingPrivate) {
field.setAccessible(true);
}
Resource resource = createBindingFromField(lookup, clazz, field);
resources.add(resource);
}
return resources;
}
示例4: parseAnnotation
import java.lang.reflect.Field; //導入方法依賴的package包/類
private void parseAnnotation(Object target) {
ArrayMap map = new ArrayMap();
Class<?> targetClass = target.getClass();
Field[] fields = targetClass.getDeclaredFields();
for (Field field : fields) {
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof TurnLeftSideslip) {
map.put("TurnLeftSideslip", annotation);
} else if (annotation instanceof TurnRightSideslip) {
map.put("TurnRightSideslip", annotation);
} else if (annotation instanceof LongTouch) {
map.put("LongTouch", annotation);
} else if (annotation instanceof Drag) {
map.put("Drag", annotation);
}
}
}
itemListener.setLimitMap(map);
}
示例5: getOrderedAnnotations
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Returns all annotationWrappers from a class to resolve the order
*
* @param sourceClass sourceClass
* @return annotationWrappers
*/
private static List<AnnotationWrapper> getOrderedAnnotations(Class<?> sourceClass) {
final List<AnnotationWrapper> annotationWrappers = new ArrayList<>();
Class<?> clazz = sourceClass;
while (clazz != null) {
for (final Field field : clazz.getDeclaredFields()) {
for (final Annotation annotation : field.getAnnotations()) {
if (annotation.annotationType() == YamlSerialize.class) {
annotationWrappers.add(new AnnotationWrapper((YamlSerialize) annotation, field));
}
}
}
clazz = clazz.getSuperclass();
}
annotationWrappers.sort((o1, o2) -> {
if (o1.annotation.orderNumber() > o2.annotation.orderNumber())
return 1;
else if (o1.annotation.orderNumber() < o2.annotation.orderNumber())
return -1;
return 0;
});
return annotationWrappers;
}
示例6: getAnnotations
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Override
public Annotation[] getAnnotations() {
Field javaField = toJava();
if (javaField != null) {
return javaField.getAnnotations();
}
return new Annotation[0];
}
示例7: getAllFieldAnnotations
import java.lang.reflect.Field; //導入方法依賴的package包/類
public Annotation[] getAllFieldAnnotations(Field field, Locatable srcPos) {
Annotation[] r = field.getAnnotations();
for( int i=0; i<r.length; i++ ) {
r[i] = LocatableAnnotation.create(r[i],srcPos);
}
return r;
}
示例8: processFields
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Processes all fields of a class
*
* @param fields
* array of fields
* @param definition
* current definition
* @param rootDefinition
* definition where the recursion started to prevent an endless
* loop
* @return list of {@link Property} objects
* @throws ParserException
* Error while the parsing process
*/
public List<Property> processFields(Field[] fields, Definition definition, Definition rootDefinition)
throws ParserException {
DataTypeFactory typeHandler = new DataTypeFactory();
List<Property> properties = new ArrayList<>();
for (Field field : fields) {
if (field.getAnnotation(JsonIgnore.class) == null && field.getAnnotation(JsonBackReference.class) == null) {
Property property = new Property();
Class<?> typeClass = field.getType();
Annotation[] annotations = field.getAnnotations();
Type genericType = field.getGenericType();
processGenerics(genericType, property, definition, rootDefinition);
DataType typeObject = typeHandler.getDataType(typeClass.getName());
String type = typeObject.getType();
String name = field.getName();
property.setName(name);
if (type.length() > 14 && (type.substring(14).equals(definition.getClassName())
|| type.substring(14).equals(rootDefinition.getClassName()))) {
property.setReference(type);
} else if (type.startsWith("#")) {
createDefinitionBySchemaAndPackageIfNotExists(type, typeClass.getTypeName(), rootDefinition);
property.setReference(type);
} else {
property.setType(type);
property.setFormat(typeObject.getFormat());
}
properties.add(property);
processAnnotations(annotations, definition, name);
}
}
return properties;
}
示例9: isCorrectFieldForAnnotations
import java.lang.reflect.Field; //導入方法依賴的package包/類
private static boolean isCorrectFieldForAnnotations(Object objectToValidate, Field field) {
boolean result = true;
for (Annotation annotation : field.getAnnotations())
result &= isAnnotationConstraintCorrect(objectToValidate, field, annotation);
return result;
}
示例10: genMessage
import java.lang.reflect.Field; //導入方法依賴的package包/類
private Message genMessage(String topic, String tag, Object msgObj) {
String messageKey= "";
try {
Field[] fields = msgObj.getClass().getDeclaredFields();
for (Field field : fields) {
Annotation[] allFAnnos= field.getAnnotations();
if(allFAnnos.length > 0) {
for (int i = 0; i < allFAnnos.length; i++) {
if(allFAnnos[i].annotationType().equals(MQKey.class)) {
field.setAccessible(true);
MQKey mqKey = MQKey.class.cast(allFAnnos[i]);
messageKey = StringUtils.isEmpty(mqKey.prefix()) ? field.get(msgObj).toString() : (mqKey.prefix() + field.get(msgObj).toString());
}
}
}
}
} catch (Exception e) {
log.error("parse key error : {}" , e.getMessage());
}
String str = gson.toJson(msgObj);
if(StringUtils.isEmpty(topic)) {
if(StringUtils.isEmpty(getTopic())) {
throw new RuntimeException("no topic defined to send this message");
}
topic = getTopic();
}
Message message = new Message(topic, str.getBytes(Charset.forName("utf-8")));
if (!StringUtils.isEmpty(tag)) {
message.setTags(tag);
} else if (!StringUtils.isEmpty(getTag())) {
message.setTags(getTag());
}
if(StringUtils.isNotEmpty(messageKey)) {
message.setKeys(messageKey);
}
return message;
}
示例11: createBindingFromField
import java.lang.reflect.Field; //導入方法依賴的package包/類
private static Resource createBindingFromField(Map<String, Type> lookup, Class clazz, Field field) {
try {
Resource resource = new Resource(clazz, lookup, field.getGenericType());
resource.fromNames = new String[]{field.getName()};
resource.name = field.getName();
resource.annotations = field.getAnnotations();
resource.field = field;
return resource;
} catch (Exception e) {
throw new RuntimeException("failed to onAttachView resource for field: " + field, e);
}
}
示例12: buildBeanProperty
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Build a {@link BeanProperty} instance using given {@link PropertyDescriptor}.
* @param beanClass Bean class
* @param parentPath Optional parent path
* @param parent Parent bean property if bean class is a nested bean class
* @param propertyDescriptor Bean property descriptor
* @return BeanProperty instance
* @throws BeanIntrospectionException Error introspecting bean class
*/
private Optional<BeanProperty<?>> buildBeanProperty(Class<?> beanClass, Path<?> parentPath, BeanProperty<?> parent,
PropertyDescriptor propertyDescriptor) throws BeanIntrospectionException {
// annotations
Annotation[] annotations = null;
Field propertyField = findDeclaredField(beanClass, propertyDescriptor.getName());
if (propertyField != null) {
// check ignore
if (propertyField.isAnnotationPresent(Ignore.class)) {
LOGGER.debug(() -> "Bean class [" + beanClass + "] - Property " + propertyDescriptor.getName()
+ " ignored according to IgnoreProperty annotation ");
return Optional.empty();
}
annotations = propertyField.getAnnotations();
}
BeanProperty.Builder<?> property = BeanProperty
.builder(propertyDescriptor.getName(), propertyDescriptor.getPropertyType()).parent(parent)
.readMethod(propertyDescriptor.getReadMethod()).writeMethod(propertyDescriptor.getWriteMethod())
.field(propertyField).annotations(annotations);
if (parent == null && parentPath != null) {
property.parent(parentPath);
}
// post processors
property = postProcessBeanProperty(property, beanClass);
return Optional.of(property);
}
示例13: getAnnotations
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static Annotation[] getAnnotations(Field f, Class<?> filterBy, boolean inherits)
{
ArrayList<Annotation> filteredAnns = new ArrayList<Annotation>();
Annotation[] allAnns = (inherits ? f.getAnnotations() : f.getDeclaredAnnotations());
for (Annotation ann : allAnns) {
if (filterBy.isAssignableFrom(ann.annotationType()))
filteredAnns.add(ann);
}
return allAnns;
}
示例14: scan
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Override
public Map<Field, Set<Annotation>> scan(final Class t) {
final Map<Field, Set<Annotation>> classFieldAnnotations = new HashMap<>();
try {
for(final Field field : t.getDeclaredFields()) {
for(final Annotation annotation : field.getAnnotations()) {
final Set<Annotation> annotatedField = classFieldAnnotations.putIfAbsent(field, createNode(annotation));
if(annotatedField != null) {
annotatedField.add(annotation);
classFieldAnnotations.replace(field, annotatedField);
}
for(Annotation primeAnnotation : annotation.annotationType().getDeclaredAnnotations()) {
final Set<Annotation> fieldPrimeAnnotated = classFieldAnnotations.putIfAbsent(field, createNode(primeAnnotation));
if(fieldPrimeAnnotated != null) {
fieldPrimeAnnotated.add(primeAnnotation);
classFieldAnnotations.replace(field, fieldPrimeAnnotated);
}
}
}
}
} catch (SecurityException e) {
logger.warning(e.toString());
}
return classFieldAnnotations;
}
示例15: getAnnotations
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 通過Field獲取Annotation[]
*
* @param field <p>需要獲取注解列表的屬性</p>
* <p>建議field通過FieldFactory獲取</p>
* @return
*/
public static Annotation[] getAnnotations(Field field) {
Annotation[] annos = annoCache.get(field);
if (annos == null) {
annos = field.getAnnotations();
annoCache.put(field, annos);
}
return annos;
}