1/*
2 * Copyright (C) 2015 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.tools.layoutlib.create;
18
19import org.objectweb.asm.ClassVisitor;
20import org.objectweb.asm.FieldVisitor;
21
22import java.util.Set;
23
24import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
25import static org.objectweb.asm.Opcodes.ACC_PROTECTED;
26import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
27
28/**
29 * Promotes given fields to public visibility.
30 */
31public class PromoteFieldClassAdapter extends ClassVisitor {
32
33    private final Set<String> mFieldNames;
34    private static final int ACC_NOT_PUBLIC = ~(ACC_PRIVATE | ACC_PROTECTED);
35
36    public PromoteFieldClassAdapter(ClassVisitor cv, Set<String> fieldNames) {
37        super(Main.ASM_VERSION, cv);
38        mFieldNames = fieldNames;
39    }
40
41    @Override
42    public FieldVisitor visitField(int access, String name, String desc, String signature,
43            Object value) {
44        if (mFieldNames.contains(name)) {
45            if ((access & ACC_PUBLIC) == 0) {
46                access = (access & ACC_NOT_PUBLIC) | ACC_PUBLIC;
47            }
48        }
49        return super.visitField(access, name, desc, signature, value);
50    }
51}
52