OSDN Git Service

Merge "Add user-friendly constructors to StdField"
[android-x86/dalvik.git] / dexgen / src / com / android / dexgen / rop / StdField.java
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.dexgen.rop;
18
19 import com.android.dexgen.rop.cst.CstNat;
20 import com.android.dexgen.rop.cst.CstType;
21 import com.android.dexgen.rop.cst.CstUtf8;
22 import com.android.dexgen.rop.cst.TypedConstant;
23
24 /**
25  * Standard implementation of {@link Field}, which directly stores
26  * all the associated data.
27  */
28 public final class StdField extends StdMember implements Field {
29     /**
30      * Constructs an instance.
31      *
32      * @param definingClass {@code non-null;} the defining class
33      * @param accessFlags access flags
34      * @param nat {@code non-null;} member name and type (descriptor)
35      * @param attributes {@code non-null;} list of associated attributes
36      */
37     public StdField(CstType definingClass, int accessFlags, CstNat nat,
38                     AttributeList attributes) {
39         super(definingClass, accessFlags, nat, attributes);
40     }
41
42     /**
43      * Constructs an instance having Java field as its pattern.
44      *
45      * @param field {@code non-null;} pattern for dex field
46      */
47     public StdField(java.lang.reflect.Field field) {
48         this(CstType.intern(field.getDeclaringClass()),
49                 field.getModifiers(),
50                 new CstNat(new CstUtf8(field.getName()),
51                         CstType.intern(field.getType()).getDescriptor()),
52                 new StdAttributeList(0));
53     }
54
55     /**
56      * Constructs an instance taking field description as user-friendly arguments.
57      *
58      * @param declaringClass {@code non-null;} the class field belongs to
59      * @param type {@code non-null;} type of the field
60      * @param name {@code non-null;} name of the field
61      * @param modifiers access flags of the field
62      */
63     public StdField(Class definingClass, Class type, String name, int modifiers) {
64         this(CstType.intern(definingClass),
65                 modifiers,
66                 new CstNat(new CstUtf8(name), CstType.intern(type).getDescriptor()),
67                 new StdAttributeList(0));
68     }
69
70     /** {@inheritDoc} */
71     public TypedConstant getConstantValue() {
72         AttributeList attribs = getAttributes();
73         AttConstantValue cval = (AttConstantValue)
74             attribs.findFirst(AttConstantValue.ATTRIBUTE_NAME);
75
76         if (cval == null) {
77             return null;
78         }
79
80         return cval.getConstantValue();
81     }
82 }