1 /*
2  * Copyright (C) 2007 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 
17 package com.android.dx.cf.direct;
18 
19 import com.android.dx.cf.iface.AttributeList;
20 import com.android.dx.cf.iface.Member;
21 import com.android.dx.cf.iface.StdMethod;
22 import com.android.dx.cf.iface.StdMethodList;
23 import com.android.dx.rop.code.AccessFlags;
24 import com.android.dx.rop.cst.CstNat;
25 import com.android.dx.rop.cst.CstType;
26 
27 /**
28  * Parser for lists of methods in a class file.
29  */
30 final /*package*/ class MethodListParser extends MemberListParser {
31     /** {@code non-null;} list in progress */
32     final private StdMethodList methods;
33 
34     /**
35      * Constructs an instance.
36      *
37      * @param cf {@code non-null;} the class file to parse from
38      * @param definer {@code non-null;} class being defined
39      * @param offset offset in {@code bytes} to the start of the list
40      * @param attributeFactory {@code non-null;} attribute factory to use
41      */
MethodListParser(DirectClassFile cf, CstType definer, int offset, AttributeFactory attributeFactory)42     public MethodListParser(DirectClassFile cf, CstType definer,
43             int offset, AttributeFactory attributeFactory) {
44         super(cf, definer, offset, attributeFactory);
45         methods = new StdMethodList(getCount());
46     }
47 
48     /**
49      * Gets the parsed list.
50      *
51      * @return {@code non-null;} the parsed list
52      */
getList()53     public StdMethodList getList() {
54         parseIfNecessary();
55         return methods;
56     }
57 
58     /** {@inheritDoc} */
59     @Override
humanName()60     protected String humanName() {
61         return "method";
62     }
63 
64     /** {@inheritDoc} */
65     @Override
humanAccessFlags(int accessFlags)66     protected String humanAccessFlags(int accessFlags) {
67         return AccessFlags.methodString(accessFlags);
68     }
69 
70     /** {@inheritDoc} */
71     @Override
getAttributeContext()72     protected int getAttributeContext() {
73         return AttributeFactory.CTX_METHOD;
74     }
75 
76     /** {@inheritDoc} */
77     @Override
set(int n, int accessFlags, CstNat nat, AttributeList attributes)78     protected Member set(int n, int accessFlags, CstNat nat,
79                          AttributeList attributes) {
80         StdMethod meth =
81             new StdMethod(getDefiner(), accessFlags, nat, attributes);
82 
83         methods.set(n, meth);
84         return meth;
85     }
86 }
87