Java Code Examples for javax.lang.model.element.VariableElement#getConstantValue()

The following examples show how to use javax.lang.model.element.VariableElement#getConstantValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RetroFacebookProcessor.java    From RetroFacebook with Apache License 2.0 6 votes vote down vote up
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 "";
}
 
Example 2
Source File: AutoValueOrOneOfProcessor.java    From auto with Apache License 2.0 6 votes vote down vote up
/**
 * 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 "";
}
 
Example 3
Source File: SwitchRewriter.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@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));
    }
  }
}
 
Example 4
Source File: ConstantsSummaryBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: SerializedFormBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: ClassFileConverter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: WebServiceVisitor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: WebServiceVisitor.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: ValueType.java    From immutables with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: PubapiVisitor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 11
Source File: WebServiceVisitor.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: Gen.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 13
Source File: Gen.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 14
Source File: LLNI.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 15
Source File: PropertyProcessor.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
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;
}
 
Example 16
Source File: Gen.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 17
Source File: LLNI.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 18
Source File: LLNI.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 19
Source File: Gen.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 20
Source File: LLNI.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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;
}