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; 27import static org.objectweb.asm.Opcodes.ASM4; 28 29/** 30 * Promotes given fields to public visibility. 31 */ 32public class PromoteFieldClassAdapter extends ClassVisitor { 33 34 private final Set<String> mFieldNames; 35 private static final int ACC_NOT_PUBLIC = ~(ACC_PRIVATE | ACC_PROTECTED); 36 37 public PromoteFieldClassAdapter(ClassVisitor cv, Set<String> fieldNames) { 38 super(ASM4, cv); 39 mFieldNames = fieldNames; 40 } 41 42 @Override 43 public FieldVisitor visitField(int access, String name, String desc, String signature, 44 Object value) { 45 if (mFieldNames.contains(name)) { 46 if ((access & ACC_PUBLIC) == 0) { 47 access = (access & ACC_NOT_PUBLIC) | ACC_PUBLIC; 48 } 49 } 50 return super.visitField(access, name, desc, signature, value); 51 } 52} 53