1 // ASM: a very small and fast Java bytecode manipulation framework 2 // Copyright (c) 2000-2011 INRIA, France Telecom 3 // All rights reserved. 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions 7 // are met: 8 // 1. Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // 2. Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // 3. Neither the name of the copyright holders nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 // THE POSSIBILITY OF SUCH DAMAGE. 28 package org.objectweb.asm.tree; 29 30 import java.util.List; 31 import org.objectweb.asm.AnnotationVisitor; 32 import org.objectweb.asm.Attribute; 33 import org.objectweb.asm.ClassVisitor; 34 import org.objectweb.asm.Opcodes; 35 import org.objectweb.asm.RecordComponentVisitor; 36 import org.objectweb.asm.TypePath; 37 38 /** 39 * A node that represents a record component. 40 * 41 * @author Remi Forax 42 */ 43 public class RecordComponentNode extends RecordComponentVisitor { 44 45 /** The record component name. */ 46 public String name; 47 48 /** The record component descriptor (see {@link org.objectweb.asm.Type}). */ 49 public String descriptor; 50 51 /** The record component signature. May be {@literal null}. */ 52 public String signature; 53 54 /** The runtime visible annotations of this record component. May be {@literal null}. */ 55 public List<AnnotationNode> visibleAnnotations; 56 57 /** The runtime invisible annotations of this record component. May be {@literal null}. */ 58 public List<AnnotationNode> invisibleAnnotations; 59 60 /** The runtime visible type annotations of this record component. May be {@literal null}. */ 61 public List<TypeAnnotationNode> visibleTypeAnnotations; 62 63 /** The runtime invisible type annotations of this record component. May be {@literal null}. */ 64 public List<TypeAnnotationNode> invisibleTypeAnnotations; 65 66 /** The non standard attributes of this record component. * May be {@literal null}. */ 67 public List<Attribute> attrs; 68 69 /** 70 * Constructs a new {@link RecordComponentNode}. <i>Subclasses must not use this constructor</i>. 71 * Instead, they must use the {@link #RecordComponentNode(int, String, String, String)} version. 72 * 73 * @param name the record component name. 74 * @param descriptor the record component descriptor (see {@link org.objectweb.asm.Type}). 75 * @param signature the record component signature. 76 * @throws IllegalStateException If a subclass calls this constructor. 77 */ RecordComponentNode(final String name, final String descriptor, final String signature)78 public RecordComponentNode(final String name, final String descriptor, final String signature) { 79 this(/* latest api = */ Opcodes.ASM9, name, descriptor, signature); 80 if (getClass() != RecordComponentNode.class) { 81 throw new IllegalStateException(); 82 } 83 } 84 85 /** 86 * Constructs a new {@link RecordComponentNode}. 87 * 88 * @param api the ASM API version implemented by this visitor. Must be one of {@link Opcodes#ASM8} 89 * or {@link Opcodes#ASM9}. 90 * @param name the record component name. 91 * @param descriptor the record component descriptor (see {@link org.objectweb.asm.Type}). 92 * @param signature the record component signature. 93 */ RecordComponentNode( final int api, final String name, final String descriptor, final String signature)94 public RecordComponentNode( 95 final int api, final String name, final String descriptor, final String signature) { 96 super(api); 97 this.name = name; 98 this.descriptor = descriptor; 99 this.signature = signature; 100 } 101 102 // ----------------------------------------------------------------------------------------------- 103 // Implementation of the FieldVisitor abstract class 104 // ----------------------------------------------------------------------------------------------- 105 106 @Override visitAnnotation(final String descriptor, final boolean visible)107 public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) { 108 AnnotationNode annotation = new AnnotationNode(descriptor); 109 if (visible) { 110 visibleAnnotations = Util.add(visibleAnnotations, annotation); 111 } else { 112 invisibleAnnotations = Util.add(invisibleAnnotations, annotation); 113 } 114 return annotation; 115 } 116 117 @Override visitTypeAnnotation( final int typeRef, final TypePath typePath, final String descriptor, final boolean visible)118 public AnnotationVisitor visitTypeAnnotation( 119 final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) { 120 TypeAnnotationNode typeAnnotation = new TypeAnnotationNode(typeRef, typePath, descriptor); 121 if (visible) { 122 visibleTypeAnnotations = Util.add(visibleTypeAnnotations, typeAnnotation); 123 } else { 124 invisibleTypeAnnotations = Util.add(invisibleTypeAnnotations, typeAnnotation); 125 } 126 return typeAnnotation; 127 } 128 129 @Override visitAttribute(final Attribute attribute)130 public void visitAttribute(final Attribute attribute) { 131 attrs = Util.add(attrs, attribute); 132 } 133 134 @Override visitEnd()135 public void visitEnd() { 136 // Nothing to do. 137 } 138 139 // ----------------------------------------------------------------------------------------------- 140 // Accept methods 141 // ----------------------------------------------------------------------------------------------- 142 143 /** 144 * Checks that this record component node is compatible with the given ASM API version. This 145 * method checks that this node, and all its children recursively, do not contain elements that 146 * were introduced in more recent versions of the ASM API than the given version. 147 * 148 * @param api an ASM API version. Must be one of {@link Opcodes#ASM8} or {@link Opcodes#ASM9}. 149 */ check(final int api)150 public void check(final int api) { 151 if (api < Opcodes.ASM8) { 152 throw new UnsupportedClassVersionException(); 153 } 154 } 155 156 /** 157 * Makes the given class visitor visit this record component. 158 * 159 * @param classVisitor a class visitor. 160 */ accept(final ClassVisitor classVisitor)161 public void accept(final ClassVisitor classVisitor) { 162 RecordComponentVisitor recordComponentVisitor = 163 classVisitor.visitRecordComponent(name, descriptor, signature); 164 if (recordComponentVisitor == null) { 165 return; 166 } 167 // Visit the annotations. 168 if (visibleAnnotations != null) { 169 for (int i = 0, n = visibleAnnotations.size(); i < n; ++i) { 170 AnnotationNode annotation = visibleAnnotations.get(i); 171 annotation.accept(recordComponentVisitor.visitAnnotation(annotation.desc, true)); 172 } 173 } 174 if (invisibleAnnotations != null) { 175 for (int i = 0, n = invisibleAnnotations.size(); i < n; ++i) { 176 AnnotationNode annotation = invisibleAnnotations.get(i); 177 annotation.accept(recordComponentVisitor.visitAnnotation(annotation.desc, false)); 178 } 179 } 180 if (visibleTypeAnnotations != null) { 181 for (int i = 0, n = visibleTypeAnnotations.size(); i < n; ++i) { 182 TypeAnnotationNode typeAnnotation = visibleTypeAnnotations.get(i); 183 typeAnnotation.accept( 184 recordComponentVisitor.visitTypeAnnotation( 185 typeAnnotation.typeRef, typeAnnotation.typePath, typeAnnotation.desc, true)); 186 } 187 } 188 if (invisibleTypeAnnotations != null) { 189 for (int i = 0, n = invisibleTypeAnnotations.size(); i < n; ++i) { 190 TypeAnnotationNode typeAnnotation = invisibleTypeAnnotations.get(i); 191 typeAnnotation.accept( 192 recordComponentVisitor.visitTypeAnnotation( 193 typeAnnotation.typeRef, typeAnnotation.typePath, typeAnnotation.desc, false)); 194 } 195 } 196 // Visit the non standard attributes. 197 if (attrs != null) { 198 for (int i = 0, n = attrs.size(); i < n; ++i) { 199 recordComponentVisitor.visitAttribute(attrs.get(i)); 200 } 201 } 202 recordComponentVisitor.visitEnd(); 203 } 204 } 205