javax.lang.model.element.VariableElement#getConstantValue ( )源码实例Demo

下面列出了javax.lang.model.element.VariableElement#getConstantValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: RetroFacebook   文件: RetroFacebookProcessor.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 {
          errorReporter.reportError(
              "serialVersionUID must be a static final long compile-time constant", field);
          break;
        }
      }
    }
  }
  return "";
}
 
源代码2 项目: auto   文件: AutoValueOrOneOfProcessor.java
/**
 * Returns a string like {@code "1234L"} if {@code type instanceof Serializable} and defines
 * {@code serialVersionUID = 1234L}; otherwise {@code ""}.
 */
final String getSerialVersionUID(TypeElement type) {
  TypeMirror serializable = elementUtils().getTypeElement(Serializable.class.getName()).asType();
  if (typeUtils().isAssignable(type.asType(), serializable)) {
    List<VariableElement> fields = ElementFilter.fieldsIn(type.getEnclosedElements());
    for (VariableElement field : fields) {
      if (field.getSimpleName().contentEquals("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 {
          errorReporter.reportError(
              field, "serialVersionUID must be a static final long compile-time constant");
          break;
        }
      }
    }
  }
  return "";
}
 
源代码3 项目: j2objc   文件: SwitchRewriter.java
@Override
public void endVisit(SwitchCase node) {
  Expression expr = node.getExpression();
  VariableElement var = expr != null ? TreeUtil.getVariableElement(expr) : null;
  if (var == null) {
    return;
  }
  TypeMirror type = var.asType();
  if (TypeUtil.isEnum(type)) {
    String enumValue =
        NameTable.getNativeEnumName(nameTable.getFullName(TypeUtil.asTypeElement(type))) + "_"
        + nameTable.getVariableBaseName(var);
    node.setExpression(new NativeExpression(enumValue, typeUtil.getInt()));
  } else if (type.getKind().isPrimitive() && var.getKind() == ElementKind.LOCAL_VARIABLE) {
    Object value = var.getConstantValue();
    if (value != null) {
      node.setExpression(TreeUtil.newLiteral(value, typeUtil));
    }
  }
}
 
源代码4 项目: openjdk-jdk9   文件: SerializedFormBuilder.java
/**
 * Build the serial UID information for the given class.
 *
 * @param node the XML element that specifies which components to document
 * @param classTree content tree to which the serial UID information will be added
 */
public void buildSerialUIDInfo(XMLNode node, Content classTree) {
    Content serialUidTree = writer.getSerialUIDInfoHeader();
    for (Element e : utils.getFieldsUnfiltered(currentTypeElement)) {
        VariableElement field = (VariableElement)e;
        if (field.getSimpleName().toString().compareTo(SERIAL_VERSION_UID) == 0 &&
            field.getConstantValue() != null) {
            writer.addSerialUIDInfo(SERIAL_VERSION_UID_HEADER,
                                    utils.constantValueExpresion(field), serialUidTree);
            break;
        }
    }
    classTree.addContent(serialUidTree);
}
 
源代码5 项目: openjdk-jdk9   文件: ConstantsSummaryBuilder.java
/**
 * Return true if the given class has constant fields to document.
 *
 * @param typeElement the class being checked.
 * @return true if the given package has constant fields to document.
 */
private boolean hasConstantField (TypeElement typeElement) {
    VisibleMemberMap visibleMemberMapFields = configuration.getVisibleMemberMap(typeElement,
        VisibleMemberMap.Kind.FIELDS);
    List<Element> fields = visibleMemberMapFields.getLeafMembers();
    for (Element f : fields) {
        VariableElement field = (VariableElement)f;
        if (field.getConstantValue() != null) {
            typeElementsWithConstFields.add(typeElement);
            return true;
        }
    }
    return false;
}
 
源代码6 项目: j2objc   文件: ClassFileConverter.java
private TreeNode convertFieldDeclaration(VariableElement element) {
  Object constantValue = element.getConstantValue();
  Expression initializer = constantValue != null
      ? TreeUtil.newLiteral(constantValue, translationEnv.typeUtil()) : null;
  FieldDeclaration fieldDecl = new FieldDeclaration(element, initializer);
  convertBodyDeclaration(fieldDecl, element);
  return fieldDecl;
}
 
源代码7 项目: openjdk-jdk8u-backup   文件: WebServiceVisitor.java
protected boolean isLegalSei(TypeElement interfaceElement) {
    for (VariableElement field : ElementFilter.fieldsIn(interfaceElement.getEnclosedElements()))
        if (field.getConstantValue() != null) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_SEI_CANNOT_CONTAIN_CONSTANT_VALUES(
                    interfaceElement.getQualifiedName(), field.getSimpleName()));
            return false;
        }
    return methodsAreLegal(interfaceElement);
}
 
