Search in sources :

Example 1 with StringConstant

use of org.eclipse.jdt.internal.compiler.impl.StringConstant in project bazel-jdt-java-toolchain by salesforce.

the class ClassFile method generateElementValue.

/**
 * @param attributeOffset
 */
private void generateElementValue(int attributeOffset, Expression defaultValue, Constant constant, TypeBinding binding) {
    if (this.contentsOffset + 3 >= this.contents.length) {
        resizeContents(3);
    }
    switch(binding.id) {
        case T_boolean:
            this.contents[this.contentsOffset++] = (byte) 'Z';
            int booleanValueIndex = this.constantPool.literalIndex(constant.booleanValue() ? 1 : 0);
            this.contents[this.contentsOffset++] = (byte) (booleanValueIndex >> 8);
            this.contents[this.contentsOffset++] = (byte) booleanValueIndex;
            break;
        case T_byte:
            this.contents[this.contentsOffset++] = (byte) 'B';
            int integerValueIndex = this.constantPool.literalIndex(constant.intValue());
            this.contents[this.contentsOffset++] = (byte) (integerValueIndex >> 8);
            this.contents[this.contentsOffset++] = (byte) integerValueIndex;
            break;
        case T_char:
            this.contents[this.contentsOffset++] = (byte) 'C';
            integerValueIndex = this.constantPool.literalIndex(constant.intValue());
            this.contents[this.contentsOffset++] = (byte) (integerValueIndex >> 8);
            this.contents[this.contentsOffset++] = (byte) integerValueIndex;
            break;
        case T_int:
            this.contents[this.contentsOffset++] = (byte) 'I';
            integerValueIndex = this.constantPool.literalIndex(constant.intValue());
            this.contents[this.contentsOffset++] = (byte) (integerValueIndex >> 8);
            this.contents[this.contentsOffset++] = (byte) integerValueIndex;
            break;
        case T_short:
            this.contents[this.contentsOffset++] = (byte) 'S';
            integerValueIndex = this.constantPool.literalIndex(constant.intValue());
            this.contents[this.contentsOffset++] = (byte) (integerValueIndex >> 8);
            this.contents[this.contentsOffset++] = (byte) integerValueIndex;
            break;
        case T_float:
            this.contents[this.contentsOffset++] = (byte) 'F';
            int floatValueIndex = this.constantPool.literalIndex(constant.floatValue());
            this.contents[this.contentsOffset++] = (byte) (floatValueIndex >> 8);
            this.contents[this.contentsOffset++] = (byte) floatValueIndex;
            break;
        case T_double:
            this.contents[this.contentsOffset++] = (byte) 'D';
            int doubleValueIndex = this.constantPool.literalIndex(constant.doubleValue());
            this.contents[this.contentsOffset++] = (byte) (doubleValueIndex >> 8);
            this.contents[this.contentsOffset++] = (byte) doubleValueIndex;
            break;
        case T_long:
            this.contents[this.contentsOffset++] = (byte) 'J';
            int longValueIndex = this.constantPool.literalIndex(constant.longValue());
            this.contents[this.contentsOffset++] = (byte) (longValueIndex >> 8);
            this.contents[this.contentsOffset++] = (byte) longValueIndex;
            break;
        case T_JavaLangString:
            this.contents[this.contentsOffset++] = (byte) 's';
            int stringValueIndex = this.constantPool.literalIndex(((StringConstant) constant).stringValue().toCharArray());
            if (stringValueIndex == -1) {
                if (!this.creatingProblemType) {
                    // report an error and abort: will lead to a problem type classfile creation
                    TypeDeclaration typeDeclaration = this.referenceBinding.scope.referenceContext;
                    typeDeclaration.scope.problemReporter().stringConstantIsExceedingUtf8Limit(defaultValue);
                } else {
                    // already inside a problem type creation : no attribute
                    this.contentsOffset = attributeOffset;
                }
            } else {
                this.contents[this.contentsOffset++] = (byte) (stringValueIndex >> 8);
                this.contents[this.contentsOffset++] = (byte) stringValueIndex;
            }
    }
}
Also used : StringConstant(org.eclipse.jdt.internal.compiler.impl.StringConstant) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Aggregations

TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1 StringConstant (org.eclipse.jdt.internal.compiler.impl.StringConstant)1