JUtils { public static Optional<Object> constantValue(Symbol.VariableSymbol symbol) { if (!symbol.isFinal() || !symbol.isStatic()) { return Optional.empty(); } if (symbol instanceof JTypeSymbol.SpecialField) { return Optional.empty(); } Object c = ((IVariableBinding) ((JVariableSymbol) symbol).binding).getConstantValue(); if (c instanceof Short) { c = Integer.valueOf((Short) c); } else if (c instanceof Byte) { c = Integer.valueOf((Byte) c); } else if (c instanceof Character) { c = Integer.valueOf((Character) c); } return Optional.ofNullable(c); } private  JUtils(); static boolean isPrimitiveWrapper(Type type); @Nullable static Type primitiveWrapperType(Type type); @Nullable static Type primitiveType(Type type); static boolean isNullType(Type type); static boolean isIntersectionType(Type type); static boolean isTypeVar(Type type); static boolean isAnnotation(Symbol.TypeSymbol typeSymbol); static boolean isEffectivelyFinal(Symbol.VariableSymbol variableSymbol); static boolean isParameter(Symbol symbol); static Optional<Object> constantValue(Symbol.VariableSymbol symbol); static Set<Type> superTypes(Symbol.TypeSymbol typeSymbol); static Symbol.TypeSymbol outermostClass(Symbol.TypeSymbol typeSymbol); static Symbol getPackage(Symbol symbol); static boolean isVarArgsMethod(Symbol.MethodSymbol method); static boolean isSynchronizedMethod(Symbol.MethodSymbol method); static boolean isNativeMethod(Symbol.MethodSymbol method); static boolean isDefaultMethod(Symbol.MethodSymbol method); @Nullable static Object defaultValue(Symbol.MethodSymbol method); static boolean isOverridable(Symbol.MethodSymbol method); static boolean isParametrizedMethod(Symbol.MethodSymbol method); static boolean isRawType(Type type); static Type declaringType(Type type); static Set<Type> directSuperTypes(Type type); @Nullable static Symbol enclosingClass(Tree t); @Nullable static Symbol importTreeSymbol(ImportTree tree); static Symbol typeParameterTreeSymbol(TypeParameterTree tree); static SymbolMetadata parameterAnnotations(Symbol.MethodSymbol method, int param);  }
@Test void constantValue() { JavaTree.CompilationUnitTreeImpl cu = test("interface I { short SHORT = 42; char CHAR = 42; byte BYTE = 42; boolean BOOLEAN = false; }"); ClassTreeImpl c = (ClassTreeImpl) cu.types().get(0); Symbol.VariableSymbol shortConstant = cu.sema.variableSymbol(((VariableTreeImpl) c.members().get(0)).variableBinding); assertThat(JUtils.constantValue(shortConstant).orElseThrow(AssertionError::new)) .isInstanceOf(Integer.class) .isEqualTo(42); Symbol.VariableSymbol charConstant = cu.sema.variableSymbol(((VariableTreeImpl) c.members().get(1)).variableBinding); assertThat(JUtils.constantValue(charConstant).orElseThrow(AssertionError::new)) .isInstanceOf(Integer.class) .isEqualTo(42); Symbol.VariableSymbol byteConstant = cu.sema.variableSymbol(((VariableTreeImpl) c.members().get(2)).variableBinding); assertThat(JUtils.constantValue(byteConstant).orElseThrow(AssertionError::new)) .isInstanceOf(Integer.class) .isEqualTo(42); Symbol.VariableSymbol booleanConstant = cu.sema.variableSymbol(((VariableTreeImpl) c.members().get(3)).variableBinding); assertThat(JUtils.constantValue(booleanConstant).orElseThrow(AssertionError::new)) .isInstanceOf(Boolean.class) .isEqualTo(Boolean.FALSE); }