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


Java Field.getDeclaredAnnotations方法代碼示例

本文整理匯總了Java中java.lang.reflect.Field.getDeclaredAnnotations方法的典型用法代碼示例。如果您正苦於以下問題:Java Field.getDeclaredAnnotations方法的具體用法?Java Field.getDeclaredAnnotations怎麽用?Java Field.getDeclaredAnnotations使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.reflect.Field的用法示例。


在下文中一共展示了Field.getDeclaredAnnotations方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getFieldAnnotations

import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
 * Gets json names from module fields annotations.
 *
 * @param type The Java module type.
 * @return List of json property names.
 */
private static List<ModuleProperty> getFieldAnnotations(Class type) {
    List<ModuleProperty> modelProperties = new ArrayList<ModuleProperty>();
    Field[] fields = type.getDeclaredFields();
     for(Field field : fields) {
        Annotation[] annotations = field.getDeclaredAnnotations();

        for(Annotation annotation : annotations){
            if(annotation instanceof JsonProperty){
                JsonProperty property = (JsonProperty) annotation;
                ModuleProperty moduleProperty = new ModuleProperty();
                moduleProperty.name = field.getName();
                moduleProperty.jsonName = property.value();
                moduleProperty.type = field.getType();
                moduleProperty.isNumeric = isTypeNumeric(field.getType());
                modelProperties.add(moduleProperty);
            }
        }
    }

    return modelProperties;
}
 
開發者ID:mattkol,項目名稱:SugarOnRest,代碼行數:28,代碼來源:ModuleInfo.java

示例2: scanSpecific

import java.lang.reflect.Field; //導入方法依賴的package包/類
private void scanSpecific(final Field field) {
    // Vert.x Defined
    final Set<Class<? extends Annotation>> defineds
            = Plugins.INFIX_MAP.keySet();
    final Annotation[] annotations = field.getDeclaredAnnotations();
    // Annotation counter
    final Set<String> set = new HashSet<>();
    final Annotation hitted = Observable.fromArray(annotations)
            .filter(annotation -> defineds.contains(annotation.annotationType()))
            .map(annotation -> {
                set.add(annotation.annotationType().getName());
                return annotation;
            }).blockingFirst();
    // Duplicated annotated
    Fn.flingUp(Values.ONE < set.size(), LOGGER,
            MultiAnnotatedException.class, getClass(),
            field.getName(), field.getDeclaringClass().getName(), set);
    // Fill typed directly.
    LOGGER.info(Info.SCANED_FIELD, this.reference,
            field.getName(),
            field.getDeclaringClass().getName(),
            hitted.annotationType().getName());
    this.fieldMap.put(field.getName(), field.getType());
}
 
開發者ID:silentbalanceyh,項目名稱:vertx-zero,代碼行數:25,代碼來源:AffluxThread.java

示例3: MentionedClass

import java.lang.reflect.Field; //導入方法依賴的package包/類
public MentionedClass()
{
    for (Field f : this.getClass().getDeclaredFields())
    {
        for (Annotation ann : f.getDeclaredAnnotations())
        {
            if (ann instanceof Mentioned)
            {
                try
                {
                    System.out.println("[Reflection] " + f.getType().getName() + " " + f.getName() + " = " + f.get(this));
                }
                catch(IllegalAccessException e)
                {}
            }
        }
    }
}
 
開發者ID:Plasmoxy,項目名稱:AquamarineLake,代碼行數:19,代碼來源:MentionedClass.java

示例4: isInjectedField

import java.lang.reflect.Field; //導入方法依賴的package包/類
private static boolean isInjectedField(final Field field) {
    for (final Annotation a : field.getDeclaredAnnotations()) {
        final Class<?> t = a.annotationType();
        if (t == javax.inject.Inject.class ||
                t == javax.ws.rs.core.Context.class ||
                t == javax.ws.rs.CookieParam.class ||
                t == javax.ws.rs.FormParam.class ||
                t == javax.ws.rs.HeaderParam.class ||
                t == javax.ws.rs.QueryParam.class ||
                t == javax.ws.rs.PathParam.class ||
                t == javax.ws.rs.BeanParam.class ||
                t == OptionalClasses.PERSISTENCE_CONTEXT) {
            return true;
        }
    }
    return false;
}
 
