check for annotation type @transient

时间:2016-03-17 10:15:37

标签: java java-ee-7

I want to skip fields annotadet with @transient in a query builder.

Something like

if(!fcol.annotationType().equals(@Transient){ do something }

somebody have any idea?

3 个答案:

答案 0 :(得分:1)

Reflections reflections = new Reflections(packageName);
reflections.getFieldsAnnotatedWith(javax.persistence.Transient.class))

答案 1 :(得分:1)

您可以在字段上使用以下方法检查字段是否具有注释(假设您的fcol的类型为java.lang.reflect.Field):

View.OnClickListener getButtonAndDoAction(final ImageButton button)  {
    return new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "button clicked" +button.getId(), Toast.LENGTH_SHORT).show();
        }
    };

使用该字段的一个例子是:

   /**
     * Returns this element's annotation for the specified type if
     * such an annotation is <em>present</em>, else null.
     *
     * @param <T> the type of the annotation to query for and return if present
     * @param annotationClass the Class object corresponding to the
     *        annotation type
     * @return this element's annotation for the specified annotation type if
     *     present on this element, else null
     * @throws NullPointerException if the given annotation class is null
     * @since 1.5
     */
    <T extends Annotation> T getAnnotation(Class<T> annotationClass);

答案 2 :(得分:0)

假设myObject是Entity的一个实例。您必须遍历该类的所有字段并检查是否存在使用@Transient注释的字段。如果此调用返回null,则表示该字段未使用此批注进行批注。所以你可以做任何你喜欢的事情:

Field[] fields = myObject.getClass().getFields();
for (Field field : fields) {
    Transient transientAnnoation = field.getAnnotation(Transient.class);
    if (transientAnnoation  == null) {
        do something
    }
 }