源代码8 项目: immutables   文件: ValueType.java
private Long findSerialVersionUID() {
  for (VariableElement field : ElementFilter.fieldsIn(element.getEnclosedElements())) {
    if (field.getSimpleName().contentEquals(SERIAL_VERSION_FIELD_NAME)
        && field.asType().getKind() == TypeKind.LONG) {
      return (Long) field.getConstantValue();
    }
  }
  return null;
}
 
源代码9 项目: openjdk-jdk9   文件: PubapiVisitor.java
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public Void visitVariable(VariableElement e, Void p) {
    if (isNonPrivate(e)) {
        Object constVal = e.getConstantValue();
        String constValStr = null;
        // TODO: This doesn't seem to be entirely accurate. What if I change
        // from, say, 0 to 0L? (And the field is public final static so that
        // it could get inlined.)
        if (constVal != null) {
            if (e.asType().toString().equals("char")) {
                // What type is 'value'? Is it already a char?
                char c = constVal.toString().charAt(0);
                constValStr = "'" + encodeChar(c) + "'";
            } else {
                constValStr = constVal.toString()
                                      .chars()
                                      .mapToObj(PubapiVisitor::encodeChar)
                                      .collect(Collectors.joining("", "\"", "\""));
            }
        }

        PubVar v = new PubVar(e.getModifiers(),
                              TypeDesc.fromType(e.asType()),
                              e.toString(),
                              constValStr);
        collectedApi.variables.put(v.identifier, v);
    }

    // Safe to not recurse here, because the only thing
    // to visit here is the constructor of a variable declaration.
    // If it happens to contain an anonymous inner class (which it might)
    // then this class is never visible outside of the package anyway, so
    // we are allowed to ignore it here.
    return null;
}
 
源代码10 项目: hottub   文件: WebServiceVisitor.java
protected boolean isLegalSei(TypeElement interfaceElement) {
    for (VariableElement field : ElementFilter.fieldsIn(interfaceElement.getEnclosedElements()))
        if (field.getConstantValue() != null) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_SEI_CANNOT_CONTAIN_CONSTANT_VALUES(
                    interfaceElement.getQualifiedName(), field.getSimpleName()));
            return false;
        }
    return methodsAreLegal(interfaceElement);
}
 
源代码11 项目: openjdk-8-source   文件: WebServiceVisitor.java
protected boolean isLegalSei(TypeElement interfaceElement) {
    for (VariableElement field : ElementFilter.fieldsIn(interfaceElement.getEnclosedElements()))
        if (field.getConstantValue() != null) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_SEI_CANNOT_CONTAIN_CONSTANT_VALUES(
                    interfaceElement.getQualifiedName(), field.getSimpleName()));
            return false;
        }
    return methodsAreLegal(interfaceElement);
}
 
源代码12 项目: TencentKona-8   文件: LLNI.java
protected String addStaticStructMember(VariableElement field, String cname) {
    String res = null;
    Object exp = null;

    if (!field.getModifiers().contains(Modifier.STATIC))
        return res;
    if (!field.getModifiers().contains(Modifier.FINAL))
        return res;

    exp = field.getConstantValue();

    if (exp != null) {
        /* Constant. */

        String     cn     = cname + "_" + field.getSimpleName();
        String     suffix = null;
        long           val = 0;
        /* Can only handle int, long, float, and double fields. */
        if (exp instanceof Byte
            || exp instanceof Short
            || exp instanceof Integer) {
            suffix = "L";
            val = ((Number)exp).intValue();
        }
        else if (exp instanceof Long) {
            // Visual C++ supports the i64 suffix, not LL
            suffix = isWindows ? "i64" : "LL";
            val = ((Long)exp).longValue();
        }
        else if (exp instanceof Float)  suffix = "f";
        else if (exp instanceof Double) suffix = "";
        else if (exp instanceof Character) {
            suffix = "L";
            Character ch = (Character) exp;
            val = ((int) ch) & 0xffff;
        }
        if (suffix != null) {
            // Some compilers will generate a spurious warning
            // for the integer constants for Integer.MIN_VALUE
            // and Long.MIN_VALUE so we handle them specially.
            if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
                (suffix.equals("LL") && (val == Long.MIN_VALUE))) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn
                    + " (" + (val + 1) + suffix + "-1)" + lineSep;
            } else if (suffix.equals("L") || suffix.endsWith("LL")) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + val + suffix + lineSep;
            } else {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + exp + suffix + lineSep;
            }
        }
    }
    return res;
}
 
源代码13 项目: TencentKona-8   文件: Gen.java
protected String defineForStatic(TypeElement c, VariableElement f)
        throws Util.Exit {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    if (!f.getModifiers().contains(Modifier.STATIC))
        util.bug("tried.to.define.non.static");

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }
            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }
    return null;
}
 
