1 /* 2 * Copyright 2014 Google LLC 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 package com.google.auto.common; 17 18 import static com.google.common.base.Preconditions.checkNotNull; 19 import static javax.lang.model.element.ElementKind.PACKAGE; 20 21 import com.google.common.base.Enums; 22 import com.google.common.collect.Ordering; 23 import java.util.Set; 24 import javax.lang.model.element.Element; 25 import javax.lang.model.element.ElementKind; 26 import javax.lang.model.element.Modifier; 27 import org.checkerframework.checker.nullness.qual.Nullable; 28 29 /** 30 * Represents the visibility of a given {@link Element}: {@code public}, {@code protected}, 31 * {@code private} or default/package-private. 32 * 33 * <p>The constants for this enum are ordered according by increasing visibility. 34 * 35 * @author Gregory Kick 36 */ 37 public enum Visibility { 38 PRIVATE, 39 DEFAULT, 40 PROTECTED, 41 PUBLIC; 42 43 // TODO(ronshapiro): remove this and reference ElementKind.MODULE directly once we start building 44 // with -source 9 45 private static final @Nullable ElementKind MODULE = 46 Enums.getIfPresent(ElementKind.class, "MODULE").orNull(); 47 48 /** 49 * Returns the visibility of the given {@link Element}. While package and module elements don't 50 * technically have a visibility associated with them, this method returns {@link #PUBLIC} for 51 * them. 52 */ ofElement(Element element)53 public static Visibility ofElement(Element element) { 54 checkNotNull(element); 55 // packages and module don't have modifiers, but they're obviously "public" 56 if (element.getKind().equals(PACKAGE) || element.getKind().equals(MODULE)) { 57 return PUBLIC; 58 } 59 Set<Modifier> modifiers = element.getModifiers(); 60 if (modifiers.contains(Modifier.PRIVATE)) { 61 return PRIVATE; 62 } else if (modifiers.contains(Modifier.PROTECTED)) { 63 return PROTECTED; 64 } else if (modifiers.contains(Modifier.PUBLIC)) { 65 return PUBLIC; 66 } else { 67 return DEFAULT; 68 } 69 } 70 71 /** 72 * Returns effective visibility of the given element meaning that it takes into account the 73 * visibility of its enclosing elements. 74 */ effectiveVisibilityOfElement(Element element)75 public static Visibility effectiveVisibilityOfElement(Element element) { 76 checkNotNull(element); 77 Visibility effectiveVisibility = PUBLIC; 78 Element currentElement = element; 79 while (currentElement != null) { 80 // NOTE: We don't use Guava's Comparators.min() because that requires Guava 30, which would 81 // make this library unusable in annotation processors using Bazel < 5.0. 82 effectiveVisibility = Ordering.natural().min(effectiveVisibility, ofElement(currentElement)); 83 currentElement = currentElement.getEnclosingElement(); 84 } 85 return effectiveVisibility; 86 } 87 } 88