aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/proxies/FieldProxy.java
blob: af6f8df45c15e0643b6a8c328c59cce52f223db1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package ftbsc.lll.proxies;

import ftbsc.lll.tools.DescriptorBuilder;
import org.objectweb.asm.Type;

import java.lang.reflect.Field;

/**
 * A container for information about class fields to be used
 * in ASM patching.
 * @since 0.3.0
 */
public class FieldProxy extends AbstractProxy {

   /**
    * The descriptor of the field's type.
    */
   private final String typeDescriptor;

   /**
    * A public constructor, builds a proxy from a {@link Field}
    * obtained from reflection.
    * @param f the {@link Field} object corresponding to this.
    */
   public FieldProxy(Field f) {
      super(f.getName(), f.getModifiers(), Type.getInternalName(f.getDeclaringClass()));
      this.typeDescriptor = Type.getDescriptor(f.getType());
   }

   /**
    * A protected constructor, called only from the builder.
    * @param name the name of the field
    * @param modifiers the modifiers of the field
    * @param parent the FQN of the parent class of the field
    * @param typeDescriptor the type descriptor of the field
    */
   FieldProxy(String name, int modifiers, String parent, String typeDescriptor) {
      super(name, modifiers, parent);
      this.typeDescriptor = typeDescriptor;
   }

   /**
    * @return the field's type descriptor
    */
   @Override
   public String getDescriptor() {
      return typeDescriptor;
   }

   /**
    * Returns a new instance of {@link FieldProxy.Builder}.
    * @param name the name of the field
    * @return the builder object for field proxies
    */
   public static Builder builder(String name) {
      return new Builder(name);
   }

   public static class Builder extends AbstractProxy.Builder<FieldProxy> {
      /**
       * The descriptor of the field's type.
       */
      private String typeDescriptor;

      /**
       * The constructor of the builder, used only internally.
       * @param name the name of the field
       */
      Builder(String name) {
         super(name);
      }

      /**
       * Sets the descriptor of the field type to the given {@link String}.
       * @param typeDescriptor the descriptor of the field type
       * @return the builder's state after the change
       */
      public Builder setDescriptor(String typeDescriptor) {
         this.typeDescriptor = typeDescriptor;
         return this;
      }

      /**
       * Sets the descriptor of the field type to match the give {@link Class}.
       * @param fqn the fully qualified name of the field type
       * @param arrayLevel the array level of the field type
       * @return the builder's state after the change
       */
      public Builder setType(String fqn, int arrayLevel) {
         this.typeDescriptor = DescriptorBuilder.nameToDescriptor(fqn, arrayLevel);
         return this;
      }

      /**
       * Sets the descriptor of the field type to match the give {@link Class}.
       * @param type a {@link Class} object representing the field type
       * @return the builder's state after the change
       */
      public Builder setType(Class<?> type) {
         this.typeDescriptor = Type.getDescriptor(type);
         return this;
      }

      /**
       * Builds a {@link FieldProxy} of the given kind.
       * @return the built {@link FieldProxy}
       */
      @Override
      public FieldProxy build() {
         return new FieldProxy(this.name, this.modifiers, this.parent, this.typeDescriptor);
      }
   }
}