1 /*
   2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.experimental.bytecode;
  27 
  28 import java.util.function.Consumer;
  29 import java.util.function.ToIntBiFunction;
  30 
  31 public class AnnotationsBuilder<S, T, E> extends AbstractBuilder<S, T, E, AnnotationsBuilder<S, T, E>> {
  32 
  33     GrowableByteBuffer annoAttribute;
  34     int nannos;
  35 
  36     AnnotationsBuilder(PoolHelper<S, T, E> poolHelper, TypeHelper<S, T> typeHelper) {
  37         super(poolHelper, typeHelper);
  38         this.annoAttribute = new GrowableByteBuffer();
  39         annoAttribute.writeChar(0);
  40     }
  41 
  42     public enum Kind {
  43         RUNTIME_VISIBLE,
  44         RUNTIME_INVISIBLE;
  45     }
  46 
  47     enum Tag {
  48         B('B'),
  49         C('C'),
  50         D('D'),
  51         F('F'),
  52         I('I'),
  53         J('J'),
  54         S('S'),
  55         Z('Z'),
  56         STRING('s'),
  57         ENUM('e'),
  58         CLASS('c'),
  59         ANNO('@'),
  60         ARRAY('[');
  61 
  62         char tagChar;
  63 
  64         Tag(char tagChar) {
  65             this.tagChar = tagChar;
  66         }
  67     }
  68 
  69     AnnotationsBuilder<S, T, E> withAnnotation(T annoType, Consumer<? super AnnotationElementBuilder> annotationBuilder) {
  70         annoAttribute.writeChar(poolHelper.putType(annoType));
  71         int offset = annoAttribute.offset;
  72         annoAttribute.writeChar(0);
  73         if (annotationBuilder != null) {
  74             AnnotationElementBuilder _builder = new AnnotationElementBuilder();
  75             int nelems = _builder.withElements(annotationBuilder);
  76             patchCharAt(offset, nelems);
  77         }
  78         nannos++;
  79         return this;
  80     }
  81 
  82     byte[] build() {
  83         patchCharAt(0, nannos);
  84         return annoAttribute.bytes();
  85     }
  86 
  87     private void patchCharAt(int offset, int newChar) {
  88         int prevOffset = annoAttribute.offset;
  89         try {
  90             annoAttribute.offset = offset;
  91             annoAttribute.writeChar(newChar);
  92         } finally {
  93             annoAttribute.offset = prevOffset;
  94         }
  95     }
  96 
  97     @SuppressWarnings({"unchecked", "rawtypes"})
  98     static Consumer NO_BUILDER =
  99             new Consumer() {
 100                 @Override
 101                 public void accept(Object o) {
 102                     //do nothing
 103                 }
 104             };
 105 
 106     public class AnnotationElementBuilder {
 107 
 108         int nelems;
 109 
 110         public AnnotationElementBuilder withString(String name, String s) {
 111             annoAttribute.writeChar(poolHelper.putUtf8(name));
 112             writeStringValue(s);
 113             return this;
 114         }
 115 
 116         private void writeStringValue(String s) {
 117             annoAttribute.writeByte(Tag.STRING.tagChar);
 118             annoAttribute.writeChar(poolHelper.putUtf8(s));
 119             nelems++;
 120         }
 121 
 122         public AnnotationElementBuilder withClass(String name, T s) {
 123             annoAttribute.writeChar(poolHelper.putUtf8(name));
 124             writeClassValue(s);
 125             return this;
 126         }
 127 
 128         private void writeClassValue(T s) {
 129             annoAttribute.writeByte(Tag.CLASS.tagChar);
 130             annoAttribute.writeChar(poolHelper.putType(s));
 131             nelems++;
 132         }
 133 
 134         public AnnotationElementBuilder withEnum(String name, T enumType, int constant) {
 135             annoAttribute.writeChar(poolHelper.putUtf8(name));
 136             writeEnumValue(enumType, constant);
 137             return this;
 138         }
 139 
 140         private void writeEnumValue(T enumType, int constant) {
 141             annoAttribute.writeByte(Tag.ENUM.tagChar);
 142             annoAttribute.writeChar(poolHelper.putType(enumType));
 143             annoAttribute.writeChar(constant);
 144             nelems++;
 145         }
 146 
 147         public AnnotationElementBuilder withAnnotation(String name, T annoType, Consumer<? super AnnotationElementBuilder> annotationBuilder) {
 148             annoAttribute.writeChar(poolHelper.putUtf8(name));
 149             writeAnnotationValue(annoType, annotationBuilder);
 150             return this;
 151         }
 152 
 153         private void writeAnnotationValue(T annoType, Consumer<? super AnnotationElementBuilder> annotationBuilder) {
 154             annoAttribute.writeByte(Tag.ANNO.tagChar);
 155             annoAttribute.writeChar(poolHelper.putType(annoType));
 156             int offset = annoAttribute.offset;
 157             annoAttribute.writeChar(0);
 158             int nelems = withNestedElements(annotationBuilder);
 159             patchCharAt(offset, nelems);
 160             this.nelems++;
 161         }
 162 
 163         public AnnotationElementBuilder withPrimitive(String name, char c) {
 164             annoAttribute.writeChar(poolHelper.putUtf8(name));
 165             writePrimitiveValue(Tag.C, (int)c, PoolHelper::putInt);
 166             return this;
 167         }
 168 
 169         public AnnotationElementBuilder withPrimitive(String name, short s) {
 170             annoAttribute.writeChar(poolHelper.putUtf8(name));
 171             writePrimitiveValue(Tag.S, (int)s, PoolHelper::putInt);
 172             return this;
 173         }
 174 
 175         public AnnotationElementBuilder withPrimitive(String name, byte b) {
 176             annoAttribute.writeChar(poolHelper.putUtf8(name));
 177             writePrimitiveValue(Tag.B, (int)b, PoolHelper::putInt);
 178             return this;
 179         }
 180 
 181         public AnnotationElementBuilder withPrimitive(String name, int i) {
 182             annoAttribute.writeChar(poolHelper.putUtf8(name));
 183             writePrimitiveValue(Tag.I, i, PoolHelper::putInt);
 184             return this;
 185         }
 186 
 187         public AnnotationElementBuilder withPrimitive(String name, float f) {
 188             annoAttribute.writeChar(poolHelper.putUtf8(name));
 189             writePrimitiveValue(Tag.F, f, PoolHelper::putFloat);
 190             return this;
 191         }
 192 
 193         public AnnotationElementBuilder withPrimitive(String name, long l) {
 194             annoAttribute.writeChar(poolHelper.putUtf8(name));
 195             writePrimitiveValue(Tag.J, l, PoolHelper::putLong);
 196             return this;
 197         }
 198 
 199         public AnnotationElementBuilder withPrimitive(String name, double d) {
 200             annoAttribute.writeChar(poolHelper.putUtf8(name));
 201             writePrimitiveValue(Tag.D, d, PoolHelper::putDouble);
 202             return this;
 203         }
 204 
 205         public AnnotationElementBuilder withPrimitive(String name, boolean b) {
 206             annoAttribute.writeChar(poolHelper.putUtf8(name));
 207             writePrimitiveValue(Tag.Z, b ? 1 : 0, PoolHelper::putInt);
 208             return this;
 209         }
 210 
 211         private <Z> void writePrimitiveValue(Tag tag, Z value, ToIntBiFunction<PoolHelper<S, T, E>, Z> poolFunc) {
 212             annoAttribute.writeByte(tag.tagChar);
 213             annoAttribute.writeChar(poolFunc.applyAsInt(poolHelper, value));
 214             nelems++;
 215         }
 216 
 217         AnnotationElementBuilder withStrings(String name, String... ss) {
 218             annoAttribute.writeChar(poolHelper.putUtf8(name));
 219             annoAttribute.writeChar(ss.length);
 220             for (String s : ss) {
 221                 writeStringValue(s);
 222             }
 223             return this;
 224         }
 225 
 226         @SuppressWarnings("unchecked")
 227         AnnotationElementBuilder withClasses(String name, T... cc) {
 228             annoAttribute.writeChar(poolHelper.putUtf8(name));
 229             annoAttribute.writeChar(cc.length);
 230             for (T c : cc) {
 231                 writeClassValue(c);
 232             }
 233             return this;
 234         }
 235 
 236         AnnotationElementBuilder withEnums(String name, T enumType, int... constants) {
 237             annoAttribute.writeChar(poolHelper.putUtf8(name));
 238             annoAttribute.writeChar(constants.length);
 239             for (int c : constants) {
 240                 writeEnumValue(enumType, c);
 241             }
 242             return this;
 243         }
 244 
 245         @SuppressWarnings("unchecked")
 246         public AnnotationElementBuilder withAnnotations(String name, T annoType, Consumer<? super AnnotationElementBuilder>... annotationBuilders) {
 247             annoAttribute.writeChar(poolHelper.putUtf8(name));
 248             annoAttribute.writeChar(annotationBuilders.length);
 249             for (Consumer<? super AnnotationElementBuilder> annotationBuilder : annotationBuilders) {
 250                 writeAnnotationValue(annoType, annotationBuilder);
 251             }
 252             return this;
 253         }
 254 
 255         public AnnotationElementBuilder withPrimitives(String name, char... cc) {
 256             annoAttribute.writeChar(poolHelper.putUtf8(name));
 257             annoAttribute.writeChar(cc.length);
 258             for (char c : cc) {
 259                 writePrimitiveValue(Tag.C, (int)c, PoolHelper::putInt);
 260             }
 261             return this;
 262         }
 263 
 264         public AnnotationElementBuilder withPrimitives(String name, short... ss) {
 265             annoAttribute.writeChar(poolHelper.putUtf8(name));
 266             annoAttribute.writeChar(ss.length);
 267             for (short s : ss) {
 268                 writePrimitiveValue(Tag.S, (int)s, PoolHelper::putInt);
 269             }
 270             return this;
 271         }
 272 
 273         public AnnotationElementBuilder withPrimitives(String name, byte... bb) {
 274             annoAttribute.writeChar(poolHelper.putUtf8(name));
 275             annoAttribute.writeChar(bb.length);
 276             for (byte b : bb) {
 277                 writePrimitiveValue(Tag.B, (int)b, PoolHelper::putInt);
 278             }
 279             return this;
 280         }
 281 
 282         public AnnotationElementBuilder withPrimitives(String name, int... ii) {
 283             annoAttribute.writeChar(poolHelper.putUtf8(name));
 284             annoAttribute.writeChar(ii.length);
 285             for (int i : ii) {
 286                 writePrimitiveValue(Tag.I, i,  PoolHelper::putInt);
 287             }
 288             return this;
 289         }
 290 
 291         public AnnotationElementBuilder withPrimitives(String name, float... ff) {
 292             annoAttribute.writeChar(poolHelper.putUtf8(name));
 293             annoAttribute.writeChar(ff.length);
 294             for (float f : ff) {
 295                 writePrimitiveValue(Tag.F, f, PoolHelper::putFloat);
 296             }
 297             return this;
 298         }
 299 
 300         public AnnotationElementBuilder withPrimitives(String name, long... ll) {
 301             annoAttribute.writeChar(poolHelper.putUtf8(name));
 302             annoAttribute.writeChar(ll.length);
 303             for (long l : ll) {
 304                 writePrimitiveValue(Tag.J, l, PoolHelper::putLong);
 305             }
 306             return this;
 307         }
 308 
 309         public AnnotationElementBuilder withPrimitives(String name, double... dd) {
 310             annoAttribute.writeChar(poolHelper.putUtf8(name));
 311             annoAttribute.writeChar(dd.length);
 312             for (double d : dd) {
 313                 writePrimitiveValue(Tag.D, d, PoolHelper::putDouble);
 314             }
 315             return this;
 316         }
 317 
 318         public AnnotationElementBuilder withPrimitives(String name, boolean... bb) {
 319             annoAttribute.writeChar(poolHelper.putUtf8(name));
 320             annoAttribute.writeChar(bb.length);
 321             for (boolean b : bb) {
 322                 writePrimitiveValue(Tag.Z, b ? 1 : 0, PoolHelper::putInt);
 323             }
 324             return this;
 325         }
 326 
 327         int withNestedElements(Consumer<? super AnnotationElementBuilder> annotationBuilder) {
 328             return withElements(new AnnotationElementBuilder(), annotationBuilder);
 329         }
 330 
 331         int withElements(Consumer<? super AnnotationElementBuilder> annotationBuilder) {
 332             return withElements(this, annotationBuilder);
 333         }
 334 
 335         private int withElements(AnnotationElementBuilder builder, Consumer<? super AnnotationElementBuilder> annotationBuilder) {
 336             annotationBuilder.accept(builder);
 337             return builder.nelems;
 338         }
 339     }
 340 }