1917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/*
2917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Copyright (C) 2007 The Android Open Source Project
3917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *
4917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Licensed under the Apache License, Version 2.0 (the "License");
5917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * you may not use this file except in compliance with the License.
6917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * You may obtain a copy of the License at
7917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *
8917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *      http://www.apache.org/licenses/LICENSE-2.0
9917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *
10917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Unless required by applicable law or agreed to in writing, software
11917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * distributed under the License is distributed on an "AS IS" BASIS,
12917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * See the License for the specific language governing permissions and
14917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * limitations under the License.
15917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */
16917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
17917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpackage com.android.dexgen.rop;
18917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
19917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.cst.CstNat;
20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.cst.CstType;
217351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgulimport com.android.dexgen.rop.cst.CstUtf8;
22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.cst.TypedConstant;
23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/**
25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Standard implementation of {@link Field}, which directly stores
26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * all the associated data.
27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */
28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic final class StdField extends StdMember implements Field {
29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * Constructs an instance.
31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     *
32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param definingClass {@code non-null;} the defining class
33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param accessFlags access flags
34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param nat {@code non-null;} member name and type (descriptor)
35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param attributes {@code non-null;} list of associated attributes
36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public StdField(CstType definingClass, int accessFlags, CstNat nat,
38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    AttributeList attributes) {
39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        super(definingClass, accessFlags, nat, attributes);
40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
427351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul    /**
437351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     * Constructs an instance having Java field as its pattern.
447351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     *
457351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     * @param field {@code non-null;} pattern for dex field
467351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     */
477351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul    public StdField(java.lang.reflect.Field field) {
487351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul        this(CstType.intern(field.getDeclaringClass()),
497351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul                field.getModifiers(),
507351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul                new CstNat(new CstUtf8(field.getName()),
517351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul                        CstType.intern(field.getType()).getDescriptor()),
527351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul                new StdAttributeList(0));
537351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul    }
547351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul
557351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul    /**
567351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     * Constructs an instance taking field description as user-friendly arguments.
577351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     *
587351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     * @param declaringClass {@code non-null;} the class field belongs to
597351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     * @param type {@code non-null;} type of the field
607351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     * @param name {@code non-null;} name of the field
617351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     * @param modifiers access flags of the field
627351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul     */
637351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul    public StdField(Class definingClass, Class type, String name, int modifiers) {
647351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul        this(CstType.intern(definingClass),
657351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul                modifiers,
667351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul                new CstNat(new CstUtf8(name), CstType.intern(type).getDescriptor()),
677351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul                new StdAttributeList(0));
687351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul    }
697351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul
70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /** {@inheritDoc} */
71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public TypedConstant getConstantValue() {
72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        AttributeList attribs = getAttributes();
73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        AttConstantValue cval = (AttConstantValue)
74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            attribs.findFirst(AttConstantValue.ATTRIBUTE_NAME);
75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        if (cval == null) {
77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            return null;
78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return cval.getConstantValue();
81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
827351211af4fdfc436eae69a3641c0880f8c8b608Piotr Gurgul}