View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.generic;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.classfile.AnnotationElementValue;
25  import org.apache.bcel.classfile.ElementValue;
26  
27  /**
28   * Generates annotation element values.
29   *
30   * @since 6.0
31   */
32  public class AnnotationElementValueGen extends ElementValueGen {
33      // For annotation element values, this is the annotation
34      private final AnnotationEntryGen a;
35  
36      /**
37       * Constructs an AnnotationElementValueGen.
38       *
39       * @param value the annotation element value.
40       * @param cpool the constant pool generator.
41       * @param copyPoolEntries whether to copy pool entries.
42       */
43      public AnnotationElementValueGen(final AnnotationElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
44          super(ANNOTATION, cpool);
45          a = new AnnotationEntryGen(value.getAnnotationEntry(), cpool, copyPoolEntries);
46      }
47  
48      /**
49       * Constructs an AnnotationElementValueGen.
50       *
51       * @param a the annotation entry generator.
52       * @param cpool the constant pool generator.
53       */
54      public AnnotationElementValueGen(final AnnotationEntryGen a, final ConstantPoolGen cpool) {
55          super(ANNOTATION, cpool);
56          this.a = a;
57      }
58  
59      /**
60       * Constructs an AnnotationElementValueGen.
61       *
62       * @param type the type.
63       * @param annotation the annotation.
64       * @param cpool the constant pool generator.
65       */
66      public AnnotationElementValueGen(final int type, final AnnotationEntryGen annotation, final ConstantPoolGen cpool) {
67          super(type, cpool);
68          if (type != ANNOTATION) {
69              throw new IllegalArgumentException("Only element values of type annotation can be built with this ctor - type specified: " + type);
70          }
71          this.a = annotation;
72      }
73  
74      @Override
75      public void dump(final DataOutputStream dos) throws IOException {
76          dos.writeByte(super.getElementValueType()); // u1 type of value (ANNOTATION == '@')
77          a.dump(dos);
78      }
79  
80      /**
81       * Gets the annotation.
82       *
83       * @return the annotation.
84       */
85      public AnnotationEntryGen getAnnotation() {
86          return a;
87      }
88  
89      /**
90       * Returns an immutable variant of this AnnotationElementValueGen.
91       *
92       * @return an immutable variant of this AnnotationElementValueGen.
93       */
94      @Override
95      public ElementValue getElementValue() {
96          return new AnnotationElementValue(super.getElementValueType(), a.getAnnotation(), getConstantPool().getConstantPool());
97      }
98  
99      @Override
100     public String stringifyValue() {
101         throw new UnsupportedOperationException("Not implemented yet");
102     }
103 }