1 /* 2 * Copyright 2016 Google Inc. All Rights Reserved. 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.google.turbine.model; 18 19 /** The visibility of a declaration. */ 20 public enum TurbineVisibility { 21 PUBLIC(3, TurbineFlag.ACC_PUBLIC), 22 PROTECTED(2, TurbineFlag.ACC_PROTECTED), 23 PACKAGE(1, 0), 24 PRIVATE(0, TurbineFlag.ACC_PRIVATE); 25 26 private final int level; 27 private final int flag; 28 TurbineVisibility(int level, int flag)29 TurbineVisibility(int level, int flag) { 30 this.level = level; 31 this.flag = flag; 32 } 33 moreVisible(TurbineVisibility other)34 public boolean moreVisible(TurbineVisibility other) { 35 return level > other.level; 36 } 37 flag()38 public int flag() { 39 return flag; 40 } 41 42 public static final int VISIBILITY_MASK = 43 TurbineFlag.ACC_PUBLIC | TurbineFlag.ACC_PRIVATE | TurbineFlag.ACC_PROTECTED; 44 45 /** 46 * Returns the {@link TurbineVisibility} corresponding to the given access bits. 47 * 48 * <p>If the input is ill-formed and corresponds to multiple visibilities, {@code PUBLIC}, {@code 49 * PROTECTED}, {@code PRIVATE}, and {@code PACKAGE}, are returned in that order. This means that 50 * turbine will occasionally produce valid output for invalid input. In general turbine performs 51 * the minimum possible error-checking, and the expectation is that it is run in parallel with 52 * javac or another non-header compiler as part of a build, and it defers well-formedness checking 53 * to the other tool. 54 */ fromAccess(int access)55 public static TurbineVisibility fromAccess(int access) { 56 if ((access & TurbineFlag.ACC_PUBLIC) == TurbineFlag.ACC_PUBLIC) { 57 return PUBLIC; 58 } 59 if ((access & TurbineFlag.ACC_PROTECTED) == TurbineFlag.ACC_PROTECTED) { 60 return PROTECTED; 61 } 62 if ((access & TurbineFlag.ACC_PRIVATE) == TurbineFlag.ACC_PRIVATE) { 63 return PRIVATE; 64 } 65 return PACKAGE; 66 } 67 setAccess(int access)68 public int setAccess(int access) { 69 access &= ~(TurbineFlag.ACC_PUBLIC | TurbineFlag.ACC_PRIVATE | TurbineFlag.ACC_PROTECTED); 70 access |= flag(); 71 return access; 72 } 73 } 74