Example usage for javax.lang.model.element VariableElement getConstantValue

List of usage examples for javax.lang.model.element VariableElement getConstantValue

Introduction

In this page you can find the example usage for javax.lang.model.element VariableElement getConstantValue.

Prototype

Object getConstantValue();

Source Link

Document

Returns the value of this variable if this is a final field initialized to a compile-time constant.

Usage

From source file:org.immutables.value.processor.meta.ValueType.java

private Object findSerialVersionUID() {
    for (VariableElement field : ElementFilter.fieldsIn(element.getEnclosedElements())) {
        if (field.getSimpleName().contentEquals(SERIAL_VERSION_FIELD_NAME)
                && field.asType().getKind() == TypeKind.LONG) {
            return field.getConstantValue();
        }//w w w. ja v  a 2  s  .co m
    }
    return null;
}

From source file:auto.parse.processor.AutoParseProcessor.java

private String getSerialVersionUID(TypeElement type) {
    Types typeUtils = processingEnv.getTypeUtils();
    TypeMirror serializable = getTypeMirror(Serializable.class);
    if (typeUtils.isAssignable(type.asType(), serializable)) {
        List<VariableElement> fields = ElementFilter.fieldsIn(type.getEnclosedElements());
        for (VariableElement field : fields) {
            if (field.getSimpleName().toString().equals("serialVersionUID")) {
                Object value = field.getConstantValue();
                if (field.getModifiers().containsAll(Arrays.asList(Modifier.STATIC, Modifier.FINAL))
                        && field.asType().getKind() == TypeKind.LONG && value != null) {
                    return value + "L";
                } else {
                    reportError("serialVersionUID must be a static final long compile-time constant", field);
                    break;
                }/*from  w  w w .  j a va 2 s. c  o m*/
            }
        }
    }
    return "";
}