View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit.asm.fields;
7   
8   import edu.umd.cs.findbugs.annotations.NonNull;
9   import edu.umd.cs.findbugs.annotations.Nullable;
10  
11  import mockit.asm.AnnotatedReader;
12  import mockit.asm.classes.ClassReader;
13  import mockit.asm.classes.ClassVisitor;
14  
15  import org.checkerframework.checker.index.qual.NonNegative;
16  
17  public final class FieldReader extends AnnotatedReader {
18      @NonNull
19      private final ClassVisitor cv;
20      @Nullable
21      private Object constantValue;
22  
23      public FieldReader(@NonNull ClassReader cr, @NonNull ClassVisitor cv) {
24          super(cr);
25          this.cv = cv;
26      }
27  
28      /**
29       * Reads each field and makes the given visitor visit it.
30       *
31       * @return the offset of the first byte following the last field in the class.
32       */
33      @NonNegative
34      public int readFields() {
35          for (int fieldCount = readUnsignedShort(); fieldCount > 0; fieldCount--) {
36              readField();
37          }
38  
39          return codeIndex;
40      }
41  
42      private void readField() {
43          access = readUnsignedShort();
44          String name = readNonnullUTF8();
45          String desc = readNonnullUTF8();
46          constantValue = null;
47  
48          readAttributes();
49  
50          FieldVisitor fv = cv.visitField(access, name, desc, signature, constantValue);
51  
52          if (fv != null) {
53              readAnnotations(fv);
54              fv.visitEnd();
55          }
56      }
57  
58      @Nullable
59      @Override
60      protected Boolean readAttribute(@NonNull String attributeName) {
61          if ("ConstantValue".equals(attributeName)) {
62              int constItemIndex = readUnsignedShort();
63  
64              if (constItemIndex > 0) {
65                  constantValue = readConst(constItemIndex);
66              }
67  
68              return true;
69          }
70  
71          return null;
72      }
73  }