001// ASM: a very small and fast Java bytecode manipulation framework 002// Copyright (c) 2000-2011 INRIA, France Telecom 003// All rights reserved. 004// 005// Redistribution and use in source and binary forms, with or without 006// modification, are permitted provided that the following conditions 007// are met: 008// 1. Redistributions of source code must retain the above copyright 009// notice, this list of conditions and the following disclaimer. 010// 2. Redistributions in binary form must reproduce the above copyright 011// notice, this list of conditions and the following disclaimer in the 012// documentation and/or other materials provided with the distribution. 013// 3. Neither the name of the copyright holders nor the names of its 014// contributors may be used to endorse or promote products derived from 015// this software without specific prior written permission. 016// 017// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 018// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 019// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 020// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 021// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 022// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 023// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 024// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 025// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 026// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 027// THE POSSIBILITY OF SUCH DAMAGE. 028package io.ebean.enhance.asm; 029 030/** 031 * A visitor to visit a Java field. The methods of this class must be called in the following order: 032 * ( {@code visitAnnotation} | {@code visitTypeAnnotation} | {@code visitAttribute} )* {@code 033 * visitEnd}. 034 * 035 * @author Eric Bruneton 036 */ 037public abstract class FieldVisitor { 038 039 /** 040 * The ASM API version implemented by this visitor. The value of this field must be one of the 041 * {@code ASM}<i>x</i> values in {@link Opcodes}. 042 */ 043 protected final int api; 044 045 /** The field visitor to which this visitor must delegate method calls. May be {@literal null}. */ 046 protected FieldVisitor fv; 047 048 /** 049 * Constructs a new {@link FieldVisitor}. 050 * 051 * @param api the ASM API version implemented by this visitor. Must be one of the {@code 052 * ASM}<i>x</i> values in {@link Opcodes}. 053 */ 054 protected FieldVisitor(final int api) { 055 this(api, null); 056 } 057 058 /** 059 * Constructs a new {@link FieldVisitor}. 060 * 061 * @param api the ASM API version implemented by this visitor. Must be one of the {@code 062 * ASM}<i>x</i> values in {@link Opcodes}. 063 * @param fieldVisitor the field visitor to which this visitor must delegate method calls. May be 064 * null. 065 */ 066 protected FieldVisitor(final int api, final FieldVisitor fieldVisitor) { 067 if (api != Opcodes.ASM9 068 && api != Opcodes.ASM8 069 && api != Opcodes.ASM7 070 && api != Opcodes.ASM6 071 && api != Opcodes.ASM5 072 && api != Opcodes.ASM4 073 && api != Opcodes.ASM10_EXPERIMENTAL) { 074 throw new IllegalArgumentException("Unsupported api " + api); 075 } 076 if (api == Opcodes.ASM10_EXPERIMENTAL) { 077 Constants.checkAsmExperimental(this); 078 } 079 this.api = api; 080 this.fv = fieldVisitor; 081 } 082 083 /** 084 * The field visitor to which this visitor must delegate method calls. May be {@literal null}. 085 * 086 * @return the field visitor to which this visitor must delegate method calls, or {@literal null}. 087 */ 088 public FieldVisitor getDelegate() { 089 return fv; 090 } 091 092 /** 093 * Visits an annotation of the field. 094 * 095 * @param descriptor the class descriptor of the annotation class. 096 * @param visible {@literal true} if the annotation is visible at runtime. 097 * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not 098 * interested in visiting this annotation. 099 */ 100 public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) { 101 if (fv != null) { 102 return fv.visitAnnotation(descriptor, visible); 103 } 104 return null; 105 } 106 107 /** 108 * Visits an annotation on the type of the field. 109 * 110 * @param typeRef a reference to the annotated type. The sort of this type reference must be 111 * {@link TypeReference#FIELD}. See {@link TypeReference}. 112 * @param typePath the path to the annotated type argument, wildcard bound, array element type, or 113 * static inner type within 'typeRef'. May be {@literal null} if the annotation targets 114 * 'typeRef' as a whole. 115 * @param descriptor the class descriptor of the annotation class. 116 * @param visible {@literal true} if the annotation is visible at runtime. 117 * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not 118 * interested in visiting this annotation. 119 */ 120 public AnnotationVisitor visitTypeAnnotation( 121 final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) { 122 if (api < Opcodes.ASM5) { 123 throw new UnsupportedOperationException("This feature requires ASM5"); 124 } 125 if (fv != null) { 126 return fv.visitTypeAnnotation(typeRef, typePath, descriptor, visible); 127 } 128 return null; 129 } 130 131 /** 132 * Visits a non standard attribute of the field. 133 * 134 * @param attribute an attribute. 135 */ 136 public void visitAttribute(final Attribute attribute) { 137 if (fv != null) { 138 fv.visitAttribute(attribute); 139 } 140 } 141 142 /** 143 * Visits the end of the field. This method, which is the last one to be called, is used to inform 144 * the visitor that all the annotations and attributes of the field have been visited. 145 */ 146 public void visitEnd() { 147 if (fv != null) { 148 fv.visitEnd(); 149 } 150 } 151}