源代码14 项目: hottub   文件: LLNI.java
protected String addStaticStructMember(VariableElement field, String cname) {
    String res = null;
    Object exp = null;

    if (!field.getModifiers().contains(Modifier.STATIC))
        return res;
    if (!field.getModifiers().contains(Modifier.FINAL))
        return res;

    exp = field.getConstantValue();

    if (exp != null) {
        /* Constant. */

        String     cn     = cname + "_" + field.getSimpleName();
        String     suffix = null;
        long           val = 0;
        /* Can only handle int, long, float, and double fields. */
        if (exp instanceof Byte
            || exp instanceof Short
            || exp instanceof Integer) {
            suffix = "L";
            val = ((Number)exp).intValue();
        }
        else if (exp instanceof Long) {
            // Visual C++ supports the i64 suffix, not LL
            suffix = isWindows ? "i64" : "LL";
            val = ((Long)exp).longValue();
        }
        else if (exp instanceof Float)  suffix = "f";
        else if (exp instanceof Double) suffix = "";
        else if (exp instanceof Character) {
            suffix = "L";
            Character ch = (Character) exp;
            val = ((int) ch) & 0xffff;
        }
        if (suffix != null) {
            // Some compilers will generate a spurious warning
            // for the integer constants for Integer.MIN_VALUE
            // and Long.MIN_VALUE so we handle them specially.
            if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
                (suffix.equals("LL") && (val == Long.MIN_VALUE))) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn
                    + " (" + (val + 1) + suffix + "-1)" + lineSep;
            } else if (suffix.equals("L") || suffix.endsWith("LL")) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + val + suffix + lineSep;
            } else {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + exp + suffix + lineSep;
            }
        }
    }
    return res;
}
 
源代码15 项目: jdk8u60   文件: LLNI.java
protected String addStaticStructMember(VariableElement field, String cname) {
    String res = null;
    Object exp = null;

    if (!field.getModifiers().contains(Modifier.STATIC))
        return res;
    if (!field.getModifiers().contains(Modifier.FINAL))
        return res;

    exp = field.getConstantValue();

    if (exp != null) {
        /* Constant. */

        String     cn     = cname + "_" + field.getSimpleName();
        String     suffix = null;
        long           val = 0;
        /* Can only handle int, long, float, and double fields. */
        if (exp instanceof Byte
            || exp instanceof Short
            || exp instanceof Integer) {
            suffix = "L";
            val = ((Number)exp).intValue();
        }
        else if (exp instanceof Long) {
            // Visual C++ supports the i64 suffix, not LL
            suffix = isWindows ? "i64" : "LL";
            val = ((Long)exp).longValue();
        }
        else if (exp instanceof Float)  suffix = "f";
        else if (exp instanceof Double) suffix = "";
        else if (exp instanceof Character) {
            suffix = "L";
            Character ch = (Character) exp;
            val = ((int) ch) & 0xffff;
        }
        if (suffix != null) {
            // Some compilers will generate a spurious warning
            // for the integer constants for Integer.MIN_VALUE
            // and Long.MIN_VALUE so we handle them specially.
            if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
                (suffix.equals("LL") && (val == Long.MIN_VALUE))) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn
                    + " (" + (val + 1) + suffix + "-1)" + lineSep;
            } else if (suffix.equals("L") || suffix.endsWith("LL")) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + val + suffix + lineSep;
            } else {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + exp + suffix + lineSep;
            }
        }
    }
    return res;
}
 
源代码16 项目: jdk8u60   文件: Gen.java
protected String defineForStatic(TypeElement c, VariableElement f)
        throws Util.Exit {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    if (!f.getModifiers().contains(Modifier.STATIC))
        util.bug("tried.to.define.non.static");

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }
            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }
    return null;
}
 
