1/***
2 * ASM: a very small and fast Java bytecode manipulation framework
3 * Copyright (c) 2000-2007 INRIA, France Telecom
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the copyright holders nor the names of its
15 *    contributors may be used to endorse or promote products derived from
16 *    this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30package org.mockito.asm.tree;
31
32import org.mockito.asm.Attribute;
33import org.mockito.asm.ClassVisitor;
34import org.mockito.asm.FieldVisitor;
35
36/**
37 * A node that represents a field.
38 *
39 * @author Eric Bruneton
40 */
41public class FieldNode extends MemberNode implements FieldVisitor {
42
43    /**
44     * The field's access flags (see {@link org.mockito.asm.Opcodes}). This
45     * field also indicates if the field is synthetic and/or deprecated.
46     */
47    public int access;
48
49    /**
50     * The field's name.
51     */
52    public String name;
53
54    /**
55     * The field's descriptor (see {@link org.mockito.asm.Type}).
56     */
57    public String desc;
58
59    /**
60     * The field's signature. May be <tt>null</tt>.
61     */
62    public String signature;
63
64    /**
65     * The field's initial value. This field, which may be <tt>null</tt> if
66     * the field does not have an initial value, must be an {@link Integer}, a
67     * {@link Float}, a {@link Long}, a {@link Double} or a {@link String}.
68     */
69    public Object value;
70
71    /**
72     * Constructs a new {@link FieldNode}.
73     *
74     * @param access the field's access flags (see
75     *        {@link org.mockito.asm.Opcodes}). This parameter also indicates
76     *        if the field is synthetic and/or deprecated.
77     * @param name the field's name.
78     * @param desc the field's descriptor (see
79     *        {@link org.mockito.asm.Type Type}).
80     * @param signature the field's signature.
81     * @param value the field's initial value. This parameter, which may be
82     *        <tt>null</tt> if the field does not have an initial value, must
83     *        be an {@link Integer}, a {@link Float}, a {@link Long}, a
84     *        {@link Double} or a {@link String}.
85     */
86    public FieldNode(
87        final int access,
88        final String name,
89        final String desc,
90        final String signature,
91        final Object value)
92    {
93        this.access = access;
94        this.name = name;
95        this.desc = desc;
96        this.signature = signature;
97        this.value = value;
98    }
99
100    /**
101     * Makes the given class visitor visit this field.
102     *
103     * @param cv a class visitor.
104     */
105    public void accept(final ClassVisitor cv) {
106        FieldVisitor fv = cv.visitField(access, name, desc, signature, value);
107        if (fv == null) {
108            return;
109        }
110        int i, n;
111        n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
112        for (i = 0; i < n; ++i) {
113            AnnotationNode an = (AnnotationNode) visibleAnnotations.get(i);
114            an.accept(fv.visitAnnotation(an.desc, true));
115        }
116        n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size();
117        for (i = 0; i < n; ++i) {
118            AnnotationNode an = (AnnotationNode) invisibleAnnotations.get(i);
119            an.accept(fv.visitAnnotation(an.desc, false));
120        }
121        n = attrs == null ? 0 : attrs.size();
122        for (i = 0; i < n; ++i) {
123            fv.visitAttribute((Attribute) attrs.get(i));
124        }
125        fv.visitEnd();
126    }
127}
128