當前位置: 首頁>>代碼示例>>Java>>正文


Java Field.getAnnotations方法代碼示例

本文整理匯總了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);
                }
            }
        }
    }
}
 
開發者ID:sdrausty,項目名稱:buildAPKsSamples,代碼行數:35,代碼來源:Injector.java

示例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));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:21,代碼來源:InjectionPoint.java

示例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;
}
 
開發者ID:zdongcoding,項目名稱:jsouplib,代碼行數:25,代碼來源:ClassReader.java

示例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);
}
 
開發者ID:qinhehu,項目名稱:Gesture,代碼行數:21,代碼來源:GestureEngine.java

示例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;
}
 
開發者ID:Shynixn,項目名稱:BlockBall,代碼行數:29,代碼來源:YamlSerializer.java

示例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];
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:HotSpotResolvedJavaFieldImpl.java

示例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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:RuntimeInlineAnnotationReader.java

示例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;
}
 
開發者ID:SPIRIT-21,項目名稱:javadoc2swagger,代碼行數:46,代碼來源:DefinitionParser.java

示例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;
}
 
開發者ID:pablo127,項目名稱:GPigValidator,代碼行數:8,代碼來源:Validator.java

示例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;
}
 
開發者ID:maihaoche,項目名稱:rocketmq-spring-boot-starter,代碼行數:38,代碼來源:AbstractMQProducer.java

示例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);
    }
}
 
開發者ID:zdongcoding,項目名稱:jsouplib,代碼行數:13,代碼來源:ClassReader.java

示例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);

}
 
開發者ID:holon-platform,項目名稱:holon-core,代碼行數:41,代碼來源:DefaultBeanIntrospector.java

示例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;
	
}
 
開發者ID:datancoffee,項目名稱:sirocco,代碼行數:14,代碼來源:FieldSupport.java

示例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;
}
 
開發者ID:GoodforGod,項目名稱:dummymaker,代碼行數:31,代碼來源:AnnotationScanner.java

示例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;
}
 
開發者ID:Strangeen,項目名稱:excel-util4j,代碼行數:18,代碼來源:AnnotationFactory.java


注:本文中的java.lang.reflect.Field.getAnnotations方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。