19cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll/*
29cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll * Copyright (C) 2009 The Android Open Source Project
39cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll *
49cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll * Licensed under the Apache License, Version 2.0 (the "License");
59cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll * you may not use this file except in compliance with the License.
69cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll * You may obtain a copy of the License at
79cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll *
89cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll *      http://www.apache.org/licenses/LICENSE-2.0
99cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll *
109cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll * Unless required by applicable law or agreed to in writing, software
119cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll * distributed under the License is distributed on an "AS IS" BASIS,
129cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll * See the License for the specific language governing permissions and
149cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll * limitations under the License.
159cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll */
169cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
179cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Mollpackage com.android.mkstubs.sourcer;
189cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
199cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Mollimport org.objectweb.asm.AnnotationVisitor;
209cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Mollimport org.objectweb.asm.Attribute;
219cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Mollimport org.objectweb.asm.FieldVisitor;
227fb8be5c6ab69d547a49da88093c57acc3694309Tor Norbyeimport org.objectweb.asm.Opcodes;
23adf4543e57086070c95b3ef439dbb6679b0bd562Raphael Mollimport org.objectweb.asm.Type;
249cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Mollimport org.objectweb.asm.signature.SignatureReader;
259cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
269cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll/**
277fb8be5c6ab69d547a49da88093c57acc3694309Tor Norbye * A field visitor that generates Java source defining a field.
289cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll */
297fb8be5c6ab69d547a49da88093c57acc3694309Tor Norbyeclass FieldSourcer extends FieldVisitor {
309cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
319cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    private final Output mOutput;
329cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    private final int mAccess;
339cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    private final String mName;
349cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    private final String mDesc;
359cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    private final String mSignature;
369cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
379cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    public FieldSourcer(Output output, int access, String name, String desc, String signature) {
387fb8be5c6ab69d547a49da88093c57acc3694309Tor Norbye        super(Opcodes.ASM4);
399cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        mOutput = output;
409cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        mAccess = access;
419cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        mName = name;
429cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        mDesc = desc;
439cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        mSignature = signature;
449cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    }
459cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
467fb8be5c6ab69d547a49da88093c57acc3694309Tor Norbye    @Override
479cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
489cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        mOutput.write("@%s", desc);
499cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        return new AnnotationSourcer(mOutput);
509cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    }
519cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
527fb8be5c6ab69d547a49da88093c57acc3694309Tor Norbye    @Override
539cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    public void visitAttribute(Attribute attr) {
549cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        mOutput.write("%s /* non-standard attribute */ ", attr.type);
559cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    }
569cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
577fb8be5c6ab69d547a49da88093c57acc3694309Tor Norbye    @Override
589cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    public void visitEnd() {
599cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        // Need to write type and field name after the annotations and attributes.
609cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
619cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        AccessSourcer as = new AccessSourcer(mOutput);
629cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        as.write(mAccess, AccessSourcer.IS_FIELD);
637fb8be5c6ab69d547a49da88093c57acc3694309Tor Norbye
649cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        if (mSignature == null) {
65adf4543e57086070c95b3ef439dbb6679b0bd562Raphael Moll            mOutput.write(" %s", Type.getType(mDesc).getClassName());
669cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        } else {
679cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll            mOutput.write(" ");
689cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll            SignatureReader sigReader = new SignatureReader(mSignature);
699cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll            SignatureSourcer sigSourcer = new SignatureSourcer();
709cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll            sigReader.acceptType(sigSourcer);
719cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll            mOutput.write(sigSourcer.toString());
729cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        }
739cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
749cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        mOutput.write(" %s", mName);
759cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
769cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll        mOutput.write(";\n");
779cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll    }
789cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll
799cdcde3017e17e1154341e697f855dd15d5d8a47Raphael Moll}
80