開發者ID:minijax,項目名稱:minijax,代碼行數:18,代碼來源:ConstructorProviderBuilder.java

示例5: invokeAnnotationByField

import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
 * Invokes the annotation of the given field
 *
 * @param field           field
 * @param annotationClazz annotation
 * @param <T>             returnType
 * @return returnValue
 */
public static <T extends Annotation> T invokeAnnotationByField(Field field, Class<T> annotationClazz) {
    if (field == null)
        throw new IllegalArgumentException("Field cannot be null!");
    if (annotationClazz == null)
        throw new IllegalArgumentException("AnnotationClass cannot be null!");
    for (final Annotation annotation : field.getDeclaredAnnotations()) {
        if (annotation.annotationType() == annotationClazz)
            return (T) annotation;
    }
    return null;
}
 
開發者ID:Shynixn,項目名稱:PetBlocks,代碼行數:20,代碼來源:ReflectionUtils.java

示例6: checkVariable

import java.lang.reflect.Field; //導入方法依賴的package包/類
private void checkVariable(Class clazz) {
    List<AnnotationField> annotationFields = (List<AnnotationField>) annotationObjectMap.get(AnnotationObjectType.FIELD);
    if(annotationFields == null){
        annotationFields = new ArrayList<>();
    }
    Field[] fields = clazz.getDeclaredFields();
    if(fields== null || fields.length == 0){
        return;
    }
    for (Field field : fields){
        Annotation[] annotations = field.getDeclaredAnnotations();
        if(annotations== null || annotations.length==0)
            continue;
        for (int i=0;i<annotations.length;i++){
            Class<? extends Annotation> annotationType = annotations[i].annotationType();
            if(annotationType.equals(Autowired.class)){
                AnnotationField annotationField = AnnotationField.builder()
                                                    .belongClassName(clazz.getName())
                                                    .variableClassName(field.getType().getName())
                                                    .variableName(field.getName())
                                                    .build();
                annotationFields.add(annotationField);
            }
        }
    }
    annotationObjectMap.put(AnnotationObjectType.FIELD, annotationFields);
}
 
開發者ID:caoyj1991,項目名稱:Core-Java,代碼行數:28,代碼來源:FrameworkAnnotationResolver.java

示例7: search

import java.lang.reflect.Field; //導入方法依賴的package包/類
private Class<? extends Annotation> search(
        final Field field
) {
    final Annotation[] annotations = field.getDeclaredAnnotations();
    final Set<Class<? extends Annotation>>
            annotationCls = Plugins.INFIX_MAP.keySet();
    Class<? extends Annotation> hitted = null;
    for (final Annotation annotation : annotations) {
        if (annotationCls.contains(annotation.annotationType())) {
            hitted = annotation.annotationType();
            break;
        }
    }
    return hitted;
}
 
開發者ID:silentbalanceyh,項目名稱:vertx-zero,代碼行數:16,代碼來源:AffluxScatter.java

示例8: configureInjectField