源代码17 项目: jasperreports   文件: PropertyProcessor.java
protected CompiledPropertyMetadata toPropertyMetadata(VariableElement element, AnnotationMirror propertyAnnotation)
{
	Map<? extends ExecutableElement, ? extends AnnotationValue> annotationValues = processingEnv.getElementUtils().getElementValuesWithDefaults(propertyAnnotation);
	
	CompiledPropertyMetadata property = new CompiledPropertyMetadata();
	
	String propName = (String) annotationValue(annotationValues, "name").getValue();
	if (propName == null || propName.isEmpty())
	{
		propName = (String) element.getConstantValue();
		if (propName == null)
		{
			processingEnv.getMessager().printMessage(Kind.WARNING, "Failed to read constant value for " + element, 
					element);
			return null;
		}
	}
	property.setName(propName);
	
	boolean deprecated = processingEnv.getElementUtils().isDeprecated(element);
	property.setDeprecated(deprecated);
	
	QualifiedNameable enclosingElement = (QualifiedNameable) element.getEnclosingElement();
	property.setConstantDeclarationClass(enclosingElement.getQualifiedName().toString());
	property.setConstantFieldName(element.getSimpleName().toString());
	
	property.setCategory((String) annotationValue(annotationValues, "category").getValue());
	property.setDefaultValue((String) annotationValue(annotationValues, "defaultValue").getValue());
	property.setSinceVersion((String) annotationValue(annotationValues, "sinceVersion").getValue());
	property.setValueType(((TypeMirror) annotationValue(annotationValues, "valueType").getValue()).toString());
	
	@SuppressWarnings("unchecked")
	List<? extends AnnotationValue> scopeValues = (List<? extends AnnotationValue>) annotationValue(annotationValues, "scopes").getValue();
	List<PropertyScope> propertyScopes = new ArrayList<>(scopeValues.size());
	for (AnnotationValue scopeValue : scopeValues)
	{
		PropertyScope scope = Enum.valueOf(PropertyScope.class, ((VariableElement) scopeValue.getValue()).getSimpleName().toString());
		propertyScopes.add(scope); 
	}
	
	//automatically adding Global if Context is present
	int contextIndex = propertyScopes.indexOf(PropertyScope.CONTEXT);
	if (contextIndex >= 0 && !propertyScopes.contains(PropertyScope.GLOBAL))
	{
		propertyScopes.add(contextIndex, PropertyScope.GLOBAL);
	}
	
	//automatically adding Report if Dataset is present
	int datasetIndex = propertyScopes.indexOf(PropertyScope.DATASET);
	if (datasetIndex >= 0 && !propertyScopes.contains(PropertyScope.REPORT))
	{
		propertyScopes.add(datasetIndex, PropertyScope.REPORT);
	}
	
	property.setScopes(propertyScopes);
	
	@SuppressWarnings("unchecked")
	List<? extends AnnotationValue> scopeQualificationValues = (List<? extends AnnotationValue>) annotationValue(annotationValues, "scopeQualifications").getValue();
	List<String> scopeQualifications = new ArrayList<>(scopeValues.size());
	for (AnnotationValue qualificationValue : scopeQualificationValues)
	{
		String qualification = (String) qualificationValue.getValue();
		scopeQualifications.add(qualification);
	}
	property.setScopeQualifications(scopeQualifications);
	
	return property;
}
 
源代码18 项目: openjdk-jdk8u   文件: LLNI.java
protected String addStaticStructMember(VariableElement field, String cname) {
    String res = null;
    Object exp = null;

    if (!field.getModifiers().contains(Modifier.STATIC))
        return res;
    if (!field.getModifiers().contains(Modifier.FINAL))
        return res;

    exp = field.getConstantValue();

    if (exp != null) {
        /* Constant. */

        String     cn     = cname + "_" + field.getSimpleName();
        String     suffix = null;
        long           val = 0;
        /* Can only handle int, long, float, and double fields. */
        if (exp instanceof Byte
            || exp instanceof Short
            || exp instanceof Integer) {
            suffix = "L";
            val = ((Number)exp).intValue();
        }
        else if (exp instanceof Long) {
            // Visual C++ supports the i64 suffix, not LL
            suffix = isWindows ? "i64" : "LL";
            val = ((Long)exp).longValue();
        }
        else if (exp instanceof Float)  suffix = "f";
        else if (exp instanceof Double) suffix = "";
        else if (exp instanceof Character) {
            suffix = "L";
            Character ch = (Character) exp;
            val = ((int) ch) & 0xffff;
        }
        if (suffix != null) {
            // Some compilers will generate a spurious warning
            // for the integer constants for Integer.MIN_VALUE
            // and Long.MIN_VALUE so we handle them specially.
            if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
                (suffix.equals("LL") && (val == Long.MIN_VALUE))) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn
                    + " (" + (val + 1) + suffix + "-1)" + lineSep;
            } else if (suffix.equals("L") || suffix.endsWith("LL")) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + val + suffix + lineSep;
            } else {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + exp + suffix + lineSep;
            }
        }
    }
    return res;
}
 
源代码19 项目: openjdk-jdk8u-backup   文件: Gen.java
protected String defineForStatic(TypeElement c, VariableElement f)
        throws Util.Exit {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    if (!f.getModifiers().contains(Modifier.STATIC))
        util.bug("tried.to.define.non.static");

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }
            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }
    return null;
}
 
源代码20 项目: openjdk-jdk9   文件: Gen.java
protected String defineForStatic(TypeElement c, VariableElement f)
        throws Util.Exit {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    if (!f.getModifiers().contains(Modifier.STATIC))
        util.bug("tried.to.define.non.static");

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }
            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }
    return null;
}