1 package com.uber.nullaway; 2 3 import com.google.errorprone.util.ASTHelpers; 4 import com.sun.tools.javac.code.Symbol; 5 import java.util.List; 6 7 /** 8 * Methods backported from {@link com.google.errorprone.util.ASTHelpers} since we do not yet require 9 * a recent-enough Error Prone version. The methods should be removed once we bump our minimum Error 10 * Prone version accordingly. We also include methods where new overloads have been added in recent 11 * Error Prone versions, to ensure binary compatibility when compiling against a new version but 12 * running on an old version. 13 */ 14 public class ASTHelpersBackports { 15 ASTHelpersBackports()16 private ASTHelpersBackports() {} 17 18 /** 19 * Returns true if the symbol is static. Returns {@code false} for module symbols. Remove once we 20 * require Error Prone 2.16.0 or higher. 21 */ 22 @SuppressWarnings("ASTHelpersSuggestions") isStatic(Symbol symbol)23 public static boolean isStatic(Symbol symbol) { 24 if (symbol.getKind().name().equals("MODULE")) { 25 return false; 26 } 27 return symbol.isStatic(); 28 } 29 30 /** 31 * A wrapper for {@link Symbol#getEnclosedElements} to avoid binary compatibility issues for 32 * covariant overrides in subtypes of {@link Symbol}. 33 * 34 * <p>Same as this ASTHelpers method in Error Prone: 35 * https://github.com/google/error-prone/blame/a1318e4b0da4347dff7508108835d77c470a7198/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java#L1148 36 * TODO: delete this method and switch to ASTHelpers once we can require Error Prone 2.20.0 37 */ 38 @SuppressWarnings("ASTHelpersSuggestions") getEnclosedElements(Symbol symbol)39 public static List<Symbol> getEnclosedElements(Symbol symbol) { 40 return symbol.getEnclosedElements(); 41 } 42 43 /** 44 * A wrapper for {@link ASTHelpers#hasDirectAnnotationWithSimpleName(Symbol, String)} to avoid 45 * binary compatibility issues with new overloads in recent Error Prone versions. NullAway code 46 * should only use this method and not call the corresponding ASTHelpers methods directly. 47 * 48 * <p>TODO: delete this method and switch to ASTHelpers once we can require Error Prone 2.24.0 49 * 50 * @param sym the symbol 51 * @param simpleName the simple name 52 * @return {@code true} iff the symbol has a direct annotation with the given simple name 53 */ hasDirectAnnotationWithSimpleName(Symbol sym, String simpleName)54 public static boolean hasDirectAnnotationWithSimpleName(Symbol sym, String simpleName) { 55 return ASTHelpers.hasDirectAnnotationWithSimpleName(sym, simpleName); 56 } 57 } 58