Subversion Repositories bacoAlunos

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1878 jmachado 1
package com.owlike.genson.reflect;
2
 
3
import java.lang.annotation.Annotation;
4
import java.lang.reflect.Type;
5
import java.util.Arrays;
6
import java.util.Collections;
7
import java.util.HashSet;
8
import java.util.Set;
9
 
10
import com.owlike.genson.annotation.JsonProperty;
11
 
12
/**
13
 * Represents a bean property, in practice it can be an object field, method (getter/setter) or
14
 * constructor parameter.
15
 *
16
 * @author eugen
17
 */
18
public abstract class BeanProperty {
19
  protected final 1.5.0/docs/api/java/lang/String.html">String name;
20
  protected final 1.5.0/docs/api/java/lang/reflect/Type.html">Type type;
21
  protected final Class<?> declaringClass;
22
  protected final Class<?> concreteClass;
23
  protected 5+0%2Fdocs%2Fapi+Annotation">Annotation[] annotations;
24
  protected final int modifiers;
25
 
26
  protected BeanProperty(1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/reflect/Type.html">Type type, Class<?> declaringClass,
27
                         Class<?> concreteClass, 5+0%2Fdocs%2Fapi+Annotation">Annotation[] annotations, int modifiers) {
28
    this.name = name;
29
    this.type = type;
30
    this.declaringClass = declaringClass;
31
    this.concreteClass = concreteClass;
32
    this.annotations = annotations;
33
    this.modifiers = modifiers;
34
  }
35
 
36
  /**
37
   * @return The class in which this property is declared
38
   */
39
  public Class<?> getDeclaringClass() {
40
    return declaringClass;
41
  }
42
 
43
  /**
44
   * @return The final concrete class from which this property has been resolved.
45
   * For example if this property is defined in class Root but was resolved for class Child extends Root,
46
   * then getConcreteClass would return Child class and getDeclaringClass would return Root class.
47
   */
48
  public Class<?> getConcreteClass() { return concreteClass; }
49
 
50
  /**
51
   * The name of this property (not necessarily the original one).
52
   */
53
  public 1.5.0/docs/api/java/lang/String.html">String getName() {
54
    return name;
55
  }
56
 
57
  /**
58
   * @return the type of the property
59
   */
60
  public 1.5.0/docs/api/java/lang/reflect/Type.html">Type getType() {
61
    return type;
62
  }
63
 
64
  public Class<?> getRawClass() {
65
    return TypeUtil.getRawClass(type);
66
  }
67
 
68
  public int getModifiers() {
69
    return modifiers;
70
  }
71
 
72
  public 1.5.0/docs/api/java/lang/String.html">String[] aliases() {
73
    JsonProperty ann = getAnnotation(JsonProperty.class);
74
    return ann != null ? ann.aliases() : new 1.5.0/docs/api/java/lang/String.html">String[]{};
75
  }
76
 
77
  public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
78
    for (5+0%2Fdocs%2Fapi+Annotation">Annotation ann : annotations)
79
      if (annotationClass.isInstance(ann)) return annotationClass.cast(ann);
80
    return null;
81
  }
82
 
83
  void updateBoth(BeanProperty otherBeanProperty) {
84
 
85
    // FIXME: we don't care for duplicate annotations as it should not change the behaviour - actually we do as it can change the behaviour...
86
    // an easy solution would be to forbid duplicate annotations, which can make sense.
87
    if (annotations.length > 0 || otherBeanProperty.annotations.length > 0) {
88
      5+0%2Fdocs%2Fapi+Annotation">Annotation[] mergedAnnotations = new 5+0%2Fdocs%2Fapi+Annotation">Annotation[annotations.length + otherBeanProperty.annotations.length];
89
 
90
      1.5.0/docs/api/java/lang/System.html">System.arraycopy(annotations, 0, mergedAnnotations, 0, annotations.length);
91
      1.5.0/docs/api/java/lang/System.html">System.arraycopy(otherBeanProperty.annotations, 0, mergedAnnotations, annotations.length, otherBeanProperty.annotations.length);
92
 
93
      if (otherBeanProperty.annotations.length > 0) this.annotations = mergedAnnotations;
94
      // update also the other bean property with the merged result.
95
      // This is easier rather than do it in one direction and then in the other one.
96
      if (annotations.length > 0) otherBeanProperty.annotations = mergedAnnotations;
97
    }
98
  }
99
 
100
  /**
101
   * Used to give priority to implementations, for example by default a method would have a higher
102
   * priority than a field because it can do some logic. The greater the priority value is the
103
   * more important is this BeanProperty.
104
   *
105
   * @return the priority of this BeanProperty
106
   */
107
  abstract int priority();
108
 
109
  abstract 1.5.0/docs/api/java/lang/String.html">String signature();
110
}