aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/turbine/bytecode/AnnotationWriter.java
blob: 34d62625edc07463e3fb74f2e52819b271499abe (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.turbine.bytecode;

import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteArrayDataOutput;
import com.google.turbine.bytecode.ClassFile.AnnotationInfo;
import com.google.turbine.bytecode.ClassFile.AnnotationInfo.ElementValue;
import com.google.turbine.bytecode.ClassFile.AnnotationInfo.ElementValue.ArrayValue;
import com.google.turbine.bytecode.ClassFile.AnnotationInfo.ElementValue.ConstTurbineAnnotationValue;
import com.google.turbine.bytecode.ClassFile.AnnotationInfo.ElementValue.ConstTurbineClassValue;
import com.google.turbine.bytecode.ClassFile.AnnotationInfo.ElementValue.ConstValue;
import com.google.turbine.bytecode.ClassFile.AnnotationInfo.ElementValue.EnumConstValue;
import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo;
import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo.FormalParameterTarget;
import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo.SuperTypeTarget;
import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo.Target;
import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo.ThrowsTarget;
import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo.TypeParameterBoundTarget;
import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo.TypeParameterTarget;
import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo.TypePath;
import com.google.turbine.model.Const.Value;
import java.util.Map;

/** Writes an {@link AnnotationInfo} to a class file. */
public class AnnotationWriter {

  final ConstantPool pool;
  final ByteArrayDataOutput output;

  public AnnotationWriter(ConstantPool pool, ByteArrayDataOutput output) {
    this.pool = pool;
    this.output = output;
  }

  public void writeAnnotation(AnnotationInfo annotation) {
    output.writeShort(pool.utf8(annotation.typeName()));
    output.writeShort(annotation.elementValuePairs().size());
    for (Map.Entry<String, ElementValue> entry : annotation.elementValuePairs().entrySet()) {
      output.writeShort(pool.utf8(entry.getKey()));
      writeElementValue(entry.getValue());
    }
  }

  void writeElementValue(ElementValue value) {
    switch (value.kind()) {
      case CONST:
        writeConstElementValue(((ConstValue) value).value());
        break;
      case ENUM:
        writeEnumElementValue((EnumConstValue) value);
        break;
      case CLASS:
        writeClassElementValue((ConstTurbineClassValue) value);
        break;
      case ARRAY:
        writeArrayElementValue((ArrayValue) value);
        break;
      case ANNOTATION:
        writeAnnotationElementValue((ConstTurbineAnnotationValue) value);
        break;
    }
  }

  private void writeConstElementValue(Value value) {
    switch (value.constantTypeKind()) {
      case BYTE:
        writeConst('B', pool.integer(value.asInteger().value()));
        break;
      case CHAR:
        writeConst('C', pool.integer(value.asInteger().value()));
        break;
      case SHORT:
        writeConst('S', pool.integer(value.asInteger().value()));
        break;
      case DOUBLE:
        writeConst('D', pool.doubleInfo(value.asDouble().value()));
        break;
      case FLOAT:
        writeConst('F', pool.floatInfo(value.asFloat().value()));
        break;
      case INT:
        writeConst('I', pool.integer(value.asInteger().value()));
        break;
      case LONG:
        writeConst('J', pool.longInfo(value.asLong().value()));
        break;
      case STRING:
        writeConst('s', pool.utf8(value.asString().value()));
        break;
      case BOOLEAN:
        writeConst('Z', pool.integer(value.asBoolean().value() ? 1 : 0));
        break;
      default:
        throw new AssertionError(value.constantTypeKind());
    }
  }

  private void writeConst(char tag, int index) {
    output.writeByte(tag);
    output.writeShort(index);
  }

  private void writeEnumElementValue(EnumConstValue value) {
    output.writeByte('e');
    output.writeShort(pool.utf8(value.typeName()));
    output.writeShort(pool.utf8(value.constName()));
  }

  private void writeClassElementValue(ConstTurbineClassValue value) {
    output.writeByte('c');
    output.writeShort(pool.utf8(value.className()));
  }

  private void writeArrayElementValue(ArrayValue value) {
    output.writeByte('[');
    output.writeShort(value.elements().size());
    for (ElementValue elementValue : value.elements()) {
      writeElementValue(elementValue);
    }
  }

  private void writeAnnotationElementValue(ConstTurbineAnnotationValue value) {
    output.writeByte('@');
    writeAnnotation(value.annotation());
  }

  public void writeTypeAnnotation(TypeAnnotationInfo annotation) {
    output.writeByte(annotation.targetType().tag());
    writeTypeAnnotationTarget(annotation.target());
    writePath(annotation.path());
    writeAnnotation(annotation.anno());
  }

  private void writePath(TypePath path) {
    ImmutableList<TypePath> flat = path.flatten();
    output.writeByte(flat.size());
    for (TypePath curr : flat) {
      output.writeByte(curr.tag());
      output.writeByte(curr.typeArgumentIndex());
    }
  }

  private void writeTypeAnnotationTarget(Target target) {
    switch (target.kind()) {
      case EMPTY:
        break;
      case TYPE_PARAMETER:
        output.writeByte(((TypeParameterTarget) target).index());
        break;
      case FORMAL_PARAMETER:
        output.writeByte(((FormalParameterTarget) target).index());
        break;
      case THROWS:
        output.writeShort(((ThrowsTarget) target).index());
        break;
      case SUPERTYPE:
        output.writeShort(((SuperTypeTarget) target).index());
        break;
      case TYPE_PARAMETER_BOUND:
        TypeParameterBoundTarget typeParameterBoundTarget = (TypeParameterBoundTarget) target;
        output.writeByte(typeParameterBoundTarget.typeParameterIndex());
        output.writeByte(typeParameterBoundTarget.boundIndex());
        break;
    }
  }
}