1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.mkstubs.sourcer;
18
19import org.objectweb.asm.AnnotationVisitor;
20import org.objectweb.asm.Attribute;
21import org.objectweb.asm.FieldVisitor;
22import org.objectweb.asm.Opcodes;
23import org.objectweb.asm.Type;
24import org.objectweb.asm.signature.SignatureReader;
25
26/**
27 * A field visitor that generates Java source defining a field.
28 */
29class FieldSourcer extends FieldVisitor {
30
31    private final Output mOutput;
32    private final int mAccess;
33    private final String mName;
34    private final String mDesc;
35    private final String mSignature;
36
37    public FieldSourcer(Output output, int access, String name, String desc, String signature) {
38        super(Opcodes.ASM4);
39        mOutput = output;
40        mAccess = access;
41        mName = name;
42        mDesc = desc;
43        mSignature = signature;
44    }
45
46    @Override
47    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
48        mOutput.write("@%s", desc);
49        return new AnnotationSourcer(mOutput);
50    }
51
52    @Override
53    public void visitAttribute(Attribute attr) {
54        mOutput.write("%s /* non-standard attribute */ ", attr.type);
55    }
56
57    @Override
58    public void visitEnd() {
59        // Need to write type and field name after the annotations and attributes.
60
61        AccessSourcer as = new AccessSourcer(mOutput);
62        as.write(mAccess, AccessSourcer.IS_FIELD);
63
64        if (mSignature == null) {
65            mOutput.write(" %s", Type.getType(mDesc).getClassName());
66        } else {
67            mOutput.write(" ");
68            SignatureReader sigReader = new SignatureReader(mSignature);
69            SignatureSourcer sigSourcer = new SignatureSourcer();
70            sigReader.acceptType(sigSourcer);
71            mOutput.write(sigSourcer.toString());
72        }
73
74        mOutput.write(" %s", mName);
75
76        mOutput.write(";\n");
77    }
78
79}
80