import java.lang.reflect.Field; //導入方法依賴的package包/類
public void configureInjectField() {
    for (Class class1 : classList) {
        log.info("{} is being configure", class1);
        BeanMetaData metaData = getMetaData(class1);
        for (Field field : metaData.getFields()) {
            Annotation[] annotations = field.getDeclaredAnnotations();
            for ( Annotation annotation : annotations) {
                if (annotation instanceof Inject) {
                    try {
                        field.setAccessible(true);
                        //先從containner中尋找bean
                        Object object = this.register.getBean(Class.forName(field.getGenericType().toString().split(" ")[1]));

                        if (object == null) {
                            object = this.register.getBean(field.getType().getName());
                        }
                        //如果沒找到,則new一個
                        if (object == null) {
                            object = Class.forName(field.getGenericType().toString().split(" ")[1]).newInstance();
                        }

                        field.set(this.register.getBean(class1), object);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

}
 
開發者ID:zhyzhyzhy,項目名稱:Ink,代碼行數:33,代碼來源:BeanDefinitionReader.java

示例9: 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

示例10: getDeclaredAnnotations

import java.lang.reflect.Field; //導入方法依賴的package包/類
@Override
public Annotation[] getDeclaredAnnotations() {
    Field javaField = toJava();
    if (javaField != null) {
        return javaField.getDeclaredAnnotations();
    }
    return new Annotation[0];
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:HotSpotResolvedJavaFieldImpl.java

示例11: testField3

import java.lang.reflect.Field; //導入方法依賴的package包/類
public void testField3() throws NoSuchFieldException
{		
	Field myField = AnnotatedElements.class.getDeclaredField("field3");
	Annotation[] annotationArray = null;
	annotationArray = myField.getDeclaredAnnotations();
	int NumExpectedAnnotations = 0;
	try
	{
		NumExpectedAnnotations = myField.getInt(null);
	}
	catch (Exception e)
	{
		fail("Error in retrieving int value from field3!");
	}
	// Make sure the number of annotations is the same as the number in the returned array
	assertTrue( NumExpectedAnnotations == annotationArray.length);			
	for (int j=0; j<annotationArray.length; j++)
	{
		if (annotationArray[j] instanceof A5)
		{
			A5 a = (A5) annotationArray[j];
			assertTrue(a.priority() == Priority.MEDIUM);					
			assertTrue(a.createdBy().equalsIgnoreCase("Bob Bobson"));					
		}
	}
	Annotation myAnnotation = null;
	myAnnotation = myField.getAnnotation(A0.class);
	assertTrue(myAnnotation != null);
	
	myField.getDeclaredAnnotations();
	
	myAnnotation = myField.getAnnotation(A1.class);
	assertTrue(myAnnotation != null);	
	myAnnotation = myField.getAnnotation(A2.class);
	assertTrue(myAnnotation != null);
	myAnnotation = myField.getAnnotation(A3.class);
	assertTrue(myAnnotation != null);
	
	myField.getDeclaredAnnotations();
	
	myAnnotation = myField.getAnnotation(A4.class);
	assertTrue(myAnnotation != null);		
	myAnnotation = myField.getAnnotation(A5.class);
	assertTrue(myAnnotation != null);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-systemtest,代碼行數:46,代碼來源:AnnotationFieldTests.java

示例12: analyseField

import java.lang.reflect.Field; //導入方法依賴的package包/類
private StateField analyseField(@NonNull Field field, String tag) {
    if (!field.isAnnotationPresent(KeepState.class))
        return null;
    KeepState keepStateNotation = null;
    for (Annotation notation : field.getDeclaredAnnotations()) {
        if (notation.annotationType().equals(KeepState.class)) {
            keepStateNotation = (KeepState) notation;
            break;
        }
    }

    if (keepStateNotation == null)
        throw new MException("check logic,keepStateNotation is not supposed to be null");

    String fieldName = field.getName();
    String bundleKey = "key_" + tag + "#" + fieldName;

    Type type = keepStateNotation.type();

    if (type == Type.Infer)
        type = TypeInferUtils.infer(field);
    else if (type == Type.Object) {
        try {
            return analyseCustoms(field, keepStateNotation);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
            e.printStackTrace();
            throw new MException(e.getMessage());
        }
    } else {
        if (type.canBeChecked()) {
            boolean isCorrectType = type.check(field);
            if (!isCorrectType) {
                MagicBox.getLogger().error("", "unCorrect Type set for" + fieldName);
                return new StateField(fieldName, field, bundleKey, Type.Infer);
            }
        } else {
            MagicBox.getLogger().debug("", "you have notated " + fieldName + " as " + type.name() +
                    ", one cannot be checked, with cautions");
        }
    }

    return new StateField(fieldName, field, bundleKey, type);
}
 
開發者ID:leobert-lan,項目名稱:MagicBox,代碼行數:44,代碼來源:Secy.java

示例13: main

import java.lang.reflect.Field; //導入方法依賴的package包/類
public static void main(String[] args) {
    Member member = new Member();
    String[] classNames = {member.getClass().getName()};

    for (String className : classNames) {
        try {
            Class clazz = Class.forName(className);
            DBTable dbTable = (DBTable) clazz.getAnnotation(DBTable.class);
            if (dbTable == null) {
                System.out.println("no dbtable class");
                return;
            }
            String tableName = dbTable.name();
            if (tableName.length() < 1)
                tableName = clazz.getName().toUpperCase();
            List<String> columnDefs = new ArrayList<>();
            for (Field field : clazz.getDeclaredFields()) {
                String columnName = null;
                Annotation[] anns = field.getDeclaredAnnotations();
                if (anns.length < 1) {
                    continue;
                }
                if (anns[0] instanceof SQLInteger) {
                    SQLInteger integer = (SQLInteger) anns[0];
                    if (integer.name().length() < 1)
                        columnName = field.getName().toUpperCase();
                    else
                        columnName = integer.name();
                    columnDefs.add(columnName + " INT" + getConstraints(integer.constraints()));
                }

                if (anns[0] instanceof SQLString) {
                    SQLString string = (SQLString) anns[0];
                    if (string.name().length() < 1)
                        columnName = field.getName().toUpperCase();
                    else
                        columnName = string.name();
                    columnDefs.add(columnName + " VARCHAR(" + string.value() + ")" + getConstraints(string.constraints()));
                }
                StringBuilder createCommand  = new StringBuilder("CREATE TABLE " + tableName + "(");
                for (String columndef : columnDefs) {
                    createCommand.append("\n     " + columndef + ",");
                }
                String tableCreate = createCommand.substring(0, createCommand.length() - 1) + ")";
                System.out.println(tableCreate.toString());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
 
開發者ID:wuhighway,項目名稱:DailyStudy,代碼行數:52,代碼來源:TableCreator.java

示例14: build

import java.lang.reflect.Field; //導入方法依賴的package包/類
ObjectParser build() {
            for (Annotation annotation : objectAnnotations) {
                parseHttpAnnotation(annotation);
            }
            if (httpMethod == null) {
                throw objectError("HTTP method annotation is required (e.g., @GET, @POST, etc.).");
            }
//            if hasBody is true then skip ,if false then isMultipart and isFromEncoded not true
            if (!hasBody) { //if false ,no body,but has multipart or fromEncoded append err
                if (isMultipart) {
                    throw objectError(
                            "Multipart can only be specified on HTTP methods with request body (e.g., @POST).");
                }
                if (isFormEncoded) {
                    throw objectError("FormUrlEncoded can only be specified on HTTP methods with "
                            + "request body (e.g., @POST).");
                }
            }

            int fieldCount = fields.length;
            for (int p = 0; p < fieldCount; p++) {
                Field field = fields[p];
                Annotation[] annotations = field.getDeclaredAnnotations();
                if (annotations == null || annotations.length == 0) {
                    System.out.println(String.format("field %s no renovate annotation found", field.getName()));
                    continue;
                } else if (field.isAnnotationPresent(Ignore.class)) {
                    System.out.println(String.format("field %s,%s is ignored", clazz.getName(), field.getName()));
                    continue;
                }
                fieldParameterHandlerMap.put(field, parseParameter(p, field.getType(), annotations, field));
            }
            if (relativeUrl == null && !gotUrl) {
                throw objectError("Missing either @%s URL or @Url parameter.", httpMethod);
            }
            if (!isFormEncoded && !isMultipart && !hasBody && gotBody) {
                throw objectError("Non-body HTTP method cannot contain @Body.");
            }
            if (isFormEncoded && !gotField) {
                throw objectError("Form-encoded method must contain at least one @Params.");
            }
            if (isMultipart && !gotPart) {
                throw objectError("Multipart method must contain at least one @Part.");
            }

            return new ObjectParser(this);
        }
 
開發者ID:baby2431,項目名稱:renovate2,代碼行數:48,代碼來源:ObjectParser.java


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