1 /* 2 * Copyright 2016 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.turbine.bytecode; 18 19 import com.google.common.collect.ImmutableList; 20 import com.google.turbine.bytecode.ClassFile.AnnotationInfo; 21 import com.google.turbine.bytecode.ClassFile.MethodInfo.ParameterInfo; 22 import com.google.turbine.bytecode.ClassFile.TypeAnnotationInfo; 23 import com.google.turbine.model.Const.Value; 24 import java.util.List; 25 26 /** Well-known JVMS §4.1 attributes. */ 27 interface Attribute { 28 29 enum Kind { 30 SIGNATURE("Signature"), 31 EXCEPTIONS("Exceptions"), 32 INNER_CLASSES("InnerClasses"), 33 CONSTANT_VALUE("ConstantValue"), 34 RUNTIME_VISIBLE_ANNOTATIONS("RuntimeVisibleAnnotations"), 35 RUNTIME_INVISIBLE_ANNOTATIONS("RuntimeInvisibleAnnotations"), 36 ANNOTATION_DEFAULT("AnnotationDefault"), 37 RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS("RuntimeVisibleParameterAnnotations"), 38 RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS("RuntimeInvisibleParameterAnnotations"), 39 DEPRECATED("Deprecated"), 40 RUNTIME_VISIBLE_TYPE_ANNOTATIONS("RuntimeVisibleTypeAnnotations"), 41 RUNTIME_INVISIBLE_TYPE_ANNOTATIONS("RuntimeInvisibleTypeAnnotations"), 42 METHOD_PARAMETERS("MethodParameters"); 43 44 private final String signature; 45 Kind(String signature)46 Kind(String signature) { 47 this.signature = signature; 48 } 49 signature()50 public String signature() { 51 return signature; 52 } 53 } 54 kind()55 Kind kind(); 56 57 /** A JVMS §4.7.6 InnerClasses attribute. */ 58 class InnerClasses implements Attribute { 59 60 final List<ClassFile.InnerClass> inners; 61 InnerClasses(List<ClassFile.InnerClass> inners)62 public InnerClasses(List<ClassFile.InnerClass> inners) { 63 this.inners = inners; 64 } 65 66 @Override kind()67 public Kind kind() { 68 return Kind.INNER_CLASSES; 69 } 70 } 71 72 /** A JVMS §4.7.9 Signature attribute. */ 73 class Signature implements Attribute { 74 75 final String signature; 76 Signature(String signature)77 public Signature(String signature) { 78 this.signature = signature; 79 } 80 81 @Override kind()82 public Kind kind() { 83 return Kind.SIGNATURE; 84 } 85 } 86 87 /** A JVMS §4.7.5 Exceptions attribute. */ 88 class ExceptionsAttribute implements Attribute { 89 90 final List<String> exceptions; 91 ExceptionsAttribute(List<String> exceptions)92 public ExceptionsAttribute(List<String> exceptions) { 93 this.exceptions = exceptions; 94 } 95 96 @Override kind()97 public Kind kind() { 98 return Kind.EXCEPTIONS; 99 } 100 } 101 102 interface Annotations extends Attribute { annotations()103 List<AnnotationInfo> annotations(); 104 } 105 106 /** A JVMS §4.7.16 RuntimeVisibleAnnotations attribute. */ 107 class RuntimeVisibleAnnotations implements Annotations { 108 List<AnnotationInfo> annotations; 109 RuntimeVisibleAnnotations(List<AnnotationInfo> annotations)110 public RuntimeVisibleAnnotations(List<AnnotationInfo> annotations) { 111 this.annotations = annotations; 112 } 113 114 @Override annotations()115 public List<AnnotationInfo> annotations() { 116 return annotations; 117 } 118 119 @Override kind()120 public Kind kind() { 121 return Kind.RUNTIME_VISIBLE_ANNOTATIONS; 122 } 123 } 124 125 /** A JVMS §4.7.17 RuntimeInvisibleAnnotations attribute. */ 126 class RuntimeInvisibleAnnotations implements Annotations { 127 List<AnnotationInfo> annotations; 128 RuntimeInvisibleAnnotations(List<AnnotationInfo> annotations)129 public RuntimeInvisibleAnnotations(List<AnnotationInfo> annotations) { 130 this.annotations = annotations; 131 } 132 133 @Override annotations()134 public List<AnnotationInfo> annotations() { 135 return annotations; 136 } 137 138 @Override kind()139 public Kind kind() { 140 return Kind.RUNTIME_INVISIBLE_ANNOTATIONS; 141 } 142 } 143 144 /** A JVMS §4.7.2 ConstantValue attribute. */ 145 class ConstantValue implements Attribute { 146 147 final Value value; 148 ConstantValue(Value value)149 public ConstantValue(Value value) { 150 this.value = value; 151 } 152 153 @Override kind()154 public Kind kind() { 155 return Kind.CONSTANT_VALUE; 156 } 157 } 158 159 /** A JVMS §4.7.22 AnnotationDefault attribute. */ 160 class AnnotationDefault implements Attribute { 161 162 private final AnnotationInfo.ElementValue value; 163 AnnotationDefault(AnnotationInfo.ElementValue value)164 public AnnotationDefault(AnnotationInfo.ElementValue value) { 165 this.value = value; 166 } 167 168 @Override kind()169 public Kind kind() { 170 return Kind.ANNOTATION_DEFAULT; 171 } 172 value()173 public AnnotationInfo.ElementValue value() { 174 return value; 175 } 176 } 177 178 interface ParameterAnnotations extends Attribute { annotations()179 List<List<AnnotationInfo>> annotations(); 180 } 181 182 /** A JVMS §4.7.18 RuntimeVisibleParameterAnnotations attribute. */ 183 class RuntimeVisibleParameterAnnotations implements ParameterAnnotations { 184 185 @Override annotations()186 public List<List<AnnotationInfo>> annotations() { 187 return annotations; 188 } 189 190 final List<List<AnnotationInfo>> annotations; 191 RuntimeVisibleParameterAnnotations(List<List<AnnotationInfo>> annotations)192 public RuntimeVisibleParameterAnnotations(List<List<AnnotationInfo>> annotations) { 193 this.annotations = annotations; 194 } 195 196 @Override kind()197 public Kind kind() { 198 return Kind.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS; 199 } 200 } 201 202 /** A JVMS §4.7.19 RuntimeInvisibleParameterAnnotations attribute. */ 203 class RuntimeInvisibleParameterAnnotations implements ParameterAnnotations { 204 205 @Override annotations()206 public List<List<AnnotationInfo>> annotations() { 207 return annotations; 208 } 209 210 final List<List<AnnotationInfo>> annotations; 211 RuntimeInvisibleParameterAnnotations(List<List<AnnotationInfo>> annotations)212 public RuntimeInvisibleParameterAnnotations(List<List<AnnotationInfo>> annotations) { 213 this.annotations = annotations; 214 } 215 216 @Override kind()217 public Kind kind() { 218 return Kind.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS; 219 } 220 } 221 222 /** A JVMS §4.7.15 Deprecated attribute. */ 223 Attribute DEPRECATED = 224 new Attribute() { 225 @Override 226 public Kind kind() { 227 return Kind.DEPRECATED; 228 } 229 }; 230 231 interface TypeAnnotations extends Attribute { annotations()232 ImmutableList<TypeAnnotationInfo> annotations(); 233 } 234 235 /** A JVMS §4.7.20 RuntimeInvisibleTypeAnnotations attribute. */ 236 class RuntimeInvisibleTypeAnnotations implements TypeAnnotations { 237 final ImmutableList<TypeAnnotationInfo> annotations; 238 RuntimeInvisibleTypeAnnotations(ImmutableList<TypeAnnotationInfo> annotations)239 public RuntimeInvisibleTypeAnnotations(ImmutableList<TypeAnnotationInfo> annotations) { 240 this.annotations = annotations; 241 } 242 243 @Override kind()244 public Kind kind() { 245 return Kind.RUNTIME_INVISIBLE_TYPE_ANNOTATIONS; 246 } 247 248 @Override annotations()249 public ImmutableList<TypeAnnotationInfo> annotations() { 250 return annotations; 251 } 252 } 253 254 /** A JVMS §4.7.20 RuntimeVisibleTypeAnnotations attribute. */ 255 class RuntimeVisibleTypeAnnotations implements TypeAnnotations { 256 final ImmutableList<TypeAnnotationInfo> annotations; 257 RuntimeVisibleTypeAnnotations(ImmutableList<TypeAnnotationInfo> annotations)258 public RuntimeVisibleTypeAnnotations(ImmutableList<TypeAnnotationInfo> annotations) { 259 this.annotations = annotations; 260 } 261 262 @Override kind()263 public Kind kind() { 264 return Kind.RUNTIME_VISIBLE_TYPE_ANNOTATIONS; 265 } 266 267 @Override annotations()268 public ImmutableList<TypeAnnotationInfo> annotations() { 269 return annotations; 270 } 271 } 272 273 /** A JVMS §4.7.24 MethodParameters attribute. */ 274 class MethodParameters implements Attribute { 275 private final ImmutableList<ParameterInfo> parameters; 276 MethodParameters(ImmutableList<ParameterInfo> parameters)277 public MethodParameters(ImmutableList<ParameterInfo> parameters) { 278 this.parameters = parameters; 279 } 280 281 /** The parameters. */ parameters()282 public ImmutableList<ParameterInfo> parameters() { 283 return parameters; 284 } 285 286 @Override kind()287 public Kind kind() { 288 return Kind.METHOD_PARAMETERS; 289 } 290 } 291 } 292